mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 10:13:14 -04:00 
			
		
		
		
	complete transfer plugin test case (#3967)
Signed-off-by: zounengren <zounengren@cmss.chinamobile.com>
This commit is contained in:
		| @@ -6,9 +6,16 @@ import ( | |||||||
| 	"github.com/caddyserver/caddy" | 	"github.com/caddyserver/caddy" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | func newTestControllerWithZones(input string, zones []string) *caddy.Controller { | ||||||
|  | 	ctr := caddy.NewTestController("dns", input) | ||||||
|  | 	ctr.ServerBlockKeys = append(ctr.ServerBlockKeys, zones...) | ||||||
|  | 	return ctr | ||||||
|  | } | ||||||
|  |  | ||||||
| func TestParse(t *testing.T) { | func TestParse(t *testing.T) { | ||||||
| 	tests := []struct { | 	tests := []struct { | ||||||
| 		input     string | 		input     string | ||||||
|  | 		zones     []string | ||||||
| 		shouldErr bool | 		shouldErr bool | ||||||
| 		exp       *Transfer | 		exp       *Transfer | ||||||
| 	}{ | 	}{ | ||||||
| @@ -18,6 +25,7 @@ func TestParse(t *testing.T) { | |||||||
|          transfer example.com example.edu { |          transfer example.com example.edu { | ||||||
|             to * 1.2.3.4 |             to * 1.2.3.4 | ||||||
|          }`, |          }`, | ||||||
|  | 			nil, | ||||||
| 			false, | 			false, | ||||||
| 			&Transfer{ | 			&Transfer{ | ||||||
| 				xfrs: []*xfr{{ | 				xfrs: []*xfr{{ | ||||||
| @@ -32,18 +40,42 @@ func TestParse(t *testing.T) { | |||||||
| 		// errors | 		// errors | ||||||
| 		{`transfer example.net example.org { | 		{`transfer example.net example.org { | ||||||
| 		 }`, | 		 }`, | ||||||
|  | 			nil, | ||||||
| 			true, | 			true, | ||||||
| 			nil, | 			nil, | ||||||
| 		}, | 		}, | ||||||
| 		{`transfer example.net example.org { | 		{`transfer example.net example.org { | ||||||
|            invalid option |            invalid option | ||||||
| 		 }`, | 		 }`, | ||||||
|  | 			nil, | ||||||
| 			true, | 			true, | ||||||
| 			nil, | 			nil, | ||||||
| 		}, | 		}, | ||||||
|  | 		{ | ||||||
|  | 			` | ||||||
|  |          transfer example.com example.edu { | ||||||
|  |             to example.com 1.2.3.4 | ||||||
|  |          }`, | ||||||
|  | 			nil, | ||||||
|  | 			true, | ||||||
|  | 			nil, | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			`transfer { | ||||||
|  | 			to 1.2.3.4 5.6.7.8:1053 [1::2]:34 | ||||||
|  | 		 }`, | ||||||
|  | 			[]string{"."}, | ||||||
|  | 			false, | ||||||
|  | 			&Transfer{ | ||||||
|  | 				xfrs: []*xfr{{ | ||||||
|  | 					Zones: []string{"."}, | ||||||
|  | 					to:    []string{"1.2.3.4:53", "5.6.7.8:1053", "[1::2]:34"}, | ||||||
|  | 				}}, | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
| 	} | 	} | ||||||
| 	for i, tc := range tests { | 	for i, tc := range tests { | ||||||
| 		c := caddy.NewTestController("dns", tc.input) | 		c := newTestControllerWithZones(tc.input, tc.zones) | ||||||
| 		transfer, err := parse(c) | 		transfer, err := parse(c) | ||||||
|  |  | ||||||
| 		if err == nil && tc.shouldErr { | 		if err == nil && tc.shouldErr { | ||||||
| @@ -52,6 +84,9 @@ func TestParse(t *testing.T) { | |||||||
| 		if err != nil && !tc.shouldErr { | 		if err != nil && !tc.shouldErr { | ||||||
| 			t.Fatalf("Test %d expected no errors, but got '%v'", i, err) | 			t.Fatalf("Test %d expected no errors, but got '%v'", i, err) | ||||||
| 		} | 		} | ||||||
|  | 		if tc.exp == nil && transfer != nil { | ||||||
|  | 			t.Fatalf("Test %d expected %v xfrs, got %#v", i, tc.exp, transfer) | ||||||
|  | 		} | ||||||
| 		if tc.shouldErr { | 		if tc.shouldErr { | ||||||
| 			continue | 			continue | ||||||
| 		} | 		} | ||||||
| @@ -83,3 +118,20 @@ func TestParse(t *testing.T) { | |||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func TestSetup(t *testing.T) { | ||||||
|  | 	c := caddy.NewTestController("dns", "transfer") | ||||||
|  | 	if err := setup(c); err == nil { | ||||||
|  | 		t.Fatal("Expected errors, but got nil") | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	c = caddy.NewTestController("dns", `transfer example.net example.org { | ||||||
|  | 			to 1.2.3.4 5.6.7.8:1053 [1::2]:34 | ||||||
|  | 		 } | ||||||
|  |          transfer example.com example.edu { | ||||||
|  |             to * 1.2.3.4 | ||||||
|  |          }`) | ||||||
|  | 	if err := setup(c); err != nil { | ||||||
|  | 		t.Fatalf("Expected no errors, but got %v", err) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user