1
2
3
4
5
6
7
8
9
10
11
12
13
14 package common
15
16 import (
17 "context"
18 "testing"
19 "time"
20
21 . "github.com/onsi/gomega"
22 v1 "k8s.io/api/core/v1"
23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24 kubectlscheme "k8s.io/kubectl/pkg/scheme"
25 "sigs.k8s.io/controller-runtime/pkg/client/fake"
26
27 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
28 "github.com/chaos-mesh/chaos-mesh/pkg/utils"
29 )
30
31 func TestGetChaosList(t *testing.T) {
32 g := NewWithT(t)
33
34 chaos1 := v1alpha1.NetworkChaos{
35 TypeMeta: metav1.TypeMeta{
36 Kind: "NetworkChaos",
37 APIVersion: "v1",
38 },
39 ObjectMeta: metav1.ObjectMeta{
40 Namespace: metav1.NamespaceDefault,
41 Name: "fakechaos-1",
42 },
43 }
44
45 chaos2 := v1alpha1.NetworkChaos{
46 TypeMeta: metav1.TypeMeta{
47 Kind: "NetworkChaos",
48 APIVersion: "v1",
49 },
50 ObjectMeta: metav1.ObjectMeta{
51 Namespace: metav1.NamespaceDefault,
52 Name: "fakechaos-2",
53 },
54 }
55
56 v1alpha1.SchemeBuilder.AddToScheme(kubectlscheme.Scheme)
57
58 client := fake.NewFakeClientWithScheme(kubectlscheme.Scheme, &chaos1, &chaos2)
59
60 tests := []struct {
61 name string
62 chaosType string
63 chaosName string
64 ns string
65 expectedNum int
66 expectedErr bool
67 }{
68 {
69 name: "Only specify chaosType",
70 chaosType: "networkchaos",
71 chaosName: "",
72 ns: "default",
73 expectedNum: 2,
74 expectedErr: false,
75 },
76 {
77 name: "Specify chaos type, chaos name and namespace",
78 chaosType: "networkchaos",
79 chaosName: "fakechaos-1",
80 ns: "default",
81 expectedNum: 1,
82 expectedErr: false,
83 },
84 {
85 name: "Specify non-exist chaos name",
86 chaosType: "networkchaos",
87 chaosName: "fakechaos-oops",
88 ns: "default",
89 expectedErr: true,
90 },
91 {
92 name: "Specify non-exist chaos types",
93 chaosType: "stresschaos",
94 chaosName: "fakechaos-1",
95 ns: "default",
96 expectedErr: true,
97 },
98 {
99 name: "Specify non-exist namespace",
100 chaosType: "networkchaos",
101 chaosName: "fakechaos-1",
102 ns: "oops",
103 expectedErr: true,
104 },
105 }
106
107 for _, test := range tests {
108 t.Run(test.name, func(t *testing.T) {
109 chaos, _, err := GetChaosList(context.Background(), test.chaosType, test.chaosName, test.ns, client)
110 if test.expectedErr {
111 g.Expect(err).NotTo(BeNil())
112 } else {
113 g.Expect(err).To(BeNil())
114 g.Expect(len(chaos)).To(Equal(test.expectedNum))
115 }
116 })
117 }
118 }
119
120 func TestGetPods(t *testing.T) {
121 g := NewWithT(t)
122
123 _ = v1alpha1.ChaosStatus{
124 Experiment: v1alpha1.ExperimentStatus{
125 Phase: v1alpha1.ExperimentPhaseRunning,
126 },
127 Scheduler: v1alpha1.ScheduleStatus{},
128 }
129
130 nodeObjects, _ := utils.GenerateNNodes("node", 2, nil)
131 podObjects0, _ := utils.GenerateNPods("pod-node0", 1, v1.PodRunning, metav1.NamespaceDefault, nil, map[string]string{"app": "pod"}, "node0")
132 daemonObjects0, _ := utils.GenerateNPods("daemon-node0", 1, v1.PodRunning, metav1.NamespaceDefault, nil, map[string]string{"app.kubernetes.io/component": "chaos-daemon"}, "node0")
133 podObjects1, _ := utils.GenerateNPods("pod-node1", 1, v1.PodRunning, metav1.NamespaceDefault, nil, map[string]string{"app": "pod"}, "node1")
134 daemonObjects1, _ := utils.GenerateNPods("daemon-node1", 1, v1.PodRunning, metav1.NamespaceDefault, nil, map[string]string{"app.kubernetes.io/component": "chaos-daemon"}, "node1")
135
136 allObjects := append(nodeObjects, daemonObjects0[0], podObjects0[0], daemonObjects1[0], podObjects1[0])
137
138 v1alpha1.SchemeBuilder.AddToScheme(kubectlscheme.Scheme)
139 client := fake.NewFakeClientWithScheme(kubectlscheme.Scheme, allObjects...)
140
141 tests := []struct {
142 name string
143 chaosSelector v1alpha1.SelectorSpec
144 chaosStatus v1alpha1.ChaosStatus
145 wait bool
146 expectedPodNum int
147 expectedDaemonNum int
148 expectedErr bool
149 }{
150 {
151 name: "chaos on two pods",
152 chaosSelector: v1alpha1.SelectorSpec{LabelSelectors: map[string]string{"app": "pod"}},
153 chaosStatus: v1alpha1.ChaosStatus{},
154 expectedPodNum: 2,
155 expectedDaemonNum: 2,
156 expectedErr: false,
157 },
158 {
159 name: "chaos on one pod",
160 chaosSelector: v1alpha1.SelectorSpec{
161 Nodes: []string{"node0"},
162 LabelSelectors: map[string]string{"app": "pod"},
163 },
164 chaosStatus: v1alpha1.ChaosStatus{},
165 expectedPodNum: 1,
166 expectedDaemonNum: 1,
167 expectedErr: false,
168 },
169 {
170 name: "wait for 100ms for chaos to start",
171 chaosSelector: v1alpha1.SelectorSpec{LabelSelectors: map[string]string{"app": "pod"}},
172 chaosStatus: v1alpha1.ChaosStatus{
173 Experiment: v1alpha1.ExperimentStatus{
174 Phase: v1alpha1.ExperimentPhaseWaiting,
175 },
176 Scheduler: v1alpha1.ScheduleStatus{
177 NextStart: &metav1.Time{Time: time.Now().Add(time.Millisecond * 50)},
178 },
179 },
180 wait: true,
181 expectedPodNum: 2,
182 expectedDaemonNum: 2,
183 expectedErr: false,
184 },
185 {
186 name: "wrong selector to get pod",
187 chaosSelector: v1alpha1.SelectorSpec{LabelSelectors: map[string]string{"app": "oops"}},
188 chaosStatus: v1alpha1.ChaosStatus{},
189 expectedErr: true,
190 },
191 }
192
193 for _, test := range tests {
194 t.Run(test.name, func(t *testing.T) {
195 timeBefore := time.Now()
196 pods, daemons, err := GetPods(context.Background(), test.chaosStatus, test.chaosSelector, client)
197 if test.wait {
198 g.Expect(time.Now().Add(time.Millisecond * -50).Before(timeBefore)).To(BeTrue())
199 }
200 if test.expectedErr {
201 g.Expect(err).NotTo(BeNil())
202 } else {
203 g.Expect(err).To(BeNil())
204 g.Expect(len(pods)).To(Equal(test.expectedPodNum))
205 g.Expect(len(daemons)).To(Equal(test.expectedDaemonNum))
206 }
207 })
208 }
209 }
210