-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathout_redis_test.go
156 lines (141 loc) · 4.73 KB
/
out_redis_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"testing"
"time"
"unsafe"
"github.com/fluent/fluent-bit-go/output"
"github.com/stretchr/testify/assert"
)
const (
timeFormat = "2006-01-02 15:04:05.999999999 -0700 MST"
)
func TestParseMap(t *testing.T) {
nestedMap := map[interface{}]interface{}{
"pod_name": []byte("test_pod"),
"container_name": "test_container",
"annotations": map[interface{}]interface{}{
"namespace_name": []byte("test_namespace"),
"checksum/config": "2e239b0ee49b0803c617dea3",
},
}
pm := parseMap(nestedMap)
assert.Equal(t, "test_pod", pm["pod_name"])
assert.Equal(t, "test_container", pm["container_name"])
assert.Equal(t, "test_namespace",
pm["annotations"].(map[string]interface{})["namespace_name"])
assert.Equal(t, "2e239b0ee49b0803c617dea3",
pm["annotations"].(map[string]interface{})["checksum/config"])
}
func TestCreateJSON(t *testing.T) {
record := make(map[interface{}]interface{})
record["key"] = "value"
record["five"] = 5
ts, _ := time.Parse(timeFormat, "2006-01-02 15:04:05.999999999 -0700 MST")
js, err := createJSON(ts, "atag", record)
if err != nil {
assert.Fail(t, "it is not expected that the call to createJSON fails:%v", err)
}
assert.NotNil(t, js, "json must not be nil")
result := make(map[string]interface{})
err = json.Unmarshal(js.data, &result)
if err != nil {
assert.Fail(t, "it is not expected that unmarshal of json fails:%v", err)
}
assert.Equal(t, result["@timestamp"], "2006-01-02T22:04:05.999999999Z")
assert.Equal(t, result["@tag"], "atag")
assert.Equal(t, result["key"], "value")
assert.Equal(t, result["five"], float64(5))
}
func BenchmarkCreateJSON(b *testing.B) {
record := make(map[interface{}]interface{})
record["key"] = "value"
record["five"] = 5
ts, _ := time.Parse(time.RFC3339Nano, "2006-01-02 15:04:05.999999999 -0700 MST")
for i := 0; i < b.N; i++ {
_, err := createJSON(ts, "atag", record)
assert.NoError(b, err)
}
}
type testrecord struct {
rc int
ts interface{}
data map[interface{}]interface{}
}
type testFluentPlugin struct {
hosts string
db string
records []testrecord
position int
logmessages []*logmessage
}
func (p *testFluentPlugin) Environment(ctx unsafe.Pointer, key string) string {
switch key {
case "Hosts":
return p.hosts
case "Password":
return "mypasswd"
case "Key":
return "testkey"
case "DB":
return p.db
case "UseTLS":
return "false"
case "TLSSkipVerify":
return "false"
}
return "unknown-" + key
}
func (p *testFluentPlugin) Unregister(ctx unsafe.Pointer) {}
func (p *testFluentPlugin) NewDecoder(data unsafe.Pointer, length int) *output.FLBDecoder { return nil }
func (p *testFluentPlugin) Exit(code int) {}
func (p *testFluentPlugin) Send(values []*logmessage) error {
p.logmessages = append(p.logmessages, values...)
return nil
}
func (p *testFluentPlugin) GetRecord(dec *output.FLBDecoder) (int, interface{}, map[interface{}]interface{}) {
if p.position < len(p.records) {
r := p.records[p.position]
p.position++
return r.rc, r.ts, r.data
}
return -1, nil, nil
}
func (p *testFluentPlugin) addrecord(rc int, ts interface{}, data map[interface{}]interface{}) {
p.records = append(p.records, testrecord{rc: rc, ts: ts, data: data})
}
func TestPluginInitialization(t *testing.T) {
plugin = &testFluentPlugin{hosts: "hosta hostb", db: "0"}
res := FLBPluginInit(nil)
assert.Equal(t, output.FLB_OK, res)
assert.Len(t, rc.pools.pools, 2)
}
func TestPluginInitializationFailure(t *testing.T) {
plugin = &testFluentPlugin{hosts: "hosta hostb", db: "a"}
res := FLBPluginInit(nil)
assert.Equal(t, output.FLB_ERROR, res)
}
func TestPluginFlusher(t *testing.T) {
testplugin := &testFluentPlugin{hosts: "hosta hostb", db: "0"}
ts := time.Date(2018, time.February, 10, 10, 11, 12, 0, time.UTC)
testrecords := map[interface{}]interface{}{
"mykey": "myvalue",
}
testplugin.addrecord(0, output.FLBTime{Time: ts}, testrecords)
testplugin.addrecord(0, uint64(ts.Unix()), testrecords)
testplugin.addrecord(0, 0, testrecords)
plugin = testplugin
res := FLBPluginFlush(nil, 0, nil)
assert.Equal(t, output.FLB_OK, res)
assert.Len(t, testplugin.logmessages, len(testplugin.records))
var parsed map[string]interface{}
err := json.Unmarshal(testplugin.logmessages[0].data, &parsed)
assert.NoError(t, err)
assert.Equal(t, testrecords["mykey"], parsed["mykey"])
assert.Equal(t, ts.Format(time.RFC3339Nano), parsed["@timestamp"])
err = json.Unmarshal(testplugin.logmessages[1].data, &parsed)
assert.NoError(t, err)
assert.Equal(t, ts.Format(time.RFC3339Nano), parsed["@timestamp"])
err = json.Unmarshal(testplugin.logmessages[2].data, &parsed)
assert.NoError(t, err)
assert.NotEqual(t, ts.Format(time.RFC3339Nano), parsed["@timestamp"])
}