forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_comparator_test.go
104 lines (100 loc) · 2.84 KB
/
response_comparator_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
package main
import (
"encoding/json"
"errors"
"testing"
"github.com/stretchr/testify/require"
"github.com/grafana/loki/tools/querytee"
)
func TestCompareStreams(t *testing.T) {
for _, tc := range []struct {
name string
expected json.RawMessage
actual json.RawMessage
err error
}{
{
name: "no streams",
expected: json.RawMessage(`[]`),
actual: json.RawMessage(`[]`),
},
{
name: "no streams in actual response",
expected: json.RawMessage(`[
{"stream":{"foo":"bar"},"values":[["1","1"]]}
]`),
actual: json.RawMessage(`[]`),
err: errors.New("expected 1 streams but got 0"),
},
{
name: "extra stream in actual response",
expected: json.RawMessage(`[
{"stream":{"foo":"bar"},"values":[["1","1"]]}
]`),
actual: json.RawMessage(`[
{"stream":{"foo":"bar"},"values":[["1","1"]]},
{"stream":{"foo1":"bar1"},"values":[["1","1"]]}
]`),
err: errors.New("expected 1 streams but got 2"),
},
{
name: "same number of streams but with different labels",
expected: json.RawMessage(`[
{"stream":{"foo":"bar"},"values":[["1","1"]]}
]`),
actual: json.RawMessage(`[
{"stream":{"foo1":"bar1"},"values":[["1","1"]]}
]`),
err: errors.New("expected stream {foo=\"bar\"} missing from actual response"),
},
{
name: "difference in number of samples",
expected: json.RawMessage(`[
{"stream":{"foo":"bar"},"values":[["1","1"],["2","2"]]}
]`),
actual: json.RawMessage(`[
{"stream":{"foo":"bar"},"values":[["1","1"]]}
]`),
err: errors.New("expected 2 values for stream {foo=\"bar\"} but got 1"),
},
{
name: "difference in sample timestamp",
expected: json.RawMessage(`[
{"stream":{"foo":"bar"},"values":[["1","1"],["2","2"]]}
]`),
actual: json.RawMessage(`[
{"stream":{"foo":"bar"},"values":[["1","1"],["3","2"]]}
]`),
err: errors.New("expected timestamp 2 but got 3 for stream {foo=\"bar\"}"),
},
{
name: "difference in sample value",
expected: json.RawMessage(`[
{"stream":{"foo":"bar"},"values":[["1","1"],["2","2"]]}
]`),
actual: json.RawMessage(`[
{"stream":{"foo":"bar"},"values":[["1","1"],["2","3"]]}
]`),
err: errors.New("expected line 2 for timestamp 2 but got 3 for stream {foo=\"bar\"}"),
},
{
name: "correct samples",
expected: json.RawMessage(`[
{"stream":{"foo":"bar"},"values":[["1","1"],["2","2"]]}
]`),
actual: json.RawMessage(`[
{"stream":{"foo":"bar"},"values":[["1","1"],["2","2"]]}
]`),
},
} {
t.Run(tc.name, func(t *testing.T) {
err := compareStreams(tc.expected, tc.actual, querytee.SampleComparisonOptions{Tolerance: 0})
if tc.err == nil {
require.NoError(t, err)
return
}
require.Error(t, err)
require.Equal(t, tc.err.Error(), err.Error())
})
}
}