Skip to content

Commit 18e67f4

Browse files
committed
[chore] Add trap stacking to package tests
1 parent 9999e36 commit 18e67f4

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

scripts/package-tests/package-tests.sh

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ DISTRO="${2:-}"
1414
SERVICE_NAME=$DISTRO
1515
PROCESS_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
1837
source "$SCRIPT_DIR"/common.sh
1938

@@ -37,7 +56,10 @@ image_name="otelcontribcol-$pkg_type-test"
3756
container_name="$image_name"
3857
container_exec="podman exec $container_name"
3958

40-
trap 'podman rm -fv $container_name >/dev/null 2>&1 || true' EXIT
59+
podman_cleanup() {
60+
podman rm -fv "$container_name" >/dev/null 2>&1 || true
61+
}
62+
add_trap_func podman_cleanup
4163

4264
podman build -t "$image_name" -f "$SCRIPT_DIR/Dockerfile.test.$pkg_type" "$SCRIPT_DIR"
4365
podman rm -fv "$container_name" >/dev/null 2>&1 || true
@@ -53,7 +75,10 @@ install_pkg "$container_name" "$PKG_PATH"
5375
# when it's not properly active. This is added as a trap because the check
5476
# for service status below will return an error exitcode if the service is
5577
# not active, triggering the end of this script because of the shell option `-e`
56-
trap '$container_exec journalctl -u "$SERVICE_NAME" || true' EXIT
78+
journalctl_logs() {
79+
$container_exec journalctl -u "$SERVICE_NAME" || true
80+
}
81+
add_trap_func journalctl_logs
5782

5883
# ensure service has started and still running after 5 seconds
5984
sleep 5

0 commit comments

Comments
 (0)