| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/mholt/caddy"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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
 | 
					
						
							|  |  |  | 		expectedDuration       time.Duration
 | 
					
						
							|  |  |  | 		expectedTo             []string
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	}{
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`auto example.org {
 | 
					
						
							|  |  |  | 				directory /tmp
 | 
					
						
							|  |  |  | 				transfer to 127.0.0.1
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							| 
									
										
										
										
											2019-02-17 23:51:47 +09:00
										 |  |  | 			false, "/tmp", "${1}", `db\.(.*)`, 60 * time.Second, 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-02-17 23:51:47 +09:00
										 |  |  | 			false, "/tmp", "${1}", `db\.(.*)`, 60 * time.Second, 60 * time.Second, nil,
 | 
					
						
							| 
									
										
										
										
											2017-08-13 18:16:25 +01:00
										 |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		{
 | 
					
						
							|  |  |  | 			`auto {
 | 
					
						
							|  |  |  | 				directory /tmp
 | 
					
						
							| 
									
										
										
										
											2017-08-22 14:52:20 +01:00
										 |  |  | 				no_reload
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 			}`,
 | 
					
						
							| 
									
										
										
										
											2019-02-17 23:51:47 +09:00
										 |  |  | 			false, "/tmp", "${1}", `db\.(.*)`, 0 * time.Second, 0 * time.Second, nil,
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`auto {
 | 
					
						
							|  |  |  | 				directory /tmp (.*) bliep
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							| 
									
										
										
										
											2019-02-17 23:51:47 +09:00
										 |  |  | 			false, "/tmp", "bliep", `(.*)`, 60 * time.Second, 60 * time.Second, nil,
 | 
					
						
							| 
									
										
										
										
											2016-11-07 19:15:21 +00:00
										 |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2017-08-22 14:52:20 +01:00
										 |  |  | 		{
 | 
					
						
							|  |  |  | 			`auto {
 | 
					
						
							|  |  |  | 				directory /tmp (.*) bliep 10
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							| 
									
										
										
										
											2019-02-17 23:51:47 +09:00
										 |  |  | 			false, "/tmp", "bliep", `(.*)`, 10 * time.Second, 10 * time.Second, nil,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`auto {
 | 
					
						
							|  |  |  | 				directory /tmp (.*) bliep
 | 
					
						
							|  |  |  | 				reload 10s
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							|  |  |  | 			false, "/tmp", "bliep", `(.*)`, 10 * time.Second, 10 * time.Second, nil,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`auto {
 | 
					
						
							|  |  |  | 				directory /tmp (.*) bliep 20
 | 
					
						
							|  |  |  | 				reload 10s
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							|  |  |  | 			false, "/tmp", "bliep", `(.*)`, 10 * time.Second, 20 * 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
 | 
					
						
							| 
									
										
										
										
											2016-11-10 12:58:40 +00:00
										 |  |  | 				upstream 8.8.8.8
 | 
					
						
							| 
									
										
										
										
											2016-11-07 19:15:21 +00:00
										 |  |  | 			}`,
 | 
					
						
							| 
									
										
										
										
											2019-02-17 23:51:47 +09:00
										 |  |  | 			false, "/tmp", "bliep", `(.*)`, 60 * time.Second, 60 * time.Second, []string{"127.0.0.1:53", "127.0.0.2:53"},
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 		// errors
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`auto example.org {
 | 
					
						
							|  |  |  | 				directory
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							| 
									
										
										
										
											2019-02-17 23:51:47 +09:00
										 |  |  | 			true, "", "${1}", `db\.(.*)`, 60 * time.Second, 60 * time.Second, nil,
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`auto example.org {
 | 
					
						
							|  |  |  | 				directory /tmp * {1}
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							| 
									
										
										
										
											2019-02-17 23:51:47 +09:00
										 |  |  | 			true, "", "${1}", ``, 60 * time.Second, 60 * time.Second, nil,
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2017-08-22 14:52:20 +01:00
										 |  |  | 		{
 | 
					
						
							|  |  |  | 			`auto example.org {
 | 
					
						
							|  |  |  | 				directory /tmp * {1} aa
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							| 
									
										
										
										
											2019-02-17 23:51:47 +09:00
										 |  |  | 			true, "", "${1}", ``, 60 * time.Second, 60 * time.Second, nil,
 | 
					
						
							| 
									
										
										
										
											2017-08-22 14:52:20 +01:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`auto example.org {
 | 
					
						
							|  |  |  | 				directory /tmp .* {1}
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							| 
									
										
										
										
											2019-02-17 23:51:47 +09:00
										 |  |  | 			true, "", "${1}", ``, 60 * time.Second, 60 * time.Second, nil,
 | 
					
						
							| 
									
										
										
										
											2017-08-22 14:52:20 +01:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`auto example.org {
 | 
					
						
							|  |  |  | 				directory /tmp .* {1}
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							| 
									
										
										
										
											2019-02-17 23:51:47 +09:00
										 |  |  | 			true, "", "${1}", ``, 60 * time.Second, 60 * time.Second, nil,
 | 
					
						
							| 
									
										
										
										
											2017-08-22 14:52:20 +01:00
										 |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		{
 | 
					
						
							|  |  |  | 			`auto example.org {
 | 
					
						
							|  |  |  | 				directory /tmp .* {1}
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							| 
									
										
										
										
											2019-02-17 23:51:47 +09:00
										 |  |  | 			true, "", "${1}", ``, 60 * time.Second, 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)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			if a.loader.duration != test.expectedDuration {
 | 
					
						
							|  |  |  | 				t.Fatalf("Test %d expected %v, got %v", i, test.expectedDuration, a.loader.duration)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |