-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraw_converter_test.go
76 lines (69 loc) · 1.72 KB
/
raw_converter_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
package promtail
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
func Test_Strip(t *testing.T) {
msg := "\x1b[38;5;140mfoo\x1b[0m bar"
res := strip(msg)
assert.Equal(t, res, "foo bar")
}
func Test_ExtractLabels(t *testing.T) {
tests := []struct {
name string
labels string
sep string
input []byte
expected LabelSet
expectedErr string
}{
{
name: "should return an empty label set if label string is blank",
labels: "",
input: []byte("info method=POST bytes_in=0 bytes_out=108"),
expected: LabelSet{},
},
{
name: "should return an empty label set if sep is blank",
labels: "method",
input: []byte("info method=POST bytes_in=0 bytes_out=108"),
expected: LabelSet{},
},
{
name: "should return a label set with only label tags",
labels: "method,bytes_in",
sep: "=",
input: []byte("info method=POST bytes_in=0 bytes_out=108"),
expected: LabelSet{
"method": "POST",
"bytes_in": "0",
},
},
{
name: "should return a label set with only label tags and strip all terminal colors",
labels: "method,bytes_in",
sep: "=",
input: []byte("\u001B[38;5;140minfo\u001B[0m method=POST bytes_in=0 bytes_out=108"),
expected: LabelSet{
"method": "POST",
"bytes_in": "0",
},
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
rsc := NewRawStreamConv(tc.labels, tc.sep)
got, err := rsc.ExtractLabels(tc.input)
if tc.expectedErr != "" {
require.Error(t, err)
assert.EqualError(t, err, tc.expectedErr)
return
}
require.NoError(t, err)
assert.Equal(t, tc.expected, got)
})
}
}