Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/nix/vm-tests/priv-request.nix

28 lines
1.1 KiB
Nix

# Python for a VM test script: `priv_request`, the one way a test talks to
# hive-priv. Splice it into `testScript` after `import json`. The VM it runs
# against must have `socat` installed.
{ pkgs }:
''
import shlex
def priv_journal(node):
return node.execute("journalctl -u hive-priv --no-pager | tail -n 30")[1]
def priv_request(node, req, timeout=120):
"""Send one request to hive-priv, wait for its reply, assert it is ok.
`-t` is how long socat waits for the reply after stdin ends; its default
is 0.5s, which drops the answer of any op that takes longer. Returns the
parsed reply.
"""
op = req["op"]
out = node.succeed(
f"echo {shlex.quote(json.dumps(req))} | "
f"${pkgs.socat}/bin/socat -t {timeout} - UNIX-CONNECT:/run/hive/priv.sock"
)
lines = out.strip().splitlines()
assert lines, f"hive-priv sent no response to {op}; journal:\n{priv_journal(node)}"
reply = json.loads(lines[-1])
assert reply.get("ok") is True, f"{op}: response={out!r}; journal:\n{priv_journal(node)}"
return reply
''