@@ -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
@@ -54,7 +76,10 @@ install_pkg "$container_name" "$PKG_PATH"
5476# when it's not properly active. This is added as a trap because the check
5577# for service status below will return an error exitcode if the service is
5678# not active, triggering the end of this script because of the shell option `-e`
57- trap ' $container_exec journalctl -u "$SERVICE_NAME" || true' EXIT
79+ journalctl_logs () {
80+ $container_exec journalctl -u " $SERVICE_NAME " || true
81+ }
82+ add_trap_func journalctl_logs
5883
5984# ensure service has started and still running after 5 seconds
6085sleep 5
0 commit comments