Files
computer-playbook/scripts/tests/environment/utils/common.sh
T
kevinveenbirkenbachandClaude Opus 5 7bcba20f9d test(environment): derive the disable set instead of listing it
07_compose_minimal.sh claimed to disable 'all variable (group-conditional)
services' and then named seven by hand. tor was not among them, so a
deploy meant to be minimal pulled svc-net-tor, which hard-depends on
web-svc-mirror, whose group-conditional prometheus flag the closure
resolver treats as true — dragging the whole web stack in. Steps 12 and
13 passed no disable at all, which is why a mariadb swarm test deployed
keycloak, mailu, matomo, prometheus and dashboard.

The set is now derived from the contract the repo already enforces
(tests/integration/roles/meta/services/test_dynamic_flags.py): a key is
group-conditional when enabled OR shared is a Jinja expression. Checking
only enabled would miss mariadb, postgres, redis, memcached, typesense.

Exclusions are load-bearing, not cosmetic: dashboard because disabling
the primary app aborts init; javascript because it aliases cdn, which
web-app-dashboard declares a hard dependency with no runtime fallback;
mariadb and postgres because steps 12 and 13 test them.

variable_services assigns to a local first — disable="$(cmd)" does not
set the exit status of a simple command, so a failing CLI would have
yielded an empty disable set and steps 12/13 would have run the full
deploy and reported green.

roundtrip could not take disable at all; it now reaches both the compose
and the swarm leg.

Not verified by a deploy: 33 of the keys are new to step 07, and the
variant axis of svc-db-mariadb/svc-db-postgres collapses because tor is
its only dimension.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-06 10:31:05 +02:00

120 lines
3.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# 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"
MATOMO_APP="web-app-matomo"
MARIADB_APP="svc-db-mariadb"
POSTGRES_APP="svc-db-postgres"
DASHBOARD_URL="https://dashboard.infinito.example"
MATOMO_URL="https://matomo.infinito.example"
: "${DASHBOARD_APP}" "${MATOMO_APP}" "${MARIADB_APP}" "${POSTGRES_APP}" "${DASHBOARD_URL}" "${MATOMO_URL}"
UTILS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${UTILS_DIR}/../../../.." && pwd)"
ensure_git_safe_directory() {
local git_probe=""
if ! command -v git >/dev/null 2>&1; then
return 0
fi
if git -C "${REPO_ROOT}" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
return 0
fi
git_probe="$(git -C "${REPO_ROOT}" rev-parse --is-inside-work-tree 2>&1 || true)"
if [[ "${git_probe}" != *"detected dubious ownership"* ]]; then
return 0
fi
if ! git config --global --get-all safe.directory 2>/dev/null | grep -Fx "${REPO_ROOT}" >/dev/null 2>&1; then
echo "Configuring Git safe.directory for the mounted workflow checkout."
git config --global --add safe.directory "${REPO_ROOT}"
fi
}
load_repo_env() {
if ! command -v python3 >/dev/null 2>&1; then
return 0
fi
local previous_pwd
previous_pwd="$(pwd)"
cd "${REPO_ROOT}"
unset INFINITO_ENV_LOADED
unset PYTHON
unset PIP
# shellcheck source=scripts/meta/env/load.sh
source scripts/meta/env/load.sh
cd "${previous_pwd}"
}
load_repo_env
ensure_git_safe_directory
# Print the generated inventory and host_vars for debugging and verification.
#
# Matrix-variant deploys write `${INFINITO_INVENTORY_DIR}-0/devices.yml`,
# `${INFINITO_INVENTORY_DIR}-1/devices.yml`, ... never the unsuffixed
# `${INFINITO_INVENTORY_DIR}/devices.yml`. Discover every variant inside the
# infinito_nexus container (where the inventories actually live) and
# print each one; fall back to the unsuffixed path when no variants
# exist.
#
# cmd is executed via `sh -lc` (see scripts/tests/deploy/local/exec/container.sh)
# — keep the inner shell strictly POSIX (no bash arrays, no `shopt`, no `(( ))`).
inspect_glob_print() {
local glob_pattern="$1" fallback="$2"
make compose-exec cmd="set -eu; \
any=; \
for f in ${glob_pattern}; do \
[ -f \"\$f\" ] || continue; \
any=1; \
echo \"==> \$f\"; \
cat \"\$f\"; \
done; \
if [ -z \"\$any\" ]; then \
echo \"==> ${fallback}\"; \
cat \"${fallback}\"; \
fi"
}
inspect() {
echo "Printing the generated inventory to verify which roles were deployed."
inspect_glob_print "${INFINITO_INVENTORY_DIR}-*/devices.yml" "${INFINITO_INVENTORY_FILE}"
echo "Printing host_vars to verify per-host configuration."
inspect_glob_print "${INFINITO_INVENTORY_DIR}-*/host_vars/localhost.yml" "${INFINITO_INVENTORY_HOST_VARS_FILE}"
}
# Check that a URL responds with the expected HTTP status code.
# Usage: assert_http_status <expected_code> <url>
assert_http_status() {
local expected="${1}"
local url="${2}"
local actual
actual="$(curl -sS -o /dev/null -w '%{http_code}' "${url}" || true)"
if [ "${actual}" != "${expected}" ]; then
echo "[FAIL] ${url} returned HTTP ${actual}, expected ${expected}" >&2
exit 1
fi
echo "[OK] ${url} returned HTTP ${actual}"
}
# Print every group-conditional service key, comma-separated for `disable=`.
# Usage: variable_services [keep_key ...]
# Param keep_key: service keys the deploy under test needs, kept enabled.
variable_services() {
local out
out="$(cd "${REPO_ROOT}" && "${PYTHON}" -m cli.meta.roles.applications.dynamic_services --exclude "$@")"
[ -n "${out}" ] || {
echo "[FAIL] variable_services resolved to an empty disable set" >&2
exit 1
}
printf '%s' "${out}"
}