| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | package log | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:16 +03:00
										 |  |  | 	"reflect" | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/response" | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 09:04:47 +08:00
										 |  |  | 	"github.com/caddyserver/caddy" | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 			Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 			Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 			Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 			Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 			Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}, { | 
					
						
							| 
									
										
										
										
											2017-11-10 15:17:12 +00:00
										 |  |  | 			NameScope: "example.net.", | 
					
						
							|  |  |  | 			Format:    CombinedLogFormat, | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 			Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							| 
									
										
										
										
											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}", | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 			Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}, { | 
					
						
							| 
									
										
										
										
											2017-11-10 15:17:12 +00:00
										 |  |  | 			NameScope: "example.org.", | 
					
						
							|  |  |  | 			Format:    "{when}", | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 			Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}}}, | 
					
						
							| 
									
										
										
										
											2019-01-08 15:40:50 +08:00
										 |  |  | 		{`log example.org example.net`, false, []Rule{{ | 
					
						
							|  |  |  | 			NameScope: "example.org.", | 
					
						
							|  |  |  | 			Format:    DefaultLogFormat, | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 			Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							| 
									
										
										
										
											2019-01-08 15:40:50 +08:00
										 |  |  | 		}, { | 
					
						
							|  |  |  | 			NameScope: "example.net.", | 
					
						
							|  |  |  | 			Format:    DefaultLogFormat, | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 			Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							| 
									
										
										
										
											2019-01-08 15:40:50 +08:00
										 |  |  | 		}}}, | 
					
						
							|  |  |  | 		{`log example.org example.net {host}`, false, []Rule{{ | 
					
						
							|  |  |  | 			NameScope: "example.org.", | 
					
						
							|  |  |  | 			Format:    "{host}", | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 			Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							| 
									
										
										
										
											2019-01-08 15:40:50 +08:00
										 |  |  | 		}, { | 
					
						
							|  |  |  | 			NameScope: "example.net.", | 
					
						
							|  |  |  | 			Format:    "{host}", | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 			Class:     map[response.Class]struct{}{response.All: {}}, | 
					
						
							| 
									
										
										
										
											2019-01-08 15:40:50 +08:00
										 |  |  | 		}}}, | 
					
						
							|  |  |  | 		{`log example.org example.net {when} { | 
					
						
							|  |  |  | 			class denial | 
					
						
							|  |  |  | 		}`, false, []Rule{{ | 
					
						
							|  |  |  | 			NameScope: "example.org.", | 
					
						
							|  |  |  | 			Format:    "{when}", | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 			Class:     map[response.Class]struct{}{response.Denial: {}}, | 
					
						
							| 
									
										
										
										
											2019-01-08 15:40:50 +08:00
										 |  |  | 		}, { | 
					
						
							|  |  |  | 			NameScope: "example.net.", | 
					
						
							|  |  |  | 			Format:    "{when}", | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 			Class:     map[response.Class]struct{}{response.Denial: {}}, | 
					
						
							| 
									
										
										
										
											2019-01-08 15:40:50 +08: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, | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 			Class:     map[response.Class]struct{}{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, | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 			Class:     map[response.Class]struct{}{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, | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 			Class:     map[response.Class]struct{}{response.Denial: {}}, | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 		}}}, | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:16 +03:00
										 |  |  | 		{`log { | 
					
						
							|  |  |  | 			class denial error | 
					
						
							|  |  |  | 		}`, false, []Rule{{ | 
					
						
							|  |  |  | 			NameScope: ".", | 
					
						
							|  |  |  | 			Format:    CommonLogFormat, | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 			Class:     map[response.Class]struct{}{response.Denial: {}, response.Error: {}}, | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:16 +03:00
										 |  |  | 		}}}, | 
					
						
							|  |  |  | 		{`log { | 
					
						
							|  |  |  | 			class denial | 
					
						
							|  |  |  | 			class error | 
					
						
							|  |  |  | 		}`, false, []Rule{{ | 
					
						
							|  |  |  | 			NameScope: ".", | 
					
						
							|  |  |  | 			Format:    CommonLogFormat, | 
					
						
							| 
									
										
										
										
											2019-04-08 11:13:46 +01:00
										 |  |  | 			Class:     map[response.Class]struct{}{response.Denial: {}, response.Error: {}}, | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:16 +03:00
										 |  |  | 		}}}, | 
					
						
							|  |  |  | 		{`log { | 
					
						
							|  |  |  | 			class abracadabra | 
					
						
							| 
									
										
										
										
											2018-04-20 11:01:06 +01:00
										 |  |  | 		}`, true, []Rule{}}, | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:16 +03:00
										 |  |  | 		{`log { | 
					
						
							|  |  |  | 			class | 
					
						
							| 
									
										
										
										
											2018-04-20 11:01:06 +01:00
										 |  |  | 		}`, true, []Rule{}}, | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:16 +03:00
										 |  |  | 		{`log { | 
					
						
							|  |  |  | 			unknown | 
					
						
							| 
									
										
										
										
											2018-04-20 11:01:06 +01:00
										 |  |  | 		}`, true, []Rule{}}, | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:16 +03:00
										 |  |  | 			if !reflect.DeepEqual(actualLogRule.Class, test.expectedLogRules[j].Class) { | 
					
						
							|  |  |  | 				t.Errorf("Test %d expected %dth LogRule Class to be  %v  , but got %v", | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 					i, j, test.expectedLogRules[j].Class, actualLogRule.Class) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |