| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | package auto | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2019-02-17 23:51:47 +09:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 09:04:47 +08:00
										 |  |  | 	"github.com/caddyserver/caddy" | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestAutoParse(t *testing.T) { | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							| 
									
										
										
										
											2019-02-17 23:51:47 +09:00
										 |  |  | 		inputFileRules         string | 
					
						
							|  |  |  | 		shouldErr              bool | 
					
						
							|  |  |  | 		expectedDirectory      string | 
					
						
							|  |  |  | 		expectedTempl          string | 
					
						
							|  |  |  | 		expectedRe             string | 
					
						
							|  |  |  | 		expectedReloadInterval time.Duration | 
					
						
							|  |  |  | 		expectedTo             []string | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`auto example.org { | 
					
						
							|  |  |  | 				directory /tmp | 
					
						
							|  |  |  | 				transfer to 127.0.0.1 | 
					
						
							|  |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 			false, "/tmp", "${1}", `db\.(.*)`, 60 * time.Second, []string{"127.0.0.1:53"}, | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-08-13 18:16:25 +01:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			`auto 10.0.0.0/24 { | 
					
						
							|  |  |  | 				directory /tmp | 
					
						
							|  |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 			false, "/tmp", "${1}", `db\.(.*)`, 60 * time.Second, nil, | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 			false, "/tmp", "${1}", `db\.(.*)`, 0 * time.Second, nil, | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`auto { | 
					
						
							|  |  |  | 				directory /tmp (.*) bliep | 
					
						
							|  |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 			false, "/tmp", "bliep", `(.*)`, 60 * time.Second, nil, | 
					
						
							| 
									
										
										
										
											2019-02-17 23:51:47 +09:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`auto { | 
					
						
							|  |  |  | 				directory /tmp (.*) bliep | 
					
						
							|  |  |  | 				reload 10s | 
					
						
							|  |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 			false, "/tmp", "bliep", `(.*)`, 10 * time.Second, nil, | 
					
						
							| 
									
										
										
										
											2017-08-22 14:52:20 +01:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-11-07 19:15:21 +00:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			`auto { | 
					
						
							|  |  |  | 				directory /tmp (.*) bliep | 
					
						
							|  |  |  | 				transfer to 127.0.0.1 | 
					
						
							|  |  |  | 				transfer to 127.0.0.2 | 
					
						
							|  |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 			false, "/tmp", "bliep", `(.*)`, 60 * time.Second, []string{"127.0.0.1:53", "127.0.0.2:53"}, | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 			true, "/tmp", "${1}", `db\.(.*)`, 0 * time.Second, nil, | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 			true, "/tmp", "bliep", `(.*)`, 10 * time.Second, nil, | 
					
						
							| 
									
										
										
										
											2017-08-22 14:52:20 +01:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-06-13 19:07:41 +08:00
										 |  |  | 		// no template specified. | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`auto { | 
					
						
							|  |  |  | 				directory /tmp (.*) | 
					
						
							|  |  |  | 			}`, | 
					
						
							|  |  |  | 			true, "/tmp", "", `(.*)`, 60 * time.Second, nil, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 			true, "", "${1}", `db\.(.*)`, 60 * time.Second, nil, | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 			true, "/tmp", "${1}", ``, 60 * time.Second, nil, | 
					
						
							| 
									
										
										
										
											2017-08-22 14:52:20 +01:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 			}`, | 
					
						
							| 
									
										
										
										
											2019-04-01 14:28:01 +08:00
										 |  |  | 			true, "/tmp", "${1}", ``, 60 * time.Second, nil, | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, test := range tests { | 
					
						
							|  |  |  | 		c := caddy.NewTestController("dns", test.inputFileRules) | 
					
						
							|  |  |  | 		a, err := autoParse(c) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		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.loader.directory != test.expectedDirectory { | 
					
						
							|  |  |  | 				t.Fatalf("Test %d expected %v, got %v", i, test.expectedDirectory, a.loader.directory) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if a.loader.template != test.expectedTempl { | 
					
						
							|  |  |  | 				t.Fatalf("Test %d expected %v, got %v", i, test.expectedTempl, a.loader.template) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if a.loader.re.String() != test.expectedRe { | 
					
						
							|  |  |  | 				t.Fatalf("Test %d expected %v, got %v", i, test.expectedRe, a.loader.re) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-02-17 23:51:47 +09:00
										 |  |  | 			if a.loader.ReloadInterval != test.expectedReloadInterval { | 
					
						
							|  |  |  | 				t.Fatalf("Test %d expected %v, got %v", i, test.expectedReloadInterval, a.loader.ReloadInterval) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-11-07 19:15:21 +00:00
										 |  |  | 			if test.expectedTo != nil { | 
					
						
							|  |  |  | 				for j, got := range a.loader.transferTo { | 
					
						
							|  |  |  | 					if got != test.expectedTo[j] { | 
					
						
							|  |  |  | 						t.Fatalf("Test %d expected %v, got %v", i, test.expectedTo[j], got) | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |