prole/tests/etc/test_init_cloudnative_pg_image_policy.sh
chrisfu a069989315 Rename prole-db to knoe-db, add knoe-auth as cluster-internal KDC
Itemized changes:

1. knoe-auth: New cluster-internal KDC and SSO gateway service
   - Created etc/init_knoe_auth.sh based on init_kdc.sh with knoe-auth naming
   - Namespace defaults to SERVICE_NAMESPACE (knoe-system)
   - ConfigMap: knoe-auth-kdc-config, Secret: knoe-auth-secrets
   - Legacy cleanup removes old auth/dog/authority deployments

2. Orchestration: knoe-auth initializes before CloudNativePG
   - Updated prole.sh to insert init_knoe_auth.sh as step 2 (before CNPG)
   - Renumbered all subsequent initialization steps

3. Kong routing: Updated init_kong.sh to route to knoe-auth in SERVICE_NAMESPACE

4. Comment/reference updates for knoe-auth
   - Updated init_common_services.sh, init_service_layer.sh, init_kerberos.sh

5. prole-db renamed to knoe-db across the entire codebase
   - Renamed prole-db/ directory to knoe-db/
   - Renamed all prole-db Kubernetes manifests (deploy/opentofu, k8s/)
   - Renamed scripts: docker-root-knoe-db.sh, docker-run-knoe-db.sh, test-cnpg-knoe-db.sh
   - Renamed etc/init_prole-db-reset.sh to etc/init_knoe-db-reset.sh
   - Renamed etc/prole-db-passwwd.sh to etc/knoe-db-passwwd.sh
   - Renamed mock_val counterparts accordingly
   - Renamed tests/etc/test_init_prole-db-reset.sh to test_init_knoe-db-reset.sh
   - Renamed docs/prole-db-documentation-mcp-architecture.md to knoe-db variant
   - Renamed modes/k3d/prole-db/ to modes/k3d/knoe-db/
   - Renamed prole-db.iml to knoe-db.iml

6. Configuration updates
   - Updated conf/dev, conf/prod, conf/test, conf/service prole.cfg files
   - Updated conf/port-mapping.cfg
   - Updated etc/prole_cfg.sh and mock_val/prole_cfg.sh
   - Updated service/prole.cfg

7. Kubernetes manifests and deploy configuration
   - Updated deploy/opentofu/k3s ArgoCD application YAMLs
   - Updated kong-configmap.yaml and kustomization.yaml
   - Updated k3s/kong-config.yml and prole-resources.yaml
   - Updated prole-mssql-db deployment YAMLs
   - Updated supabase helm render and deploy scripts

8. Infrastructure and GCP Terraform
   - Updated deploy/gcp/terraform: folders, groups, IAM, service-projects

9. Python/installer code updates
   - Updated knoe/core: actions, build_context, controller, env, milestones
   - Updated knoe/milestone.py
   - Updated knoe/ui/screens: cfg, database, database_options, deploy, docker,
     navigation, security, services, validate
   - Updated knoe.spec, status.py

10. Shell script updates
    - Updated etc/: build_db, init_cloudnative_pg, init_cnpg_backup,
      init_db_manager, init_forgejo, init_gitlab, init_monitoring, init_openbao,
      init_port_forwards, init_postgrest, init_supabase_ports, status
    - Updated mock_val/ counterparts for all above scripts
    - Updated prole-net/init-prole-dns.sh
    - Updated bin/prole-kpf.sh, gitea/deploy.sh, supabase/deploy.sh

11. Test updates
    - Updated tests/etc/: test_init_cloudnative_pg*, test_init_cnpg_backup*,
      test_init_kdc*, test_init_kerberos*, test_init_kong*, test_prole_cfg*
    - Updated tests/installer/: test_actions_helpers, test_cfg_save_kubecontext,
      test_controller, test_core_classes, test_milestones, test_milestones_extended,
      test_namespace_propagation
    - Updated tests/: test_database_options, test_navigation,
      test_render_supabase_hostname, test_docker_build_fix,
      test_all_prole_home_fixes, silent_install_test, final_test

12. Documentation updates
    - Updated docs/: DOCKER-BUILD-FIX, PROLE-CFG-SECRETS, PROLE-HOME-DIRECTORY,
      build-system, patent
    - Updated scan/network_description.txt
    - Updated pom.xml

13. Miscellaneous script updates
    - Updated root-level: _adopt_replica_pvcs, _fix_replica_merlin, _import_pi,
      _patch_cluster, _prebind_pvcs, _rebind_d002, _rebind_d002b, test_resolve
    - Updated scripts/generate_spec.py

Co-authored-by: Junie <junie@jetbrains.com>
2026-03-22 22:16:21 -07:00

267 lines
6.1 KiB
Bash

#!/usr/bin/env bash
# Unit tests for etc/init_cloudnative_pg.sh image policy behavior:
# - no-op when desired image already deployed
# - fail early on version mismatch without --upgrade (before namespace creation)
# - on --upgrade + mismatch, trigger a rollout/restart hook
set -euo pipefail
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
REPO_ROOT=$(cd "$SCRIPT_DIR/../.." && pwd)
SCRIPT_UNDER_TEST="$REPO_ROOT/etc/init_cloudnative_pg.sh"
TMP_DIR=$(mktemp -d)
trap 'rm -rf "$TMP_DIR"' EXIT
BIN_DIR="$TMP_DIR/bin"
mkdir -p "$BIN_DIR"
MOCK_LOG="$TMP_DIR/mock_calls.log"
cat <<'K_EOF' > "$BIN_DIR/kubectl"
#!/usr/bin/env bash
set -euo pipefail
echo "Mocked kubectl called with $@" >> "${MOCK_LOG:-/tmp/mock_calls.log}"
args="$*"
if [[ "$args" == *"get --raw=/readyz"* || "$args" == *"get --raw='/readyz'"* || "$args" == *"get --raw=\"/readyz\""* ]]; then
echo "ok"
exit 0
fi
if [[ "$args" == *"version --short"* ]]; then
echo "Client Version: v0.0.0"
echo "Server Version: v0.0.0"
exit 0
fi
# CNPG webhook wait: return an endpoint IP so the wait loop passes.
if [[ "$args" == *"get endpoints"*"cnpg-webhook-service"* ]]; then
echo "10.42.0.10"
exit 0
fi
if [[ "$args" == *"get endpoints barman-cloud"* ]]; then
echo "10.42.0.11"
exit 0
fi
# Barman plugin service registration: return a service name for label-based lookups.
if [[ "$args" == *"get svc"*"-l cnpg.io/pluginName=barman-cloud.cloudnative-pg.io"*"-o jsonpath="* ]]; then
echo "barman-cloud"
exit 0
fi
if [[ "$args" == *"get svc barman-cloud"*"-o jsonpath="*".metadata.name"* ]]; then
echo "barman-cloud"
exit 0
fi
if [[ "$args" == *"get svc barman-cloud"*"-o jsonpath="*"pluginClientSecret"* ]]; then
echo "barman-cloud-client-tls"
exit 0
fi
if [[ "$args" == *"get svc barman-cloud"*"-o jsonpath="*"pluginServerSecret"* ]]; then
echo "barman-cloud-server-tls"
exit 0
fi
if [[ "$args" == *"get svc barman-cloud"*"-o jsonpath="*"pluginPort"* ]]; then
echo "443"
exit 0
fi
if [[ "$args" == *"get namespace"* ]]; then
exit 0
fi
if [[ "$args" == *"create namespace"* ]]; then
exit 0
fi
if [[ "$args" == *"get cluster"*"-o jsonpath="*".spec.imageName"* ]]; then
echo "${MOCK_CLUSTER_IMAGE:-}"
exit 0
fi
if [[ "$args" == *"get cluster"* ]]; then
exit 0
fi
if [[ "$args" == *"patch cluster"* ]]; then
exit 0
fi
if [[ "$args" == *"cnpg version"* ]]; then
exit 0
fi
if [[ "$args" == *"cnpg restart"* ]]; then
exit 0
fi
if [[ "$args" == *"apply"* ]]; then
exit 0
fi
if [[ "$args" == *"get secret"* ]]; then
exit 0
fi
exit 0
K_EOF
chmod +x "$BIN_DIR/kubectl"
mock_ok_tool() {
cat <<M_EOF > "$BIN_DIR/$1"
#!/usr/bin/env bash
echo "Mocked $1 called with \$@" >> "${MOCK_LOG:-/tmp/mock_calls.log}"
exit 0
M_EOF
chmod +x "$BIN_DIR/$1"
}
# Prevent accidental real calls during tests.
mock_ok_tool docker
mock_ok_tool k3d
mock_ok_tool curl
mock_ok_tool jq
mock_ok_tool skopeo
export PATH="$BIN_DIR:$PATH"
export MOCK_LOG
CONF_DIR="$TMP_DIR/conf"
mkdir -p "$CONF_DIR"
SERVICE_DIR="$TMP_DIR/service"
mkdir -p "$SERVICE_DIR/secrets"
printf '%s' "dummy-private-key" > "$SERVICE_DIR/secrets/admin.key"
printf '%s' "dummy-public-key" > "$SERVICE_DIR/secrets/admin.pub"
write_cfg() {
local prole_home_val="$1"
cat <<C_EOF > "$CONF_DIR/prole.cfg"
[User]
NAMESPACE = test-ns
SERVICE_NAMESPACE = test-system
PROLE_HOME = ${prole_home_val}
PROLE_SERVICE = ${SERVICE_DIR}
[Global]
DEPLOYMENT_MODE = k3d
[Docker Build]
LOCAL_REGISTRY = localhost:5000
LOCAL_REGISTRY_INTERNAL = k3d-prole-registry:5000
C_EOF
}
reset_log() {
: > "$MOCK_LOG"
}
assert_not_called() {
local needle="$1"
if grep -q "$needle" "$MOCK_LOG"; then
echo "FAILURE: expected not to see '$needle' in mock log"
sed -n '1,200p' "$MOCK_LOG" || true
exit 1
fi
}
assert_called() {
local needle="$1"
if ! grep -q "$needle" "$MOCK_LOG"; then
echo "FAILURE: expected to see '$needle' in mock log"
sed -n '1,200p' "$MOCK_LOG" || true
exit 1
fi
}
run_case() {
local name="$1"; shift
local expected_rc="$1"; shift
reset_log
local out="$TMP_DIR/${name}.out"
local err="$TMP_DIR/${name}.err"
set +e
"$@" >"$out" 2>"$err"
local rc=$?
set -e
if [[ "$rc" != "$expected_rc" ]]; then
echo "FAILURE: $name expected RC=$expected_rc got RC=$rc"
echo "--- stdout ---"
sed -n '1,200p' "$out" || true
echo "--- stderr ---"
sed -n '1,200p' "$err" || true
echo "--- mock log ---"
sed -n '1,200p' "$MOCK_LOG" || true
exit 1
fi
}
###############################################
# Case 1: no-op on same image (initialize)
###############################################
export PROLE_CONF="$CONF_DIR"
export PROLE_SERVICE="$SERVICE_DIR"
write_cfg "$REPO_ROOT"
export MOCK_CLUSTER_IMAGE="k3d-prole-registry:5000/knoe-db:18-140"
run_case "noop_initialize" 0 \
bash "$SCRIPT_UNDER_TEST" --mode k3d --namespace test-ns initialize knoe-db 18-140
assert_not_called "create namespace"
###############################################
# Case 2: mismatch without --upgrade fails early
###############################################
export MOCK_CLUSTER_IMAGE="k3d-prole-registry:5000/knoe-db:18-138"
run_case "mismatch_initialize" 1 \
bash "$SCRIPT_UNDER_TEST" --mode k3d --namespace test-ns initialize knoe-db 18-140
assert_not_called "create namespace"
###############################################
# Case 3: mismatch with --upgrade triggers restart hook (deploy)
###############################################
PROLE_HOME_UPGRADE="$TMP_DIR/prole_home"
mkdir -p "$PROLE_HOME_UPGRADE/deploy/opentofu/k3s/manifests/prole"
cat <<'Y_EOF' > "$PROLE_HOME_UPGRADE/deploy/opentofu/k3s/manifests/prole/knoe-db.yaml"
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: knoe-db
spec:
instances: 1
imageName: knoe-db:dummy
Y_EOF
cat <<'Y_EOF' > "$PROLE_HOME_UPGRADE/deploy/opentofu/k3s/manifests/prole/knoe-db-barman-objectstore.yaml"
apiVersion: postgresql.cnpg.io/v1
kind: ObjectStore
metadata:
name: knoe-db
spec: {}
Y_EOF
write_cfg "$PROLE_HOME_UPGRADE"
export MOCK_CLUSTER_IMAGE="k3d-prole-registry:5000/knoe-db:18-138"
run_case "upgrade_deploy" 0 \
bash "$SCRIPT_UNDER_TEST" --mode k3d --namespace test-ns --upgrade deploy 18-140
assert_called "cnpg restart"
echo "SUCCESS"