Skip to content

Commit 07983eb

Browse files
committed
fix: clean sed backups via EXIT trap; deterministic apply order
Install an EXIT trap removing Containerfile .bak files so a sed failure mid-apply (die path) cannot strand backup files on disk; the redundant per-file rm in the loop is folded into the trap. Iterate the apply phase over MANAGED_ARGS in declaration order instead of undefined associative-array key order, matching the plan and report loops and removing a latent ordering hazard. Add a regression test asserting no .bak files remain after an update. Signed-off-by: Jonathan Springer <jps@s390x.com>
1 parent 738a288 commit 07983eb

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

scripts/container-bump-image-versions.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,20 @@ main() {
116116
fi
117117

118118
# ---- Apply phase ----
119-
for arg in "${!new_tag[@]}"; do
119+
# sed -i.bak leaves the backup behind if it fails mid-write; remove any
120+
# stray backups on exit, including the die path, so nothing leaks.
121+
trap 'rm -f "${CONTAINERFILE_PATH}.bak" "${WHEELS_CONTAINERFILE_PATH}.bak"' EXIT
122+
for arg in "${MANAGED_ARGS[@]}"; do
123+
[ -n "${new_tag[$arg]:-}" ] || continue
120124
local files=("$CONTAINERFILE_PATH")
121125
# UBI_MINIMAL is pinned in both Containerfiles; keep them identical.
122126
if [ "$arg" = "UBI_MINIMAL" ]; then
123127
files+=("$WHEELS_CONTAINERFILE_PATH")
124128
fi
125129
local f
126130
for f in "${files[@]}"; do
127-
if sed -i.bak "s|^ARG ${arg}=.*|ARG ${arg}=${image_of[$arg]}:${new_tag[$arg]}|" "$f"; then
128-
rm -f "${f}.bak"
129-
else
130-
die "failed to update ${arg} in $f"
131-
fi
131+
sed -i.bak "s|^ARG ${arg}=.*|ARG ${arg}=${image_of[$arg]}:${new_tag[$arg]}|" "$f" \
132+
|| die "failed to update ${arg} in $f"
132133
echo " updated $f"
133134
done
134135
done

tests/scripts/container-bump-image-versions.bats

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ write_tags() { # write_tags <fixture-name> <tag>...
175175
cmp "$WHEELS_CONTAINERFILE_PATH" "$TEST_DIR/wheels.Containerfile.orig"
176176
}
177177

178+
@test "leaves no sed backup files behind after an update" {
179+
write_tags ubi10.json "10.2-1784669000"
180+
write_tags nodejs.json "10.2-1784669001"
181+
write_tags ubi-minimal.json "10.2-1784669047"
182+
183+
run "$SCRIPT"
184+
[ "$status" -eq 0 ]
185+
[ ! -e "${CONTAINERFILE_PATH}.bak" ]
186+
[ ! -e "${WHEELS_CONTAINERFILE_PATH}.bak" ]
187+
}
188+
178189
@test "refuses to manage a pin that is not a full build tag" {
179190
sed -i.bak 's|^ARG UBI_BASE=.*|ARG UBI_BASE=registry.access.redhat.com/ubi10:latest|' "$CONTAINERFILE_PATH"
180191
rm -f "${CONTAINERFILE_PATH}.bak"

0 commit comments

Comments
 (0)