Skip to content

Commit 3093602

Browse files
committed
[chore] Add trap stacking to package tests
1 parent b2c73c6 commit 3093602

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

@@ -38,7 +57,10 @@ image_name="otelcontribcol-$pkg_type-test"
3857
container_name="$image_name"
3958
container_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

4365
podman build -t "$image_name" -f "$SCRIPT_DIR/Dockerfile.test.$pkg_type" "$SCRIPT_DIR"
4466
podman 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
6085
sleep 5

0 commit comments

Comments
 (0)