# Shared helpers for the praxis proxmox bats test suite. # # Sourced (via `load`) by the per-script .bats files to build a consistent # sandbox: a temp STUB_DIR, a CALL_LOG, a sandbox ROOT with a mocked # api.sh + recording stubs for the provision siblings. Each .bats file # may further specialize the sandbox in its own setup(). # # Usage from a .bats file: # setup() { # load setup_helper # praxis_sandbox_init # sets STUB_DIR, LOG, ROOT, mocks # PROXMOX_API_URL="https://proxmox.test:8006/api2/json" # ... # } # teardown() { praxis_sandbox_teardown; } # # Helpers exported (functions): # praxis_sandbox_init — create the sandbox + default mocks # praxis_sandbox_teardown — rm -rf the sandbox # praxis_log_stub # — write a recording stub at ROOT/.sh # that logs ":" to $CALL_LOG and # exits ${:-0} # praxis_mock_api_default — install the default mocked api.sh # (pve_env no-op, pve_nextid → STUB_NEXTID, # pve_get → STUB_PVE_GET, pve_curl no-op, # pve_poll no-op). Tests may override # individual funcs after calling this. # praxis_sandbox_init — create the sandbox. Idempotent-ish: callers usually # invoke once in setup(). Sets these globals for the test: # STUB_DIR — temp dir root (cleaned in teardown) # CALL_LOG — shared call log path (tests grep this) # ROOT — sandbox root dir (real SCRIPT_DIR stand-in; siblings live here) praxis_sandbox_init() { STUB_DIR="$(mktemp -d)" export STUB_DIR CALL_LOG="${STUB_DIR}/calls.log" : > "$CALL_LOG" 2>/dev/null || true export CALL_LOG ROOT="${STUB_DIR}/root" mkdir -p "$ROOT" export ROOT # Default mocked api.sh — tests can overwrite ${ROOT}/api.sh after this. praxis_mock_api_default } praxis_sandbox_teardown() { [ -n "${STUB_DIR:-}" ] && rm -rf "$STUB_DIR" } # praxis_log_stub — write a recording stub at # ${ROOT}/.sh that logs ":" to $CALL_LOG and exits # with ${:-0}. The stub is chmod +x. praxis_log_stub() { _name="$1"; _exit_var="$2" printf '#!/bin/sh\necho "%s:$*" >> "%s"\nexit ${%s:-0}\n' \ "$_name" "$CALL_LOG" "$_exit_var" > "${ROOT}/${_name}.sh" chmod +x "${ROOT}/${_name}.sh" } # praxis_mock_api_default — install the default mocked api.sh. # pve_env no-op; pve_nextid returns ${STUB_NEXTID:-200}; pve_get returns # ${STUB_PVE_GET:-}; pve_curl no-op; pve_poll no-op. Override by writing # your own ${ROOT}/api.sh after calling this (or by redefining funcs in # your own setup). praxis_mock_api_default() { cat > "${ROOT}/api.sh" <<'ASTUB' pve_env() { :; } pve_nextid() { printf '%s\n' "${STUB_NEXTID:-200}"; } pve_get() { printf '%s\n' "${STUB_PVE_GET:-}"; } pve_curl() { :; } pve_poll() { :; } pve_tls_insecure() { printf '%s\n' "${STUB_TLS_INSECURE:-}"; } pve_auth_header() { printf 'PVEAPIToken=%s' "${PROXMOX_API_TOKEN:-}"; } pve_lxc_env_args() { first=1 for pair in "$@"; do [ "$first" -eq 0 ] && printf '\n' printf '%s' "lxc.environment=${pair}" first=0 done } ASTUB chmod +x "${ROOT}/api.sh" } # praxis_common_env — export the common Proxmox env vars used by every # test (all mocked; no live endpoint). Tests may override per-scenario. praxis_common_env() { export PROXMOX_API_URL="https://proxmox.test:8006/api2/json" export PROXMOX_API_TOKEN="root@pam!test=secret" export PROXMOX_NODE="testnode" export PROXMOX_STORAGE="local" export PROXMOX_TEMPLATE_VOLID="local:vztmpl/debian-12-template.tar.zst" export GITEA_TOKEN="gitea-test-token" export PRAXIS_VERSION="v0.2" export PRAXIS_PORT="8789" export PROXMOX_LXC_VMID="200" }