1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package pod
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/pkg/selector/generic"
25 . "github.com/chaos-mesh/chaos-mesh/pkg/testutils"
26 )
27
28 func TestFilterPodByNode(t *testing.T) {
29 g := NewGomegaWithT(t)
30
31 pods := []v1.Pod{
32 NewPod(PodArg{Name: "p1", Namespace: "n1", Nodename: "node1"}),
33 NewPod(PodArg{Name: "p2", Namespace: "n2", Nodename: "node1"}),
34 NewPod(PodArg{Name: "p3", Namespace: "n2", Nodename: "node2"}),
35 NewPod(PodArg{Name: "p4", Namespace: "n4", Nodename: "node3"}),
36 }
37
38 nodes := []v1.Node{
39 NewNode("node1", map[string]string{"disktype": "ssd", "zone": "az1"}),
40 NewNode("node2", map[string]string{"disktype": "hdd", "zone": "az1"}),
41 }
42
43 node1AndNode2Selector := &nodeSelector{nodes: nodes}
44 emptyNodeSelector := &nodeSelector{empty: true}
45 noNodeSelector := &nodeSelector{}
46
47 tcs := []struct {
48 name string
49 pod v1.Pod
50 selector generic.Selector
51 match bool
52 }{
53 {
54 name: "filter pods from node1 and node2",
55 pod: pods[0],
56 selector: node1AndNode2Selector,
57 match: true,
58 }, {
59 name: "filter pods from node1 and node2",
60 pod: pods[2],
61 selector: node1AndNode2Selector,
62 match: true,
63 }, {
64 name: "filter pods from node1 and node2",
65 pod: pods[3],
66 selector: node1AndNode2Selector,
67 match: false,
68 }, {
69 name: "empty filter",
70 pod: pods[0],
71 selector: emptyNodeSelector,
72 match: true,
73 }, {
74 name: "filter no nodes",
75 pod: pods[0],
76 selector: noNodeSelector,
77 match: false,
78 },
79 }
80
81 for _, tc := range tcs {
82 g.Expect(tc.selector.Match(&tc.pod)).To(Equal(tc.match), tc.name)
83 }
84 }
85