@@ -75,8 +75,8 @@ func TestAddPostStartLifecycleHooks(t *testing.T) {
75
75
tests := loadAllPostStartTestCasesOrPanic (t , "./testdata/postStart" )
76
76
for _ , tt := range tests {
77
77
t .Run (fmt .Sprintf ("%s (%s)" , tt .Name , tt .testPath ), func (t * testing.T ) {
78
- var timeout int32
79
- err := AddPostStartLifecycleHooks (tt .Input .Devfile , tt .Input .Containers , & timeout )
78
+ var timeout string
79
+ err := AddPostStartLifecycleHooks (tt .Input .Devfile , tt .Input .Containers , timeout )
80
80
if tt .Output .ErrRegexp != nil && assert .Error (t , err ) {
81
81
assert .Regexp (t , * tt .Output .ErrRegexp , err .Error (), "Error message should match" )
82
82
} else {
@@ -298,13 +298,13 @@ func TestGenerateScriptWithTimeout(t *testing.T) {
298
298
tests := []struct {
299
299
name string
300
300
escapedUserScript string
301
- timeoutSeconds int32
301
+ timeout string
302
302
expectedScript string
303
303
}{
304
304
{
305
305
name : "Basic script with timeout" ,
306
306
escapedUserScript : "echo 'hello world'\n sleep 1" ,
307
- timeoutSeconds : 10 ,
307
+ timeout : "10s" ,
308
308
expectedScript : `
309
309
export POSTSTART_TIMEOUT_DURATION="10"
310
310
export POSTSTART_KILL_AFTER_DURATION="5"
@@ -350,7 +350,7 @@ exit $exit_code
350
350
{
351
351
name : "Script with zero timeout (no timeout)" ,
352
352
escapedUserScript : "echo 'running indefinitely...'" ,
353
- timeoutSeconds : 0 ,
353
+ timeout : "0s" ,
354
354
expectedScript : `
355
355
export POSTSTART_TIMEOUT_DURATION="0"
356
356
export POSTSTART_KILL_AFTER_DURATION="5"
@@ -395,7 +395,7 @@ exit $exit_code
395
395
{
396
396
name : "Empty user script" ,
397
397
escapedUserScript : "" ,
398
- timeoutSeconds : 5 ,
398
+ timeout : "5s" ,
399
399
expectedScript : `
400
400
export POSTSTART_TIMEOUT_DURATION="5"
401
401
export POSTSTART_KILL_AFTER_DURATION="5"
@@ -440,7 +440,7 @@ exit $exit_code
440
440
{
441
441
name : "User script with already escaped single quotes" ,
442
442
escapedUserScript : "echo 'it'\\ ''s complex'" ,
443
- timeoutSeconds : 30 ,
443
+ timeout : "30s" ,
444
444
expectedScript : `
445
445
export POSTSTART_TIMEOUT_DURATION="30"
446
446
export POSTSTART_KILL_AFTER_DURATION="5"
@@ -479,14 +479,59 @@ else
479
479
fi
480
480
fi
481
481
482
+ exit $exit_code
483
+ ` ,
484
+ },
485
+ {
486
+ name : "User script with minute timeout" ,
487
+ escapedUserScript : "echo 'wait for it...'" ,
488
+ timeout : "2m" ,
489
+ expectedScript : `
490
+ export POSTSTART_TIMEOUT_DURATION="120"
491
+ export POSTSTART_KILL_AFTER_DURATION="5"
492
+
493
+ _TIMEOUT_COMMAND_PART=""
494
+ _WAS_TIMEOUT_USED="false" # Use strings "true" or "false" for shell boolean
495
+
496
+ if command -v timeout >/dev/null 2>&1; then
497
+ echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
498
+ _TIMEOUT_COMMAND_PART="timeout --preserve-status --kill-after=${POSTSTART_KILL_AFTER_DURATION} ${POSTSTART_TIMEOUT_DURATION}"
499
+ _WAS_TIMEOUT_USED="true"
500
+ else
501
+ echo "[postStart hook] WARNING: 'timeout' utility not found. Executing commands without timeout." >&2
502
+ fi
503
+
504
+ # Execute the user's script
505
+ ${_TIMEOUT_COMMAND_PART} /bin/sh -c 'echo 'wait for it...''
506
+ exit_code=$?
507
+
508
+ # Check the exit code based on whether timeout was attempted
509
+ if [ "$_WAS_TIMEOUT_USED" = "true" ]; then
510
+ if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
511
+ echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
512
+ elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
513
+ echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
514
+ elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
515
+ echo "[postStart hook] Commands failed with exit code $exit_code." >&2
516
+ else
517
+ echo "[postStart hook] Commands completed successfully within the time limit." >&2
518
+ fi
519
+ else
520
+ if [ $exit_code -ne 0 ]; then
521
+ echo "[postStart hook] Commands failed with exit code $exit_code (no timeout)." >&2
522
+ else
523
+ echo "[postStart hook] Commands completed successfully (no timeout)." >&2
524
+ fi
525
+ fi
526
+
482
527
exit $exit_code
483
528
` ,
484
529
},
485
530
}
486
531
487
532
for _ , tt := range tests {
488
533
t .Run (tt .name , func (t * testing.T ) {
489
- script := generateScriptWithTimeout (tt .escapedUserScript , tt .timeoutSeconds )
534
+ script := generateScriptWithTimeout (tt .escapedUserScript , tt .timeout )
490
535
assert .Equal (t , tt .expectedScript , script )
491
536
})
492
537
}
0 commit comments