-
Notifications
You must be signed in to change notification settings - Fork 106
/
outputcond_test.go
87 lines (76 loc) · 2.04 KB
/
outputcond_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package outputcond
import (
"context"
"strings"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tsaikd/gogstash/config"
"github.com/tsaikd/gogstash/config/goglog"
"github.com/tsaikd/gogstash/config/logevent"
outputstdout "github.com/tsaikd/gogstash/output/stdout"
)
func init() {
goglog.Logger.SetLevel(logrus.DebugLevel)
config.RegistOutputHandler(outputstdout.ModuleName, outputstdout.InitHandler)
config.RegistOutputHandler(ModuleName, InitHandler)
}
func Test_filter_cond_module_invalid(t *testing.T) {
assert := assert.New(t)
assert.NotNil(assert)
require := require.New(t)
require.NotNil(require)
conf, err := config.LoadFromYAML([]byte(strings.TrimSpace(`
debugch: true
output:
- type: cond
condition: "!'level' == 'ERROR'"
output:
- type: add_field
key: foo
value: bar
`)))
require.NoError(err)
_, err = InitHandler(context.TODO(), conf.OutputRaw[0], nil)
require.Error(err)
}
func Test_output_cond_module(t *testing.T) {
assert := assert.New(t)
assert.NotNil(assert)
require := require.New(t)
require.NotNil(require)
ctx := context.Background()
conf, err := config.LoadFromYAML([]byte(strings.TrimSpace(`
debugch: true
output:
- type: cond
condition: "level == 'ERROR'"
output:
- type: stdout
`)))
require.NoError(err)
require.NoError(conf.Start(ctx))
conf.TestInputEvent(logevent.LogEvent{
Timestamp: time.Now(),
Message: "outputstdout test message",
Extra: map[string]any{
"level": "ERROR",
"foo": "bar",
},
})
if event, err := conf.TestGetOutputEvent(300 * time.Millisecond); assert.NoError(err) {
require.Equal("outputstdout test message", event.Message)
}
conf.TestInputEvent(logevent.LogEvent{
Timestamp: time.Now(),
Message: "outputstdout test message",
Extra: map[string]any{
"level": "WARN",
},
})
if event, err := conf.TestGetOutputEvent(300 * time.Millisecond); assert.NoError(err) {
require.Equal("outputstdout test message", event.Message)
}
}