| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | package log
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/response"
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	"github.com/mholt/caddy"
 | 
					
						
							|  |  |  | )
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestLogParse(t *testing.T) {
 | 
					
						
							|  |  |  | 	tests := []struct {
 | 
					
						
							|  |  |  | 		inputLogRules    string
 | 
					
						
							|  |  |  | 		shouldErr        bool
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		expectedLogRules []Rule
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}{
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		{`log`, false, []Rule{{
 | 
					
						
							| 
									
										
										
										
											2017-11-10 15:17:12 +00:00
										 |  |  | 			NameScope: ".",
 | 
					
						
							|  |  |  | 			Format:    DefaultLogFormat,
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}}},
 | 
					
						
							| 
									
										
										
										
											2017-11-13 10:23:27 +01:00
										 |  |  | 		{`log example.org`, false, []Rule{{
 | 
					
						
							| 
									
										
										
										
											2017-11-10 15:17:12 +00:00
										 |  |  | 			NameScope: "example.org.",
 | 
					
						
							|  |  |  | 			Format:    DefaultLogFormat,
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}}},
 | 
					
						
							| 
									
										
										
										
											2017-11-13 10:23:27 +01:00
										 |  |  | 		{`log example.org. {common}`, false, []Rule{{
 | 
					
						
							| 
									
										
										
										
											2017-11-10 15:17:12 +00:00
										 |  |  | 			NameScope: "example.org.",
 | 
					
						
							|  |  |  | 			Format:    CommonLogFormat,
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}}},
 | 
					
						
							| 
									
										
										
										
											2017-11-13 10:23:27 +01:00
										 |  |  | 		{`log example.org {combined}`, false, []Rule{{
 | 
					
						
							| 
									
										
										
										
											2017-11-10 15:17:12 +00:00
										 |  |  | 			NameScope: "example.org.",
 | 
					
						
							|  |  |  | 			Format:    CombinedLogFormat,
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}}},
 | 
					
						
							| 
									
										
										
										
											2017-11-13 10:23:27 +01:00
										 |  |  | 		{`log example.org.
 | 
					
						
							|  |  |  | 		log example.net {combined}`, false, []Rule{{
 | 
					
						
							| 
									
										
										
										
											2017-11-10 15:17:12 +00:00
										 |  |  | 			NameScope: "example.org.",
 | 
					
						
							|  |  |  | 			Format:    DefaultLogFormat,
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}, {
 | 
					
						
							| 
									
										
										
										
											2017-11-10 15:17:12 +00:00
										 |  |  | 			NameScope: "example.net.",
 | 
					
						
							|  |  |  | 			Format:    CombinedLogFormat,
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}}},
 | 
					
						
							| 
									
										
										
										
											2017-11-13 10:23:27 +01:00
										 |  |  | 		{`log example.org {host}
 | 
					
						
							|  |  |  | 			  log example.org {when}`, false, []Rule{{
 | 
					
						
							| 
									
										
										
										
											2017-11-10 15:17:12 +00:00
										 |  |  | 			NameScope: "example.org.",
 | 
					
						
							|  |  |  | 			Format:    "{host}",
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}, {
 | 
					
						
							| 
									
										
										
										
											2017-11-10 15:17:12 +00:00
										 |  |  | 			NameScope: "example.org.",
 | 
					
						
							|  |  |  | 			Format:    "{when}",
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}}},
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-13 10:23:27 +01:00
										 |  |  | 		{`log example.org {
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 				class all
 | 
					
						
							|  |  |  | 			}`, false, []Rule{{
 | 
					
						
							| 
									
										
										
										
											2017-11-10 15:17:12 +00:00
										 |  |  | 			NameScope: "example.org.",
 | 
					
						
							|  |  |  | 			Format:    CommonLogFormat,
 | 
					
						
							|  |  |  | 			Class:     response.All,
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 		}}},
 | 
					
						
							| 
									
										
										
										
											2017-11-13 10:23:27 +01:00
										 |  |  | 		{`log example.org {
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 			class denial
 | 
					
						
							|  |  |  | 		}`, false, []Rule{{
 | 
					
						
							| 
									
										
										
										
											2017-11-10 15:17:12 +00:00
										 |  |  | 			NameScope: "example.org.",
 | 
					
						
							|  |  |  | 			Format:    CommonLogFormat,
 | 
					
						
							|  |  |  | 			Class:     response.Denial,
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 		}}},
 | 
					
						
							|  |  |  | 		{`log {
 | 
					
						
							|  |  |  | 			class denial
 | 
					
						
							|  |  |  | 		}`, false, []Rule{{
 | 
					
						
							| 
									
										
										
										
											2017-11-10 15:17:12 +00:00
										 |  |  | 			NameScope: ".",
 | 
					
						
							|  |  |  | 			Format:    CommonLogFormat,
 | 
					
						
							|  |  |  | 			Class:     response.Denial,
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 		}}},
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	for i, test := range tests {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		c := caddy.NewTestController("dns", test.inputLogRules)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		actualLogRules, err := logParse(c)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if err == nil && test.shouldErr {
 | 
					
						
							| 
									
										
										
										
											2017-11-13 10:23:27 +01:00
										 |  |  | 			t.Errorf("Test %d with input '%s' didn't error, but it should have", i, test.inputLogRules)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		} else if err != nil && !test.shouldErr {
 | 
					
						
							| 
									
										
										
										
											2017-11-13 10:23:27 +01:00
										 |  |  | 			t.Errorf("Test %d with input '%s' errored, but it shouldn't have; got '%v'",
 | 
					
						
							|  |  |  | 				i, test.inputLogRules, err)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		if len(actualLogRules) != len(test.expectedLogRules) {
 | 
					
						
							|  |  |  | 			t.Fatalf("Test %d expected %d no of Log rules, but got %d ",
 | 
					
						
							|  |  |  | 				i, len(test.expectedLogRules), len(actualLogRules))
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		for j, actualLogRule := range actualLogRules {
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 12:58:08 +00:00
										 |  |  | 			if actualLogRule.NameScope != test.expectedLogRules[j].NameScope {
 | 
					
						
							| 
									
										
										
										
											2017-11-13 10:23:27 +01:00
										 |  |  | 				t.Errorf("Test %d expected %dth LogRule NameScope for '%s' to be  %s  , but got %s",
 | 
					
						
							|  |  |  | 					i, j, test.inputLogRules, test.expectedLogRules[j].NameScope, actualLogRule.NameScope)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if actualLogRule.Format != test.expectedLogRules[j].Format {
 | 
					
						
							| 
									
										
										
										
											2017-11-13 10:23:27 +01:00
										 |  |  | 				t.Errorf("Test %d expected %dth LogRule Format for '%s' to be  %s  , but got %s",
 | 
					
						
							|  |  |  | 					i, j, test.inputLogRules, test.expectedLogRules[j].Format, actualLogRule.Format)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if actualLogRule.Class != test.expectedLogRules[j].Class {
 | 
					
						
							|  |  |  | 				t.Errorf("Test %d expected %dth LogRule Class to be  %s  , but got %s",
 | 
					
						
							|  |  |  | 					i, j, test.expectedLogRules[j].Class, actualLogRule.Class)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }
 |