@@ -33,8 +33,9 @@ type postStartTestCase struct {
3333}
3434
3535type  postStartTestInput  struct  {
36- 	Devfile     * dw.DevWorkspaceTemplateSpec  `json:"devfile,omitempty"` 
37- 	Containers  []corev1.Container            `json:"containers,omitempty"` 
36+ 	Devfile       * dw.DevWorkspaceTemplateSpec  `json:"devfile,omitempty"` 
37+ 	DebugEnabled  bool                          `json:"debugEnabled,omitempty"` 
38+ 	Containers    []corev1.Container            `json:"containers,omitempty"` 
3839}
3940
4041type  postStartTestOutput  struct  {
@@ -76,7 +77,7 @@ func TestAddPostStartLifecycleHooks(t *testing.T) {
7677	for  _ , tt  :=  range  tests  {
7778		t .Run (fmt .Sprintf ("%s (%s)" , tt .Name , tt .testPath ), func (t  * testing.T ) {
7879			var  timeout  string 
79- 			err  :=  AddPostStartLifecycleHooks (tt .Input .Devfile , tt .Input .Containers , timeout )
80+ 			err  :=  AddPostStartLifecycleHooks (tt .Input .Devfile , tt .Input .Containers , timeout ,  tt . Input . DebugEnabled )
8081			if  tt .Output .ErrRegexp  !=  nil  &&  assert .Error (t , err ) {
8182				assert .Regexp (t , * tt .Output .ErrRegexp , err .Error (), "Error message should match" )
8283			} else  {
@@ -299,15 +300,18 @@ func TestGenerateScriptWithTimeout(t *testing.T) {
299300		name               string 
300301		escapedUserScript  string 
301302		timeout            string 
303+ 		debugEnabled       bool 
302304		expectedScript     string 
303305	}{
304306		{
305307			name :              "Basic script with timeout" ,
306308			escapedUserScript : "echo 'hello world'\n sleep 1" ,
307309			timeout :           "10s" ,
310+ 			debugEnabled :      false ,
308311			expectedScript : ` 
309312export POSTSTART_TIMEOUT_DURATION="10" 
310313export POSTSTART_KILL_AFTER_DURATION="5" 
314+ export DEBUG_ENABLED="false" 
311315
312316_TIMEOUT_COMMAND_PART="" 
313317_WAS_TIMEOUT_USED="false" # Use strings "true" or "false" for shell boolean 
@@ -325,6 +329,11 @@ ${_TIMEOUT_COMMAND_PART} /bin/sh -c 'echo 'hello world'
325329sleep 1' 
326330exit_code=$? 
327331
332+ if [ "$DEBUG_ENABLED" = "true" ] && [ $exit_code -ne 0 ]; then 
333+   echo "[postStart] failure encountered, sleep for debugging" >&2 
334+   sleep 3600 
335+ fi 
336+ 
328337# Check the exit code based on whether timeout was attempted 
329338if [ "$_WAS_TIMEOUT_USED" = "true" ]; then 
330339  if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM) 
@@ -351,9 +360,11 @@ exit $exit_code
351360			name :              "Script with zero timeout (no timeout)" ,
352361			escapedUserScript : "echo 'running indefinitely...'" ,
353362			timeout :           "0s" ,
363+ 			debugEnabled :      false ,
354364			expectedScript : ` 
355365export POSTSTART_TIMEOUT_DURATION="0" 
356366export POSTSTART_KILL_AFTER_DURATION="5" 
367+ export DEBUG_ENABLED="false" 
357368
358369_TIMEOUT_COMMAND_PART="" 
359370_WAS_TIMEOUT_USED="false" # Use strings "true" or "false" for shell boolean 
370381${_TIMEOUT_COMMAND_PART} /bin/sh -c 'echo 'running indefinitely...'' 
371382exit_code=$? 
372383
384+ if [ "$DEBUG_ENABLED" = "true" ] && [ $exit_code -ne 0 ]; then 
385+   echo "[postStart] failure encountered, sleep for debugging" >&2 
386+   sleep 3600 
387+ fi 
388+ 
373389# Check the exit code based on whether timeout was attempted 
374390if [ "$_WAS_TIMEOUT_USED" = "true" ]; then 
375391  if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM) 
@@ -396,9 +412,11 @@ exit $exit_code
396412			name :              "Empty user script" ,
397413			escapedUserScript : "" ,
398414			timeout :           "5s" ,
415+ 			debugEnabled :      false ,
399416			expectedScript : ` 
400417export POSTSTART_TIMEOUT_DURATION="5" 
401418export POSTSTART_KILL_AFTER_DURATION="5" 
419+ export DEBUG_ENABLED="false" 
402420
403421_TIMEOUT_COMMAND_PART="" 
404422_WAS_TIMEOUT_USED="false" # Use strings "true" or "false" for shell boolean 
415433${_TIMEOUT_COMMAND_PART} /bin/sh -c '' 
416434exit_code=$? 
417435
436+ if [ "$DEBUG_ENABLED" = "true" ] && [ $exit_code -ne 0 ]; then 
437+   echo "[postStart] failure encountered, sleep for debugging" >&2 
438+   sleep 3600 
439+ fi 
440+ 
418441# Check the exit code based on whether timeout was attempted 
419442if [ "$_WAS_TIMEOUT_USED" = "true" ]; then 
420443  if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM) 
@@ -441,9 +464,11 @@ exit $exit_code
441464			name :              "User script with already escaped single quotes" ,
442465			escapedUserScript : "echo 'it'\\ ''s complex'" ,
443466			timeout :           "30s" ,
467+ 			debugEnabled :      false ,
444468			expectedScript : ` 
445469export POSTSTART_TIMEOUT_DURATION="30" 
446470export POSTSTART_KILL_AFTER_DURATION="5" 
471+ export DEBUG_ENABLED="false" 
447472
448473_TIMEOUT_COMMAND_PART="" 
449474_WAS_TIMEOUT_USED="false" # Use strings "true" or "false" for shell boolean 
460485${_TIMEOUT_COMMAND_PART} /bin/sh -c 'echo 'it'\''s complex'' 
461486exit_code=$? 
462487
488+ if [ "$DEBUG_ENABLED" = "true" ] && [ $exit_code -ne 0 ]; then 
489+   echo "[postStart] failure encountered, sleep for debugging" >&2 
490+   sleep 3600 
491+ fi 
492+ 
463493# Check the exit code based on whether timeout was attempted 
464494if [ "$_WAS_TIMEOUT_USED" = "true" ]; then 
465495  if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM) 
@@ -486,9 +516,11 @@ exit $exit_code
486516			name :              "User script with minute timeout" ,
487517			escapedUserScript : "echo 'wait for it...'" ,
488518			timeout :           "2m" ,
519+ 			debugEnabled :      false ,
489520			expectedScript : ` 
490521export POSTSTART_TIMEOUT_DURATION="120" 
491522export POSTSTART_KILL_AFTER_DURATION="5" 
523+ export DEBUG_ENABLED="false" 
492524
493525_TIMEOUT_COMMAND_PART="" 
494526_WAS_TIMEOUT_USED="false" # Use strings "true" or "false" for shell boolean 
505537${_TIMEOUT_COMMAND_PART} /bin/sh -c 'echo 'wait for it...'' 
506538exit_code=$? 
507539
540+ if [ "$DEBUG_ENABLED" = "true" ] && [ $exit_code -ne 0 ]; then 
541+   echo "[postStart] failure encountered, sleep for debugging" >&2 
542+   sleep 3600 
543+ fi 
544+ 
545+ # Check the exit code based on whether timeout was attempted 
546+ if [ "$_WAS_TIMEOUT_USED" = "true" ]; then 
547+   if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM) 
548+     echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2 
549+   elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL) 
550+     echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2 
551+   elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code 
552+     echo "[postStart hook] Commands failed with exit code $exit_code." >&2 
553+   else 
554+     echo "[postStart hook] Commands completed successfully within the time limit." >&2 
555+   fi 
556+ else 
557+   if [ $exit_code -ne 0 ]; then 
558+     echo "[postStart hook] Commands failed with exit code $exit_code (no timeout)." >&2 
559+   else 
560+     echo "[postStart hook] Commands completed successfully (no timeout)." >&2 
561+   fi 
562+ fi 
563+ 
564+ exit $exit_code 
565+ ` ,
566+ 		},
567+ 		{
568+ 			name :              "Basic script with timeout and debug enabled" ,
569+ 			escapedUserScript : "echo 'hello world'\n sleep 1" ,
570+ 			timeout :           "10s" ,
571+ 			debugEnabled :      true ,
572+ 			expectedScript : ` 
573+ export POSTSTART_TIMEOUT_DURATION="10" 
574+ export POSTSTART_KILL_AFTER_DURATION="5" 
575+ export DEBUG_ENABLED="true" 
576+ 
577+ _TIMEOUT_COMMAND_PART="" 
578+ _WAS_TIMEOUT_USED="false" # Use strings "true" or "false" for shell boolean 
579+ 
580+ if command -v timeout >/dev/null 2>&1; then 
581+   echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2 
582+   _TIMEOUT_COMMAND_PART="timeout --preserve-status --kill-after=${POSTSTART_KILL_AFTER_DURATION} ${POSTSTART_TIMEOUT_DURATION}" 
583+   _WAS_TIMEOUT_USED="true" 
584+ else 
585+   echo "[postStart hook] WARNING: 'timeout' utility not found. Executing commands without timeout." >&2 
586+ fi 
587+ 
588+ # Execute the user's script 
589+ ${_TIMEOUT_COMMAND_PART} /bin/sh -c 'echo 'hello world' 
590+ sleep 1' 
591+ exit_code=$? 
592+ 
593+ if [ "$DEBUG_ENABLED" = "true" ] && [ $exit_code -ne 0 ]; then 
594+   echo "[postStart] failure encountered, sleep for debugging" >&2 
595+   sleep 3600 
596+ fi 
597+ 
508598# Check the exit code based on whether timeout was attempted 
509599if [ "$_WAS_TIMEOUT_USED" = "true" ]; then 
510600  if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM) 
@@ -531,7 +621,7 @@ exit $exit_code
531621
532622	for  _ , tt  :=  range  tests  {
533623		t .Run (tt .name , func (t  * testing.T ) {
534- 			script  :=  generateScriptWithTimeout (tt .escapedUserScript , tt .timeout )
624+ 			script  :=  generateScriptWithTimeout (tt .debugEnabled ,  tt . escapedUserScript , tt .timeout )
535625			assert .Equal (t , tt .expectedScript , script )
536626		})
537627	}
0 commit comments