@@ -14,6 +14,25 @@ DISTRO="${2:-}"
1414SERVICE_NAME=$DISTRO
1515PROCESS_NAME=$DISTRO
1616
17+ # Global array for trap functions
18+ TRAP_FUNCS=()
19+
20+ # Helper to add functions to the trap list
21+ add_trap_func () {
22+ TRAP_FUNCS+=(" $1 " )
23+ }
24+
25+ # Wrapper that calls each trap'ed function.
26+ # It uses LIFO order, like Go's `defer`.
27+ run_traps () {
28+ if [ " ${# TRAP_FUNCS[@]} " -gt 0 ]; then
29+ for (( i = ${# TRAP_FUNCS[@]} - 1 ; i >= 0 ; i-- )) ; do
30+ " ${TRAP_FUNCS[i]} "
31+ done
32+ fi
33+ }
34+ trap ' run_traps' EXIT
35+
1736# shellcheck source=scripts/package-tests/common.sh
1837source " $SCRIPT_DIR " /common.sh
1938
@@ -38,7 +57,10 @@ image_name="otelcontribcol-$pkg_type-test"
3857container_name=" $image_name "
3958container_exec=" podman exec $container_name "
4059
41- trap ' podman rm -fv $container_name >/dev/null 2>&1 || true' EXIT
60+ podman_cleanup () {
61+ podman rm -fv " $container_name " > /dev/null 2>&1 || true
62+ }
63+ add_trap_func podman_cleanup
4264
4365podman build -t " $image_name " -f " $SCRIPT_DIR /Dockerfile.test.$pkg_type " " $SCRIPT_DIR "
4466podman rm -fv " $container_name " > /dev/null 2>&1 || true
@@ -50,6 +72,15 @@ podman run --name "$container_name" -d "$image_name"
5072$container_exec systemctl is-system-running --quiet --wait
5173install_pkg " $container_name " " $PKG_PATH "
5274
75+ # If we got to this point, we might need to check the logs of the systemd service
76+ # when it's not properly active. This is added as a trap because the check
77+ # for service status below will return an error exitcode if the service is
78+ # not active, triggering the end of this script because of the shell option `-e`
79+ journalctl_logs () {
80+ $container_exec journalctl -u " $SERVICE_NAME " || true
81+ }
82+ add_trap_func journalctl_logs
83+
5384# ensure service has started and still running after 5 seconds
5485sleep 5
5586echo " Checking $SERVICE_NAME service status ..."
0 commit comments