prole/tests/etc/test_init_cloudnative_pg_instances.sh
chrisfu 8134e27ec3 Add unit tests for init_cloudnative_pg and enable opt-in Docker pre-loading
- Introduced `test_init_cloudnative_pg_instances.sh` to validate CNPG instance count enforcement (default 3, configurable via `CNPG_INSTANCES`).
- Implemented support for opt-in Docker image pre-loading (`DOCKER_PRELOAD`/`PROLE_DOCKER_PRELOAD`) in `init_common_services.sh`.
- Added tests and validation for default pre-load behavior and toggle functionality in `test_init_common_services.sh`.
- Made `SERVICE_NAMESPACE` values consistent across configurations.
2026-03-08 21:53:49 -07:00

167 lines
4.3 KiB
Bash

#!/usr/bin/env bash
# Unit test for etc/init_cloudnative_pg.sh
# Verifies CNPG instance count enforcement (default 3; override via CNPG_INSTANCES).
set -euo pipefail
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
PROLE_HOME=$(cd "$SCRIPT_DIR/../.." && pwd)
ETC_DIR="$PROLE_HOME/etc"
SCRIPT_UNDER_TEST="$ETC_DIR/init_cloudnative_pg.sh"
TMP_DIR=$(mktemp -d)
trap 'rm -rf "$TMP_DIR"' EXIT
BIN_DIR="$TMP_DIR/bin"
mkdir -p "$BIN_DIR"
mock_tool() {
cat <<M_EOF > "$BIN_DIR/$1"
#!/usr/bin/env bash
echo "Mocked $1 called with \$@" >> "$TMP_DIR/mock_calls.log"
exit 0
M_EOF
chmod +x "$BIN_DIR/$1"
}
cat <<'K_EOF' > "$BIN_DIR/kubectl"
#!/usr/bin/env bash
set -euo pipefail
_log_file="${TMP_DIR}/mock_calls.log"
echo "Mocked kubectl called with $*" >> "${_log_file}"
args="$*"
# Short-circuit CNPG operator + webhook waits
if [[ "${args}" == *"get deployment"*"-n cnpg-system"*"cnpg-controller-manager"* ]]; then
exit 0
fi
if [[ "${args}" == *"get endpoints"*"cnpg-webhook-service"* ]]; then
echo "10.42.0.10"
exit 0
fi
# Avoid cert-manager installation waits
if [[ "${args}" == *"get crd"*"certificates.cert-manager.io"* ]]; then
exit 0
fi
if [[ "${args}" == *"-n cert-manager"*"get deploy"* ]]; then
exit 0
fi
# Avoid barman plugin waits
if [[ "${args}" == *"get crd"*"barmanobjectstores.barmancloud.cnpg.io"* ]]; then
exit 0
fi
if [[ "${args}" == *"-n cnpg-system"*"get secret"*"barman-cloud-client-tls"* ]]; then
exit 0
fi
if [[ "${args}" == *"-n cnpg-system"*"get secret"*"barman-cloud-server-tls"* ]]; then
exit 0
fi
# Namespace check in ensure_namespace()
if [[ "${args}" == "get namespace test-ns" ]]; then
exit 0
fi
# Capture applied CNPG manifest
if [[ "${args}" == apply* && "${args}" == *"-n"*"test-ns"* && "${args}" == *"-f"*"-"* ]]; then
cat > "${TMP_DIR}/applied.yaml"
echo "cluster.postgresql.cnpg.io/prole-db configured"
exit 0
fi
exit 0
K_EOF
chmod +x "$BIN_DIR/kubectl"
mock_tool curl
mock_tool docker
mock_tool k3d
mock_tool skopeo
mock_tool ansible-playbook
mock_tool tofu
mock_tool terraform
mock_tool ollama
mock_tool jq
export PATH="$BIN_DIR:$PATH"
export TMP_DIR
mkdir -p "$TMP_DIR/service/secrets"
printf '%s' "dummy-private-key" > "$TMP_DIR/service/secrets/admin.key"
printf '%s' "dummy-public-key" > "$TMP_DIR/service/secrets/admin.pub"
run_case() {
local label="$1"
local cnpg_instances_cfg="${2:-}"
local conf_dir="$TMP_DIR/conf-${label}"
local env_home="$TMP_DIR/env-${label}"
mkdir -p "$conf_dir"
mkdir -p "$env_home"
cat <<C_EOF > "$conf_dir/prole.cfg"
[User]
NAMESPACE = test-ns
SERVICE_NAMESPACE = test-system
PROLE_HOME = $PROLE_HOME
PROLE_SERVICE = $TMP_DIR/service
[Global]
DEPLOYMENT_MODE = k3s
${cnpg_instances_cfg}
C_EOF
# `etc/prole_cfg.sh` sources `$PROLE_HOME/env.sh` unconditionally if present.
# The repo's generated `env.sh` hard-codes PROLE_CONF to the real conf dir,
# so for tests we provide a minimal env.sh that points PROLE_CONF at our temp config.
cat <<E_EOF > "$env_home/env.sh"
#!/usr/bin/env bash
export PROLE_HOME="$PROLE_HOME"
export PROLE_CONF="$conf_dir"
export PROLE_SERVICE="$TMP_DIR/service"
E_EOF
chmod +x "$env_home/env.sh"
rm -f "$TMP_DIR/applied.yaml"
set +e
PROLE_HOME="$env_home" \
bash "$SCRIPT_UNDER_TEST" --mode k3s start >/dev/null 2>"$TMP_DIR/stderr-${label}"
local rc=$?
set -e
if [[ $rc -ne 0 ]]; then
echo "FAILURE: init_cloudnative_pg.sh returned rc=$rc for case '${label}'"
sed -n '1,200p' "$TMP_DIR/mock_calls.log" || true
sed -n '1,200p' "$TMP_DIR/stderr-${label}" || true
exit 1
fi
if [[ ! -f "$TMP_DIR/applied.yaml" ]]; then
echo "FAILURE: did not capture applied manifest for case '${label}'"
sed -n '1,200p' "$TMP_DIR/mock_calls.log" || true
sed -n '1,200p' "$TMP_DIR/stderr-${label}" || true
exit 1
fi
}
# Default: should apply 3 instances
run_case "default" ""
if ! grep -qE '^\s*instances:\s*3\s*$' "$TMP_DIR/applied.yaml"; then
echo "FAILURE: expected instances: 3 in applied manifest (default)"
sed -n '1,120p' "$TMP_DIR/applied.yaml" || true
exit 1
fi
# Override via prole.cfg: should apply 2 instances
run_case "override" "CNPG_INSTANCES = 2"
if ! grep -qE '^\s*instances:\s*2\s*$' "$TMP_DIR/applied.yaml"; then
echo "FAILURE: expected instances: 2 in applied manifest (override)"
sed -n '1,120p' "$TMP_DIR/applied.yaml" || true
exit 1
fi
echo "SUCCESS"