github.com/observiq/carbon@v0.9.11-0.20200820160507-1b872e368a5e/operator/builtin/parser/syslog_test.go (about) 1 package parser 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/observiq/carbon/entry" 9 "github.com/observiq/carbon/operator" 10 "github.com/observiq/carbon/testutil" 11 "github.com/stretchr/testify/mock" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestSyslogParser(t *testing.T) { 16 basicConfig := func() *SyslogParserConfig { 17 cfg := NewSyslogParserConfig("test_operator_id") 18 cfg.OutputIDs = []string{"output1"} 19 return cfg 20 } 21 22 cases := []struct { 23 name string 24 config *SyslogParserConfig 25 inputRecord interface{} 26 expectedTimestamp time.Time 27 expectedRecord interface{} 28 }{ 29 { 30 "RFC3164", 31 func() *SyslogParserConfig { 32 cfg := basicConfig() 33 cfg.Protocol = "rfc3164" 34 return cfg 35 }(), 36 "<34>Jan 12 06:30:00 1.2.3.4 apache_server: test message", 37 time.Date(time.Now().Year(), 1, 12, 6, 30, 0, 0, time.UTC), 38 map[string]interface{}{ 39 "appname": "apache_server", 40 "facility": 4, 41 "hostname": "1.2.3.4", 42 "message": "test message", 43 "priority": 34, 44 "severity": 2, 45 }, 46 }, 47 { 48 "RFC3164Bytes", 49 func() *SyslogParserConfig { 50 cfg := basicConfig() 51 cfg.Protocol = "rfc3164" 52 return cfg 53 }(), 54 []byte("<34>Jan 12 06:30:00 1.2.3.4 apache_server: test message"), 55 time.Date(time.Now().Year(), 1, 12, 6, 30, 0, 0, time.UTC), 56 map[string]interface{}{ 57 "appname": "apache_server", 58 "facility": 4, 59 "hostname": "1.2.3.4", 60 "message": "test message", 61 "priority": 34, 62 "severity": 2, 63 }, 64 }, 65 { 66 "RFC5424", 67 func() *SyslogParserConfig { 68 cfg := basicConfig() 69 cfg.Protocol = "rfc5424" 70 return cfg 71 }(), 72 `<86>1 2015-08-05T21:58:59.693Z 192.168.2.132 SecureAuth0 23108 ID52020 [SecureAuth@27389 UserHostAddress="192.168.2.132" Realm="SecureAuth0" UserID="Tester2" PEN="27389"] Found the user for retrieving user's profile`, 73 time.Date(2015, 8, 5, 21, 58, 59, 693000000, time.UTC), 74 map[string]interface{}{ 75 "appname": "SecureAuth0", 76 "facility": 10, 77 "hostname": "192.168.2.132", 78 "message": "Found the user for retrieving user's profile", 79 "msg_id": "ID52020", 80 "priority": 86, 81 "proc_id": "23108", 82 "severity": 6, 83 "structured_data": map[string]map[string]string{ 84 "SecureAuth@27389": { 85 "PEN": "27389", 86 "Realm": "SecureAuth0", 87 "UserHostAddress": "192.168.2.132", 88 "UserID": "Tester2", 89 }, 90 }, 91 "version": 1, 92 }, 93 }, 94 { 95 "RFC5424LongSDName", 96 func() *SyslogParserConfig { 97 cfg := basicConfig() 98 cfg.Protocol = "rfc5424" 99 return cfg 100 }(), 101 `<86>1 2015-08-05T21:58:59.693Z 192.168.2.132 SecureAuth0 23108 ID52020 [verylongsdnamethatisgreaterthan32bytes@12345 UserHostAddress="192.168.2.132"] my message`, 102 time.Date(2015, 8, 5, 21, 58, 59, 693000000, time.UTC), 103 map[string]interface{}{ 104 "appname": "SecureAuth0", 105 "facility": 10, 106 "hostname": "192.168.2.132", 107 "message": "my message", 108 "msg_id": "ID52020", 109 "priority": 86, 110 "proc_id": "23108", 111 "severity": 6, 112 "structured_data": map[string]map[string]string{ 113 "verylongsdnamethatisgreaterthan32bytes@12345": { 114 "UserHostAddress": "192.168.2.132", 115 }, 116 }, 117 "version": 1, 118 }, 119 }, 120 } 121 122 for _, tc := range cases { 123 t.Run(tc.name, func(t *testing.T) { 124 buildContext := testutil.NewBuildContext(t) 125 newOperator, err := tc.config.Build(buildContext) 126 require.NoError(t, err) 127 syslogParser := newOperator.(*SyslogParser) 128 129 mockOutput := testutil.NewMockOperator("output1") 130 entryChan := make(chan *entry.Entry, 1) 131 mockOutput.On("Process", mock.Anything, mock.Anything).Run(func(args mock.Arguments) { 132 entryChan <- args.Get(1).(*entry.Entry) 133 }).Return(nil) 134 135 err = syslogParser.SetOutputs([]operator.Operator{mockOutput}) 136 require.NoError(t, err) 137 138 newEntry := entry.New() 139 newEntry.Record = tc.inputRecord 140 err = syslogParser.Process(context.Background(), newEntry) 141 require.NoError(t, err) 142 143 select { 144 case e := <-entryChan: 145 require.Equal(t, e.Record, tc.expectedRecord) 146 require.Equal(t, tc.expectedTimestamp, e.Timestamp) 147 case <-time.After(time.Second): 148 require.FailNow(t, "Timed out waiting for entry to be processed") 149 } 150 }) 151 } 152 }