Skip to content

Commit 90ee7c8

Browse files
committed
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 <[email protected]> Fixes: #17935
1 parent a1ac6c3 commit 90ee7c8

File tree

4 files changed

+71
-21
lines changed

4 files changed

+71
-21
lines changed

pkg/specgenutil/util.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ func CreateExitCommandArgs(storageConfig storageTypes.StoreOptions, config *conf
285285
"--db-backend", config.Engine.DBBackend,
286286
fmt.Sprintf("--transient-store=%t", storageConfig.TransientStore),
287287
}
288+
for _, dir := range config.Engine.HooksDir.Get() {
289+
command = append(command, []string{"--hooks-dir", dir}...)
290+
}
288291
if storageConfig.ImageStore != "" {
289292
command = append(command, []string{"--imagestore", storageConfig.ImageStore}...)
290293
}

test/system/030-run.bats

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,50 @@ EOF
14971497
done
14981498
}
14991499

1500+
# bats test_tags=ci:parallel
1501+
@test "podman run --restart preserves hooks-dir" {
1502+
# regression test for #17935 to ensure hooks are run on successful restarts
1503+
ctr=c_$(safename)
1504+
hooksdir=$PODMAN_TMPDIR/hooks_$(safename)
1505+
1506+
skip_if_remote "--hooks-dir is not usable with remote"
1507+
1508+
mkdir -p "$hooksdir"
1509+
cat > "$hooksdir/settings.json" <<EOF
1510+
{
1511+
"version": "1.0.0",
1512+
"when": { "always": true },
1513+
"hook": {
1514+
"path": "$hooksdir/hook.sh"
1515+
},
1516+
"stages": ["prestart", "poststop"]
1517+
}
1518+
EOF
1519+
cat >"$hooksdir/hook.sh" <<EOF
1520+
#!/bin/sh
1521+
# consume stdin
1522+
cat > /dev/null
1523+
echo ran >> "$hooksdir/log"
1524+
EOF
1525+
chmod +x "$hooksdir/hook.sh"
1526+
1527+
run_podman run -d --restart=on-failure:2 \
1528+
--name="$ctr" --hooks-dir="$hooksdir" $IMAGE false
1529+
1530+
wait_for_restart_count "$ctr" 2 "restart preserves hooks-dir"
1531+
1532+
# also make sure 3rd restart has finished
1533+
# wait also waits until 'cleanup' is done, so poststop hook is
1534+
# done running after this command
1535+
run_podman wait "$ctr"
1536+
run_podman rm "$ctr"
1537+
1538+
# check prestart and poststop ran 3 times each
1539+
assert "$(wc -l < "$hooksdir/log")" = 6
1540+
1541+
rm -r "$hooksdir"
1542+
}
1543+
15001544
# bats test_tags=ci:parallel
15011545
@test "podman run - custom static_dir" {
15021546
# regression test for #19938 to make sure the cleanup process uses the same

test/system/500-networking.bats

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -968,27 +968,6 @@ EOF
968968
fi
969969
}
970970

971-
function wait_for_restart_count() {
972-
local cname="$1"
973-
local count="$2"
974-
local tname="$3"
975-
976-
local timeout=10
977-
while :; do
978-
# Previously this would fail as the container would run out of ips after 5 restarts.
979-
run_podman inspect --format "{{.RestartCount}}" $cname
980-
if [[ "$output" == "$2" ]]; then
981-
break
982-
fi
983-
984-
timeout=$((timeout - 1))
985-
if [[ $timeout -eq 0 ]]; then
986-
die "Timed out waiting for RestartCount with $tname"
987-
fi
988-
sleep 0.5
989-
done
990-
}
991-
992971
# Test for https://github.com/containers/podman/issues/18615
993972
# CANNOT BE PARALLELIZED due to strict checking of /run/netns
994973
@test "podman network cleanup --userns + --restart" {

test/system/helpers.bash

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,30 @@ function ensure_no_mountpoint() {
13471347
fi
13481348
}
13491349

1350+
###########################
1351+
# ensure container has been restarted requested times
1352+
###########################
1353+
function wait_for_restart_count() {
1354+
local cname="$1"
1355+
local count="$2"
1356+
local tname="$3"
1357+
1358+
local timeout=10
1359+
while :; do
1360+
# Previously this would fail as the container would run out of ips after 5 restarts.
1361+
run_podman inspect --format "{{.RestartCount}}" $cname
1362+
if [[ "$output" == "$2" ]]; then
1363+
break
1364+
fi
1365+
1366+
timeout=$((timeout - 1))
1367+
if [[ $timeout -eq 0 ]]; then
1368+
die "Timed out waiting for RestartCount with $tname"
1369+
fi
1370+
sleep 0.5
1371+
done
1372+
}
1373+
13501374

13511375
# END miscellaneous tools
13521376
###############################################################################

0 commit comments

Comments
 (0)