| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | package auto
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/mholt/caddy"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestAutoParse(t *testing.T) {
 | 
					
						
							|  |  |  | 	tests := []struct {
 | 
					
						
							|  |  |  | 		inputFileRules    string
 | 
					
						
							|  |  |  | 		shouldErr         bool
 | 
					
						
							|  |  |  | 		expectedDirectory string
 | 
					
						
							|  |  |  | 		expectedTempl     string
 | 
					
						
							|  |  |  | 		expectedRe        string
 | 
					
						
							| 
									
										
										
										
											2016-11-07 19:15:21 +00:00
										 |  |  | 		expectedTo        []string
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	}{
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`auto example.org {
 | 
					
						
							|  |  |  | 				directory /tmp
 | 
					
						
							|  |  |  | 				transfer to 127.0.0.1
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							| 
									
										
										
										
											2016-11-07 19:15:21 +00:00
										 |  |  | 			false, "/tmp", "${1}", `db\.(.*)`, []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
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							|  |  |  | 			false, "/tmp", "${1}", `db\.(.*)`, nil,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		{
 | 
					
						
							|  |  |  | 			`auto {
 | 
					
						
							|  |  |  | 				directory /tmp
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							| 
									
										
										
										
											2016-11-07 19:15:21 +00:00
										 |  |  | 			false, "/tmp", "${1}", `db\.(.*)`, nil,
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`auto {
 | 
					
						
							|  |  |  | 				directory /tmp (.*) bliep
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							| 
									
										
										
										
											2016-11-07 19:15:21 +00:00
										 |  |  | 			false, "/tmp", "bliep", `(.*)`, nil,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`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
										 |  |  | 			}`,
 | 
					
						
							|  |  |  | 			false, "/tmp", "bliep", `(.*)`, []string{"127.0.0.1:53", "127.0.0.2:53"},
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 		// errors
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`auto example.org {
 | 
					
						
							|  |  |  | 				directory
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							| 
									
										
										
										
											2016-11-07 19:15:21 +00:00
										 |  |  | 			true, "", "${1}", `db\.(.*)`, nil,
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`auto example.org {
 | 
					
						
							|  |  |  | 				directory /tmp * {1}
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							| 
									
										
										
										
											2016-11-07 19:15:21 +00:00
										 |  |  | 			true, "", "${1}", ``, nil,
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			`auto example.org {
 | 
					
						
							|  |  |  | 				directory /tmp .* {1}
 | 
					
						
							|  |  |  | 			}`,
 | 
					
						
							| 
									
										
										
										
											2016-11-07 19:15:21 +00:00
										 |  |  | 			true, "", "${1}", ``, 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)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |