Skip to content

Commit 7b544e1

Browse files
[chore] Pull systemd service logs on package test failure (#860)
* [chore] Pull systemd service logs on package test failure * [chore] Add trap stacking to package tests
1 parent 769df5c commit 7b544e1

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

scripts/package-tests/package-tests.sh

Lines changed: 32 additions & 1 deletion
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
@@ -50,6 +72,15 @@ podman run --name "$container_name" -d "$image_name"
5072
$container_exec systemctl is-system-running --quiet --wait
5173
install_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
5485
sleep 5
5586
echo "Checking $SERVICE_NAME service status ..."

0 commit comments

Comments
 (0)