#!/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")