diff --git a/pkg/specgenutil/util.go b/pkg/specgenutil/util.go index 222e50f934..ff152f7709 100644 --- a/pkg/specgenutil/util.go +++ b/pkg/specgenutil/util.go @@ -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}...) } diff --git a/test/system/030-run.bats b/test/system/030-run.bats index d89d3c1ca2..83c5f92eda 100644 --- a/test/system/030-run.bats +++ b/test/system/030-run.bats @@ -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" <"$hooksdir/hook.sh" < /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 diff --git a/test/system/500-networking.bats b/test/system/500-networking.bats index 89237483af..f0b7d52312 100644 --- a/test/system/500-networking.bats +++ b/test/system/500-networking.bats @@ -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" { diff --git a/test/system/helpers.bash b/test/system/helpers.bash index 838bdf13ed..e61c17f8e0 100644 --- a/test/system/helpers.bash +++ b/test/system/helpers.bash @@ -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 ###############################################################################