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
|
// SPDX-License-Identifier: GPL-3.0-or-later
package pipeline
import (
"testing"
"github.com/netdata/netdata/go/go.d.plugin/agent/discovery/sd/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
func TestTargetClassificator_classify(t *testing.T) {
config := `
- selector: "rule0"
tags: "skip"
match:
- tags: "skip"
expr: '{{ glob .Name "*" }}'
- selector: "!skip rule1"
tags: "foo1"
match:
- tags: "bar1"
expr: '{{ glob .Name "mock*1*" }}'
- tags: "bar2"
expr: '{{ glob .Name "mock*2*" }}'
- selector: "!skip rule2"
tags: "foo2"
match:
- tags: "bar3"
expr: '{{ glob .Name "mock*3*" }}'
- tags: "bar4"
expr: '{{ glob .Name "mock*4*" }}'
- selector: "rule3"
tags: "foo3"
match:
- tags: "bar5"
expr: '{{ glob .Name "mock*5*" }}'
- tags: "bar6"
expr: '{{ glob .Name "mock*6*" }}'
`
tests := map[string]struct {
target model.Target
wantTags model.Tags
}{
"no rules match": {
target: newMockTarget("mock1"),
wantTags: nil,
},
"one rule one match": {
target: newMockTarget("mock4", "rule2"),
wantTags: mustParseTags("foo2 bar4"),
},
"one rule two match": {
target: newMockTarget("mock56", "rule3"),
wantTags: mustParseTags("foo3 bar5 bar6"),
},
"all rules all matches": {
target: newMockTarget("mock123456", "rule1 rule2 rule3"),
wantTags: mustParseTags("foo1 foo2 foo3 bar1 bar2 bar3 bar4 bar5 bar6"),
},
"applying labels after every rule": {
target: newMockTarget("mock123456", "rule0 rule1 rule2 rule3"),
wantTags: mustParseTags("skip foo3 bar5 bar6"),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
var cfg []ClassifyRuleConfig
err := yaml.Unmarshal([]byte(config), &cfg)
require.NoError(t, err, "yaml unmarshalling of config")
clr, err := newTargetClassificator(cfg)
require.NoError(t, err, "targetClassificator creation")
assert.Equal(t, test.wantTags, clr.classify(test.target))
})
}
}
|