1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package namespace
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 pods := []v1.Pod{
33 NewPod(PodArg{Name: "p1", Namespace: "n1"}),
34 NewPod(PodArg{Name: "p2", Namespace: "n2"}),
35 NewPod(PodArg{Name: "p4", Namespace: "n4"}),
36 }
37
38 option := generic.Option{
39 ClusterScoped: true,
40 TargetNamespace: "",
41 EnableFilterNamespace: false,
42 }
43
44 n2Selector, err := New(v1alpha1.GenericSelectorSpec{Namespaces: []string{"n2"}}, option)
45 g.Expect(err).ShouldNot(HaveOccurred())
46
47 emptySelector, err := New(v1alpha1.GenericSelectorSpec{}, option)
48 g.Expect(err).ShouldNot(HaveOccurred())
49
50 n2AndN3Selector, err := New(v1alpha1.GenericSelectorSpec{Namespaces: []string{"n2", "n3"}}, option)
51 g.Expect(err).ShouldNot(HaveOccurred())
52
53 n2AndN4Selector, err := New(v1alpha1.GenericSelectorSpec{Namespaces: []string{"n2", "n4"}}, option)
54 g.Expect(err).ShouldNot(HaveOccurred())
55
56 tcs := []struct {
57 name string
58 pod v1.Pod
59 selector generic.Selector
60 match bool
61 }{
62 {
63 name: "filter n2, exist n2 namespace",
64 pod: pods[1],
65 selector: n2Selector,
66 match: true,
67 }, {
68 name: "filter n2, not exist n2 namespace",
69 pod: pods[0],
70 selector: n2Selector,
71 match: false,
72 }, {
73 name: "empty filter",
74 pod: pods[0],
75 selector: emptySelector,
76 match: true,
77 }, {
78 name: "filter n2 and n3, exist n2 namespace",
79 pod: pods[1],
80 selector: n2AndN3Selector,
81 match: true,
82 }, {
83 name: "filter n2 and n3, not exist n2 namespace",
84 pod: pods[0],
85 selector: n2AndN3Selector,
86 match: false,
87 }, {
88 name: "filter n2 and n4, exist n2 namespace",
89 pod: pods[1],
90 selector: n2AndN4Selector,
91 match: true,
92 }, {
93 name: "filter n2 and n4, exist n4 namespace",
94 pod: pods[2],
95 selector: n2AndN4Selector,
96 match: true,
97 }, {
98 name: "filter n2 and n4, not exist n2 namespace",
99 pod: pods[0],
100 selector: n2AndN4Selector,
101 match: false,
102 },
103 }
104
105 for _, tc := range tcs {
106 g.Expect(tc.selector.Match(&tc.pod)).To(Equal(tc.match), tc.name)
107 }
108 }
109