#!/usr/bin/env bash # prole/tests/test_init_authority.sh # Validates the YAML generation of init_authority.sh SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) PROLE_HOME=$(cd "$SCRIPT_DIR/../.." && pwd) INIT_AUTHORITY="$PROLE_HOME/etc/init_authority.sh" if [[ ! -f "$INIT_AUTHORITY" ]]; then echo "ERROR: $INIT_AUTHORITY not found" exit 1 fi # Mock environment export PROLE_SERVICE="$PROLE_HOME/etc" export NAMESPACE="test-namespace" export PROLE_HOME="$PROLE_HOME" TMP_DIR=$(mktemp -d) trap 'rm -rf "$TMP_DIR"' EXIT # Mock other tools to avoid actual execution mock_tool() { cat < "$TMP_DIR/$1" #!/usr/bin/env bash # LOG THE CALL echo "Mocked $1 called with \$@" >> "$TMP_DIR/mock_calls.log" case "\$1" in *v1/kv/data/prole/test-namespace/admin) echo '{"data":{"data":{"root_password":"REDACTED", "admin_public_key_b64":"LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUF6S3Z4CkNvS0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLQo="}}}' ;; *) if [[ "$1" == "base64" ]]; then /usr/bin/base64 "\$@" elif [[ "$1" == "jq" ]]; then /usr/bin/jq "\$@" elif [[ "$1" == "kubectl" ]]; then case "\$*" in *apply*) if [[ ! -t 0 ]]; then cat > "$TMP_DIR/generated_manifest.yaml" fi # If the manifest is NOT valid YAML, we want to simulate the failure if python3 -c "import yaml, sys; yaml.safe_load(open('$TMP_DIR/generated_manifest.yaml'))" 2>/dev/null; then echo "deployment.apps/dog created" exit 0 else echo "error: error parsing STDIN: error converting YAML to JSON: yaml: line 34: could not find expected ':'" >&2 exit 1 fi ;; *rollout*) echo "deployment \"dog\" successfully rolled out" exit 0 ;; *get*pod*) echo "dog-pod-123" exit 0 ;; *) exit 0 ;; esac else exit 0 fi ;; esac M_EOF chmod +x "$TMP_DIR/$1" } mock_tool curl mock_tool kubectl export PATH="$TMP_DIR:/usr/bin:/bin" # We'll run a subshell where we override read_from_bao if possible, # but init_authority.sh sources prole_cfg.sh and defines its own functions. # Instead, we will rely on the fact that it uses curl/jq which we mocked. echo "Running init_authority.sh start (mocked)..." # We need to suppress the errors from configure_authority which follows deploy_dog # Also ensure the PATH is exported to subshells export PATH bash "$INIT_AUTHORITY" start > "$TMP_DIR/out" 2>&1 || true if [[ -f "$TMP_DIR/generated_manifest.yaml" ]]; then echo "Validating generated YAML..." # Use python3 to check if it's valid YAML if python3 -c "import yaml, sys; yaml.safe_load(open('$TMP_DIR/generated_manifest.yaml'))" 2>"$TMP_DIR/yaml_error"; then echo "SUCCESS: YAML is valid." else echo "FAILURE: YAML is invalid." cat "$TMP_DIR/yaml_error" echo "--- Generated YAML ---" cat "$TMP_DIR/generated_manifest.yaml" echo "----------------------" exit 1 fi else echo "FAILURE: No YAML manifest generated." echo "--- Output ---" cat "$TMP_DIR/out" echo "--------------" echo "--- Mock calls ---" cat "$TMP_DIR/mock_calls.log" || echo "No mock calls log" echo "------------------" exit 1 fi