...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package label
17
18 import (
19 "testing"
20
21 . "github.com/onsi/gomega"
22 v1 "k8s.io/api/core/v1"
23
24 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
25 "github.com/chaos-mesh/chaos-mesh/pkg/selector/generic"
26 . "github.com/chaos-mesh/chaos-mesh/pkg/testutils"
27 )
28
29 func TestMatch(t *testing.T) {
30 g := NewGomegaWithT(t)
31
32 p2Selector, err := New(v1alpha1.GenericSelectorSpec{LabelSelectors: map[string]string{"p2": "p2"}}, generic.Option{})
33 g.Expect(err).ShouldNot(HaveOccurred())
34
35 emptySelector, err := New(v1alpha1.GenericSelectorSpec{}, generic.Option{})
36 g.Expect(err).ShouldNot(HaveOccurred())
37
38 tcs := []struct {
39 name string
40 pod v1.Pod
41 selector generic.Selector
42 match bool
43 }{
44 {
45 name: "filter p2, exist p2 labels",
46 pod: NewPod(PodArg{Name: "p2", Labels: map[string]string{"p2": "p2"}}),
47 selector: p2Selector,
48 match: true,
49 }, {
50 name: "filter p2, not exist p2 labels",
51 pod: NewPod(PodArg{Name: "p1", Labels: map[string]string{"p1": "p1"}}),
52 selector: p2Selector,
53 match: false,
54 }, {
55 name: "empty filter",
56 pod: NewPod(PodArg{Name: "p1", Labels: map[string]string{"p1": "p1"}}),
57 selector: emptySelector,
58 match: true,
59 },
60 }
61
62 for _, tc := range tcs {
63 g.Expect(tc.selector.Match(&tc.pod)).To(Equal(tc.match), tc.name)
64 }
65 }
66