diff --git a/checksums/SHA256SUMS b/checksums/SHA256SUMS index 9d49246..1786850 100644 --- a/checksums/SHA256SUMS +++ b/checksums/SHA256SUMS @@ -16,14 +16,14 @@ d7d2fd7ab651d1f2f026bdb11d62e2c0c6ce6ee3128c9814d09550cf4304b3f2 ocf/loxthelion 70431e0d390a3612339c7b5887286c1be4d6b305cbe8898e7895a211c26b6087 ocf/loxthelion/managed-container.d/engine.sh 6c92c095ce10ac95d843fdc1c49e31c19491ca22fdac960affd8649606d76d82 ocf/loxthelion/managed-container.d/lifecycles/systemd.sh 5f3528932ea72ef42c18465e7f31f9a430407e8d4ff7c03c705fe8cae39dc26e ocf/loxthelion/managed-container.d/proxy.sh -fb01359a84062ec3e910a8b27e460953a0ae1641a69167b3497f2594911d2a00 ocf/loxthelion/managed-container.d/proxy_client.py +1acc1776f521557a047e7a53e5253f3b4373e9269cb3d9c5872b3e48033d2c18 ocf/loxthelion/managed-container.d/proxy_client.py f82e2571d309bce8e4d7e6aee6b61b68feb7d99dc85bb8b688b6123c13ccf6a7 ocf/loxthelion/managed-container.d/runtimes/podman.sh 4a99efea3d4a185e0630db89cfacc28aa8777bad9c747794ae69f70d87cd0327 pacemaker/README.md a42bb59a058ffeb59c1baa96e403856910bbbaea98f9104ff93daf4b5970c780 postgres-host/postgres-ha-bootstrap 957a5a72ed277d0216e070abb80733483be3d421b9fa488e27fdd75bb2919548 postgres-host/postgres-ha-peer-config.sh deb09e3ae40629f43b262ba4f9a7ebf59a1dca3da9d8c94478f670aea28bd0e1 quadlet/postgres.container 04a125068677bbee6e71d313ca9d95d35faa32851b79de781cd7b9f466561445 quadlet/postgres.env.example -0fb32ab5a9a51044ae9e4ae75b7d70bfb7f5e640c41a3d0de02277c9a8c44c91 tests/run-all.sh +82162526c92a18c90ee425a28a89667fc211c7be4825ef2e91747e97efca41bd tests/run-all.sh 5e5d4fdd2b6c7e0b8b146de7aad06c1f96b221a10fec525b00d40e0d85709fb0 tests/test-entrypoint-structure.sh b83f0415c09f0be34c51722370e5b07c51b9a9756d305d3b3f50827f484aa4dd tests/test-proxy-client-actions.py c3b988d7b2b1b138bb2530df9172f96bb0f5edc2f1561f38f0055695f4126134 tests/test-proxy-protocol.sh diff --git a/ocf/loxthelion/managed-container.d/proxy_client.py b/ocf/loxthelion/managed-container.d/proxy_client.py index 659b1ad..382af58 100755 --- a/ocf/loxthelion/managed-container.d/proxy_client.py +++ b/ocf/loxthelion/managed-container.d/proxy_client.py @@ -17,13 +17,21 @@ class ProtocolFailure(Exception): pass -def send_request(socket_path, resource, action): +def send_request( + socket_path, + resource, + action, + parameters=None, +): request = { "protocol_version": PROTOCOL_VERSION, "action": action, "resource": resource, } + if parameters is not None: + request["parameters"] = parameters + try: sock = socket.socket( socket.AF_UNIX, @@ -166,12 +174,26 @@ def format_result(action, result): def main(): - if len(sys.argv) != 6: + if len(sys.argv) not in (6, 7): raise SystemExit(11) socket_path = sys.argv[1] resource = sys.argv[2] action = sys.argv[3] + request_parameters = None + + if len(sys.argv) == 7: + try: + request_parameters = json.loads(sys.argv[6]) + + except ( + TypeError, + json.JSONDecodeError, + ): + raise SystemExit(11) + + if not isinstance(request_parameters, dict): + raise SystemExit(11) try: transport_failure = int(sys.argv[4]) @@ -188,6 +210,7 @@ def main(): socket_path, resource, action, + request_parameters, ) except TransportFailure: diff --git a/tests/run-all.sh b/tests/run-all.sh index abf639d..ff6fa2d 100755 --- a/tests/run-all.sh +++ b/tests/run-all.sh @@ -23,6 +23,7 @@ echo ">>> Proxy protocol contract" echo echo ">>> Generic proxy client actions" python3 ./tests/test-proxy-client-actions.py +python3 ./tests/test-proxy-client-parameters.py echo echo ">>> Python syntax" diff --git a/tests/test-proxy-client-parameters.py b/tests/test-proxy-client-parameters.py new file mode 100755 index 0000000..6e929fe --- /dev/null +++ b/tests/test-proxy-client-parameters.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 + +import importlib.util +import json +import socket +import tempfile +import threading +from pathlib import Path + + +CLIENT_PATH = Path( + "ocf/loxthelion/managed-container.d/proxy_client.py" +) + +spec = importlib.util.spec_from_file_location( + "proxy_client", + CLIENT_PATH, +) +client = importlib.util.module_from_spec(spec) +spec.loader.exec_module(client) + +with tempfile.TemporaryDirectory() as directory: + socket_path = str(Path(directory) / "test.sock") + received = {} + + listener = socket.socket( + socket.AF_UNIX, + socket.SOCK_STREAM, + ) + listener.bind(socket_path) + listener.listen(1) + + def serve(): + connection, _ = listener.accept() + + with connection: + raw = b"" + + while b"\n" not in raw: + chunk = connection.recv(65536) + + if not chunk: + break + + raw += chunk + + received.update( + json.loads(raw.decode().strip()) + ) + + connection.sendall( + json.dumps({ + "protocol_version": 1, + "ok": True, + "resource": "postgresql-ha", + "action": "notify", + "result": {"ocf_rc": 0}, + }).encode() + + b"\n" + ) + + thread = threading.Thread(target=serve) + thread.start() + + client.send_request( + socket_path, + "postgresql-ha", + "notify", + { + "type": "pre", + "operation": "demote", + }, + ) + + thread.join() + listener.close() + + assert received["action"] == "notify" + assert received["parameters"] == { + "type": "pre", + "operation": "demote", + } + +print("PROXY_CLIENT_PARAMETER_TEST_PASS=1")