| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | package autopath | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 	"reflect" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/test" | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 09:04:47 +08:00
										 |  |  | 	"github.com/caddyserver/caddy" | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestSetupAutoPath(t *testing.T) { | 
					
						
							|  |  |  | 	resolv, rm, err := test.TempFile(os.TempDir(), resolvConf) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-08-09 11:00:03 -07:00
										 |  |  | 		t.Fatalf("Could not create resolv.conf test file %s: %s", resolvConf, err) | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	defer rm() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		input              string | 
					
						
							|  |  |  | 		shouldErr          bool | 
					
						
							| 
									
										
										
										
											2017-08-13 18:16:25 +01:00
										 |  |  | 		expectedZone       string | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		expectedMw         string   // expected plugin. | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 		expectedSearch     []string // expected search path | 
					
						
							|  |  |  | 		expectedErrContent string   // substring from the expected error. Empty for positive cases. | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		// positive | 
					
						
							| 
									
										
										
										
											2017-08-13 18:16:25 +01:00
										 |  |  | 		{`autopath @kubernetes`, false, "", "kubernetes", nil, ""}, | 
					
						
							|  |  |  | 		{`autopath example.org @kubernetes`, false, "example.org.", "kubernetes", nil, ""}, | 
					
						
							|  |  |  | 		{`autopath 10.0.0.0/8 @kubernetes`, false, "10.in-addr.arpa.", "kubernetes", nil, ""}, | 
					
						
							|  |  |  | 		{`autopath ` + resolv, false, "", "", []string{"bar.com.", "baz.com.", ""}, ""}, | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 		// negative | 
					
						
							| 
									
										
										
										
											2017-08-13 18:16:25 +01:00
										 |  |  | 		{`autopath kubernetes`, true, "", "", nil, "open kubernetes: no such file or directory"}, | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  | 		{`autopath`, true, "", "", nil, "no resolv-conf"}, | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, test := range tests { | 
					
						
							|  |  |  | 		c := caddy.NewTestController("dns", test.input) | 
					
						
							|  |  |  | 		ap, mw, err := autoPathParse(c) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if test.shouldErr && err == nil { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected error but found %s for input %s", i, err, test.input) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			if !test.shouldErr { | 
					
						
							|  |  |  | 				t.Errorf("Test %d: Expected no error but found one for input %s. Error was: %v", i, test.input, err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if !strings.Contains(err.Error(), test.expectedErrContent) { | 
					
						
							|  |  |  | 				t.Errorf("Test %d: Expected error to contain: %v, found error: %v, input: %s", i, test.expectedErrContent, err, test.input) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if !test.shouldErr && mw != test.expectedMw { | 
					
						
							| 
									
										
										
										
											2017-09-16 14:13:28 +01:00
										 |  |  | 			t.Errorf("Test %d, Plugin not correctly set for input %s. Expected: %s, actual: %s", i, test.input, test.expectedMw, mw) | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if !test.shouldErr && ap.search != nil { | 
					
						
							|  |  |  | 			if !reflect.DeepEqual(test.expectedSearch, ap.search) { | 
					
						
							|  |  |  | 				t.Errorf("Test %d, wrong searchpath for input %s. Expected: '%v', actual: '%v'", i, test.input, test.expectedSearch, ap.search) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-08-13 18:16:25 +01:00
										 |  |  | 		if !test.shouldErr && test.expectedZone != "" { | 
					
						
							|  |  |  | 			if test.expectedZone != ap.Zones[0] { | 
					
						
							|  |  |  | 				t.Errorf("Test %d, expected zone %q for input %s, got: %q", i, test.expectedZone, test.input, ap.Zones[0]) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const resolvConf = `nameserver 1.2.3.4 | 
					
						
							|  |  |  | domain foo.com | 
					
						
							|  |  |  | search bar.com baz.com | 
					
						
							|  |  |  | options ndots:5 | 
					
						
							|  |  |  | ` |