github.com/Jeffail/benthos/v3@v3.65.0/lib/processor/parse_log_test.go (about) 1 package processor 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/Jeffail/benthos/v3/lib/log" 9 "github.com/Jeffail/benthos/v3/lib/message" 10 "github.com/Jeffail/benthos/v3/lib/metrics" 11 ) 12 13 func TestParseLogCases(t *testing.T) { 14 type testCase struct { 15 name string 16 input string 17 output string 18 format string 19 codec string 20 bestEff bool 21 } 22 tests := []testCase{ 23 { 24 name: "valid syslog_rfc5424 input, valid json output", 25 format: "syslog_rfc5424", 26 codec: "json", 27 bestEff: true, 28 input: `<42>4 2049-10-11T22:14:15.003Z toaster.smarthome myapp - 2 [home01 device_id="43"] failed to make a toast.`, 29 output: `{"appname":"myapp","facility":5,"hostname":"toaster.smarthome","message":"failed to make a toast.","msgid":"2","priority":42,"severity":2,"structureddata":{"home01":{"device_id":"43"}},"timestamp":"2049-10-11T22:14:15.003Z","version":4}`, 30 }, 31 { 32 name: "invalid syslog_rfc5424 input, invalid json output", 33 format: "syslog_rfc5424", 34 codec: "json", 35 bestEff: true, 36 input: `not a syslog at all.`, 37 output: `not a syslog at all.`, 38 }, 39 { 40 name: "valid syslog_rfc3164 input, valid json output", 41 format: "syslog_rfc3164", 42 codec: "json", 43 bestEff: true, 44 input: `<28>Dec 2 16:49:23 host app[23410]: Test`, 45 output: fmt.Sprintf(`{"appname":"app","facility":3,"hostname":"host","message":"Test","priority":28,"procid":"23410","severity":4,"timestamp":"%v-12-02T16:49:23Z"}`, time.Now().Year()), 46 }, 47 } 48 49 for _, test := range tests { 50 conf := NewConfig() 51 conf.ParseLog.Format = test.format 52 conf.ParseLog.Codec = test.codec 53 conf.ParseLog.BestEffort = test.bestEff 54 proc, err := NewParseLog(conf, nil, log.Noop(), metrics.Noop()) 55 if err != nil { 56 t.Fatal(err) 57 } 58 t.Run(test.name, func(tt *testing.T) { 59 msgsOut, res := proc.ProcessMessage(message.New([][]byte{[]byte(test.input)})) 60 if res != nil { 61 tt.Fatal(res.Error()) 62 } 63 if len(msgsOut) != 1 { 64 tt.Fatalf("Wrong count of result messages: %v != 1", len(msgsOut)) 65 } 66 if exp, act := test.output, string(msgsOut[0].Get(0).Get()); exp != act { 67 tt.Errorf("Wrong result: %v != %v", act, exp) 68 } 69 }) 70 } 71 } 72 73 func TestParseLogRFC5424(t *testing.T) { 74 type testCase struct { 75 name string 76 input string 77 output string 78 } 79 tests := []testCase{ 80 { 81 name: "valid syslog_rfc5424 1", 82 input: `<42>4 2049-10-11T22:14:15.003Z toaster.smarthome myapp - 2 [home01 device_id="43"] failed to make a toast.`, 83 output: `{"appname":"myapp","facility":5,"hostname":"toaster.smarthome","message":"failed to make a toast.","msgid":"2","priority":42,"severity":2,"structureddata":{"home01":{"device_id":"43"}},"timestamp":"2049-10-11T22:14:15.003Z","version":4}`, 84 }, 85 { 86 name: "valid syslog_rfc5424 2", 87 input: `<23>4 2032-10-11T22:14:15.003Z foo.bar baz - 10 [home02 device_id="44"] test log.`, 88 output: `{"appname":"baz","facility":2,"hostname":"foo.bar","message":"test log.","msgid":"10","priority":23,"severity":7,"structureddata":{"home02":{"device_id":"44"}},"timestamp":"2032-10-11T22:14:15.003Z","version":4}`, 89 }, 90 } 91 92 conf := NewConfig() 93 conf.ParseLog.Format = "syslog_rfc5424" 94 conf.ParseLog.BestEffort = true 95 proc, err := NewParseLog(conf, nil, log.Noop(), metrics.Noop()) 96 if err != nil { 97 t.Fatal(err) 98 } 99 100 for _, test := range tests { 101 t.Run(test.name, func(tt *testing.T) { 102 msgsOut, res := proc.ProcessMessage(message.New([][]byte{[]byte(test.input)})) 103 if res != nil { 104 tt.Fatal(res.Error()) 105 } 106 if len(msgsOut) != 1 { 107 tt.Fatalf("Wrong count of result messages: %v != 1", len(msgsOut)) 108 } 109 if exp, act := test.output, string(msgsOut[0].Get(0).Get()); exp != act { 110 tt.Errorf("Wrong result: %v != %v", act, exp) 111 } 112 }) 113 } 114 }