-
Notifications
You must be signed in to change notification settings - Fork 350
/
health_test.go
501 lines (403 loc) · 17.1 KB
/
health_test.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//go:build integration
// +build integration
// To enable compilation of this file in Goland, go to "Settings -> Go -> Vendoring & Build Tags -> Custom Tags" and add "integration"
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 traits
import (
"encoding/json"
"fmt"
"github.com/onsi/gomega/gstruct"
"strings"
"testing"
"time"
camelv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
. "github.com/apache/camel-k/v2/e2e/support"
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
)
func TestHealthTrait(t *testing.T) {
RegisterTestingT(t)
t.Run("Readiness condition with stopped route scaled", func(t *testing.T) {
name := RandomizedSuffixName("java")
Expect(KamelRunWithID(operatorID, ns, "files/Java.java",
"-t", "health.enabled=true",
// Enable Jolokia for the test to stop the Camel route
"-t", "jolokia.enabled=true",
"-t", "jolokia.use-ssl-client-authentication=false",
"-t", "jolokia.protocol=http",
"--name", name,
).Execute()).To(Succeed())
Eventually(IntegrationPodPhase(ns, name), TestTimeoutLong).Should(Equal(corev1.PodRunning))
Eventually(IntegrationPhase(ns, name), TestTimeoutShort).Should(Equal(v1.IntegrationPhaseRunning))
Eventually(IntegrationConditionStatus(ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Should(Equal(corev1.ConditionTrue))
Eventually(IntegrationLogs(ns, name), TestTimeoutShort).Should(ContainSubstring("Magicstring!"))
Expect(ScaleIntegration(ns, name, 3)).To(Succeed())
// Check the readiness condition becomes falsy
Eventually(IntegrationConditionStatus(ns, name, v1.IntegrationConditionReady), TestTimeoutShort).Should(Equal(corev1.ConditionFalse))
// Check the scale cascades into the Deployment scale
Eventually(IntegrationPods(ns, name), TestTimeoutShort).Should(HaveLen(3))
// Check it also cascades into the Integration scale subresource Status field
Eventually(IntegrationStatusReplicas(ns, name), TestTimeoutShort).
Should(gstruct.PointTo(BeNumerically("==", 3)))
// Finally check the readiness condition becomes truthy back
Eventually(IntegrationConditionStatus(ns, name, v1.IntegrationConditionReady), TestTimeoutMedium).Should(Equal(corev1.ConditionTrue))
pods := IntegrationPods(ns, name)()
for i, pod := range pods {
// Stop the Camel route
request := map[string]string{
"type": "exec",
"mbean": "org.apache.camel:context=camel-1,name=\"route1\",type=routes",
"operation": "stop()",
}
body, err := json.Marshal(request)
Expect(err).To(BeNil())
response, err := TestClient().CoreV1().RESTClient().Post().
AbsPath(fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/proxy/jolokia/", ns, pod.Name)).
Body(body).
DoRaw(TestContext)
Expect(err).To(BeNil())
Expect(response).To(ContainSubstring(`"status":200`))
Eventually(IntegrationConditionStatus(ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Should(Equal(corev1.ConditionFalse))
Eventually(IntegrationCondition(ns, name, v1.IntegrationConditionReady), TestTimeoutLong).Should(And(
WithTransform(IntegrationConditionReason, Equal(v1.IntegrationConditionRuntimeNotReadyReason)),
WithTransform(IntegrationConditionMessage, Equal(fmt.Sprintf("%d/3 pods are not ready", i+1)))))
}
Eventually(IntegrationCondition(ns, name, v1.IntegrationConditionReady), TestTimeoutLong).Should(
Satisfy(func(c *v1.IntegrationCondition) bool {
if c.Status != corev1.ConditionFalse {
return false
}
if len(c.Pods) != 3 {
return false
}
var r *v1.HealthCheckResponse
for _, pod := range c.Pods {
for h := range pod.Health {
if pod.Health[h].Name == "camel-routes" {
r = &pod.Health[h]
}
}
if r == nil {
return false
}
if r.Data == nil {
return false
}
var data map[string]interface{}
if err := json.Unmarshal(r.Data, &data); err != nil {
return false
}
if data["check.kind"].(string) != "READINESS" || data["route.status"].(string) != "Stopped" {
return false
}
}
return true
}))
Eventually(IntegrationPhase(ns, name), TestTimeoutShort).Should(Equal(v1.IntegrationPhaseError))
// Clean-up
Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
})
t.Run("Readiness condition with stopped route", func(t *testing.T) {
name := RandomizedSuffixName("java")
Expect(KamelRunWithID(operatorID, ns, "files/Java.java",
"-t", "health.enabled=true",
// Enable Jolokia for the test to stop the Camel route
"-t", "jolokia.enabled=true",
"-t", "jolokia.use-ssl-client-authentication=false",
"-t", "jolokia.protocol=http",
"--name", name,
).Execute()).To(Succeed())
Eventually(IntegrationPodPhase(ns, name), TestTimeoutLong).Should(Equal(corev1.PodRunning))
Eventually(IntegrationPhase(ns, name), TestTimeoutShort).Should(Equal(v1.IntegrationPhaseRunning))
Eventually(IntegrationConditionStatus(ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Should(Equal(corev1.ConditionTrue))
Eventually(IntegrationLogs(ns, name), TestTimeoutShort).Should(ContainSubstring("Magicstring!"))
pod := IntegrationPod(ns, name)()
// Stop the Camel route
request := map[string]string{
"type": "exec",
"mbean": "org.apache.camel:context=camel-1,name=\"route1\",type=routes",
"operation": "stop()",
}
body, err := json.Marshal(request)
Expect(err).To(BeNil())
response, err := TestClient().CoreV1().RESTClient().Post().
AbsPath(fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/proxy/jolokia/", ns, pod.Name)).
Body(body).
DoRaw(TestContext)
Expect(err).To(BeNil())
Expect(response).To(ContainSubstring(`"status":200`))
// Check the ready condition has turned false
Eventually(IntegrationConditionStatus(ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Should(Equal(corev1.ConditionFalse))
// And it contains details about the runtime state
//
// TODO
// Integration has different runtime state reporting on OCP4
//
// lastProbeTime: null
// lastTransitionTime: "2021-12-08T20:12:14Z"
// message: 'containers with unready status: [integration]'
// reason: ContainersNotReady
// status: "False"
// type: Ready
//
Eventually(IntegrationCondition(ns, name, v1.IntegrationConditionReady), TestTimeoutLong).Should(And(
WithTransform(IntegrationConditionReason, Equal(v1.IntegrationConditionRuntimeNotReadyReason)),
WithTransform(IntegrationConditionMessage, Equal("1/1 pods are not ready"))))
Eventually(IntegrationCondition(ns, name, v1.IntegrationConditionReady), TestTimeoutLong).Should(
Satisfy(func(c *v1.IntegrationCondition) bool {
if c.Status != corev1.ConditionFalse {
return false
}
if len(c.Pods) != 1 {
return false
}
var r *v1.HealthCheckResponse
for h := range c.Pods[0].Health {
if c.Pods[0].Health[h].Name == "camel-routes" {
r = &c.Pods[0].Health[h]
}
}
if r == nil {
return false
}
if r.Data == nil {
return false
}
var data map[string]interface{}
if err := json.Unmarshal(r.Data, &data); err != nil {
return false
}
return data["check.kind"].(string) == "READINESS" && data["route.status"].(string) == "Stopped"
}))
Eventually(IntegrationPhase(ns, name), TestTimeoutShort).Should(Equal(v1.IntegrationPhaseError))
// Clean-up
Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
})
t.Run("Readiness condition with stopped binding", func(t *testing.T) {
name := RandomizedSuffixName("stopped-binding")
Expect(CreateTimerKamelet(ns, "my-health-timer-source")()).To(Succeed())
Expect(CreateLogKamelet(ns, "my-health-log-sink")()).To(Succeed())
Expect(KamelBindWithID(operatorID, ns,
"my-health-timer-source",
"my-health-log-sink",
"-p", "source.message=Magicstring!",
"-p", "sink.loggerName=binding",
"--annotation", "trait.camel.apache.org/health.enabled=true",
"--annotation", "trait.camel.apache.org/jolokia.enabled=true",
"--annotation", "trait.camel.apache.org/jolokia.use-ssl-client-authentication=false",
"--annotation", "trait.camel.apache.org/jolokia.protocol=http",
"--name", name,
).Execute()).To(Succeed())
Eventually(IntegrationPodPhase(ns, name), TestTimeoutLong).Should(Equal(corev1.PodRunning))
Eventually(IntegrationPhase(ns, name), TestTimeoutShort).Should(Equal(v1.IntegrationPhaseRunning))
Eventually(IntegrationConditionStatus(ns, name, v1.IntegrationConditionReady), TestTimeoutShort).Should(Equal(corev1.ConditionTrue))
Eventually(IntegrationLogs(ns, name), TestTimeoutShort).Should(ContainSubstring("Magicstring!"))
pod := IntegrationPod(ns, name)()
// Stop the Camel route
request := map[string]string{
"type": "exec",
"mbean": "org.apache.camel:context=camel-1,name=\"binding\",type=routes",
"operation": "stop()",
}
body, err := json.Marshal(request)
Expect(err).To(BeNil())
response, err := TestClient().CoreV1().RESTClient().Post().
AbsPath(fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/proxy/jolokia/", ns, pod.Name)).
Body(body).
DoRaw(TestContext)
Expect(err).To(BeNil())
Expect(response).To(ContainSubstring(`"status":200`))
// Check the ready condition has turned false
Eventually(IntegrationConditionStatus(ns, name, v1.IntegrationConditionReady), TestTimeoutShort).
Should(Equal(corev1.ConditionFalse))
// And it contains details about the runtime state
Eventually(IntegrationCondition(ns, name, v1.IntegrationConditionReady), TestTimeoutLong).Should(And(
WithTransform(IntegrationConditionReason, Equal(v1.IntegrationConditionRuntimeNotReadyReason)),
WithTransform(IntegrationConditionMessage, Equal("1/1 pods are not ready"))))
Eventually(IntegrationCondition(ns, name, v1.IntegrationConditionReady), TestTimeoutLong).Should(
Satisfy(func(c *v1.IntegrationCondition) bool {
if c.Status != corev1.ConditionFalse {
return false
}
if len(c.Pods) != 1 {
return false
}
var r *v1.HealthCheckResponse
for h := range c.Pods[0].Health {
if c.Pods[0].Health[h].Name == "camel-routes" {
r = &c.Pods[0].Health[h]
}
}
if r == nil {
return false
}
if r.Data == nil {
return false
}
var data map[string]interface{}
if err := json.Unmarshal(r.Data, &data); err != nil {
return false
}
return data["check.kind"].(string) == "READINESS" && data["route.status"].(string) == "Stopped" && data["route.id"].(string) == "binding"
}))
Eventually(PipeCondition(ns, name, camelv1.PipeConditionReady), TestTimeoutLong).Should(
Satisfy(func(c *camelv1.PipeCondition) bool {
if c.Status != corev1.ConditionFalse {
return false
}
if len(c.Pods) != 1 {
return false
}
var r *v1.HealthCheckResponse
for h := range c.Pods[0].Health {
if c.Pods[0].Health[h].Name == "camel-routes" {
r = &c.Pods[0].Health[h]
}
}
if r == nil {
return false
}
if r.Data == nil {
return false
}
var data map[string]interface{}
if err := json.Unmarshal(r.Data, &data); err != nil {
return false
}
return data["check.kind"].(string) == "READINESS" && data["route.status"].(string) == "Stopped" && data["route.id"].(string) == "binding"
}))
// Clean-up
Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
})
t.Run("Readiness condition with never ready route", func(t *testing.T) {
name := RandomizedSuffixName("never-ready")
Expect(KamelRunWithID(operatorID, ns, "files/NeverReady.java",
"--name", name,
"-t", "health.enabled=true",
).Execute()).To(Succeed())
Eventually(IntegrationPodPhase(ns, name), TestTimeoutLong).Should(Equal(corev1.PodRunning))
Eventually(IntegrationPhase(ns, name), TestTimeoutShort).Should(Equal(v1.IntegrationPhaseRunning))
Consistently(IntegrationConditionStatus(ns, name, v1.IntegrationConditionReady), 1*time.Minute).
Should(Equal(corev1.ConditionFalse))
Eventually(IntegrationPhase(ns, name), TestTimeoutLong).Should(Equal(v1.IntegrationPhaseError))
// Check that the error message is propagated from health checks even if deployment never becomes ready
Eventually(IntegrationCondition(ns, name, v1.IntegrationConditionReady), TestTimeoutLong).Should(And(
WithTransform(IntegrationConditionReason, Equal(v1.IntegrationConditionRuntimeNotReadyReason)),
WithTransform(IntegrationConditionMessage, Equal("1/1 pods are not ready"))))
Eventually(IntegrationCondition(ns, name, v1.IntegrationConditionReady), TestTimeoutLong).Should(
Satisfy(func(c *v1.IntegrationCondition) bool {
if c.Status != corev1.ConditionFalse {
return false
}
if len(c.Pods) != 1 {
return false
}
var r *v1.HealthCheckResponse
for h := range c.Pods[0].Health {
if c.Pods[0].Health[h].Name == "camel-routes" {
r = &c.Pods[0].Health[h]
}
}
if r == nil {
return false
}
if r.Data == nil {
return false
}
var data map[string]interface{}
if err := json.Unmarshal(r.Data, &data); err != nil {
return false
}
return data["check.kind"].(string) == "READINESS" && data["route.status"].(string) == "Stopped" && data["route.id"].(string) == "never-ready"
}))
})
t.Run("Startup condition with never ready route", func(t *testing.T) {
name := RandomizedSuffixName("startup-probe-never-ready-route")
Expect(KamelRunWithID(operatorID, ns, "files/NeverReady.java",
"--name", name,
"-t", "health.enabled=true",
"-t", "health.startup-probe-enabled=true",
"-t", "health.startup-timeout=60",
).Execute()).To(Succeed())
Eventually(IntegrationPodPhase(ns, name), TestTimeoutMedium).Should(Equal(corev1.PodRunning))
Eventually(IntegrationPhase(ns, name), TestTimeoutMedium).Should(Equal(v1.IntegrationPhaseRunning))
Consistently(IntegrationConditionStatus(ns, name, v1.IntegrationConditionReady), 1*time.Minute).Should(Equal(corev1.ConditionFalse))
Eventually(IntegrationPhase(ns, name), TestTimeoutLong).Should(Equal(v1.IntegrationPhaseError))
Eventually(IntegrationCondition(ns, name, v1.IntegrationConditionReady), TestTimeoutLong).Should(And(
WithTransform(IntegrationConditionReason, Equal(v1.IntegrationConditionRuntimeNotReadyReason)),
WithTransform(IntegrationConditionMessage, Equal("1/1 pods are not ready"))))
Eventually(IntegrationCondition(ns, name, v1.IntegrationConditionReady), TestTimeoutLong).Should(
Satisfy(func(c *v1.IntegrationCondition) bool {
if c.Status != corev1.ConditionFalse {
return false
}
if len(c.Pods) != 1 {
return false
}
var r *v1.HealthCheckResponse
for h := range c.Pods[0].Health {
if c.Pods[0].Health[h].Name == "camel-routes" && c.Pods[0].Health[h].Status == "DOWN" {
r = &c.Pods[0].Health[h]
}
}
if r == nil {
return false
}
if r.Data == nil {
return false
}
var data map[string]interface{}
if err := json.Unmarshal(r.Data, &data); err != nil {
return false
}
return data["check.kind"].(string) == "READINESS" && data["route.status"].(string) == "Stopped" && data["route.id"].(string) == "never-ready"
}))
Satisfy(func(events *corev1.EventList) bool {
for e := range events.Items {
if events.Items[e].Type == "Warning" && events.Items[e].Reason == "Unhealthy" && strings.Contains(events.Items[e].Message, "Startup probe failed") {
return true
}
}
return false
})
})
t.Run("Startup condition with ready route", func(t *testing.T) {
name := RandomizedSuffixName("startup-probe-ready-route")
Expect(KamelRunWithID(operatorID, ns, "files/Java.java",
"--name", name,
"-t", "health.enabled=true",
"-t", "health.startup-probe-enabled=true",
"-t", "health.startup-timeout=60",
).Execute()).To(Succeed())
Eventually(IntegrationPodPhase(ns, name), TestTimeoutMedium).Should(Equal(corev1.PodRunning))
Eventually(IntegrationPhase(ns, name), TestTimeoutMedium).Should(Equal(v1.IntegrationPhaseRunning))
Eventually(IntegrationCondition(ns, name, v1.IntegrationConditionReady), TestTimeoutMedium).Should(And(
WithTransform(IntegrationConditionReason, Equal(v1.IntegrationConditionDeploymentReadyReason)),
WithTransform(IntegrationConditionMessage, Equal("1/1 ready replicas"))))
Satisfy(func(is *v1.IntegrationSpec) bool {
if *is.Traits.Health.Enabled == true && *is.Traits.Health.StartupProbeEnabled == true && is.Traits.Health.StartupTimeout == 60 {
return true
}
return false
})
})
Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
}