diff --git a/tests/all b/tests/all index 764e66a84..be2267308 100755 --- a/tests/all +++ b/tests/all @@ -98,3 +98,5 @@ echo "# tests/rootfs-smoke-link" # the smoke test's install-link checks, on sm tests/rootfs-smoke-link echo "# tests/build-packages-deps" # a package's dependencies into the build container, behind a library change too tests/build-packages-deps +echo "# tests/build-packages-cache" # a forced same-version rebuild drops the stale cached package +tests/build-packages-cache diff --git a/tests/build-packages-cache b/tests/build-packages-cache new file mode 100755 index 000000000..eb9e84b93 --- /dev/null +++ b/tests/build-packages-cache @@ -0,0 +1,54 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +set -euo pipefail +cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." +python3 - <<'PYTEST' +"""Exercise same-version rebuild cache invalidation without Docker/signing.""" +from pathlib import Path +import subprocess +import tempfile + +root = Path.cwd() +source = (root / 'tools/build-packages').read_text() +a = source.index('add_to_repo() {') +b = source.index('\n}\n', a) + 3 +function = source[a:b] +with tempfile.TemporaryDirectory() as directory: + p = Path(directory) + for name in ('repo', 'cache/pkg', 'built'): + (p / name).mkdir(parents=True) + base = 'device-0.1.0-1-aarch64.pkg.tar.zst' + rebuilt = p / 'built' / base + rebuilt.write_text('pkgname = device\nnew bytes\n') + cached = p / 'cache/pkg' / base + signature = Path(str(cached) + '.sig') + unrelated = p / 'cache/pkg/other.pkg.tar.zst' + unrelated.write_text('untouched') + command = '''set -euo pipefail +repo=$1/repo; ALARM_CACHE=$1/cache +local_name=${2:-} +pkginfo() { cat "$1"; } +alarm_die() { exit 99; } +''' + function + '\nadd_to_repo "$1/built/' + base + '"\n' + cached.write_text('pkgname = device\nstale bytes\n') + signature.write_text('stale signature') + subprocess.run(['bash', '-c', command, 'test', str(p)], check=True, + stdout=subprocess.DEVNULL) + assert not cached.exists() and not signature.exists() + assert unrelated.read_text() == 'untouched' + assert (p / 'repo' / base).read_bytes() == rebuilt.read_bytes() + cached.write_bytes(rebuilt.read_bytes()) + signature.write_text('matching signature') + subprocess.run(['bash', '-c', command, 'test', str(p)], check=True, + stdout=subprocess.DEVNULL) + assert cached.exists() and signature.read_text() == 'matching signature' + imported_signature = Path(str(rebuilt) + '.sig') + imported_signature.write_text('imported signature') + subprocess.run(['bash', '-c', command, 'test', str(p), 'comet'], check=True, + stdout=subprocess.DEVNULL) + assert (p / 'repo' / (base + '.sig')).read_text() == 'imported signature' +print('ok - forced same-version rebuild removes stale cached bytes and signature') +print('ok - identical and unrelated cache entries remain') +print('ok - a local import retains its supplied signature') + +PYTEST diff --git a/tests/local-builds b/tests/local-builds index e2cb9f170..f84aa53b4 100755 --- a/tests/local-builds +++ b/tests/local-builds @@ -13,6 +13,7 @@ fail() { printf 'not ok - %s\n' "$1" >&2; exit 1; } pass() { printf 'ok - %s\n' "$1"; } command -v gpg >/dev/null && command -v gpgv >/dev/null || { pass "# skip: no gpg"; exit 0; } t=$(mktemp -d) +export ALARM_CACHE=$t/cache # add_to_repo removes from the package cache: never the developer's own export GNUPGHOME=$t/home && mkdir -m 700 "$GNUPGHOME" # never the user's ~/.gnupg trap 'gpgconf --homedir "$t/signer" --kill gpg-agent 2>/dev/null || true; gpgconf --homedir "$t/other" --kill gpg-agent 2>/dev/null || true; rm -rf "$t"' EXIT diff --git a/tests/pinned-packages b/tests/pinned-packages index edf58de52..8ea52fbbb 100755 --- a/tests/pinned-packages +++ b/tests/pinned-packages @@ -18,6 +18,7 @@ for tool in gpg repo-add bsdtar zstd curl; do command -v "$tool" >/dev/null || { pass "# skip: no $tool"; exit 0; } done t=$(mktemp -d) +export ALARM_CACHE=$t/cache # add_to_repo removes from the package cache: never the developer's own export GNUPGHOME=$t/home && mkdir -m 700 "$GNUPGHOME" # never the user's ~/.gnupg R=$t/repo cleanup() { diff --git a/tools/build-packages b/tools/build-packages index 3533fe9e6..0a436b739 100755 --- a/tools/build-packages +++ b/tools/build-packages @@ -201,6 +201,11 @@ add_to_repo() { cp -f "$file" "$repo/$base" rm -f "$repo/$base.sig" if [[ -n $local_name && -f $file.sig ]]; then cp -f "$file.sig" "$repo/$base.sig"; fi + # A forced rebuild can replace bytes without changing the filename. + # Pacstrap must fetch the replacement and its new signature together. + if [[ -f $ALARM_CACHE/pkg/$base ]] && ! cmp -s "$repo/$base" "$ALARM_CACHE/pkg/$base"; then + rm -f -- "$ALARM_CACHE/pkg/$base" "$ALARM_CACHE/pkg/$base.sig" + fi echo " $base" done }