@@ -75,8 +75,8 @@ func TestAddPostStartLifecycleHooks(t *testing.T) {
7575 tests := loadAllPostStartTestCasesOrPanic (t , "./testdata/postStart" )
7676 for _ , tt := range tests {
7777 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 )
8080 if tt .Output .ErrRegexp != nil && assert .Error (t , err ) {
8181 assert .Regexp (t , * tt .Output .ErrRegexp , err .Error (), "Error message should match" )
8282 } else {
@@ -298,13 +298,13 @@ func TestGenerateScriptWithTimeout(t *testing.T) {
298298 tests := []struct {
299299 name string
300300 escapedUserScript string
301- timeoutSeconds int32
301+ timeout string
302302 expectedScript string
303303 }{
304304 {
305305 name : "Basic script with timeout" ,
306306 escapedUserScript : "echo 'hello world'\n sleep 1" ,
307- timeoutSeconds : 10 ,
307+ timeout : "10s" ,
308308 expectedScript : `
309309export POSTSTART_TIMEOUT_DURATION="10"
310310export POSTSTART_KILL_AFTER_DURATION="5"
@@ -350,7 +350,7 @@ exit $exit_code
350350 {
351351 name : "Script with zero timeout (no timeout)" ,
352352 escapedUserScript : "echo 'running indefinitely...'" ,
353- timeoutSeconds : 0 ,
353+ timeout : "0s" ,
354354 expectedScript : `
355355export POSTSTART_TIMEOUT_DURATION="0"
356356export POSTSTART_KILL_AFTER_DURATION="5"
@@ -395,7 +395,7 @@ exit $exit_code
395395 {
396396 name : "Empty user script" ,
397397 escapedUserScript : "" ,
398- timeoutSeconds : 5 ,
398+ timeout : "5s" ,
399399 expectedScript : `
400400export POSTSTART_TIMEOUT_DURATION="5"
401401export POSTSTART_KILL_AFTER_DURATION="5"
@@ -440,7 +440,7 @@ exit $exit_code
440440 {
441441 name : "User script with already escaped single quotes" ,
442442 escapedUserScript : "echo 'it'\\ ''s complex'" ,
443- timeoutSeconds : 30 ,
443+ timeout : "30s" ,
444444 expectedScript : `
445445export POSTSTART_TIMEOUT_DURATION="30"
446446export POSTSTART_KILL_AFTER_DURATION="5"
@@ -479,14 +479,59 @@ else
479479 fi
480480fi
481481
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+
482527exit $exit_code
483528` ,
484529 },
485530 }
486531
487532 for _ , tt := range tests {
488533 t .Run (tt .name , func (t * testing.T ) {
489- script := generateScriptWithTimeout (tt .escapedUserScript , tt .timeoutSeconds )
534+ script := generateScriptWithTimeout (tt .escapedUserScript , tt .timeout )
490535 assert .Equal (t , tt .expectedScript , script )
491536 })
492537 }
0 commit comments