-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlabels_matcher_test.go
131 lines (127 loc) · 4.34 KB
/
labels_matcher_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
package promtable_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/uschen/promtable"
"github.com/uschen/promtable/prompb"
)
func TestQueryMatchersToLabelsMatcher(t *testing.T) {
tests := []struct {
input []*prompb.LabelMatcher
matches [][]prompb.Label
notmatches [][]prompb.Label
}{
{
input: []*prompb.LabelMatcher{
{
Type: prompb.LabelMatcher_EQ,
Name: "l1",
Value: "v1,v1.2",
},
{
Type: prompb.LabelMatcher_EQ,
Name: "l2",
Value: "v2#v2.1",
},
},
matches: [][]prompb.Label{
[]prompb.Label{{Name: "l1", Value: "v1,v1.2"}, {Name: "l2", Value: "v2#v2.1"}},
[]prompb.Label{{Name: "l1", Value: "v1,v1.2"}, {Name: "l2", Value: "v2#v2.1"}},
[]prompb.Label{{Name: "l1", Value: "v1,v1.2"}, {Name: "l2", Value: "v2#v2.1"}, {Name: "l3", Value: "v3"}}, // more values
[]prompb.Label{{Name: "l1", Value: "v1,v1.2"}, {Name: "l2", Value: "v2#v2.1"}},
},
notmatches: [][]prompb.Label{
[]prompb.Label{{Name: "l1", Value: "v1,v1.2"}, {Name: "l2", Value: "v2"}}, // one wrong value
[]prompb.Label{{Name: "l1", Value: "v1"}, {Name: "l2", Value: "v2"}}, // two wrong values
[]prompb.Label{},
[]prompb.Label{{Name: "l2", Value: "v2#v2.1"}, {Name: "l1", Value: "v1,v1.2"}},
},
},
{
input: []*prompb.LabelMatcher{
{
Type: prompb.LabelMatcher_EQ,
Name: "l2",
Value: "pods",
},
},
matches: [][]prompb.Label{
[]prompb.Label{{Name: "l1", Value: "v1,v1.2"}, {Name: "l2", Value: "pods"}},
[]prompb.Label{{Name: "l1", Value: "v1,v1.2"}, {Name: "l2", Value: "pods"}, {Name: "l3", Value: "v3"}}, // more values
},
notmatches: [][]prompb.Label{
[]prompb.Label{{Name: "l1", Value: "v1,v1.2"}, {Name: "l2", Value: "v2"}}, // one wrong value
[]prompb.Label{{Name: "l1", Value: "v1"}, {Name: "l2", Value: "v2"}}, // two wrong values
[]prompb.Label{},
},
},
{
input: []*prompb.LabelMatcher{
{
Type: prompb.LabelMatcher_NEQ,
Name: "l2",
Value: "pods",
},
},
matches: [][]prompb.Label{
[]prompb.Label{{Name: "l1", Value: "v1,v1.2"}, {Name: "l2", Value: "v2"}},
[]prompb.Label{{Name: "l1", Value: "v1"}, {Name: "l2", Value: "v2"}},
[]prompb.Label{},
},
notmatches: [][]prompb.Label{
[]prompb.Label{{Name: "l1", Value: "v1,v1.2"}, {Name: "l2", Value: "pods"}},
[]prompb.Label{{Name: "l1", Value: "v1,v1.2"}, {Name: "l2", Value: "pods"}, {Name: "l3", Value: "v3"}}, // more values
},
},
{
input: []*prompb.LabelMatcher{
{
Type: prompb.LabelMatcher_RE,
Name: "l2",
Value: "v2",
},
},
matches: [][]prompb.Label{
[]prompb.Label{{Name: "l1", Value: "v1,v1.2"}, {Name: "l2", Value: "v2"}},
[]prompb.Label{{Name: "l1", Value: "v1,v1.2"}, {Name: "l2", Value: "v2"}, {Name: "l3", Value: "v3"}}, // more values
},
notmatches: [][]prompb.Label{
[]prompb.Label{{Name: "l1", Value: "v1,v1.2"}, {Name: "l2", Value: "v3"}}, // one wrong value
[]prompb.Label{{Name: "l1", Value: "v1"}, {Name: "l2", Value: "v4"}}, // two wrong values
[]prompb.Label{},
},
},
{
input: []*prompb.LabelMatcher{
{
Type: prompb.LabelMatcher_NRE,
Name: "l2",
Value: "v2",
},
},
matches: [][]prompb.Label{
[]prompb.Label{{Name: "l1", Value: "v1,v1.2"}, {Name: "l2", Value: "v3"}},
[]prompb.Label{{Name: "l1", Value: "v1,v1.2"}, {Name: "l2", Value: "v3"}, {Name: "l3", Value: "v3"}}, // more values
[]prompb.Label{},
},
notmatches: [][]prompb.Label{
[]prompb.Label{{Name: "l1", Value: "v1,v1.2"}, {Name: "l2", Value: "v2"}}, // one wrong value
[]prompb.Label{{Name: "l1", Value: "v1"}, {Name: "l2", Value: "v2"}}, // two wrong values
},
},
}
for _, test := range tests {
actual, err := promtable.QueryMatchersToLabelsMatcher(test.input)
assert.Nil(t, err)
for i := range test.matches {
// sort.Slice(test.matches[i], func(k, j int) bool { return test.matches[i][k].Name < test.matches[i][j].Name })
ok := actual.Match(test.matches[i])
assert.True(t, ok, "should match: ", test.matches[i])
}
for i := range test.notmatches {
// sort.Slice(test.notmatches[i], func(k, j int) bool { return test.notmatches[i][k].Name < test.notmatches[i][j].Name })
ok := actual.Match(test.notmatches[i])
assert.False(t, ok, "should NOT match: ", test.notmatches[i])
}
}
}