From 3937de82dc81b0401e0343ae026697d7a86cb353 Mon Sep 17 00:00:00 2001 From: Lyncoln Date: Fri, 18 Sep 2026 09:43:41 -0400 Subject: [PATCH] Accept validated PostgreSQL self-restart demotion --- checksums/SHA256SUMS | 5 +- .../applications/postgresql/native_peers.sh | 23 +++- tests/run-all.sh | 1 + tests/test-demote-database-response-matrix.sh | 118 ++++++++++++++++++ 4 files changed, 139 insertions(+), 8 deletions(-) create mode 100755 tests/test-demote-database-response-matrix.sh diff --git a/checksums/SHA256SUMS b/checksums/SHA256SUMS index 4cb534c..91027b2 100644 --- a/checksums/SHA256SUMS +++ b/checksums/SHA256SUMS @@ -2,7 +2,7 @@ 5aa02fa065d1dba55486838fa4f0cd8eba982edaaf73cdc7d818223a09cbadc6 ocf/loxthelion/managed-container 10932919c4339b553c7c9dbf30b45a917c51c0a08f362e12678003d58b12fa82 ocf/loxthelion/managed-container.d/applications/postgresql/auth.sh 61d64b4a3a15c354f101ef34c5dc0425723f4204207d359b5377d2b9f04f6b23 ocf/loxthelion/managed-container.d/applications/postgresql/demote.sh -e111dfe454a6e1edd80edc0c303119e045e7c88fe77b69a2745e58a022d0d317 ocf/loxthelion/managed-container.d/applications/postgresql/native_peers.sh +86bb5bacd27e4082a7f753108c1f5d5e777ff04fba8699d62ed148c17224abdf ocf/loxthelion/managed-container.d/applications/postgresql/native_peers.sh bcfde96b98f4727a8c97d42a39b7c6ec925b1b89d17ef67cc58eb7bd67afbfae ocf/loxthelion/managed-container.d/applications/postgresql/notify.sh 957a5a72ed277d0216e070abb80733483be3d421b9fa488e27fdd75bb2919548 ocf/loxthelion/managed-container.d/applications/postgresql/peer_config.sh 286d282565239bdb600563bac116575f3f56580e06f8f8df86c1a0fd0af96f07 ocf/loxthelion/managed-container.d/applications/postgresql/plugin.sh @@ -23,9 +23,10 @@ a42bb59a058ffeb59c1baa96e403856910bbbaea98f9104ff93daf4b5970c780 postgres-host/ 957a5a72ed277d0216e070abb80733483be3d421b9fa488e27fdd75bb2919548 postgres-host/postgres-ha-peer-config.sh deb09e3ae40629f43b262ba4f9a7ebf59a1dca3da9d8c94478f670aea28bd0e1 quadlet/postgres.container 04a125068677bbee6e71d313ca9d95d35faa32851b79de781cd7b9f466561445 quadlet/postgres.env.example -186c2350d1f4eaddd756fbc680319ce2bfb7ee3462cf1d421f549ba8ccca8d24 tests/run-all.sh +ad248df2911e87fbe456c095f4e30dd54b2da52c9e2efa0619d3a2bf16a0f30c tests/run-all.sh a373c0560391a2d8f63c3ba31609ab16b219d9b532b4ab9f8aea6dc45de10555 tests/test-demote-database-gate.sh 688eeb756c7f346e7e55acc555004ba12b48bc7c3d309073658d5e84a6e5e348 tests/test-demote-database-parameter.sh +a3b3b2dad6bfdab8ffc7b3cabc6e1b9f188ff743fcadf073e5e8bdcf1827b2b9 tests/test-demote-database-response-matrix.sh 22d3eb56e8cd3ae1a58e2616f44abd3818f79d7e5df55c1c158e36dca7c77f42 tests/test-demote-database-shadow.sh 5e5d4fdd2b6c7e0b8b146de7aad06c1f96b221a10fec525b00d40e0d85709fb0 tests/test-entrypoint-structure.sh 2faee72cf2fdcbfa667c3e1c2a7aabc053b96a9b8127ff7440ddb717fd2dc314 tests/test-notify-daemon-shadow.sh diff --git a/ocf/loxthelion/managed-container.d/applications/postgresql/native_peers.sh b/ocf/loxthelion/managed-container.d/applications/postgresql/native_peers.sh index a72d752..56594e8 100755 --- a/ocf/loxthelion/managed-container.d/applications/postgresql/native_peers.sh +++ b/ocf/loxthelion/managed-container.d/applications/postgresql/native_peers.sh @@ -71,12 +71,23 @@ import os response = json.loads(os.environ["MC_DAEMON_RESPONSE"]) -if ( - response.get("ocf_rc") != 0 - or response.get("ready") is not True - or response.get("mutation") is not False - or response.get("final_handoff_required") is not True -): +common_valid = ( + response.get("ocf_rc") == 0 + and response.get("ready") is True + and response.get("mutation") is False +) + +peer_handoff = ( + response.get("final_handoff_required") is True +) + +local_restart = ( + response.get("final_handoff_required") is False + and response.get("handoff_mode") == "local_restart" + and response.get("reason") == "local_restart_demotion_ready" +) + +if not common_valid or not (peer_handoff or local_restart): raise SystemExit(1) '; then ocf_log info \ diff --git a/tests/run-all.sh b/tests/run-all.sh index a287169..300bc9a 100755 --- a/tests/run-all.sh +++ b/tests/run-all.sh @@ -27,6 +27,7 @@ python3 ./tests/test-proxy-client-parameters.py ./tests/test-notify-daemon-shadow.sh ./tests/test-notify-target-shadow.sh ./tests/test-demote-database-shadow.sh +./tests/test-demote-database-response-matrix.sh ./tests/test-promote-database-shadow.sh ./tests/test-demote-database-gate.sh ./tests/test-promote-database-gate.sh diff --git a/tests/test-demote-database-response-matrix.sh b/tests/test-demote-database-response-matrix.sh new file mode 100755 index 0000000..2d44963 --- /dev/null +++ b/tests/test-demote-database-response-matrix.sh @@ -0,0 +1,118 @@ +#!/usr/bin/env bash +set -euo pipefail + +ROOT="$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)" +FILE="$ROOT/ocf/loxthelion/managed-container.d/applications/postgresql/native_peers.sh" +FAKE="$(mktemp)" + +cleanup() +{ + rm -f "$FAKE" +} + +trap cleanup EXIT + +ocf_log() +{ + : +} + +export OCF_RESOURCE_INSTANCE=postgresql-ha +export MC_PROXY_CLIENT="$FAKE" +export MC_DEMOTE_DATABASE_GATE=true + +# shellcheck disable=SC1090 +. "$FILE" + +write_response() +{ + cat > "$FAKE" + chmod 0755 "$FAKE" +} + +echo "===== REMOTE HANDOFF =====" + +write_response <<'PY' +import json +print(json.dumps({ + "ocf_rc": 0, + "ready": True, + "mutation": False, + "final_handoff_required": True, + "reason": "demotion_preflight_ready", +})) +PY + +if ! mc_pg_demote_database_shadow; then + echo "FAIL: valid remote handoff response was rejected" + exit 1 +fi + +echo "remote_handoff accepted" + +echo +echo "===== LOCAL RESTART =====" + +write_response <<'PY' +import json +print(json.dumps({ + "ocf_rc": 0, + "ready": True, + "mutation": False, + "final_handoff_required": False, + "handoff_mode": "local_restart", + "reason": "local_restart_demotion_ready", +})) +PY + +if ! mc_pg_demote_database_shadow; then + echo "FAIL: valid local restart response was rejected" + exit 1 +fi + +echo "local_restart accepted" + +echo +echo "===== ARBITRARY FALSE HANDOFF =====" + +write_response <<'PY' +import json +print(json.dumps({ + "ocf_rc": 0, + "ready": True, + "mutation": False, + "final_handoff_required": False, +})) +PY + +if mc_pg_demote_database_shadow; then + echo "FAIL: arbitrary final_handoff_required=false was accepted" + exit 1 +fi + +echo "arbitrary_false rejected" + +echo +echo "===== FAKE LOCAL RESTART =====" + +write_response <<'PY' +import json +print(json.dumps({ + "ocf_rc": 0, + "ready": True, + "mutation": False, + "final_handoff_required": False, + "handoff_mode": "local_restart", + "reason": "wrong_reason", +})) +PY + +if mc_pg_demote_database_shadow; then + echo "FAIL: fake local restart response was accepted" + exit 1 +fi + +echo "fake_local_restart rejected" + +echo +echo "DEMOTE_DATABASE_RESPONSE_MATRIX_TEST_PASS=1"