test: add library, and check example for readiness

This commit is contained in:
Sridhar Ratnakumar 2023-06-13 18:01:35 -04:00
parent 3964517bb9
commit 9b4a7886f7
2 changed files with 46 additions and 8 deletions

View File

@ -57,12 +57,10 @@
};
testScript = ''
machine.wait_for_unit("default.target")
machine.succeed("""
sleep 10 # HACK
journalctl -u process-compose.service
curl -v http://localhost:8213/
""")
process_compose.wait(lambda procs:
procs["sqlite-web"]["is_ready"] == "Ready"
)
machine.succeed("curl -v http://localhost:8213/")
'';
};
};

View File

@ -2,6 +2,41 @@
let
inherit (lib) types mkOption;
testLibrary = ''
class ProcessCompose:
# GET /processes of process-compose swagger API.
def get_processes(self, port=8080):
import json
url = f"http://localhost:{port}/processes"
return json.loads(
machine.succeed("${lib.getExe pkgs.xh} get " + url))
def wait(self, ready, timeout_mins=1):
import time
import json
machine.wait_for_unit("default.target")
timeout = time.time() + 60*timeout_mins # 1 minutes from now
data = None
while True:
if time.time() > timeout:
print("Processes not started!")
machine.succeed("""
journalctl -u process-compose.service
cat process-compose*log
""")
raise Exception("Processes not started!")
info = self.get_processes(${if config.port == null then "8080" else builtins.toString config.port})
print(json.dumps(info))
data = { x["name"]: x for x in info["data"] }
if ready(data):
break
else:
time.sleep(1)
return data
process_compose = ProcessCompose()
'';
in
{
options = {
@ -12,14 +47,19 @@ in
configuration, followed by the specified testScript.
Useful if you want to test your configuration in CI.
The testScript will have access to `process_comose.wait` function that
it can be used to get the running process information, after waiting for
the specified readiness status.
'';
default = null;
};
outputs.check = mkOption {
type = types.nullOr types.package;
default = if config.testScript == null then null else
default =
if config.testScript == null then null else
pkgs.nixosTest {
inherit (config) testScript;
testScript = testLibrary + "\n" + config.testScript;
name = "process-compose-${name}-test";
nodes.machine = {
systemd.services.process-compose = {