Skip to content

Commit d50e932

Browse files
committed
fixup! fixup! fixup! test: add/update tests related to postStart hook commands
Signed-off-by: Oleksii Kurinnyi <[email protected]>
1 parent 838b06b commit d50e932

File tree

5 files changed

+252
-99
lines changed

5 files changed

+252
-99
lines changed

pkg/library/lifecycle/poststart_test.go

Lines changed: 112 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -308,22 +308,39 @@ func TestGenerateScriptWithTimeout(t *testing.T) {
308308
export POSTSTART_TIMEOUT_DURATION="10"
309309
export POSTSTART_KILL_AFTER_DURATION="5"
310310
311-
echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
311+
_TIMEOUT_COMMAND_PART=""
312+
_WAS_TIMEOUT_USED="false" # Use strings "true" or "false" for shell boolean
312313
313-
# Run the user's script under the 'timeout' utility.
314-
timeout --preserve-status --kill-after="${POSTSTART_KILL_AFTER_DURATION}" "${POSTSTART_TIMEOUT_DURATION}" /bin/sh -c 'echo 'hello world'
314+
if command -v timeout >/dev/null 2>&1; then
315+
echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
316+
_TIMEOUT_COMMAND_PART="timeout --preserve-status --kill-after=\"${POSTSTART_KILL_AFTER_DURATION}\" \"${POSTSTART_TIMEOUT_DURATION}\""
317+
_WAS_TIMEOUT_USED="true"
318+
else
319+
echo "[postStart hook] WARNING: 'timeout' utility not found. Executing commands without timeout." >&2
320+
fi
321+
322+
# Execute the user's script
323+
${_TIMEOUT_COMMAND_PART} /bin/sh -c 'echo 'hello world'
315324
sleep 1'
316325
exit_code=$?
317326
318-
# Check the exit code from 'timeout'
319-
if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
320-
echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
321-
elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
322-
echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
323-
elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
324-
echo "[postStart hook] Commands failed with exit code $exit_code." >&2
327+
# Check the exit code based on whether timeout was attempted
328+
if [ "$_WAS_TIMEOUT_USED" = "true" ]; then
329+
if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
330+
echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
331+
elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
332+
echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
333+
elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
334+
echo "[postStart hook] Commands failed with exit code $exit_code." >&2
335+
else
336+
echo "[postStart hook] Commands completed successfully within the time limit." >&2
337+
fi
325338
else
326-
echo "[postStart hook] Commands completed successfully within the time limit." >&2
339+
if [ $exit_code -ne 0 ]; then
340+
echo "[postStart hook] Commands failed with exit code $exit_code (no timeout)." >&2
341+
else
342+
echo "[postStart hook] Commands completed successfully (no timeout)." >&2
343+
fi
327344
fi
328345
329346
exit $exit_code
@@ -337,21 +354,38 @@ exit $exit_code
337354
export POSTSTART_TIMEOUT_DURATION="0"
338355
export POSTSTART_KILL_AFTER_DURATION="5"
339356
340-
echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
357+
_TIMEOUT_COMMAND_PART=""
358+
_WAS_TIMEOUT_USED="false" # Use strings "true" or "false" for shell boolean
359+
360+
if command -v timeout >/dev/null 2>&1; then
361+
echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
362+
_TIMEOUT_COMMAND_PART="timeout --preserve-status --kill-after=\"${POSTSTART_KILL_AFTER_DURATION}\" \"${POSTSTART_TIMEOUT_DURATION}\""
363+
_WAS_TIMEOUT_USED="true"
364+
else
365+
echo "[postStart hook] WARNING: 'timeout' utility not found. Executing commands without timeout." >&2
366+
fi
341367
342-
# Run the user's script under the 'timeout' utility.
343-
timeout --preserve-status --kill-after="${POSTSTART_KILL_AFTER_DURATION}" "${POSTSTART_TIMEOUT_DURATION}" /bin/sh -c 'echo 'running indefinitely...''
368+
# Execute the user's script
369+
${_TIMEOUT_COMMAND_PART} /bin/sh -c 'echo 'running indefinitely...''
344370
exit_code=$?
345371
346-
# Check the exit code from 'timeout'
347-
if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
348-
echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
349-
elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
350-
echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
351-
elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
352-
echo "[postStart hook] Commands failed with exit code $exit_code." >&2
372+
# Check the exit code based on whether timeout was attempted
373+
if [ "$_WAS_TIMEOUT_USED" = "true" ]; then
374+
if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
375+
echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
376+
elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
377+
echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
378+
elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
379+
echo "[postStart hook] Commands failed with exit code $exit_code." >&2
380+
else
381+
echo "[postStart hook] Commands completed successfully within the time limit." >&2
382+
fi
353383
else
354-
echo "[postStart hook] Commands completed successfully within the time limit." >&2
384+
if [ $exit_code -ne 0 ]; then
385+
echo "[postStart hook] Commands failed with exit code $exit_code (no timeout)." >&2
386+
else
387+
echo "[postStart hook] Commands completed successfully (no timeout)." >&2
388+
fi
355389
fi
356390
357391
exit $exit_code
@@ -365,21 +399,38 @@ exit $exit_code
365399
export POSTSTART_TIMEOUT_DURATION="5"
366400
export POSTSTART_KILL_AFTER_DURATION="5"
367401
368-
echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
402+
_TIMEOUT_COMMAND_PART=""
403+
_WAS_TIMEOUT_USED="false" # Use strings "true" or "false" for shell boolean
369404
370-
# Run the user's script under the 'timeout' utility.
371-
timeout --preserve-status --kill-after="${POSTSTART_KILL_AFTER_DURATION}" "${POSTSTART_TIMEOUT_DURATION}" /bin/sh -c ''
405+
if command -v timeout >/dev/null 2>&1; then
406+
echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
407+
_TIMEOUT_COMMAND_PART="timeout --preserve-status --kill-after=\"${POSTSTART_KILL_AFTER_DURATION}\" \"${POSTSTART_TIMEOUT_DURATION}\""
408+
_WAS_TIMEOUT_USED="true"
409+
else
410+
echo "[postStart hook] WARNING: 'timeout' utility not found. Executing commands without timeout." >&2
411+
fi
412+
413+
# Execute the user's script
414+
${_TIMEOUT_COMMAND_PART} /bin/sh -c ''
372415
exit_code=$?
373416
374-
# Check the exit code from 'timeout'
375-
if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
376-
echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
377-
elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
378-
echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
379-
elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
380-
echo "[postStart hook] Commands failed with exit code $exit_code." >&2
417+
# Check the exit code based on whether timeout was attempted
418+
if [ "$_WAS_TIMEOUT_USED" = "true" ]; then
419+
if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
420+
echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
421+
elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
422+
echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
423+
elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
424+
echo "[postStart hook] Commands failed with exit code $exit_code." >&2
425+
else
426+
echo "[postStart hook] Commands completed successfully within the time limit." >&2
427+
fi
381428
else
382-
echo "[postStart hook] Commands completed successfully within the time limit." >&2
429+
if [ $exit_code -ne 0 ]; then
430+
echo "[postStart hook] Commands failed with exit code $exit_code (no timeout)." >&2
431+
else
432+
echo "[postStart hook] Commands completed successfully (no timeout)." >&2
433+
fi
383434
fi
384435
385436
exit $exit_code
@@ -393,21 +444,38 @@ exit $exit_code
393444
export POSTSTART_TIMEOUT_DURATION="30"
394445
export POSTSTART_KILL_AFTER_DURATION="5"
395446
396-
echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
447+
_TIMEOUT_COMMAND_PART=""
448+
_WAS_TIMEOUT_USED="false" # Use strings "true" or "false" for shell boolean
449+
450+
if command -v timeout >/dev/null 2>&1; then
451+
echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
452+
_TIMEOUT_COMMAND_PART="timeout --preserve-status --kill-after=\"${POSTSTART_KILL_AFTER_DURATION}\" \"${POSTSTART_TIMEOUT_DURATION}\""
453+
_WAS_TIMEOUT_USED="true"
454+
else
455+
echo "[postStart hook] WARNING: 'timeout' utility not found. Executing commands without timeout." >&2
456+
fi
397457
398-
# Run the user's script under the 'timeout' utility.
399-
timeout --preserve-status --kill-after="${POSTSTART_KILL_AFTER_DURATION}" "${POSTSTART_TIMEOUT_DURATION}" /bin/sh -c 'echo 'it'\''s complex''
458+
# Execute the user's script
459+
${_TIMEOUT_COMMAND_PART} /bin/sh -c 'echo 'it'\''s complex''
400460
exit_code=$?
401461
402-
# Check the exit code from 'timeout'
403-
if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
404-
echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
405-
elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
406-
echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
407-
elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
408-
echo "[postStart hook] Commands failed with exit code $exit_code." >&2
462+
# Check the exit code based on whether timeout was attempted
463+
if [ "$_WAS_TIMEOUT_USED" = "true" ]; then
464+
if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
465+
echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
466+
elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
467+
echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
468+
elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
469+
echo "[postStart hook] Commands failed with exit code $exit_code." >&2
470+
else
471+
echo "[postStart hook] Commands completed successfully within the time limit." >&2
472+
fi
409473
else
410-
echo "[postStart hook] Commands completed successfully within the time limit." >&2
474+
if [ $exit_code -ne 0 ]; then
475+
echo "[postStart hook] Commands failed with exit code $exit_code (no timeout)." >&2
476+
else
477+
echo "[postStart hook] Commands completed successfully (no timeout)." >&2
478+
fi
411479
fi
412480
413481
exit $exit_code

pkg/library/lifecycle/testdata/postStart/adds_all_postStart_commands.yaml

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,39 @@ output:
4444
export POSTSTART_TIMEOUT_DURATION="0"
4545
export POSTSTART_KILL_AFTER_DURATION="5"
4646
47-
echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
47+
_TIMEOUT_COMMAND_PART=""
48+
_WAS_TIMEOUT_USED="false" # Use strings "true" or "false" for shell boolean
4849
49-
# Run the user's script under the 'timeout' utility.
50-
timeout --preserve-status --kill-after="${POSTSTART_KILL_AFTER_DURATION}" "${POSTSTART_TIMEOUT_DURATION}" /bin/sh -c 'set -e
50+
if command -v timeout >/dev/null 2>&1; then
51+
echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
52+
_TIMEOUT_COMMAND_PART="timeout --preserve-status --kill-after=\"${POSTSTART_KILL_AFTER_DURATION}\" \"${POSTSTART_TIMEOUT_DURATION}\""
53+
_WAS_TIMEOUT_USED="true"
54+
else
55+
echo "[postStart hook] WARNING: 'timeout' utility not found. Executing commands without timeout." >&2
56+
fi
57+
58+
# Execute the user's script
59+
${_TIMEOUT_COMMAND_PART} /bin/sh -c 'set -e
5160
echo '\''hello world 1'\'''
5261
exit_code=$?
5362
54-
# Check the exit code from 'timeout'
55-
if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
56-
echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
57-
elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
58-
echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
59-
elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
60-
echo "[postStart hook] Commands failed with exit code $exit_code." >&2
63+
# Check the exit code based on whether timeout was attempted
64+
if [ "$_WAS_TIMEOUT_USED" = "true" ]; then
65+
if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
66+
echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
67+
elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
68+
echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
69+
elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
70+
echo "[postStart hook] Commands failed with exit code $exit_code." >&2
71+
else
72+
echo "[postStart hook] Commands completed successfully within the time limit." >&2
73+
fi
6174
else
62-
echo "[postStart hook] Commands completed successfully within the time limit." >&2
75+
if [ $exit_code -ne 0 ]; then
76+
echo "[postStart hook] Commands failed with exit code $exit_code (no timeout)." >&2
77+
else
78+
echo "[postStart hook] Commands completed successfully (no timeout)." >&2
79+
fi
6380
fi
6481
6582
exit $exit_code
@@ -84,22 +101,39 @@ output:
84101
export POSTSTART_TIMEOUT_DURATION="0"
85102
export POSTSTART_KILL_AFTER_DURATION="5"
86103
87-
echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
104+
_TIMEOUT_COMMAND_PART=""
105+
_WAS_TIMEOUT_USED="false" # Use strings "true" or "false" for shell boolean
106+
107+
if command -v timeout >/dev/null 2>&1; then
108+
echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
109+
_TIMEOUT_COMMAND_PART="timeout --preserve-status --kill-after=\"${POSTSTART_KILL_AFTER_DURATION}\" \"${POSTSTART_TIMEOUT_DURATION}\""
110+
_WAS_TIMEOUT_USED="true"
111+
else
112+
echo "[postStart hook] WARNING: 'timeout' utility not found. Executing commands without timeout." >&2
113+
fi
88114
89-
# Run the user's script under the 'timeout' utility.
90-
timeout --preserve-status --kill-after="${POSTSTART_KILL_AFTER_DURATION}" "${POSTSTART_TIMEOUT_DURATION}" /bin/sh -c 'set -e
115+
# Execute the user's script
116+
${_TIMEOUT_COMMAND_PART} /bin/sh -c 'set -e
91117
cd '\''/tmp/test-dir'\'' && echo '\''hello world 2'\'''
92118
exit_code=$?
93119
94-
# Check the exit code from 'timeout'
95-
if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
96-
echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
97-
elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
98-
echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
99-
elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
100-
echo "[postStart hook] Commands failed with exit code $exit_code." >&2
120+
# Check the exit code based on whether timeout was attempted
121+
if [ "$_WAS_TIMEOUT_USED" = "true" ]; then
122+
if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
123+
echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
124+
elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
125+
echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
126+
elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
127+
echo "[postStart hook] Commands failed with exit code $exit_code." >&2
128+
else
129+
echo "[postStart hook] Commands completed successfully within the time limit." >&2
130+
fi
101131
else
102-
echo "[postStart hook] Commands completed successfully within the time limit." >&2
132+
if [ $exit_code -ne 0 ]; then
133+
echo "[postStart hook] Commands failed with exit code $exit_code (no timeout)." >&2
134+
else
135+
echo "[postStart hook] Commands completed successfully (no timeout)." >&2
136+
fi
103137
fi
104138
105139
exit $exit_code

0 commit comments

Comments
 (0)