podman: remember hooks-dir on restarts

When podman restarts config values within the Engine are lost.
Add --hook-dirs arguments as appropriate to the cleanup command
so that hooks are preserved on restarts due to the on-restart setting

Tests: add a check that prestart/poststop hooks ran every time after 2
restarts.
`wait_for_restart_count` was re-used to wait for restarts and moved to
helpers file.

Signed-off-by: Dominique Martinet <dominique.martinet@atmark-techno.com>
Fixes: #17935
This commit is contained in:
Dominique Martinet 2025-05-14 17:24:27 +09:00
parent a1ac6c33cc
commit 90ee7c86a3
4 changed files with 71 additions and 21 deletions

View File

@ -285,6 +285,9 @@ func CreateExitCommandArgs(storageConfig storageTypes.StoreOptions, config *conf
"--db-backend", config.Engine.DBBackend,
fmt.Sprintf("--transient-store=%t", storageConfig.TransientStore),
}
for _, dir := range config.Engine.HooksDir.Get() {
command = append(command, []string{"--hooks-dir", dir}...)
}
if storageConfig.ImageStore != "" {
command = append(command, []string{"--imagestore", storageConfig.ImageStore}...)
}

View File

@ -1497,6 +1497,50 @@ EOF
done
}
# bats test_tags=ci:parallel
@test "podman run --restart preserves hooks-dir" {
# regression test for #17935 to ensure hooks are run on successful restarts
ctr=c_$(safename)
hooksdir=$PODMAN_TMPDIR/hooks_$(safename)
skip_if_remote "--hooks-dir is not usable with remote"
mkdir -p "$hooksdir"
cat > "$hooksdir/settings.json" <<EOF
{
"version": "1.0.0",
"when": { "always": true },
"hook": {
"path": "$hooksdir/hook.sh"
},
"stages": ["prestart", "poststop"]
}
EOF
cat >"$hooksdir/hook.sh" <<EOF
#!/bin/sh
# consume stdin
cat > /dev/null
echo ran >> "$hooksdir/log"
EOF
chmod +x "$hooksdir/hook.sh"
run_podman run -d --restart=on-failure:2 \
--name="$ctr" --hooks-dir="$hooksdir" $IMAGE false
wait_for_restart_count "$ctr" 2 "restart preserves hooks-dir"
# also make sure 3rd restart has finished
# wait also waits until 'cleanup' is done, so poststop hook is
# done running after this command
run_podman wait "$ctr"
run_podman rm "$ctr"
# check prestart and poststop ran 3 times each
assert "$(wc -l < "$hooksdir/log")" = 6
rm -r "$hooksdir"
}
# bats test_tags=ci:parallel
@test "podman run - custom static_dir" {
# regression test for #19938 to make sure the cleanup process uses the same

View File

@ -968,27 +968,6 @@ EOF
fi
}
function wait_for_restart_count() {
local cname="$1"
local count="$2"
local tname="$3"
local timeout=10
while :; do
# Previously this would fail as the container would run out of ips after 5 restarts.
run_podman inspect --format "{{.RestartCount}}" $cname
if [[ "$output" == "$2" ]]; then
break
fi
timeout=$((timeout - 1))
if [[ $timeout -eq 0 ]]; then
die "Timed out waiting for RestartCount with $tname"
fi
sleep 0.5
done
}
# Test for https://github.com/containers/podman/issues/18615
# CANNOT BE PARALLELIZED due to strict checking of /run/netns
@test "podman network cleanup --userns + --restart" {

View File

@ -1347,6 +1347,30 @@ function ensure_no_mountpoint() {
fi
}
###########################
# ensure container has been restarted requested times
###########################
function wait_for_restart_count() {
local cname="$1"
local count="$2"
local tname="$3"
local timeout=10
while :; do
# Previously this would fail as the container would run out of ips after 5 restarts.
run_podman inspect --format "{{.RestartCount}}" $cname
if [[ "$output" == "$2" ]]; then
break
fi
timeout=$((timeout - 1))
if [[ $timeout -eq 0 ]]; then
die "Timed out waiting for RestartCount with $tname"
fi
sleep 0.5
done
}
# END miscellaneous tools
###############################################################################