fix(ci): take main's ref for a bare first-party image ref in a drifted file (#5329)

<!-- Thank you for making a contribution! Here are some tips for you:
- Use Conventional Commits for the PR title: `type(scope): description`
- Types: feat, fix, security (a fix that comes through a security
advisory), docs, style, refactor, perf, test, build, ci, chore
- Scopes are not an exhaustive list — pick the most specific scope for
the change and extend the list when a genuinely new area appears.
Examples:
- System components: dashboard, platform, operator, cilium, kube-ovn,
linstor, fluxcd, cluster-api
- Managed apps: postgres, mariadb, redis, kafka, clickhouse,
virtual-machine, kubernetes
- Development and maintenance: api, hack, tests, ci, docs, maintenance
- Breaking changes: append `!` after type/scope (`feat(api)!: ...`) or
add a `BREAKING CHANGE:` footer
- If it's a work in progress, consider creating this PR as a draft.
- Don't hesistate to ask for opinion and review in the community chats,
even if it's still a draft.
- Fixes merged to `main` reach the release lines the backport batch runs
for automatically, so do not open backport PRs by hand:
https://github.com/cozystack/cozystack/blob/main/docs/release/backports.md
-->

## What this PR does

Closes #5328.

A PR whose merge commit predates a config change to
`packages/system/observability-proxy/values.yaml` fails E2E with
`observability-proxy` in `ImagePullBackOff` on
`observability-proxy:v0.0.0`. `hack/overlay-main-images.sh` skips a file
that differs from the `cozystack-packages:main` artifact in anything but
ref lines, so the bare ref committed for a new image stays, and that tag
was never pushed. The merge commit is fixed when the run starts, so a
rerun, or a PR not pushed to since, drifts on files it never touched.
This comes back every time the proxy's `values.yaml` changes on main,
and it will hit every new image the same way until a release stamps its
ref.

### Behavior changes

- In a drifted values file, a bare first-party ref
(`ghcr.io/cozystack/cozystack/<name>:<tag>`, no digest) under an image
key now takes the artifact line with the same key and indentation, a
digest, and the same first-party image (`.../cozystack/<name>`). Exactly
one such line must exist; none or two leave the ref as committed.
- The rest of a drifted file stays as the PR has it, including every
digest-pinned ref. The drift guard still keeps a PR's chart from running
against an image built for a newer one wherever the committed ref pulls.
- Third-party refs are never replaced. Main can bump one together with
its config, and the committed pair is the one known to work.
- The other digest-less first-party refs in the tree get the same
treatment. `backup-http-client:v1.6.0`, `redis-backup:v1.6.0` and
`kubernetes-backup:v1.6.0` in `backupstrategy-controller` (#4264) are
not on ghcr either, so a drifted run could not pull them before and now
can. `migration-controller:latest` does exist; in a drifted file it now
runs main's image with the PR's older config. That is the trade-off: it
is the same component built from the tree, and a digest-less ref is not
reproducible anyway.
- Packages that took main's images this way are named in their own
`Image overlay bare refs` annotation, and the summary line counts them
as `bare-refs=N`. A rewrite that cannot write the file is counted as
`failed`.
- `docs/agents/image-refs.md` now says how PR CI stamps a bare ref in a
drifted file, instead of only promising that it does.

### Not in this PR

- Pinning a real tag+digest for the proxy in `values.yaml`. The only
pushed tags are nightlies, which retention prunes after 14 builds, and
digests pinned on main are not protected from it, so the pin would break
again within weeks. A release tag appears with release-1.8.
- The other digest-less first-party refs from #4264.
- The split shape (`repository:` + `tag:` without `digest:`), a
single-quoted value, or one followed by a `# comment`. No package writes
a bare first-party ref that way today; such a line stays as committed.

### Tests

New cases in `hack/overlay-main-images_test.bats`: a drifted file takes
the artifact ref for its bare ref and keeps its config; a digest-pinned
sibling stays; no replacement for a different image name, for an
artifact line without a digest, for a third-party ref even when main
pins one, for a third-party image with the same name, or for two
candidate lines; a rewrite that cannot write the file is counted as
failed and leaves no temporary file. The suite passes under bats and
under the POSIX runner with `dash`. The script was also run against an
older copy of the proxy's real `values.yaml`: only the `image:` line
changed.

### Screenshots

<!-- REQUIRED for UI changes: attach screenshots or screen recordings
demonstrating
the visual impact of your changes. PRs with UI changes without
screenshots will not be merged. -->

Not a UI change.

### Downstream repositories

<!--
Cozystack is upstream for repositories that are NOT kept in sync with it
automatically, and nothing in CI compares the two sides, so a change
here can break them silently.

Which change forces what, and which file to touch, is the trigger map in
docs/agents/contributing.md under "Downstream Repositories". Walk it
before you tick anything.

IF YOU ARE AN AI AGENT OR AN LLM, THIS IS PART OF THE TASK, NOT A FORM
TO FILL IN:

1. Walk the trigger map against the actual diff of this PR, file by
file. Do not decide from the PR title or from your memory of what you
changed.
2. For every repository you tick, open the follow-up PR there and link
it on the same line. A ticked box with no link claims work that does not
exist, which is worse than an empty box. Search that repository for an
open PR or issue covering it first, and link that instead of filing a
duplicate.
3. When the follow-up is out of scope, or needs a decision that is not
yours to make, open an issue in that repository instead, link that, and
say so in the PR body. Do not push a speculative PR into another
repository just to fill a line here.
4. Do not tick "No downstream repository is affected" to make the
checklist go away. If you are unsure, leave every box empty and say so
in the PR body, so a human decides.
-->

- [x] No downstream repository is affected by this change
- [ ] [cozystack/website](https://github.com/cozystack/website) -
follow-up:
- [ ]
[cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack)
- follow-up:
- [ ]
[cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack)
- follow-up:
- [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up:
- [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up:
- [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) -
follow-up:
- [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) -
follow-up:
- [ ]
[cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server)
- follow-up:
- [ ]
[cozystack/external-apps-example](https://github.com/cozystack/external-apps-example)
- follow-up:
- [ ] [cozystack/examples](https://github.com/cozystack/examples) -
follow-up:
- [ ] [cozystack/community](https://github.com/cozystack/community) -
follow-up:

The diff touches `hack/overlay-main-images.sh`, its bats file and one
paragraph of `docs/agents/image-refs.md`. Nothing under `hack/` moves or
is renamed and no make target changes (the ccp trigger), and no package,
values key, CRD or naming changes.

### Release note

<!--  Write a release note:
- Explain what has changed internally and for users.
- Start with the same `type(scope):` prefix as in the PR title
- Follow the guidelines at
https://github.com/kubernetes/community/blob/master/contributors/guide/release-notes.md.
-->

```release-note
fix(ci): a PR behind main no longer installs a never-pushed placeholder tag for a new first-party image, such as observability-proxy:v0.0.0, when that package's values.yaml changed on main since the PR's merge commit.
```


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Documentation**
* Clarified that PR CI may update eligible, digest-less first-party
image references while preserving other committed configuration.
Third-party references retain their committed tags and digests.
* **Bug Fixes**
* Image references are left unchanged when a matching current-main image
is missing or ambiguous, and failed file updates are reported.
* **Tests**
* Added coverage for reference matching, unchanged configuration, and
failed updates.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Aleksei Sviridkin authored and GitHub committed 2026-11-14 20:15:52 +03:00
commit 90797d9de9
3 files changed
+265 -8

No files matched your search

+1 -1
View File
@@ -42,7 +42,7 @@ If a package stamps a reference somewhere the two globs do not cover, add that f
## Invariants
- **Every first-party reference is tag+digest.** A bare tag with no digest is not reproducible and must not be committed. The one exception is the first commit of a new image: it has no digest until it is built, so it lands as a bare `<repo>:<tag>` that the producing `make image` replaces. PR CI and release preparation both stamp it before anything pulls it, but a tree applied without a build pulls a tag that does not exist.
- **Every first-party reference is tag+digest.** A bare tag with no digest is not reproducible and must not be committed. The one exception is the first commit of a new image: it has no digest until it is built, so it lands as a bare `<repo>:<tag>` that the producing `make image` replaces. PR CI and release preparation both stamp it before anything pulls it, but a tree applied without a build pulls a tag that does not exist. In PR CI the stamp comes from `hack/overlay-main-images.sh` when the PR does not rebuild the package, and it takes a bare first-party ref even from a file whose configuration drifted from the artifact, because a tag that was never pushed cannot install. The cost is that any other digest-less first-party ref in such a file also runs main's image with the PR's older config; third-party refs in it keep their committed pair.
- **Exactly one producer stamps each reference.** A reference no `Makefile` writes will never be refreshed by anything, including release preparation.
- **A package that stamps a reference is in the root `Makefile` `build:` list.** `build-matrix.sh`, the main build and release preparation all iterate that one list, so a package outside it is never rebuilt by any path and its pin freezes permanently. `hack/build-matrix_test.bats` computes the set of packages whose Makefile runs `docker buildx build` and fails on any the list misses.
- **Every consumer of references reads all three storage shapes**, via `hack/lib/image-refs.sh`.
+127 -7
View File
@@ -42,9 +42,13 @@
# a change: `yq -i`, which every `make image` uses to stamp the ref, drops one.
# If any OTHER line differs — the PR branched from a
# main whose config for that file differs from the artifact's base — the file is
# left on its committed ref (safe degradation, logged, never fatal). CI checks
# out the pull_request merge commit (current main + PR), so for an unbuilt file
# the config already matches current main and only ref lines differ.
# left on its committed ref (safe degradation, logged, never fatal), except
# that a bare first-party ref in it, one with no digest, still takes the
# artifact's ref (see replace_bare_refs). CI checks out the pull_request merge
# commit (main + PR), so for an unbuilt file the config matches main as of
# that merge commit. It is fixed when the run starts, so a rerun, or a PR not
# pushed to since, can be older than the artifact and drift on files it never
# touched.
#
# Usage: hack/overlay-main-images.sh <mainpkgs-dir> <built-matrix-json> [touched-pkg-dirs]
# <mainpkgs-dir> = extracted cozystack-packages:main tree; its root holds
@@ -123,8 +127,95 @@ img_line='(@sha256:|^[[:space:]]*(- )?(image|repository|registry|tag|digest):|^[
# match branch for this same shape and for this same reason.
bare_ref_line='^[^[:space:]#][^[:space:]]*/[^[:space:]]+$'
# A drifted file keeps its committed refs. For a ref pinned by digest that is an
# older image which pulls and matches the PR's config, so it is the safe answer.
# A first-party ref with no digest is not: docs/agents/image-refs.md forbids it
# except as the first commit of a new image, whose tag was never pushed, and
# keeping that makes the component fail to pull. The merge commit CI checks out
# is fixed when the run starts, so a PR whose merge commit predates a config
# change to such a file drifts on it, and a new image fails every such PR until
# a release stamps its ref (#5328).
#
# replace_bare_refs <cur> <new> rewrites, in <cur> only, each line whose value is
# a bare first-party ref (ghcr.io/cozystack/cozystack/<name>:<tag>, no digest)
# under an image key, taking the line from <new> that has the same key and
# indentation, an @sha256: digest and the same first-party image, i.e. a path
# ending in /cozystack/<name>. Exactly one such line must exist; two are
# ambiguous, and none means build-main did not stamp it, or the key moved
# (another indentation or parent) or the image was renamed on main since the
# PR's merge commit. Every other line of
# <cur> stays as the PR has it.
#
# The trade-off is deliberate. A bare first-party ref that does exist (an older
# release tag or a moving `latest`, #4264) also takes main's image, so it runs
# with the PR's older config rather than as committed. It is the same component
# built from the tree, and a digest-less ref is not reproducible anyway; the
# alternative is a ref that may not pull at all. Third-party refs are never
# touched: main can bump one alongside its config, and the committed pair is
# the one known to work together. The split shape (`repository:` + `tag:`
# without `digest:`) is not handled, nor is a single-quoted value or one
# followed by a `# comment`; such a line stays as committed, and no package
# writes a bare first-party ref that way today. Prints nothing and fails when
# the file could not be rewritten, so the caller counts it as failed.
#
# Prints the number of lines replaced.
replace_bare_refs() {
tmp="$1.overlay-bare.$$"
n=$(awk -v newf="$2" -v out="$tmp" '
function key_prefix(s) {
if (match(s, /^[[:space:]]*(- )?([A-Za-z]+[Ii]mage|image):[[:space:]]*/)) return substr(s, 1, RLENGTH)
return ""
}
function ref_value(s, p, v) {
v = substr(s, length(p) + 1)
sub(/[[:space:]]+$/, "", v)
gsub(/"/, "", v)
return v
}
function image_name(v) {
sub(/@.*/, "", v)
sub(/.*\//, "", v)
sub(/:.*/, "", v)
return v
}
BEGIN {
while ((getline l < newf) > 0) {
p = key_prefix(l)
if (p == "" || index(l, "@sha256:") == 0) continue
v = ref_value(l, p)
name = image_name(v)
if (index(v, "/cozystack/" name ":") == 0) continue
k = p SUBSEP name
cnt[k]++
line[k] = l
}
close(newf)
}
{
p = key_prefix($0)
if (p != "") {
v = ref_value($0, p)
if (v ~ /^ghcr\.io\/cozystack\/cozystack\/[^@[:space:]\/]+:[^@[:space:]\/]+$/) {
k = p SUBSEP image_name(v)
if (cnt[k] == 1) { print line[k] > out; r++; next }
}
}
print > out
}
END { close(out); print r + 0 }
' "$1")
case "$n" in
''|*[!0-9]*) rm -f "$tmp"; return 1 ;;
0) rm -f "$tmp" ;;
*) mv "$tmp" "$1" || { rm -f "$tmp"; return 1; } ;;
esac
echo "$n"
}
overlaid=0
same=0
bare=0
bare_files=""
skipped=0
drift=0
drift_files=""
@@ -162,8 +253,26 @@ for new in $(find "$MAINPKGS" -type d -name charts -prune -o \
if diff "$cur" "$new" | sed -n 's/^[<>] //p' \
| grep -vE '^[[:space:]]*$' | grep -qvE "$line_pat"; then
echo "drift (non-ref change) in $cur -> keeping committed ref"
drift=$((drift + 1))
r=0
case "$cur" in
*.yaml)
if ! r=$(replace_bare_refs "$cur" "$new"); then
echo "WARN: rewriting bare refs failed for $cur (keeping committed refs)"
failed=$((failed + 1))
r=0
fi
;;
esac
if [ "$r" -eq 0 ]; then
echo "drift (non-ref change) in $cur -> keeping committed ref"
else
echo "drift (non-ref change) in $cur -> keeping committed refs except bare first-party ones"
echo "bare first-party ref in $cur -> current-main ($r line(s)); its other refs stay committed"
bare=$((bare + r))
d=$(dirname "${cur#packages/}")
bare_files="$bare_files $d"
fi
# Name the PACKAGE, not the directory the file happens to sit in: an
# images/*.tag drift would otherwise read as "<pkg>/images", which is not
# something anyone can go and look at.
@@ -181,7 +290,7 @@ for new in $(find "$MAINPKGS" -type d -name charts -prune -o \
fi
done
echo "Overlay current-main images: overlaid=$overlaid same=$same skipped(rebuilt/owned/edited)=$skipped drift=$drift failed=$failed"
echo "Overlay current-main images: overlaid=$overlaid same=$same skipped(rebuilt/owned/edited)=$skipped drift=$drift bare-refs=$bare failed=$failed"
# Keeping the committed ref is the safe answer to a config that does not match
# the artifact, but it is not a neutral one: that component then runs its last
@@ -192,7 +301,18 @@ echo "Overlay current-main images: overlaid=$overlaid same=$same skipped(rebuilt
# cost three unrelated PRs, one round each.
if [ "$drift" -gt 0 ]; then
drift_pkgs=$(echo "$drift_files" | tr ' ' '\n' | grep -v '^$' | sort -u)
printf '::warning title=Image overlay drift::%s package(s) kept committed release refs, so they run release images in this lane rather than current main: %s\n' \
"$(printf '%s\n' "$drift_pkgs" | wc -l | tr -d ' ')" \
except=""
[ "$bare" -eq 0 ] || except=" (bare first-party refs excepted, see Image overlay bare refs)"
printf '::warning title=Image overlay drift::%s package(s) kept committed release refs%s, so they run release images in this lane rather than current main: %s\n' \
"$(printf '%s\n' "$drift_pkgs" | wc -l | tr -d ' ')" "$except" \
"$(printf '%s' "$drift_pkgs" | tr '\n' ' ' | sed 's/ $//')"
fi
# Among those, a package whose bare first-party ref took main's image runs that
# image with the PR's older config, not its committed ref; name it separately
# so the warning above does not read as the whole story for it.
if [ "$bare" -gt 0 ]; then
bare_pkgs=$(echo "$bare_files" | tr ' ' '\n' | grep -v '^$' | sort -u)
printf '::warning title=Image overlay bare refs::%s drifted package(s) took current-main images for bare first-party refs, with the config committed in this PR: %s\n' \
"$(printf '%s\n' "$bare_pkgs" | wc -l | tr -d ' ')" \
"$(printf '%s' "$bare_pkgs" | tr '\n' ' ' | sed 's/ $//')"
fi
+137
View File
@@ -502,3 +502,140 @@ load test_helper
cd "$root"
rm -rf "$w"
}
# A drifted file keeps its committed refs, and for a digest-pinned ref that is
# a pullable older image. A bare <repo>:<tag> is the first commit of a new image
# (docs/agents/image-refs.md) and names a tag no registry serves, so keeping it
# turns drift into ImagePullBackOff. That hit every fork PR whose merge commit
# predated a config change to observability-proxy's values.yaml (#5328). Only
# such a bare line takes the artifact's ref; the rest of the file stays as the
# PR has it.
@test "a drifted file still takes the artifact ref for a bare (digest-less) ref line" {
root=$(pwd)
w=$(mktemp -d)
mkdir -p "$w/packages/system/proxy" "$w/main/system/proxy"
printf 'proxy:\n image: "ghcr.io/cozystack/cozystack/proxy:v0.0.0"\n lookback: 1h\n' \
> "$w/packages/system/proxy/values.yaml"
printf 'proxy:\n image: "iad.ocir.io/x/cozystack/proxy:main@sha256:bbbb"\n lookback: 1h\n prefix: cozy-\n' \
> "$w/main/system/proxy/values.yaml"
( cd "$w" && "$root/hack/overlay-main-images.sh" main '[]' ) > "$w/out.txt"
grep -q 'drift (non-ref change) in packages/system/proxy/values.yaml' "$w/out.txt"
grep -q ' image: "iad.ocir.io/x/cozystack/proxy:main@sha256:bbbb"' "$w/packages/system/proxy/values.yaml"
# The config stays the PR's: the new key is not introduced.
if grep -q 'prefix:' "$w/packages/system/proxy/values.yaml"; then
echo "FAIL: a config line was taken from the artifact" >&2; exit 1
fi
grep -q 'bare first-party ref in packages/system/proxy/values.yaml -> current-main' "$w/out.txt"
grep -q '::warning title=Image overlay bare refs::1 drifted package(s) .*: system/proxy$' "$w/out.txt"
grep -q '::warning title=Image overlay drift::1 package(s) kept committed release refs (bare first-party refs excepted, see Image overlay bare refs), .*: system/proxy$' "$w/out.txt"
grep -q 'bare-refs=1' "$w/out.txt"
rm -rf "$w"
}
@test "a drifted file replaces only its bare ref, not a digest-pinned sibling" {
root=$(pwd)
w=$(mktemp -d)
mkdir -p "$w/packages/system/pair" "$w/main/system/pair"
printf 'tuning: old\nimage: ghcr.io/cozystack/cozystack/pair:v1.5.0@sha256:aaaa\nclientImage: ghcr.io/cozystack/cozystack/pair-client:latest\n' \
> "$w/packages/system/pair/values.yaml"
printf 'tuning: new\nimage: iad.ocir.io/x/cozystack/pair:main@sha256:bbbb\nclientImage: iad.ocir.io/x/cozystack/pair-client:main@sha256:cccc\n' \
> "$w/main/system/pair/values.yaml"
( cd "$w" && "$root/hack/overlay-main-images.sh" main '[]' ) > "$w/out.txt"
grep -q '^image: ghcr.io/cozystack/cozystack/pair:v1.5.0@sha256:aaaa$' "$w/packages/system/pair/values.yaml"
grep -q '^clientImage: iad.ocir.io/x/cozystack/pair-client:main@sha256:cccc$' "$w/packages/system/pair/values.yaml"
grep -q '^tuning: old$' "$w/packages/system/pair/values.yaml"
rm -rf "$w"
}
@test "a bare ref is not replaced by an artifact line for a different image" {
# The same key can hold another image in the artifact, e.g. after a rename.
# Matching on the key alone would swap in an image the chart never asked for.
root=$(pwd)
w=$(mktemp -d)
mkdir -p "$w/packages/system/ren" "$w/main/system/ren"
printf 'tuning: old\nimage: ghcr.io/cozystack/cozystack/old-name:v0.0.0\n' > "$w/packages/system/ren/values.yaml"
printf 'tuning: new\nimage: iad.ocir.io/x/cozystack/new-name:main@sha256:bbbb\n' > "$w/main/system/ren/values.yaml"
( cd "$w" && "$root/hack/overlay-main-images.sh" main '[]' ) > "$w/out.txt"
grep -q '^image: ghcr.io/cozystack/cozystack/old-name:v0.0.0$' "$w/packages/system/ren/values.yaml"
grep -q 'bare-refs=0' "$w/out.txt"
rm -rf "$w"
}
@test "a bare ref is kept when the artifact carries no digest for it either" {
# A ref build-main does not stamp has no digest in the artifact, so there is
# nothing more pullable to take.
root=$(pwd)
w=$(mktemp -d)
mkdir -p "$w/packages/system/nd" "$w/main/system/nd"
printf 'tuning: old\nimage: ghcr.io/cozystack/cozystack/nd:v0.0.0\n' > "$w/packages/system/nd/values.yaml"
printf 'tuning: new\nimage: iad.ocir.io/x/cozystack/nd:main\n' > "$w/main/system/nd/values.yaml"
( cd "$w" && "$root/hack/overlay-main-images.sh" main '[]' ) > "$w/out.txt"
grep -q '^image: ghcr.io/cozystack/cozystack/nd:v0.0.0$' "$w/packages/system/nd/values.yaml"
grep -q 'bare-refs=0' "$w/out.txt"
rm -rf "$w"
}
@test "a bare third-party ref in a drifted file is kept even when main pins one" {
# Main can bump a third-party image together with its config in one file. A
# PR behind it has the older pair, which is the one known to work together;
# taking main's image alone would run it with the config it replaced.
root=$(pwd)
w=$(mktemp -d)
mkdir -p "$w/packages/system/kc" "$w/main/system/kc"
printf 'features: old\nimage: quay.io/keycloak/keycloak:26.6.3\n' > "$w/packages/system/kc/values.yaml"
printf 'features: new\nimage: quay.io/keycloak/keycloak:27.0.0@sha256:abcd\n' > "$w/main/system/kc/values.yaml"
( cd "$w" && "$root/hack/overlay-main-images.sh" main '[]' ) > "$w/out.txt"
grep -q '^image: quay.io/keycloak/keycloak:26.6.3$' "$w/packages/system/kc/values.yaml"
grep -q 'bare-refs=0' "$w/out.txt"
if grep -q 'Image overlay bare refs' "$w/out.txt"; then
echo "FAIL: a bare-refs annotation with nothing replaced" >&2; exit 1
fi
rm -rf "$w"
}
@test "a bare first-party ref is not replaced by a third-party image of the same name" {
root=$(pwd)
w=$(mktemp -d)
mkdir -p "$w/packages/system/same" "$w/main/system/same"
printf 'tuning: old\nimage: ghcr.io/cozystack/cozystack/redis:v0.0.0\n' > "$w/packages/system/same/values.yaml"
printf 'tuning: new\nimage: docker.io/library/redis:8.0@sha256:abcd\n' > "$w/main/system/same/values.yaml"
( cd "$w" && "$root/hack/overlay-main-images.sh" main '[]' ) > "$w/out.txt"
grep -q '^image: ghcr.io/cozystack/cozystack/redis:v0.0.0$' "$w/packages/system/same/values.yaml"
grep -q 'bare-refs=0' "$w/out.txt"
rm -rf "$w"
}
@test "a bare ref is kept when the artifact has two candidate lines for it" {
# Two lines with the same key and image name (e.g. one per component in a
# list) cannot be told apart, so neither is taken.
root=$(pwd)
w=$(mktemp -d)
mkdir -p "$w/packages/system/dup" "$w/main/system/dup"
printf 'tuning: old\na:\n image: ghcr.io/cozystack/cozystack/dup:v0.0.0\n' > "$w/packages/system/dup/values.yaml"
printf 'tuning: new\na:\n image: iad.ocir.io/x/cozystack/dup:main@sha256:bbbb\nb:\n image: iad.ocir.io/x/cozystack/dup:main@sha256:cccc\n' \
> "$w/main/system/dup/values.yaml"
( cd "$w" && "$root/hack/overlay-main-images.sh" main '[]' ) > "$w/out.txt"
grep -q '^ image: ghcr.io/cozystack/cozystack/dup:v0.0.0$' "$w/packages/system/dup/values.yaml"
grep -q 'bare-refs=0' "$w/out.txt"
rm -rf "$w"
}
@test "a bare-ref rewrite that cannot write the file is counted as failed, not replaced" {
# Mode bits do not stop root from writing, and the POSIX runner has no
# `skip`, so there is nothing to observe as root.
if [ "$(id -u)" -eq 0 ]; then return 0; fi
root=$(pwd)
w=$(mktemp -d)
mkdir -p "$w/packages/system/ro" "$w/main/system/ro"
printf 'tuning: old\nimage: ghcr.io/cozystack/cozystack/ro:v0.0.0\n' > "$w/packages/system/ro/values.yaml"
printf 'tuning: new\nimage: iad.ocir.io/x/cozystack/ro:main@sha256:bbbb\n' > "$w/main/system/ro/values.yaml"
chmod a-w "$w/packages/system/ro"
( cd "$w" && "$root/hack/overlay-main-images.sh" main '[]' ) > "$w/out.txt" 2>&1
chmod u+w "$w/packages/system/ro"
grep -q '^image: ghcr.io/cozystack/cozystack/ro:v0.0.0$' "$w/packages/system/ro/values.yaml"
grep -q 'bare-refs=0 failed=1' "$w/out.txt"
if ls "$w/packages/system/ro" | grep -q 'overlay-bare'; then
echo "FAIL: temp file left behind" >&2; exit 1
fi
rm -rf "$w"
}