diff --git a/.dockerignore b/.dockerignore index 3b5333d185..780ab36b13 100644 --- a/.dockerignore +++ b/.dockerignore @@ -21,6 +21,7 @@ job-logs.txt .claude/skills .claude/agents .claude/commands +.claude/settings.local.json *.lock # Host shell / editor / tooling state accidentally surfaced inside this working copy diff --git a/.github/workflows/test-environment.yml b/.github/workflows/test-environment.yml index 90441ff5c5..bcfad61ebe 100644 --- a/.github/workflows/test-environment.yml +++ b/.github/workflows/test-environment.yml @@ -63,6 +63,7 @@ jobs: -e GITHUB_REPOSITORY="${{ github.repository }}" \ -e GITHUB_REPOSITORY_OWNER="${{ github.repository_owner }}" \ -e GITHUB_SHA="${{ github.sha }}" \ + -e DEV_RUNTIME_IMAGE \ -e INFINITO_DISTRO=debian \ -e INFINITO_IMAGE=infinito-debian \ -e INFINITO_PULL_POLICY=never \ diff --git a/.gitignore b/.gitignore index 2500d1dcfc..770ea6c192 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ job-logs.txt .claude/skills .claude/agents .claude/commands +.claude/settings.local.json *.lock # Host shell / editor / tooling state accidentally surfaced inside this working copy diff --git a/scripts/tests/environment/00_orchestrator.sh b/scripts/tests/environment/00_orchestrator.sh index 1988b55f9d..c7ca1cb724 100755 --- a/scripts/tests/environment/00_orchestrator.sh +++ b/scripts/tests/environment/00_orchestrator.sh @@ -11,8 +11,8 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Resolve to an absolute path so the self-exclusion works regardless of whether # the script was invoked with a relative path (e.g. bash ./scripts/…/00_orchestrator.sh). SELF="${SCRIPT_DIR}/$(basename "${BASH_SOURCE[0]}")" -# shellcheck source=scripts/tests/environment/lib.sh -source "${SCRIPT_DIR}/lib.sh" +# shellcheck source=scripts/tests/environment/utils/common.sh +source "${SCRIPT_DIR}/utils/common.sh" # pkgmgr images may reset the working directory before invoking this script. cd "${REPO_ROOT}" diff --git a/scripts/tests/environment/01_install.sh b/scripts/tests/environment/01_install.sh index 794116a5df..3e2ab66792 100755 --- a/scripts/tests/environment/01_install.sh +++ b/scripts/tests/environment/01_install.sh @@ -3,8 +3,8 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -# shellcheck source=scripts/tests/environment/lib.sh -source "${SCRIPT_DIR}/lib.sh" +# shellcheck source=scripts/tests/environment/utils/common.sh +source "${SCRIPT_DIR}/utils/common.sh" bash "${REPO_ROOT}/scripts/install/package.sh" diff --git a/scripts/tests/environment/04_bootstrap.sh b/scripts/tests/environment/04_bootstrap.sh index 4790456e5f..1e575564e3 100755 --- a/scripts/tests/environment/04_bootstrap.sh +++ b/scripts/tests/environment/04_bootstrap.sh @@ -3,8 +3,8 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -# shellcheck source=scripts/tests/environment/lib.sh -source "${SCRIPT_DIR}/lib.sh" +# shellcheck source=scripts/tests/environment/utils/common.sh +source "${SCRIPT_DIR}/utils/common.sh" cd "${REPO_ROOT}" diff --git a/scripts/tests/environment/05_commit.sh b/scripts/tests/environment/05_commit.sh index e65f6c15f2..02ce87ae60 100755 --- a/scripts/tests/environment/05_commit.sh +++ b/scripts/tests/environment/05_commit.sh @@ -3,8 +3,8 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -# shellcheck source=scripts/tests/environment/lib.sh -source "${SCRIPT_DIR}/lib.sh" +# shellcheck source=scripts/tests/environment/utils/common.sh +source "${SCRIPT_DIR}/utils/common.sh" cd "${REPO_ROOT}" diff --git a/scripts/tests/environment/07_deploy_minimal.sh b/scripts/tests/environment/07_deploy_minimal.sh index 099f3f76f0..5b80cca0cf 100755 --- a/scripts/tests/environment/07_deploy_minimal.sh +++ b/scripts/tests/environment/07_deploy_minimal.sh @@ -3,8 +3,10 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -# shellcheck source=scripts/tests/environment/lib.sh -source "${SCRIPT_DIR}/lib.sh" +# shellcheck source=scripts/tests/environment/utils/common.sh +source "${SCRIPT_DIR}/utils/common.sh" +# shellcheck source=scripts/tests/environment/utils/cache.sh +source "${SCRIPT_DIR}/utils/cache.sh" echo "Snapshotting cache counters before the deploy." CACHE_BEFORE="$(cache_snapshot)" @@ -13,9 +15,15 @@ echo "Deploying dashboard with matomo disabled to verify SERVICES_DISABLED suppr make deploy-fresh-purged-apps APPS="${DASHBOARD_APP}" SERVICES_DISABLED="matomo" inspect -echo "Verifying that the local caches saw deploy traffic." +echo "Actively probing both caches to confirm pull-through works end-to-end." +probe_caches + +echo "Probing the DiD inner-build cache path (frontend hijack + apt rewrite)." +probe_did_inner_build + +echo "Verifying that BOTH local caches saw real pull-through traffic." CACHE_AFTER="$(cache_snapshot)" -assert_cache_used "${CACHE_BEFORE}" "${CACHE_AFTER}" +assert_caches_used "${CACHE_BEFORE}" "${CACHE_AFTER}" echo "Trusting the local CA certificate so HTTPS endpoints are reachable from the host." make trust-ca diff --git a/scripts/tests/environment/08_deploy_performance.sh b/scripts/tests/environment/08_deploy_performance.sh index bf84b933e7..6576ed0296 100755 --- a/scripts/tests/environment/08_deploy_performance.sh +++ b/scripts/tests/environment/08_deploy_performance.sh @@ -3,8 +3,8 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -# shellcheck source=scripts/tests/environment/lib.sh -source "${SCRIPT_DIR}/lib.sh" +# shellcheck source=scripts/tests/environment/utils/common.sh +source "${SCRIPT_DIR}/utils/common.sh" echo "Deploying matomo (full cycle: deploy + update pass) so it becomes reachable via its dedicated inventory entry." make deploy-fresh-purged-apps APPS="${MATOMO_APP}" FULL_CYCLE=true diff --git a/scripts/tests/environment/09_deploy_reuse.sh b/scripts/tests/environment/09_deploy_reuse.sh index 08c85d917a..ad28418c65 100755 --- a/scripts/tests/environment/09_deploy_reuse.sh +++ b/scripts/tests/environment/09_deploy_reuse.sh @@ -3,8 +3,8 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -# shellcheck source=scripts/tests/environment/lib.sh -source "${SCRIPT_DIR}/lib.sh" +# shellcheck source=scripts/tests/environment/utils/common.sh +source "${SCRIPT_DIR}/utils/common.sh" echo "Redeploying matomo while keeping inventory and packages to validate state reuse." make deploy-reuse-kept-apps APPS="${MATOMO_APP}" diff --git a/scripts/tests/environment/README.md b/scripts/tests/environment/README.md index 0a9d934919..d68dc4af9d 100644 --- a/scripts/tests/environment/README.md +++ b/scripts/tests/environment/README.md @@ -20,7 +20,8 @@ The entry point is [00_orchestrator.sh](00_orchestrator.sh). | [08_deploy_performance.sh](08_deploy_performance.sh) | Deploys the full application set on performance hardware | | [09_deploy_reuse.sh](09_deploy_reuse.sh) | Redeploys reusing existing inventory and packages | | [10_teardown.sh](10_teardown.sh) | Shuts down the stack and reverses environment changes | -| [lib.sh](lib.sh) | Shared variables and helper functions | +| [utils/common.sh](utils/common.sh) | Shared bootstrap, constants, and generic helpers (HTTP assertion, inventory inspection) | +| [utils/cache.sh](utils/cache.sh) | Cache-stack assertions and probes (registry-cache, package-cache, DiD inner-build) | ## Usage diff --git a/scripts/tests/environment/utils/cache.sh b/scripts/tests/environment/utils/cache.sh new file mode 100755 index 0000000000..622c7b3d51 --- /dev/null +++ b/scripts/tests/environment/utils/cache.sh @@ -0,0 +1,236 @@ +#!/usr/bin/env bash +# Cache-stack assertions and probes for the environment test suite. +# Verifies that BOTH the registry-cache and the package-cache (Nexus + +# nginx frontend) actually serve traffic during a deploy, including +# the DiD inner-build path that the runner-side traffic alone does +# NOT exercise. +# +# This file is sourced ON TOP OF common.sh by callers that need cache +# checks (currently only 07_deploy_minimal.sh). +set -euo pipefail + +# Map runtime image to the Nexus proxy repo that proxies its system +# package manager. Empty string means no system-level proxy is wired +# for that runtime (e.g. Arch / Manjaro use pacman, which is not +# cached). Echoes the proxy name on stdout. +expected_system_proxy_for_runtime() { + local image="${DEV_RUNTIME_IMAGE:-}" + case "${image}" in + *debian*) echo "apt-debian" ;; + *ubuntu*) echo "apt-ubuntu" ;; + *fedora*) echo "yum-fedora" ;; + *centos* | *rocky*) echo "yum-rocky" ;; + *) echo "" ;; + esac +} + +# Count Nexus request-log entries for a given proxy repo, or for any +# repo when the argument is empty. Filters out health-probe, admin, +# and UI traffic so the count reflects real pull-through activity. +nexus_repo_requests() { + local repo="${1:-}" + local pattern + if [[ -n "${repo}" ]]; then + pattern="\"(GET|HEAD|POST|PUT) /repository/${repo}/" + else + pattern="\"(GET|HEAD|POST|PUT) /repository/[^/ ]+/" + fi + docker exec infinito-package-cache sh -c \ + "grep -cE '${pattern}' /nexus-data/log/request.log 2>/dev/null || echo 0" \ + 2>/dev/null | awk '{print $1+0}' +} + +# Count registry-cache flow entries (HIT, MISS, REVALIDATED, UPDATING, +# STALE). Both HIT and MISS prove the cache layer is engaged; counting +# only HITs misses cold deploys where everything is a first pull. +registry_cache_flows() { + docker logs infinito-registry-cache 2>&1 | + grep -cE '"upstream_cache_status":"(HIT|MISS|REVALIDATED|UPDATING|STALE)"' || + true +} + +# Snapshot all cache counters relevant for the AND-assertion. Output +# is a single space-separated line consumed by ``assert_caches_used``. +# Order matters: total-nexus, expected-distro-proxy, registry-flows. +cache_snapshot() { + local nexus_total proxy_count registry_flows expected_proxy + expected_proxy="$(expected_system_proxy_for_runtime)" + nexus_total="$(nexus_repo_requests "")" + proxy_count="$(nexus_repo_requests "${expected_proxy}")" + registry_flows="$(registry_cache_flows)" + printf '%s %s %s\n' \ + "${nexus_total:-0}" "${proxy_count:-0}" "${registry_flows:-0}" +} + +# Assert that BOTH cache stacks were engaged during the deploy. Fails +# loudly if either stack is silent and reports a per-cache breakdown +# so the operator can see which side regressed. +# +# Usage: assert_caches_used +# where and are the strings returned by +# ``cache_snapshot`` (3 fields each). +assert_caches_used() { + local before="${1}" after="${2}" + local nexus_total_before proxy_before registry_before + local nexus_total_after proxy_after registry_after + read -r nexus_total_before proxy_before registry_before <<<"${before}" + read -r nexus_total_after proxy_after registry_after <<<"${after}" + + local delta_nexus_total=$((nexus_total_after - nexus_total_before)) + local delta_proxy=$((proxy_after - proxy_before)) + local delta_registry=$((registry_after - registry_before)) + local expected_proxy + expected_proxy="$(expected_system_proxy_for_runtime)" + + echo "[cache] package proxy requests (any repo) +${delta_nexus_total}" \ + "(now ${nexus_total_after})" + if [[ -n "${expected_proxy}" ]]; then + echo "[cache] package proxy '${expected_proxy}' +${delta_proxy}" \ + "(now ${proxy_after})" + else + echo "[cache] no Nexus system-proxy mapped for" \ + "DEV_RUNTIME_IMAGE='${DEV_RUNTIME_IMAGE:-?}'; skipping per-proxy check" + fi + echo "[cache] registry-cache flows (HIT|MISS|...) +${delta_registry}" \ + "(now ${registry_after})" + + local -a failures=() + + # Package-cache assertion: cumulative proxy traffic must exist for + # the runtime's expected system-package proxy. Cumulative (not + # delta) because most apt/yum traffic happens during 01_install, + # well before this assertion fires. + if [[ "${nexus_total_after}" -le 0 ]]; then + failures+=("package-cache: no /repository// traffic ever observed (Nexus seems unused)") + fi + if [[ -n "${expected_proxy}" && "${proxy_after}" -le 0 ]]; then + failures+=("package-cache: expected proxy '${expected_proxy}' for ${DEV_RUNTIME_IMAGE} has no cached requests") + fi + + # Registry-cache assertion: the deploy itself must produce flow + # entries (HIT or MISS), since image pulls are part of the + # deploy. + if [[ "${delta_registry}" -le 0 ]]; then + failures+=("registry-cache: no HIT/MISS recorded during this deploy") + fi + + if ((${#failures[@]} > 0)); then + echo "[FAIL] cache assertions:" >&2 + printf ' - %s\n' "${failures[@]}" >&2 + exit 1 + fi + echo "[OK] both cache stacks observed activity (registry + package-cache, including '${expected_proxy:-N/A}')" +} + +# Map runtime image to a (proxy, upstream-path) pair that the active +# probe can fetch through Nexus. The path picks a small, reliably- +# available index file on the upstream so the probe stays cheap. +_probe_target_for_runtime() { + local image="${DEV_RUNTIME_IMAGE:-}" + case "${image}" in + *debian*) echo "apt-debian dists/bookworm/Release" ;; + *ubuntu*) echo "apt-ubuntu dists/jammy/Release" ;; + *fedora*) echo "yum-fedora releases/40/Everything/x86_64/os/repodata/repomd.xml" ;; + *centos* | *rocky*) echo "yum-rocky 9/BaseOS/x86_64/os/repodata/repomd.xml" ;; + *) echo "" ;; + esac +} + +# Active end-to-end probe: hit each cache through its public surface +# and verify the request reaches the cache and is logged as proxy +# traffic. Catches regressions where the cache stack is up but stops +# proxying (mis-routed nginx, dead upstream, broken auth, expired CA, +# etc.). Calls ``exit 1`` if either probe fails. +probe_caches() { + local probe_target proxy_repo upstream_path + probe_target="$(_probe_target_for_runtime)" + if [[ -z "${probe_target}" ]]; then + echo "[probe] no probe target mapped for" \ + "DEV_RUNTIME_IMAGE='${DEV_RUNTIME_IMAGE:-?}'; skipping package probe" + else + read -r proxy_repo upstream_path <<<"${probe_target}" + echo "[probe] package-cache: GET /repository/${proxy_repo}/${upstream_path}" + if ! docker exec infinito-package-cache curl \ + --silent --fail --show-error --max-time 60 \ + --output /dev/null \ + "http://localhost:8081/repository/${proxy_repo}/${upstream_path}"; then + echo "[FAIL] package-cache: proxy '${proxy_repo}' did not serve" \ + "'${upstream_path}'. The cache stack is up but pull-through is broken." >&2 + exit 1 + fi + echo "[OK] package-cache: '${proxy_repo}' served '${upstream_path}'" + fi + + # Registry-cache (rpardini/docker-registry-proxy) is a transparent + # CONNECT proxy used by dockerd. It does not expose /v2/ directly; + # its only public HTTP endpoint is /ca.crt. A 200 there proves the + # cache container is alive and reachable via the proxy port. The + # AND-assertion against ``delta_registry > 0`` then confirms the + # deploy actually flowed through it. + echo "[probe] registry-cache: GET /ca.crt" + if ! docker exec infinito-registry-cache wget \ + --quiet --tries=1 --timeout=10 \ + --output-document=/dev/null \ + "http://127.0.0.1:3128/ca.crt"; then + echo "[FAIL] registry-cache: /ca.crt did not respond. The cache" \ + "container is unhealthy." >&2 + exit 1 + fi + echo "[OK] registry-cache: /ca.crt responded" +} + +# Active DiD probe: drive a throwaway ``docker build`` whose +# ``apt-get update`` MUST flow through the package-cache frontend +# (``deb.debian.org`` is hijacked via ``--add-host`` to the frontend +# IP, exactly mirroring the runtime override that +# ``roles/sys-svc-compose/files/compose.py`` injects for app builds). +# Verifies the inner-build / DiD cache path, which the +# Ansible-driven runner-side traffic alone does NOT exercise. +probe_did_inner_build() { + local frontend_ip + frontend_ip="${INFINITO_PACKAGE_CACHE_FRONTEND_IP:-}" + if [[ -z "${frontend_ip}" ]]; then + echo "[probe-did] INFINITO_PACKAGE_CACHE_FRONTEND_IP unset; skipping DiD probe" + return 0 + fi + + local network="${COMPOSE_NETWORK:-infinito-nexus_default}" + local before after delta tmpdir tag rc=0 + before="$(docker logs infinito-package-cache-frontend 2>&1 | wc -l)" + + tmpdir="$(mktemp -d)" + cat >"${tmpdir}/Dockerfile" <<-'EOF' + FROM debian:bookworm-slim + RUN apt-get update -o Acquire::http::No-Cache=true + EOF + tag="cache-did-probe-$$" + + echo "[probe-did] docker build --add-host deb.debian.org:${frontend_ip}" \ + "--network ${network} (apt-get update via frontend)" + # Force the classic builder: BuildKit rejects user-defined compose + # networks ("not supported by buildkit"). The classic builder + # accepts arbitrary `--network ` so we can put the build on + # the same network as the frontend container. + if ! DOCKER_BUILDKIT=0 docker build --no-cache \ + --network "${network}" \ + --add-host "deb.debian.org:${frontend_ip}" \ + -t "${tag}" "${tmpdir}" 2>&1 | tail -10; then + rc=1 + fi + rm -rf "${tmpdir}" + docker rmi "${tag}" >/dev/null 2>&1 || true + + if [[ "${rc}" -ne 0 ]]; then + echo "[FAIL] DiD probe: docker build failed (frontend, network or upstream broken)" >&2 + exit 1 + fi + + after="$(docker logs infinito-package-cache-frontend 2>&1 | wc -l)" + delta=$((after - before)) + if [[ "${delta}" -le 0 ]]; then + echo "[FAIL] DiD probe: build succeeded but frontend logged no new requests" >&2 + echo " (extra_hosts hijack or nginx upstream rewrite is broken)" >&2 + exit 1 + fi + echo "[OK] DiD inner-build flowed through cache frontend (+${delta} log lines)" +} diff --git a/scripts/tests/environment/lib.sh b/scripts/tests/environment/utils/common.sh similarity index 59% rename from scripts/tests/environment/lib.sh rename to scripts/tests/environment/utils/common.sh index 9b46406dfe..58e2869bb1 100755 --- a/scripts/tests/environment/lib.sh +++ b/scripts/tests/environment/utils/common.sh @@ -1,5 +1,8 @@ #!/usr/bin/env bash -# Shared variables and helpers for the environment test suite. +# Shared bootstrap, constants, and generic helpers for the environment +# test suite. Sourcing this file loads the repository env modules and +# configures git's safe.directory for the mounted workflow checkout. +# Cache-specific helpers live in cache.sh. set -euo pipefail DASHBOARD_APP="web-app-dashboard" @@ -10,8 +13,8 @@ MATOMO_URL="https://matomo.infinito.example" # These constants are part of the sourced interface consumed by sibling scripts. : "${DASHBOARD_APP}" "${MATOMO_APP}" "${DASHBOARD_URL}" "${MATOMO_URL}" -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -REPO_ROOT="$(cd "${SCRIPT_DIR}/../../.." && pwd)" +UTILS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +REPO_ROOT="$(cd "${UTILS_DIR}/../../../.." && pwd)" ensure_git_safe_directory() { local git_probe="" @@ -63,32 +66,6 @@ inspect() { make exec CMD="cat ${HOST_VARS_FILE}" } -# Snapshot cache request counters. Outputs ` `. -cache_snapshot() { - local nexus_lines registry_hits - nexus_lines="$(docker exec infinito-package-cache sh -c 'wc -l /dev/null | awk '{print $1}')" - registry_hits="$(docker logs infinito-registry-cache 2>&1 | grep -c '"upstream_cache_status":"HIT"' || true)" - printf '%s %s\n' "${nexus_lines:-0}" "${registry_hits:-0}" -} - -# Assert that cache traffic grew between two snapshots. -# Usage: assert_cache_used (both as ` ` pairs) -assert_cache_used() { - local before="${1}" after="${2}" - local nexus_before registry_before nexus_after registry_after - read -r nexus_before registry_before <<<"${before}" - read -r nexus_after registry_after <<<"${after}" - local delta_nexus=$((nexus_after - nexus_before)) - local delta_registry=$((registry_after - registry_before)) - echo "[cache] nexus requests +${delta_nexus} (was ${nexus_before}, now ${nexus_after})" - echo "[cache] registry HITs +${delta_registry} (was ${registry_before}, now ${registry_after})" - if [[ "${delta_nexus}" -le 0 && "${delta_registry}" -le 0 ]]; then - echo "[FAIL] no cache traffic observed during deploy" >&2 - exit 1 - fi - echo "[OK] cache observed activity during deploy" -} - # Check that a URL responds with the expected HTTP status code. # Usage: assert_http_status assert_http_status() {