-
Notifications
You must be signed in to change notification settings - Fork 804
/
http_patch.go
395 lines (339 loc) · 10.3 KB
/
http_patch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright 2021 Chaos Mesh Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package httpchaos
import (
"context"
"io"
"strings"
"time"
. "github.com/onsi/ginkgo/v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
"k8s.io/kubernetes/test/e2e/framework"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
"github.com/chaos-mesh/chaos-mesh/e2e-test/e2e/util"
)
func TestcaseHttpPatchThenRecover(
ns string,
cli client.Client,
c HTTPE2EClient,
port uint16,
) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
By("waiting on e2e helper ready")
err := util.WaitHTTPE2EHelperReady(*c.C, c.IP, port)
framework.ExpectNoError(err, "wait e2e helper ready error")
body := `{"msg":"Hello","target":"World"}`
secret := "Bar"
By("waiting for assertion normal behaviour")
err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
resp, err := getPodHttp(c, port, secret, body)
if err != nil {
return false, err
}
defer resp.Body.Close()
s := strings.Join(resp.Header[SECRET], "; ")
b, err := io.ReadAll(resp.Body)
if err != nil {
return false, err
}
klog.Infof("Status(%d), Secret(%s), Body(%s)", resp.StatusCode, s, string(b))
if s == secret && string(b) == body {
return true, nil
}
return false, nil
})
framework.ExpectNoError(err, "helper server doesn't work as expected")
By("deploy helper server successfully")
By("create http patch chaos CRD objects")
patchbody := `{"target": "Chaos Mesh"}`
patchSecret := "Foo!"
expectedSecret := "Bar; Foo!"
expectedBody := `{"msg":"Hello","target":"Chaos Mesh"}`
httpChaos := &v1alpha1.HTTPChaos{
ObjectMeta: metav1.ObjectMeta{
Name: "http-chaos",
Namespace: ns,
},
Spec: v1alpha1.HTTPChaosSpec{
PodSelector: v1alpha1.PodSelector{
Selector: v1alpha1.PodSelectorSpec{
GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
Namespaces: []string{ns},
LabelSelectors: map[string]string{"app": "http"},
},
},
Mode: v1alpha1.OneMode,
},
Port: 8080,
Target: "Request",
PodHttpChaosActions: v1alpha1.PodHttpChaosActions{
Patch: &v1alpha1.PodHttpChaosPatchActions{
Headers: [][]string{
{SECRET, patchSecret},
},
Body: &v1alpha1.PodHttpChaosPatchBodyAction{
Type: "JSON",
Value: patchbody,
},
},
},
},
}
err = cli.Create(ctx, httpChaos)
framework.ExpectNoError(err, "create http chaos error")
By("waiting for assertion HTTP patch")
err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
resp, err := getPodHttp(c, port, secret, body)
if err != nil {
return false, err
}
defer resp.Body.Close()
s := strings.Join(resp.Header[SECRET], "; ")
b, err := io.ReadAll(resp.Body)
if err != nil {
return false, err
}
klog.Infof("Status(%d), Secret(%s), Body(%s)", resp.StatusCode, s, string(b))
if s == expectedSecret && string(b) == expectedBody {
return true, nil
}
return false, nil
})
framework.ExpectNoError(err, "http chaos doesn't work as expected")
By("apply http chaos successfully")
By("delete chaos CRD objects")
// delete chaos CRD
err = cli.Delete(ctx, httpChaos)
framework.ExpectNoError(err, "failed to delete http chaos")
By("waiting for assertion recovering")
err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
resp, err := getPodHttp(c, port, secret, body)
if err != nil {
return false, err
}
defer resp.Body.Close()
s := strings.Join(resp.Header[SECRET], "; ")
b, err := io.ReadAll(resp.Body)
if err != nil {
return false, err
}
klog.Infof("Status(%d), Secret(%s), Body(%s)", resp.StatusCode, s, string(b))
if s == secret && string(b) == body {
return true, nil
}
return false, nil
})
framework.ExpectNoError(err, "fail to recover http chaos")
}
func TestcaseHttpPatchPauseAndUnPause(
ns string,
cli client.Client,
c HTTPE2EClient,
port uint16,
) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
By("waiting on e2e helper ready")
err := util.WaitHTTPE2EHelperReady(*c.C, c.IP, port)
framework.ExpectNoError(err, "wait e2e helper ready error")
body := `{"msg":"Hello","target":"World"}`
secret := "Bar"
By("waiting for assertion normal behaviour")
err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
resp, err := getPodHttp(c, port, secret, body)
if err != nil {
return false, err
}
defer resp.Body.Close()
s := strings.Join(resp.Header[SECRET], "; ")
b, err := io.ReadAll(resp.Body)
if err != nil {
return false, err
}
klog.Infof("Status(%d), Secret(%s), Body(%s)", resp.StatusCode, s, string(b))
if s == secret && string(b) == body {
return true, nil
}
return false, nil
})
framework.ExpectNoError(err, "helper server doesn't work as expected")
By("deploy helper server successfully")
By("create http patch chaos CRD objects")
patchbody := `{"target": "Chaos Mesh"}`
patchSecret := "Foo!"
expectedSecret := "Bar; Foo!"
expectedBody := `{"msg":"Hello","target":"Chaos Mesh"}`
httpChaos := &v1alpha1.HTTPChaos{
ObjectMeta: metav1.ObjectMeta{
Name: "http-chaos",
Namespace: ns,
},
Spec: v1alpha1.HTTPChaosSpec{
PodSelector: v1alpha1.PodSelector{
Selector: v1alpha1.PodSelectorSpec{
GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
Namespaces: []string{ns},
LabelSelectors: map[string]string{"app": "http"},
},
},
Mode: v1alpha1.OneMode,
},
Port: 8080,
Target: "Request",
PodHttpChaosActions: v1alpha1.PodHttpChaosActions{
Patch: &v1alpha1.PodHttpChaosPatchActions{
Headers: [][]string{
{SECRET, patchSecret},
},
Body: &v1alpha1.PodHttpChaosPatchBodyAction{
Type: "JSON",
Value: patchbody,
},
},
},
},
}
err = cli.Create(ctx, httpChaos)
framework.ExpectNoError(err, "create http chaos error")
chaosKey := types.NamespacedName{
Namespace: ns,
Name: "http-chaos",
}
By("waiting for assertion http chaos")
err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
chaos := &v1alpha1.HTTPChaos{}
err = cli.Get(ctx, chaosKey, chaos)
framework.ExpectNoError(err, "get http chaos error")
for _, c := range chaos.GetStatus().Conditions {
if c.Type == v1alpha1.ConditionAllInjected {
if c.Status != corev1.ConditionTrue {
return false, nil
}
} else if c.Type == v1alpha1.ConditionSelected {
if c.Status != corev1.ConditionTrue {
return false, nil
}
}
}
resp, err := getPodHttp(c, port, secret, body)
if err != nil {
return false, err
}
defer resp.Body.Close()
s := strings.Join(resp.Header[SECRET], "; ")
b, err := io.ReadAll(resp.Body)
if err != nil {
return false, err
}
klog.Infof("Status(%d), Secret(%s), Body(%s)", resp.StatusCode, s, string(b))
if s == expectedSecret && string(b) == expectedBody {
return true, nil
}
return false, nil
})
framework.ExpectNoError(err, "http chaos doesn't work as expected")
By("pause http patch chaos experiment")
// pause experiment
err = util.PauseChaos(ctx, cli, httpChaos)
framework.ExpectNoError(err, "pause chaos error")
By("waiting for assertion about pause")
err = wait.Poll(1*time.Second, 1*time.Minute, func() (done bool, err error) {
chaos := &v1alpha1.HTTPChaos{}
err = cli.Get(ctx, chaosKey, chaos)
framework.ExpectNoError(err, "get http chaos error")
for _, c := range chaos.GetStatus().Conditions {
if c.Type == v1alpha1.ConditionAllRecovered {
if c.Status != corev1.ConditionTrue {
return false, nil
}
} else if c.Type == v1alpha1.ConditionSelected {
if c.Status != corev1.ConditionTrue {
return false, nil
}
}
}
return true, err
})
framework.ExpectNoError(err, "check paused chaos failed")
// wait 1 min to check whether io patch still exists
err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
resp, err := getPodHttp(c, port, secret, body)
if err != nil {
return false, err
}
defer resp.Body.Close()
s := strings.Join(resp.Header[SECRET], "; ")
b, err := io.ReadAll(resp.Body)
if err != nil {
return false, err
}
klog.Infof("Status(%d), Secret(%s), Body(%s)", resp.StatusCode, s, string(b))
if s == secret && string(b) == body {
return true, nil
}
return false, nil
})
framework.ExpectNoError(err, "fail to recover http chaos")
By("resume http patch chaos experiment")
// resume experiment
err = util.UnPauseChaos(ctx, cli, httpChaos)
framework.ExpectNoError(err, "resume chaos error")
By("assert that http patch is effective again")
err = wait.Poll(1*time.Second, 1*time.Minute, func() (done bool, err error) {
chaos := &v1alpha1.HTTPChaos{}
err = cli.Get(ctx, chaosKey, chaos)
framework.ExpectNoError(err, "get http chaos error")
for _, c := range chaos.GetStatus().Conditions {
if c.Type == v1alpha1.ConditionAllInjected {
if c.Status != corev1.ConditionTrue {
return false, nil
}
} else if c.Type == v1alpha1.ConditionSelected {
if c.Status != corev1.ConditionTrue {
return false, nil
}
}
}
return true, err
})
framework.ExpectNoError(err, "check resumed chaos failed")
err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
resp, err := getPodHttp(c, port, secret, body)
if err != nil {
return false, err
}
defer resp.Body.Close()
s := strings.Join(resp.Header[SECRET], "; ")
b, err := io.ReadAll(resp.Body)
if err != nil {
return false, err
}
klog.Infof("Status(%d), Secret(%s), Body(%s)", resp.StatusCode, s, string(b))
if s == expectedSecret && string(b) == expectedBody {
return true, nil
}
return false, nil
})
framework.ExpectNoError(err, "HTTP chaos doesn't work as expected")
By("cleanup")
// cleanup
cli.Delete(ctx, httpChaos)
}