172 lines
3.3 KiB
Python
Executable File
172 lines
3.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
sys.path.insert(0, "daemon")
|
|
|
|
import server
|
|
from registry import RegistryError
|
|
|
|
|
|
class FakeRegistry:
|
|
def __init__(self):
|
|
self.resources = {
|
|
"postgresql-ha": object(),
|
|
}
|
|
self.executions = []
|
|
|
|
def list_resources(self):
|
|
return {
|
|
"postgresql-ha": {
|
|
"resource": "postgresql-ha",
|
|
}
|
|
}
|
|
|
|
def execute(self, resource, action):
|
|
self.executions.append(
|
|
(resource, action)
|
|
)
|
|
|
|
return {
|
|
"ok": True,
|
|
"resource": resource,
|
|
"plugin": "postgresql",
|
|
"action": action,
|
|
"result": {
|
|
"accepted": True,
|
|
},
|
|
}
|
|
|
|
|
|
def require(condition, message):
|
|
if not condition:
|
|
raise SystemExit("FAIL: " + message)
|
|
|
|
|
|
def require_registry_error(callback, message):
|
|
try:
|
|
callback()
|
|
|
|
except RegistryError:
|
|
return
|
|
|
|
raise SystemExit("FAIL: " + message)
|
|
|
|
|
|
registry = FakeRegistry()
|
|
|
|
print("===== PACEMAKER RESOURCE BINDING =====")
|
|
|
|
require_registry_error(
|
|
lambda: server.load_pacemaker_resource_binding(
|
|
registry,
|
|
{},
|
|
),
|
|
"missing binding was accepted",
|
|
)
|
|
|
|
print("missing_binding rejected")
|
|
|
|
require_registry_error(
|
|
lambda: server.load_pacemaker_resource_binding(
|
|
registry,
|
|
{
|
|
"PACEMAKER_RESOURCE_ID": "other-resource",
|
|
},
|
|
),
|
|
"unknown binding was accepted",
|
|
)
|
|
|
|
print("unknown_binding rejected")
|
|
|
|
binding = server.load_pacemaker_resource_binding(
|
|
registry,
|
|
{
|
|
"PACEMAKER_RESOURCE_ID": "postgresql-ha",
|
|
},
|
|
)
|
|
|
|
require(
|
|
binding == "postgresql-ha",
|
|
"valid binding was not loaded",
|
|
)
|
|
|
|
print("valid_binding accepted")
|
|
|
|
server.registry = registry
|
|
server.pacemaker_resource_id = binding
|
|
|
|
ping = server.handle_request({
|
|
"protocol_version": 1,
|
|
"action": "ping",
|
|
})
|
|
|
|
require(ping["ok"] is True, "ping failed")
|
|
require(
|
|
ping["pacemaker_resource"] == "postgresql-ha",
|
|
"ping did not report the binding",
|
|
)
|
|
require(
|
|
ping["protocol_version"] == 1,
|
|
"ping lost the protocol version",
|
|
)
|
|
|
|
print("ping_binding reported")
|
|
|
|
response = server.handle_request({
|
|
"protocol_version": 1,
|
|
"resource": "postgresql-ha",
|
|
"action": "monitor",
|
|
})
|
|
|
|
require(
|
|
response["ok"] is True,
|
|
"matching resource was rejected",
|
|
)
|
|
require(
|
|
registry.executions
|
|
== [("postgresql-ha", "monitor")],
|
|
"matching request was not executed exactly once",
|
|
)
|
|
|
|
print("matching_resource executed")
|
|
|
|
response = server.handle_request({
|
|
"protocol_version": 1,
|
|
"resource": "different-resource",
|
|
"action": "monitor",
|
|
})
|
|
|
|
require(
|
|
response["ok"] is False,
|
|
"mismatched resource was accepted",
|
|
)
|
|
require(
|
|
response["error_code"]
|
|
== "resource_binding_mismatch",
|
|
"wrong mismatch classification",
|
|
)
|
|
require(
|
|
response["resource"]
|
|
== "different-resource",
|
|
"rejected resource was not echoed",
|
|
)
|
|
require(
|
|
response["action"] == "monitor",
|
|
"rejected action was not echoed",
|
|
)
|
|
require(
|
|
registry.executions
|
|
== [("postgresql-ha", "monitor")],
|
|
"mismatched request reached the registry",
|
|
)
|
|
require(
|
|
response["protocol_version"] == 1,
|
|
"mismatch response lost protocol metadata",
|
|
)
|
|
|
|
print("mismatched_resource rejected")
|
|
|
|
print()
|
|
print("RESOURCE_BINDING_TEST_PASS=1")
|