share one hive-priv request helper across the vm tests
This commit is contained in:
parent
525de62b06
commit
c9b5705ad8
1 changed files with 33 additions and 17 deletions
28
nix/vm-tests/priv-request.nix
Normal file
28
nix/vm-tests/priv-request.nix
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
# 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
|
||||
''
|
||||
Loading…
Reference in a new issue