forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloki_test.go
228 lines (213 loc) · 5.97 KB
/
loki_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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package log
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSyslogFromConfigLine(t *testing.T) {
t.Parallel()
tests := [...]struct {
line string
err bool
res lokiHook
}{
{
line: "loki", // default settings
res: lokiHook{
addr: "http://127.0.0.1:3100/loki/api/v1/push",
limit: 100,
pushPeriod: time.Second * 1,
levels: logrus.AllLevels,
msgMaxSize: 1024 * 1024,
droppedLabels: map[string]string{"level": "warning"},
droppedMsg: "k6 dropped %d log messages because they were above the limit of %d messages / %s",
},
},
{
line: "loki=somewhere:1233,label.something=else,label.foo=bar,limit=32,level=info,allowedLabels=[something],pushPeriod=5m32s,msgMaxSize=1231,header.x-test=123,header.authorization=token foobar",
res: lokiHook{
addr: "somewhere:1233",
headers: [][2]string{{"x-test", "123"}, {"authorization", "token foobar"}},
limit: 32,
pushPeriod: time.Minute*5 + time.Second*32,
levels: logrus.AllLevels[:5],
labels: [][2]string{{"something", "else"}, {"foo", "bar"}},
msgMaxSize: 1231,
allowedLabels: []string{"something"},
droppedLabels: map[string]string{"something": "else"},
droppedMsg: "k6 dropped %d log messages because they were above the limit of %d messages / %s foo=bar level=warning",
},
},
{
line: "lokino",
err: true,
},
{
line: "loki=something,limit=word",
err: true,
},
{
line: "loki=something,level=notlevel",
err: true,
},
{
line: "loki=something,unknownoption",
err: true,
},
{
line: "loki=something,label=somethng",
err: true,
},
}
for _, test := range tests {
test := test
t.Run(test.line, func(t *testing.T) {
t.Parallel()
// no parallel because this is way too fast and parallel will only slow it down
res, err := LokiFromConfigLine(nil, test.line)
if test.err {
require.Error(t, err)
return
}
hook, ok := res.(*lokiHook)
require.True(t, ok)
require.NoError(t, err)
test.res.client = hook.client
test.res.ch = hook.ch
require.Equal(t, &test.res, res)
})
}
}
func TestLogEntryMarshal(t *testing.T) {
t.Parallel()
entry := logEntry{
t: 9223372036854775807, // the actual max
msg: "something",
}
expected := []byte(`["9223372036854775807","something"]`)
s, err := json.Marshal(entry)
require.NoError(t, err)
require.Equal(t, expected, s)
}
func TestFilterLabels(t *testing.T) {
t.Parallel()
cases := []struct {
allowedLabels []string
labels map[string]string
expectedLabels map[string]string
msg string
result string
}{
{
allowedLabels: []string{"a", "b"},
labels: map[string]string{"a": "1", "b": "2", "d": "3", "c": "4", "e": "5"},
expectedLabels: map[string]string{"a": "1", "b": "2"},
msg: "some msg",
result: "some msg c=4 d=3 e=5",
},
{
allowedLabels: []string{"a", "b"},
labels: map[string]string{"d": "3", "c": "4", "e": "5"},
expectedLabels: map[string]string{},
msg: "some msg",
result: "some msg c=4 d=3 e=5",
},
{
allowedLabels: []string{"a", "b"},
labels: map[string]string{"a": "1", "d": "3", "c": "4", "e": "5"},
expectedLabels: map[string]string{"a": "1"},
msg: "some msg",
result: "some msg c=4 d=3 e=5",
},
}
for i, c := range cases {
c := c
t.Run(fmt.Sprint(i), func(t *testing.T) {
t.Parallel()
h := &lokiHook{}
h.allowedLabels = c.allowedLabels
result := h.filterLabels(c.labels, c.msg)
assert.Equal(t, c.result, result)
assert.Equal(t, c.expectedLabels, c.labels)
})
}
}
func TestLokiFlushingOnStop(t *testing.T) {
t.Parallel()
receivedData := make(chan string, 1)
srv := httptest.NewServer(
http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
b, err := io.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
receivedData <- string(b)
close(receivedData) // this is mostly as this should never be called twice, so panicking here in that case
}),
)
configLine := fmt.Sprintf("loki=%s,pushPeriod=1h", srv.URL)
h, err := LokiFromConfigLine(nil, configLine)
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
wg := new(sync.WaitGroup)
now := time.Now()
wg.Add(1)
go func() {
defer wg.Done()
err = h.Fire(&logrus.Entry{Time: now, Level: logrus.InfoLevel, Message: "test message"})
time.Sleep(time.Millisecond * 10)
cancel()
}()
h.Listen(ctx)
wg.Wait()
select {
case data := <-receivedData:
require.JSONEq(t,
fmt.Sprintf(
`{"streams":[{"stream":{"level":"info"},"values":[["%d","test message"]]}]}`, now.UnixNano()),
data)
default:
t.Fatal("No logs were received from loki before hook has finished")
}
}
func TestLokiHeaders(t *testing.T) {
t.Parallel()
receivedHeaders := make(chan http.Header, 1)
srv := httptest.NewServer(
http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
receivedHeaders <- req.Header
close(receivedHeaders) // see comment in TestLokiFlushingOnStop
}),
)
configLine := fmt.Sprintf("loki=%s,pushPeriod=1h,header.X-Foo=bar,header.Test=hello world,header.X-Foo=baz", srv.URL)
h, err := LokiFromConfigLine(nil, configLine)
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
wg := new(sync.WaitGroup)
now := time.Now()
wg.Add(1)
go func() {
defer wg.Done()
err = h.Fire(&logrus.Entry{Time: now, Level: logrus.InfoLevel, Message: "test message"})
time.Sleep(time.Millisecond * 10)
cancel()
}()
h.Listen(ctx)
wg.Wait()
select {
case headers := <-receivedHeaders:
require.Equal(t, []string{"bar", "baz"}, headers["X-Foo"])
require.Equal(t, []string{"hello world"}, headers["Test"])
default:
t.Fatal("No logs were received from loki before hook has finished")
}
}