-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace_test.go
67 lines (60 loc) · 1.83 KB
/
replace_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
package main
import (
"testing"
"strconv"
"github.com/stretchr/testify/assert"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
)
func newTestReplace(t testing.TB, config ReplaceConfig, ) *Replace {
c, err := common.NewConfigFrom(map[string]interface{}{
"field": config.Field,
"target": config.Target,
"find": config.Find,
"replace": config.Replace,
"regex": strconv.FormatBool(config.Regex),
})
if err != nil {
t.Fatal(err)
}
r, err := New(c)
if err != nil {
t.Fatal(err)
}
return r.(*Replace)
}
func TestReplaceHashes(t *testing.T) {
var tests = []ReplaceConfig {
{Find: "FIND_ME", Replace: "REPLACED"},
{Field: "message", Target: "message", Find: "FIND_ME", Replace: "REPLACED"},
{Field: "message1", Target: "message1", Find: "FIND_ME", Replace: "REPLACED"},
{Field: "message1", Target: "message1", Find: "[^\\w]", Replace: " ", Regex: true},
}
var testEvents = []struct {
fieldName string
fieldContents string
}{
{"message", "some message with -> FIND_ME <-"},
{"message", "some message with -> FIND_ME <-"},
{"message1", "some message with -> FIND_ME <-"},
{"message1", "some message with -> FIND_ME <-"},
}
var results = []struct {
targetFieldName string
targetFieldContents string
}{
{"message", "some message with -> REPLACED <-"},
{"message", "some message with -> REPLACED <-"},
{"message1", "some message with -> REPLACED <-"},
{"message1", "some message with FIND_ME "},
}
for i := range tests {
f := newTestReplace(t, tests[i])
event := &beat.Event{Fields: common.MapStr{testEvents[i].fieldName: testEvents[i].fieldContents}}
event, err := f.Run(event)
if assert.NoError(t, err) {
t.Logf("Running test case %d", i)
assert.Equal(t, results[i].targetFieldContents, event.Fields[results[i].targetFieldName])
}
}
}