| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | package auto | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2025-06-05 00:37:52 +03:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2019-02-17 23:51:47 +09:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 18:14:41 +02:00
										 |  |  | 	"github.com/coredns/caddy" | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestAutoParse(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2025-06-05 00:37:52 +03:00
										 |  |  | 	t.Parallel() | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	tests := []struct { | 
					
						
							| 
									
										
										
										
											2019-02-17 23:51:47 +09:00
										 |  |  | 		inputFileRules         string | 
					
						
							|  |  |  | 		shouldErr              bool | 
					
						
							|  |  |  | 		expectedDirectory      string | 
					
						
							|  |  |  | 		expectedTempl          string | 
					
						
							|  |  |  | 		expectedRe             string | 
					
						
							|  |  |  | 		expectedReloadInterval time.Duration | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`auto example.org { | 
					
						
							|  |  |  | 				directory /tmp | 
					
						
							|  |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2020-09-24 11:30:39 -07:00
										 |  |  | 			false, "/tmp", "${1}", `db\.(.*)`, 60 * time.Second, | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-08-13 18:16:25 +01:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			`auto 10.0.0.0/24 { | 
					
						
							|  |  |  | 				directory /tmp | 
					
						
							|  |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2020-09-24 11:30:39 -07:00
										 |  |  | 			false, "/tmp", "${1}", `db\.(.*)`, 60 * time.Second, | 
					
						
							| 
									
										
										
										
											2017-08-13 18:16:25 +01:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			`auto { | 
					
						
							|  |  |  | 				directory /tmp | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 				reload 0 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2020-09-24 11:30:39 -07:00
										 |  |  | 			false, "/tmp", "${1}", `db\.(.*)`, 0 * time.Second, | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`auto { | 
					
						
							|  |  |  | 				directory /tmp (.*) bliep | 
					
						
							|  |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2020-09-24 11:30:39 -07:00
										 |  |  | 			false, "/tmp", "bliep", `(.*)`, 60 * time.Second, | 
					
						
							| 
									
										
										
										
											2019-02-17 23:51:47 +09:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`auto { | 
					
						
							|  |  |  | 				directory /tmp (.*) bliep | 
					
						
							|  |  |  | 				reload 10s | 
					
						
							|  |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2020-09-24 11:30:39 -07:00
										 |  |  | 			false, "/tmp", "bliep", `(.*)`, 10 * time.Second, | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		// errors | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 		// NO_RELOAD has been deprecated. | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 			`auto { | 
					
						
							|  |  |  | 				directory /tmp | 
					
						
							|  |  |  | 				no_reload | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2020-09-24 11:30:39 -07:00
										 |  |  | 			true, "/tmp", "${1}", `db\.(.*)`, 0 * time.Second, | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 		// TIMEOUT has been deprecated. | 
					
						
							| 
									
										
										
										
											2017-08-22 14:52:20 +01:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 			`auto { | 
					
						
							|  |  |  | 				directory /tmp (.*) bliep 10 | 
					
						
							| 
									
										
										
										
											2017-08-22 14:52:20 +01:00
										 |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2020-09-24 11:30:39 -07:00
										 |  |  | 			true, "/tmp", "bliep", `(.*)`, 10 * time.Second, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// TRANSFER has been deprecated. | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`auto { | 
					
						
							|  |  |  | 				directory /tmp (.*) bliep 10 | 
					
						
							|  |  |  | 				transfer to 127.0.0.1 | 
					
						
							|  |  |  | 			}`, | 
					
						
							|  |  |  | 			true, "/tmp", "bliep", `(.*)`, 10 * time.Second, | 
					
						
							| 
									
										
										
										
											2017-08-22 14:52:20 +01:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-06-13 19:07:41 +08:00
										 |  |  | 		// no template specified. | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`auto { | 
					
						
							|  |  |  | 				directory /tmp (.*) | 
					
						
							|  |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2020-09-24 11:30:39 -07:00
										 |  |  | 			true, "/tmp", "", `(.*)`, 60 * time.Second, | 
					
						
							| 
									
										
										
										
											2019-06-13 19:07:41 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 		// no directory specified. | 
					
						
							| 
									
										
										
										
											2017-08-22 14:52:20 +01:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			`auto example.org { | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 				directory | 
					
						
							| 
									
										
										
										
											2017-08-22 14:52:20 +01:00
										 |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2020-09-24 11:30:39 -07:00
										 |  |  | 			true, "", "${1}", `db\.(.*)`, 60 * time.Second, | 
					
						
							| 
									
										
										
										
											2017-08-22 14:52:20 +01:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 		// illegal REGEXP. | 
					
						
							| 
									
										
										
										
											2017-08-22 14:52:20 +01:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			`auto example.org { | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 				directory /tmp * {1} | 
					
						
							| 
									
										
										
										
											2017-08-22 14:52:20 +01:00
										 |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2020-09-24 11:30:39 -07:00
										 |  |  | 			true, "/tmp", "${1}", ``, 60 * time.Second, | 
					
						
							| 
									
										
										
										
											2017-08-22 14:52:20 +01:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2025-06-05 00:37:52 +03:00
										 |  |  | 		// non-existent directory. | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`auto example.org { | 
					
						
							|  |  |  | 				directory /foobar/coredns * {1} | 
					
						
							|  |  |  | 			}`, | 
					
						
							|  |  |  | 			true, "/tmp", "${1}", ``, 60 * time.Second, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 		// unexpected argument. | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			`auto example.org { | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 				directory /tmp (.*) {1} aa | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2020-09-24 11:30:39 -07:00
										 |  |  | 			true, "/tmp", "${1}", ``, 60 * time.Second, | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2025-06-05 00:37:52 +03:00
										 |  |  | 		// upstream directive should not error and should consume args | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`auto example.org { | 
					
						
							|  |  |  | 				directory /tmp | 
					
						
							|  |  |  | 				upstream 8.8.8.8 1.1.1.1 | 
					
						
							|  |  |  | 			}`, | 
					
						
							|  |  |  | 			false, "/tmp", "${1}", `db\.(.*)`, 60 * time.Second, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// upstream directive with no args should not error | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`auto example.org { | 
					
						
							|  |  |  | 				directory /tmp | 
					
						
							|  |  |  | 				upstream | 
					
						
							|  |  |  | 			}`, | 
					
						
							|  |  |  | 			false, "/tmp", "${1}", `db\.(.*)`, 60 * time.Second, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, test := range tests { | 
					
						
							| 
									
										
										
										
											2025-06-05 00:37:52 +03:00
										 |  |  | 		t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) { | 
					
						
							|  |  |  | 			t.Parallel() | 
					
						
							|  |  |  | 			c := caddy.NewTestController("dns", test.inputFileRules) | 
					
						
							|  |  |  | 			a, err := autoParse(c) | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-05 00:37:52 +03:00
										 |  |  | 			if err == nil && test.shouldErr { | 
					
						
							|  |  |  | 				t.Fatalf("Test %d expected errors, but got no error", i) | 
					
						
							|  |  |  | 			} else if err != nil && !test.shouldErr { | 
					
						
							|  |  |  | 				t.Fatalf("Test %d expected no errors, but got '%v'", i, err) | 
					
						
							|  |  |  | 			} else if !test.shouldErr { | 
					
						
							|  |  |  | 				if a.directory != test.expectedDirectory { | 
					
						
							|  |  |  | 					t.Fatalf("Test %d expected %v, got %v", i, test.expectedDirectory, a.directory) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if a.template != test.expectedTempl { | 
					
						
							|  |  |  | 					t.Fatalf("Test %d expected %v, got %v", i, test.expectedTempl, a.template) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if a.re.String() != test.expectedRe { | 
					
						
							|  |  |  | 					t.Fatalf("Test %d expected %v, got %v", i, test.expectedRe, a.re) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if a.ReloadInterval != test.expectedReloadInterval { | 
					
						
							|  |  |  | 					t.Fatalf("Test %d expected %v, got %v", i, test.expectedReloadInterval, a.ReloadInterval) | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2025-06-05 00:37:52 +03:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-11-20 00:19:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestSetupReload(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2025-06-05 00:37:52 +03:00
										 |  |  | 	t.Parallel() | 
					
						
							| 
									
										
										
										
											2021-11-20 00:19:25 +08:00
										 |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		name    string | 
					
						
							|  |  |  | 		config  string | 
					
						
							|  |  |  | 		wantErr bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: "reload valid", | 
					
						
							|  |  |  | 			config: `auto { | 
					
						
							|  |  |  | 				directory . | 
					
						
							|  |  |  | 				reload 5s | 
					
						
							|  |  |  | 			}`, | 
					
						
							|  |  |  | 			wantErr: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: "reload disable", | 
					
						
							|  |  |  | 			config: `auto { | 
					
						
							|  |  |  | 				directory . | 
					
						
							|  |  |  | 				reload 0 | 
					
						
							|  |  |  | 			}`, | 
					
						
							|  |  |  | 			wantErr: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: "reload invalid", | 
					
						
							|  |  |  | 			config: `auto { | 
					
						
							|  |  |  | 				directory . | 
					
						
							|  |  |  | 				reload -1s | 
					
						
							|  |  |  | 			}`, | 
					
						
							|  |  |  | 			wantErr: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: "reload invalid", | 
					
						
							|  |  |  | 			config: `auto { | 
					
						
							|  |  |  | 				directory . | 
					
						
							|  |  |  | 				reload | 
					
						
							|  |  |  | 			}`, | 
					
						
							|  |  |  | 			wantErr: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for _, tt := range tests { | 
					
						
							|  |  |  | 		t.Run(tt.name, func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2025-06-05 00:37:52 +03:00
										 |  |  | 			t.Parallel() | 
					
						
							| 
									
										
										
										
											2021-11-20 00:19:25 +08:00
										 |  |  | 			ctr := caddy.NewTestController("dns", tt.config) | 
					
						
							|  |  |  | 			if err := setup(ctr); (err != nil) != tt.wantErr { | 
					
						
							|  |  |  | 				t.Errorf("Error: setup() error = %v, wantErr %v", err, tt.wantErr) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |