1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package v1alpha1
17
18 import (
19 "net/http"
20
21 . "github.com/onsi/ginkgo"
22 . "github.com/onsi/gomega"
23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24 )
25
26 var _ = Describe("HTTPChaos Webhook", func() {
27 Context("webhook.Validator of httpchaos", func() {
28 It("Validate", func() {
29 type TestCase struct {
30 name string
31 chaos HTTPChaos
32 execute func(chaos *HTTPChaos) error
33 expect string
34 }
35 errorDuration := "400S"
36 errorMethod := "gET"
37 validMethod := http.MethodGet
38
39 tcs := []TestCase{
40 {
41 name: "simple ValidateCreate",
42 chaos: HTTPChaos{
43 ObjectMeta: metav1.ObjectMeta{
44 Namespace: metav1.NamespaceDefault,
45 Name: "foo1",
46 },
47 Spec: HTTPChaosSpec{
48 Target: PodHttpRequest,
49 Port: 80,
50 },
51 },
52 execute: func(chaos *HTTPChaos) error {
53 return chaos.ValidateCreate()
54 },
55 expect: "",
56 },
57 {
58 name: "simple ValidateUpdate",
59 chaos: HTTPChaos{
60 ObjectMeta: metav1.ObjectMeta{
61 Namespace: metav1.NamespaceDefault,
62 Name: "foo2",
63 },
64 Spec: HTTPChaosSpec{
65 Target: PodHttpRequest,
66 Port: 80,
67 },
68 },
69 execute: func(chaos *HTTPChaos) error {
70 return chaos.ValidateUpdate(chaos)
71 },
72 expect: "",
73 },
74 {
75 name: "simple ValidateDelete",
76 chaos: HTTPChaos{
77 ObjectMeta: metav1.ObjectMeta{
78 Namespace: metav1.NamespaceDefault,
79 Name: "foo3",
80 },
81 Spec: HTTPChaosSpec{
82 Target: PodHttpRequest,
83 Port: 80,
84 },
85 },
86 execute: func(chaos *HTTPChaos) error {
87 return chaos.ValidateDelete()
88 },
89 expect: "",
90 },
91 {
92 name: "parse the duration error",
93 chaos: HTTPChaos{
94 ObjectMeta: metav1.ObjectMeta{
95 Namespace: metav1.NamespaceDefault,
96 Name: "foo6",
97 },
98 Spec: HTTPChaosSpec{
99 Duration: &errorDuration,
100 Target: PodHttpRequest,
101 Port: 80,
102 },
103 },
104 execute: func(chaos *HTTPChaos) error {
105 return chaos.ValidateCreate()
106 },
107 expect: "error",
108 },
109 {
110 name: "validate value with FixedPercentMode",
111 chaos: HTTPChaos{
112 ObjectMeta: metav1.ObjectMeta{
113 Namespace: metav1.NamespaceDefault,
114 Name: "foo7",
115 },
116 Spec: HTTPChaosSpec{
117 PodSelector: PodSelector{
118 Value: "0",
119 Mode: FixedMode,
120 },
121 Port: 80,
122 Target: PodHttpRequest,
123 },
124 },
125 execute: func(chaos *HTTPChaos) error {
126 return chaos.ValidateCreate()
127 },
128 expect: "error",
129 },
130 {
131 name: "validate value with FixedPercentMode, parse value error",
132 chaos: HTTPChaos{
133 ObjectMeta: metav1.ObjectMeta{
134 Namespace: metav1.NamespaceDefault,
135 Name: "foo8",
136 },
137 Spec: HTTPChaosSpec{
138 PodSelector: PodSelector{
139 Value: "num",
140 Mode: FixedMode,
141 },
142 Port: 80,
143 Target: PodHttpRequest,
144 },
145 },
146 execute: func(chaos *HTTPChaos) error {
147 return chaos.ValidateCreate()
148 },
149 expect: "error",
150 },
151 {
152 name: "validate value with RandomMaxPercentMode",
153 chaos: HTTPChaos{
154 ObjectMeta: metav1.ObjectMeta{
155 Namespace: metav1.NamespaceDefault,
156 Name: "foo9",
157 },
158 Spec: HTTPChaosSpec{
159 PodSelector: PodSelector{
160 Value: "0",
161 Mode: RandomMaxPercentMode,
162 },
163 Port: 80,
164 Target: PodHttpRequest,
165 },
166 },
167 execute: func(chaos *HTTPChaos) error {
168 return chaos.ValidateCreate()
169 },
170 expect: "error",
171 },
172 {
173 name: "validate value with RandomMaxPercentMode ,parse value error",
174 chaos: HTTPChaos{
175 ObjectMeta: metav1.ObjectMeta{
176 Namespace: metav1.NamespaceDefault,
177 Name: "foo10",
178 },
179 Spec: HTTPChaosSpec{
180 PodSelector: PodSelector{
181 Value: "num",
182 Mode: RandomMaxPercentMode,
183 },
184 Port: 80,
185 Target: PodHttpRequest,
186 },
187 },
188 execute: func(chaos *HTTPChaos) error {
189 return chaos.ValidateCreate()
190 },
191 expect: "error",
192 },
193 {
194 name: "validate value with FixedPercentMode",
195 chaos: HTTPChaos{
196 ObjectMeta: metav1.ObjectMeta{
197 Namespace: metav1.NamespaceDefault,
198 Name: "foo11",
199 },
200 Spec: HTTPChaosSpec{
201 PodSelector: PodSelector{
202 Value: "101",
203 Mode: FixedPercentMode,
204 },
205 Port: 80,
206 Target: PodHttpRequest,
207 },
208 },
209 execute: func(chaos *HTTPChaos) error {
210 return chaos.ValidateCreate()
211 },
212 expect: "error",
213 },
214 {
215 name: "validate port 1",
216 chaos: HTTPChaos{
217 ObjectMeta: metav1.ObjectMeta{
218 Namespace: metav1.NamespaceDefault,
219 Name: "foo12",
220 },
221 Spec: HTTPChaosSpec{
222 Target: PodHttpRequest,
223 },
224 },
225 execute: func(chaos *HTTPChaos) error {
226 return chaos.ValidateCreate()
227 },
228 expect: "error",
229 },
230 {
231 name: "validate port 2",
232 chaos: HTTPChaos{
233 ObjectMeta: metav1.ObjectMeta{
234 Namespace: metav1.NamespaceDefault,
235 Name: "foo13",
236 },
237 Spec: HTTPChaosSpec{
238 Port: -1,
239 Target: PodHttpRequest,
240 },
241 },
242 execute: func(chaos *HTTPChaos) error {
243 return chaos.ValidateCreate()
244 },
245 expect: "error",
246 },
247 {
248 name: "validate target 1",
249 chaos: HTTPChaos{
250 ObjectMeta: metav1.ObjectMeta{
251 Namespace: metav1.NamespaceDefault,
252 Name: "foo14",
253 },
254 Spec: HTTPChaosSpec{
255 Port: 80,
256 },
257 },
258 execute: func(chaos *HTTPChaos) error {
259 return chaos.ValidateCreate()
260 },
261 expect: "error",
262 },
263 {
264 name: "validate target 2",
265 chaos: HTTPChaos{
266 ObjectMeta: metav1.ObjectMeta{
267 Namespace: metav1.NamespaceDefault,
268 Name: "foo15",
269 },
270 Spec: HTTPChaosSpec{
271 Port: 80,
272 Target: "request",
273 },
274 },
275 execute: func(chaos *HTTPChaos) error {
276 return chaos.ValidateCreate()
277 },
278 expect: "error",
279 },
280 {
281 name: "valid method 1",
282 chaos: HTTPChaos{
283 ObjectMeta: metav1.ObjectMeta{
284 Namespace: metav1.NamespaceDefault,
285 Name: "foo16",
286 },
287 Spec: HTTPChaosSpec{
288 Port: 80,
289 Target: PodHttpRequest,
290 Method: &validMethod,
291 },
292 },
293 execute: func(chaos *HTTPChaos) error {
294 return chaos.ValidateCreate()
295 },
296 expect: "ok",
297 },
298 {
299 name: "valid method 2",
300 chaos: HTTPChaos{
301 ObjectMeta: metav1.ObjectMeta{
302 Namespace: metav1.NamespaceDefault,
303 Name: "foo17",
304 },
305 Spec: HTTPChaosSpec{
306 Port: 80,
307 Target: PodHttpResponse,
308 Method: &errorMethod,
309 },
310 },
311 execute: func(chaos *HTTPChaos) error {
312 return chaos.ValidateCreate()
313 },
314 expect: "ok",
315 },
316 {
317 name: "invalid method",
318 chaos: HTTPChaos{
319 ObjectMeta: metav1.ObjectMeta{
320 Namespace: metav1.NamespaceDefault,
321 Name: "foo18",
322 },
323 Spec: HTTPChaosSpec{
324 Port: 80,
325 Target: PodHttpRequest,
326 Method: &errorMethod,
327 },
328 },
329 execute: func(chaos *HTTPChaos) error {
330 return chaos.ValidateCreate()
331 },
332 expect: "error",
333 },
334 }
335
336 for _, tc := range tcs {
337 err := tc.execute(&tc.chaos)
338 if tc.expect == "error" {
339 Expect(err).To(HaveOccurred())
340 } else {
341 Expect(err).NotTo(HaveOccurred())
342 }
343 }
344 })
345 })
346 })
347