-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcrashes_test.go
581 lines (492 loc) · 20 KB
/
crashes_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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
package vizzini_test
import (
"fmt"
"net/http"
"time"
"code.cloudfoundry.org/lager/v3"
. "code.cloudfoundry.org/vizzini/matchers"
"code.cloudfoundry.org/bbs/models"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func MakeGraceExit(baseURL string, status int) {
//make sure Grace is up first
Eventually(EndpointCurler(baseURL + "/env")).Should(Equal(http.StatusOK))
//make Grace exit
for i := 0; i < 10; i++ {
url := fmt.Sprintf("%s/exit/%d", baseURL, status)
resp, err := http.Post(url, "application/octet-stream", nil)
Expect(err).NotTo(HaveOccurred())
resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return
}
logger.Info(
"grace-exit-request-failed",
lager.Data{"response-status-code": resp.StatusCode, "attempt": i, "desired-exit-status": status},
)
time.Sleep(10 * time.Millisecond)
}
Fail("failed to make grace exit")
}
func TellGraceToDeleteFile(baseURL string, filename string) {
url := fmt.Sprintf("%s/file/%s", baseURL, filename)
req, err := http.NewRequest("DELETE", url, nil)
Expect(err).NotTo(HaveOccurred())
resp, err := http.DefaultClient.Do(req)
Expect(err).NotTo(HaveOccurred())
resp.Body.Close()
Expect(resp.StatusCode).To(Equal(http.StatusOK))
}
var _ = Describe("Crashes", func() {
var lrp *models.DesiredLRP
var url string
BeforeEach(func() {
url = fmt.Sprintf("http://%s", RouteForGuid(guid))
lrp = DesiredLRPWithGuid(guid)
})
Describe("Annotating the Crash Reason", func() {
BeforeEach(func() {
Expect(bbsClient.DesireLRP(logger, traceID, lrp)).To(Succeed())
})
It("adds the crash reason to the application", func() {
MakeGraceExit(url, 17)
Eventually(ActualGetter(logger, guid, 0)).Should(BeActualLRPWithStateAndCrashCount(guid, 0, models.ActualLRPStateRunning, 1))
actualLRP, err := ActualLRPByProcessGuidAndIndex(logger, guid, 0)
Expect(err).NotTo(HaveOccurred())
Expect(actualLRP.CrashReason).To(ContainSubstring("Exited with status 17"))
})
})
Describe("backoff behavior", func() {
BeforeEach(func() {
Expect(bbsClient.DesireLRP(logger, traceID, lrp)).To(Succeed())
})
It("{SLOW} restarts the application immediately twice, and then starts backing it off, and updates the modification tag as it goes", func() {
actualLRP, err := ActualLRPByProcessGuidAndIndex(logger, guid, 0)
Expect(err).NotTo(HaveOccurred())
tag := actualLRP.ModificationTag
By("immediately restarting #1")
MakeGraceExit(url, 1)
Eventually(ActualGetter(logger, guid, 0)).Should(BeActualLRPWithStateAndCrashCount(guid, 0, models.ActualLRPStateRunning, 1))
restartedActualLRP, err := ActualLRPByProcessGuidAndIndex(logger, guid, 0)
Expect(err).NotTo(HaveOccurred())
Expect(restartedActualLRP.InstanceGuid).NotTo(Equal(actualLRP.InstanceGuid))
Expect(restartedActualLRP.ModificationTag.Epoch).To(Equal(tag.Epoch))
Expect(restartedActualLRP.ModificationTag.Index).To(BeNumerically(">", tag.Index))
By("immediately restarting #2")
MakeGraceExit(url, 1)
Eventually(ActualGetter(logger, guid, 0)).Should(BeActualLRPWithStateAndCrashCount(guid, 0, models.ActualLRPStateRunning, 2))
By("eventually restarting #3 (slow)")
MakeGraceExit(url, 1)
Eventually(ActualGetter(logger, guid, 0), ConvergerInterval).Should(BeActualLRPWithStateAndCrashCount(guid, 0, models.ActualLRPStateCrashed, 3))
Consistently(ActualGetter(logger, guid, 0), CrashRestartTimeout-5*time.Second).Should(BeActualLRPWithStateAndCrashCount(guid, 0, models.ActualLRPStateCrashed, 3))
Eventually(ActualGetter(logger, guid, 0), ConvergerInterval*2).Should(BeActualLRPWithStateAndCrashCount(guid, 0, models.ActualLRPStateRunning, 3))
Eventually(EndpointCurler(url + "/env")).Should(Equal(http.StatusOK))
})
It("deletes the crashed ActualLRP when scaling down", func() {
By("immediately restarting #1")
MakeGraceExit(url, 1)
Eventually(ActualGetter(logger, guid, 0)).Should(BeActualLRPWithStateAndCrashCount(guid, 0, models.ActualLRPStateRunning, 1))
By("immediately restarting #2")
MakeGraceExit(url, 1)
Eventually(ActualGetter(logger, guid, 0)).Should(BeActualLRPWithStateAndCrashCount(guid, 0, models.ActualLRPStateRunning, 2))
By("eventually restarting #3")
MakeGraceExit(url, 1)
Eventually(ActualGetter(logger, guid, 0), ConvergerInterval).Should(BeActualLRPWithStateAndCrashCount(guid, 0, models.ActualLRPStateCrashed, 3))
By("deleting the DesiredLRP")
Expect(bbsClient.RemoveDesiredLRP(logger, traceID, guid)).To(Succeed())
Eventually(ActualByProcessGuidGetter(logger, guid)).Should(BeEmpty())
})
})
Describe("killing crashed applications", func() {
BeforeEach(func() {
Expect(bbsClient.DesireLRP(logger, traceID, lrp)).To(Succeed())
})
It("should delete the Crashed ActualLRP succesfully", func() {
By("immediately restarting #1")
MakeGraceExit(url, 1)
Eventually(ActualGetter(logger, guid, 0)).Should(BeActualLRPWithStateAndCrashCount(guid, 0, models.ActualLRPStateRunning, 1))
By("immediately restarting #2")
MakeGraceExit(url, 1)
Eventually(ActualGetter(logger, guid, 0)).Should(BeActualLRPWithStateAndCrashCount(guid, 0, models.ActualLRPStateRunning, 2))
By("eventually restarting #3")
MakeGraceExit(url, 1)
Eventually(ActualGetter(logger, guid, 0), ConvergerInterval).Should(BeActualLRPWithStateAndCrashCount(guid, 0, models.ActualLRPStateCrashed, 3))
actualLRPKey := models.NewActualLRPKey(guid, 0, domain)
Expect(bbsClient.RetireActualLRP(logger, traceID, &actualLRPKey)).To(Succeed())
Eventually(ActualByProcessGuidGetter(logger, guid)).Should(BeEmpty())
})
})
Context("with no monitor action", func() {
BeforeEach(func() {
lrp.Monitor = nil
})
Context("when running a single action", func() {
BeforeEach(func() {
Expect(bbsClient.DesireLRP(logger, traceID, lrp)).To(Succeed())
Eventually(EndpointCurler(url + "/env")).Should(Equal(http.StatusOK))
})
It("comes up as soon as the process starts", func() {
Eventually(ActualGetter(logger, guid, 0)).Should(BeActualLRPWithState(guid, 0, models.ActualLRPStateRunning))
})
Context("when the process dies with exit code 0", func() {
BeforeEach(func() {
MakeGraceExit(url, 0)
})
It("gets restarted immediately", func() {
Eventually(ActualGetter(logger, guid, 0)).Should(BeActualLRPWithStateAndCrashCount(guid, 0, models.ActualLRPStateRunning, 1))
Eventually(EndpointCurler(url + "/env")).Should(Equal(http.StatusOK))
})
})
Context("when the process dies with exit code 1", func() {
BeforeEach(func() {
MakeGraceExit(url, 1)
})
It("gets restarted immediately", func() {
Eventually(ActualGetter(logger, guid, 0)).Should(BeActualLRPWithStateAndCrashCount(guid, 0, models.ActualLRPStateRunning, 1))
Eventually(EndpointCurler(url + "/env")).Should(Equal(http.StatusOK))
})
})
})
Context("when running several actions", func() {
Context("codependently", func() {
BeforeEach(func() {
lrp.Action = models.WrapAction(models.Codependent(
&models.RunAction{
Path: "bash",
Args: []string{"-c", "while true; do sleep 1; done"},
User: "vcap",
},
&models.RunAction{
Path: "/tmp/grace/grace",
Env: []*models.EnvironmentVariable{{Name: "PORT", Value: "8080"}},
User: "vcap",
},
))
})
JustBeforeEach(func() {
Expect(bbsClient.DesireLRP(logger, traceID, lrp)).To(Succeed())
})
Context("when one of the actions finishes", func() {
JustBeforeEach(func() {
Eventually(EndpointCurler(url + "/env")).Should(Equal(http.StatusOK))
MakeGraceExit(url, 0)
})
It("gets restarted immediately", func() {
Eventually(ActualGetter(logger, guid, 0)).Should(BeActualLRPWithStateAndCrashCount(guid, 0, models.ActualLRPStateRunning, 1))
Eventually(EndpointCurler(url + "/env")).Should(Equal(http.StatusOK))
})
})
})
Context("with a failing check definition", func() {
JustBeforeEach(func() {
Expect(bbsClient.DesireLRP(logger, traceID, lrp)).To(Succeed())
})
Context("when failing readiness", func() {
BeforeEach(func() {
lrp.Action = models.WrapAction(
models.Parallel(
&models.RunAction{
Path: "/tmp/grace/grace",
Env: []*models.EnvironmentVariable{{Name: "PORT", Value: "8080"}},
User: "vcap",
},
),
)
lrp.StartTimeoutMs = 3000
})
Context("with both http and tcp healthcheck", func() {
BeforeEach(func() {
lrp.CheckDefinition = &models.CheckDefinition{
Checks: []*models.Check{
{
TcpCheck: &models.TCPCheck{
Port: 9090,
},
},
{
HttpCheck: &models.HTTPCheck{
Port: 9090,
Path: "/ping",
},
},
},
}
})
It("shows the monitor crash reasons", func() {
if !config.EnableDeclarativeHealthcheck {
Skip("declarative are not enabled")
}
Eventually(ActualGetter(logger, guid, 0), HealthyCheckInterval+5*time.Second).Should(BeActualLRPWithCrashCount(guid, 0, 1))
actualLRP, err := ActualGetter(logger, guid, 0)()
Expect(err).NotTo(HaveOccurred())
Expect(actualLRP.CrashReason).To(ContainSubstring("Instance never healthy after"))
Expect(actualLRP.CrashReason).To(MatchRegexp("failed to make TCP connection to .*:9090: dial tcp .*:9090: connect: connection refused"))
Expect(actualLRP.CrashReason).To(ContainSubstring("failed to make HTTP request to '/ping' on port 9090: connection refused"))
})
})
})
Context("when failing liveness", func() {
BeforeEach(func() {
lrp.Action = models.WrapAction(
models.Parallel(
&models.RunAction{
Path: "bash",
Args: []string{"-c", "while true; do sleep 1; done"},
User: "vcap",
},
&models.RunAction{
Path: "/tmp/grace/grace",
Env: []*models.EnvironmentVariable{{Name: "PORT", Value: "8080"}},
User: "vcap",
},
),
)
})
Context("with http healthcheck", func() {
BeforeEach(func() {
lrp.CheckDefinition = &models.CheckDefinition{
Checks: []*models.Check{
{
HttpCheck: &models.HTTPCheck{
Port: 8080,
Path: "/ping",
},
},
},
}
})
It("shows the monitor crash reasons", func() {
if !config.EnableDeclarativeHealthcheck {
Skip("declarative are not enabled")
}
Eventually(ActualGetter(logger, guid, 0)).Should(BeActualLRPWithState(guid, 0, models.ActualLRPStateRunning))
MakeGraceExit(url, 0)
Eventually(ActualGetter(logger, guid, 0), HealthyCheckInterval+10*time.Second).Should(BeActualLRPWithCrashCount(guid, 0, 1))
actualLRP, err := ActualGetter(logger, guid, 0)()
Expect(err).NotTo(HaveOccurred())
Expect(actualLRP.CrashReason).To(ContainSubstring("Instance became unhealthy: Liveness check unsuccessful: failed to make HTTP request to '/ping' on port 8080: connection refused"))
})
})
Context("with tcp healthcheck", func() {
BeforeEach(func() {
lrp.CheckDefinition = &models.CheckDefinition{
Checks: []*models.Check{
{
TcpCheck: &models.TCPCheck{
Port: 8080,
},
},
},
}
})
It("shows the monitor crash reasons", func() {
if !config.EnableDeclarativeHealthcheck {
Skip("declarative are not enabled")
}
Eventually(ActualGetter(logger, guid, 0)).Should(BeActualLRPWithState(guid, 0, models.ActualLRPStateRunning))
MakeGraceExit(url, 0)
Eventually(ActualGetter(logger, guid, 0), HealthyCheckInterval+10*time.Second).Should(BeActualLRPWithCrashCount(guid, 0, 1))
actualLRP, err := ActualGetter(logger, guid, 0)()
Expect(err).NotTo(HaveOccurred())
Expect(actualLRP.CrashReason).To(MatchRegexp("Instance became unhealthy: Liveness check unsuccessful: failed to make TCP connection to .*:8080: dial tcp .*:8080: connect: connection refused"))
})
})
Context("with both http and tcp healthcheck", func() {
BeforeEach(func() {
lrp.CheckDefinition = &models.CheckDefinition{
Checks: []*models.Check{
{
TcpCheck: &models.TCPCheck{
Port: 8080,
},
},
{
HttpCheck: &models.HTTPCheck{
Port: 8080,
Path: "/ping",
},
},
},
}
})
It("shows the monitor crash reasons", func() {
if !config.EnableDeclarativeHealthcheck {
Skip("declarative are not enabled")
}
Eventually(ActualGetter(logger, guid, 0)).Should(BeActualLRPWithState(guid, 0, models.ActualLRPStateRunning))
MakeGraceExit(url, 0)
Eventually(ActualGetter(logger, guid, 0), HealthyCheckInterval+10*time.Second).Should(BeActualLRPWithCrashCount(guid, 0, 1))
actualLRP, err := ActualGetter(logger, guid, 0)()
Expect(err).NotTo(HaveOccurred())
Expect(actualLRP.CrashReason).To(ContainSubstring("Instance became unhealthy:"))
Expect(actualLRP.CrashReason).To(SatisfyAny(
MatchRegexp("failed to make TCP connection to .*:8080: dial tcp .*:8080: connect: connection refused"),
ContainSubstring("failed to make HTTP request to '/ping' on port 8080: connection refused"),
))
})
})
})
})
Context("in parallel", func() {
BeforeEach(func() {
lrp.Action = models.WrapAction(models.Parallel(
&models.RunAction{
Path: "bash",
Args: []string{"-c", "while true; do sleep 1; done"},
User: "vcap",
},
&models.RunAction{
Path: "/tmp/grace/grace",
Env: []*models.EnvironmentVariable{{Name: "PORT", Value: "8080"}},
User: "vcap",
},
))
Expect(bbsClient.DesireLRP(logger, traceID, lrp)).To(Succeed())
Eventually(EndpointCurler(url + "/env")).Should(Equal(http.StatusOK))
})
Context("when one of the actions finishes", func() {
BeforeEach(func() {
MakeGraceExit(url, 2)
})
It("does not crash", func() {
Consistently(ActualGetter(logger, guid, 0), 5).Should(BeActualLRPWithState(guid, 0, models.ActualLRPStateRunning))
})
})
})
})
})
Context("with a monitor action", func() {
Context("when the monitor eventually succeeds", func() {
var directURL string
var indirectURL string
BeforeEach(func() {
lrp.Action = models.WrapAction(&models.RunAction{
Path: "/tmp/grace/grace",
Args: []string{"-upFile=up"},
User: "vcap",
Env: []*models.EnvironmentVariable{{Name: "PORT", Value: "8080"}},
})
lrp.Monitor = models.WrapAction(&models.RunAction{
Path: "cat",
Args: []string{"/tmp/up"},
User: "vcap",
})
Expect(bbsClient.DesireLRP(logger, traceID, lrp)).To(Succeed())
Eventually(ActualGetter(logger, guid, 0)).Should(BeActualLRPWithState(guid, 0, models.ActualLRPStateRunning))
Eventually(EndpointCurler(url + "/env")).Should(Equal(http.StatusOK))
directURL = "https://" + TLSDirectAddressFor(guid, 0, 8080)
indirectURL = "http://" + RouteForGuid(guid)
})
It("enters the running state", func() {
Expect(ActualGetter(logger, guid, 0)()).To(BeActualLRPWithState(guid, 0, models.ActualLRPStateRunning))
})
Context("when the process dies with exit code 0", func() {
BeforeEach(func() {
MakeGraceExit(indirectURL, 0)
})
It("does not get marked as crashed (may have daemonized)", func() {
Consistently(ActualGetter(logger, guid, 0), 3).Should(BeActualLRPWithStateAndCrashCount(guid, 0, models.ActualLRPStateRunning, 0))
})
})
Context("when the process dies with exit code 0 and the monitor subsequently fails", func() {
BeforeEach(func() {
//tell grace to delete the file then exit, it's highly unlikely that the health check will run
//between these two lines so the test should actually be covering the edge case in question
TellGraceToDeleteFile(url, "up")
MakeGraceExit(indirectURL, 0)
})
It("{SLOW} is marked as crashed", func() {
Consistently(ActualGetter(logger, guid, 0), 2).Should(BeActualLRPWithState(guid, 0, models.ActualLRPStateRunning), "Banking on the fact that the health check runs every thirty seconds and is unlikely to run immediately")
Eventually(ActualGetter(logger, guid, 0), HealthyCheckInterval+5*time.Second).Should(BeActualLRPWithCrashCount(guid, 0, 1))
})
})
Context("when the process dies with exit code 1", func() {
BeforeEach(func() {
MakeGraceExit(indirectURL, 1)
})
It("is marked as crashed (immediately)", func() {
Eventually(ActualGetter(logger, guid, 0), HealthyCheckInterval/3).Should(BeActualLRPWithCrashCount(guid, 0, 1))
})
})
Context("when the monitor subsequently fails", func() {
BeforeEach(func() {
TellGraceToDeleteFile(indirectURL, "up")
})
It("{SLOW} is marked as crashed (and reaped)", func() {
actualLRP, err := ActualLRPByProcessGuidAndIndex(logger, guid, 0)
Expect(err).ToNot(HaveOccurred())
tlsConfig, err := containerProxyTLSConfig(actualLRP.InstanceGuid)
Expect(err).NotTo(HaveOccurred())
httpClient := &http.Client{
Timeout: time.Second,
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
}
By("first validate that we can connect to the container directly using " + directURL)
_, err = httpClient.Get(directURL + "/env")
Expect(err).NotTo(HaveOccurred())
By("being marked as crashed")
Eventually(ActualGetter(logger, guid, 0), HealthyCheckInterval+10*time.Second).Should(BeActualLRPWithCrashCount(guid, 0, 1))
By("tearing down the process -- this reaches out to the container's direct address " + directURL + " and ensures we can't reach it")
_, err = httpClient.Get(directURL + "/env")
Expect(err).To(HaveOccurred())
})
})
})
Context("when the monitor never succeeds", func() {
JustBeforeEach(func() {
lrp.Monitor = models.WrapAction(&models.RunAction{
Path: "false",
User: "vcap",
})
Expect(bbsClient.DesireLRP(logger, traceID, lrp)).To(Succeed())
Eventually(ActualGetter(logger, guid, 0)).Should(BeActualLRPWithState(guid, 0, models.ActualLRPStateClaimed))
})
Context("when the process dies with exit code 0", func() {
BeforeEach(func() {
lrp.Action = models.WrapAction(&models.RunAction{
Path: "/tmp/grace/grace",
Args: []string{"-exitAfter=2s", "-exitAfterCode=0"},
User: "vcap",
Env: []*models.EnvironmentVariable{{Name: "PORT", Value: "8080"}},
})
})
It("does not get marked as crash, as it has presumably daemonized and we are waiting on the health check", func() {
Consistently(ActualGetter(logger, guid, 0), 3).Should(BeActualLRPWithStateAndCrashCount(guid, 0, models.ActualLRPStateClaimed, 0))
})
})
Context("when the process dies with exit code 1", func() {
BeforeEach(func() {
lrp.Action = models.WrapAction(&models.RunAction{
Path: "/tmp/grace/grace",
Args: []string{"-exitAfter=2s", "-exitAfterCode=1"},
User: "vcap",
Env: []*models.EnvironmentVariable{{Name: "PORT", Value: "8080"}},
})
})
It("gets marked as crashed (immediately)", func() {
Eventually(ActualGetter(logger, guid, 0), 30*time.Second).Should(BeActualLRPWithCrashCount(guid, 0, 1))
})
})
Context("and there is a StartTimeout", func() {
BeforeEach(func() {
lrp.StartTimeoutMs = 3000
})
It("never enters the running state and is marked as crashed after the StartTimeout", func() {
Consistently(ActualGetter(logger, guid, 0), 3).Should(BeActualLRPWithState(guid, 0, models.ActualLRPStateClaimed))
Eventually(ActualGetter(logger, guid, 0)).Should(BeActualLRPWithCrashCount(guid, 0, 1))
})
})
Context("and there is no start timeout", func() {
BeforeEach(func() {
lrp.StartTimeoutMs = 0
})
It("never enters the running state, and never crashes", func() {
Consistently(ActualGetter(logger, guid, 0), 5).Should(BeActualLRPWithStateAndCrashCount(guid, 0, models.ActualLRPStateClaimed, 0))
})
})
})
})
})