| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) {
 | 
					
						
							|  |  |  | 	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
										 |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 		},
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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-10-17 18:37:56 +01:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |