dynix/tests/harmonia/test-script.py

170 lines
5.9 KiB
Python
Raw Normal View History

# SPDX-FileCopyrightText: 2026 Qyriad <qyriad@qyriad.me>
#
# SPDX-License-Identifier: EUPL-1.1
2026-02-10 14:59:44 +01:00
import functools
from pathlib import Path
2026-03-22 17:15:04 +01:00
import json
2026-02-10 14:59:44 +01:00
import shlex
import textwrap
import tomllib
from typing import Any, cast, TYPE_CHECKING
from beartype import beartype
from test_driver.machine import Machine
from test_driver.errors import RequestedAssertionFailed
if TYPE_CHECKING:
global machine
machine = cast(Machine, ...)
assert machine.shell is not None
ls = "eza -lah --color=always --group-directories-first"
indent = functools.partial(textwrap.indent, prefix=' ')
@beartype
def run_log(machine: Machine, *commands: str, timeout: int | None = 60) -> str:
output = ""
for command in commands:
with machine.nested(f"must succeed: {command}"):
(status, out) = machine.execute(f"{command} | tee /dev/stderr", timeout=timeout)
if status != 0:
machine.log(f"output: {out}")
raise RequestedAssertionFailed(
f"command `{command}` failed (exit code {status})",
)
output += out
return output
@beartype
def get_config_file() -> dict[str, Any]:
machine.wait_for_unit("harmonia.service")
pid = int(machine.get_unit_property("harmonia.service", "MainPID"))
env_lines: list[str] = machine.succeed(f"cat /proc/{pid}/environ").replace("\0", "\n").splitlines()
pairs: list[list[str]] = [line.split("=", maxsplit=1) for line in env_lines]
env = dict(pairs)
config_file = Path(env["CONFIG_FILE"])
machine.log(f"copying from VM: {config_file=}")
machine.copy_from_vm(config_file.as_posix())
config_file_path = machine.out_dir / config_file.name
with open(config_file_path, "rb") as f:
config_data = tomllib.load(f)
2026-03-22 17:15:04 +01:00
try:
config_file_path.unlink()
except Exception as e:
machine.log(f"Couldn't unlike path {config_file_path}: {e}")
raise
2026-02-10 14:59:44 +01:00
return config_data
2026-02-16 18:02:39 +01:00
@beartype
2026-03-22 17:15:04 +01:00
def dynix_append_daemon(option: str, value: Any):
#machine.succeed(f'''
# dynix append {shlex.quote(option)} {shlex.quote(str(value))}
#'''.strip())
payload = json.dumps(dict(
action="append",
args=dict(
name=option,
value=value,
),
))
machine.succeed(f'''
echo '{payload}' | socat -T10 -,ignoreeof /run/user/0/dynix.sock
''')
@beartype
def dynix_append_traditional(option: str, value: Any):
2026-02-16 18:02:39 +01:00
machine.succeed(f'''
dynix append {shlex.quote(option)} {shlex.quote(str(value))}
'''.strip())
expr = textwrap.dedent("""
2026-02-18 14:01:39 +01:00
(import <nixpkgs/nixos> { }).config.dynamicism.applyDynamicConfiguration { }
2026-02-16 18:02:39 +01:00
""").strip()
machine.succeed(rf"""
nix run --show-trace --log-format raw-with-logs --impure -E {shlex.quote(expr)}
""".strip())
2026-03-22 17:15:04 +01:00
@beartype
def dynix_append(option: str, value: Any):
use_daemon = True
#use_daemon = False
if use_daemon:
dynix_append_daemon(option, value)
else:
dynix_append_traditional(option, value)
machine.log("Doing test initialization and checks")
2026-02-10 14:59:44 +01:00
machine.wait_for_unit("default.target")
2026-02-18 14:01:39 +01:00
machine.wait_for_unit("install-dynix.service")
dynix_out = machine.succeed("dynix --version")
assert "dynix" in dynix_out, f"dynix not in {dynix_out=}"
2026-02-10 14:59:44 +01:00
2026-03-22 17:15:04 +01:00
machine.succeed("systemctl start user@0.service")
machine.wait_for_unit("user@0.service")
machine.succeed(textwrap.dedent(r'''
systemd-run --collect --unit=dynix-daemon.service \
-E "RUST_LOG=trace" \
-E "PATH=$PATH" \
-E "NIX_PATH=$NIX_PATH" \
-E "NIXOS_CONFIG=$NIXOS_CONFIG" \
-p "SuccessExitStatus=0 2" \
dynix daemon --color=always
'''))
machine.wait_for_unit("dynix-daemon.service")
machine.log("Checking initial harmonia.service conditions")
2026-02-10 14:59:44 +01:00
# Config should have our initial values.
config_toml = get_config_file()
assert int(config_toml['workers']) == 4, f"{config_toml['workers']=} != 4"
assert int(config_toml['max_connection_rate']) == 256, f"{config_toml['max_connection_rate']=} != 256"
with machine.nested("must succeed: initial nixos-rebuild switch"):
machine.succeed("env PAGER= nixos-rebuild switch --log-format raw-with-logs --no-reexec -v --fallback")
2026-02-10 14:59:44 +01:00
# Config should not have changed.
config_toml = get_config_file()
assert int(config_toml['workers']) == 4, f"{config_toml['workers']=} != 4"
assert int(config_toml['max_connection_rate']) == 256, f"{config_toml['max_connection_rate']=} != 256"
2026-03-22 17:15:04 +01:00
machine.log("Testing dynamic workers=20")
2026-02-10 14:59:44 +01:00
new_workers = 20
2026-02-16 18:02:39 +01:00
dynix_append("services.harmonia.settings.workers", new_workers)
2026-02-10 14:59:44 +01:00
2026-03-22 17:15:04 +01:00
machine.log("Testing that workers, but not max_connectin_rate, changed")
2026-02-10 14:59:44 +01:00
# Workers, but not max connection rate, should have changed.
config_toml = get_config_file()
assert int(config_toml['workers']) == new_workers, f"{config_toml['workers']=} != {new_workers}"
assert int(config_toml['max_connection_rate']) == 256, f"{config_toml['max_connection_rate']=} != 256"
2026-03-22 17:15:04 +01:00
machine.log("Testing dynamic max_connection_rate=100")
2026-02-10 14:59:44 +01:00
new_max_connection_rate = 100
2026-02-16 18:02:39 +01:00
dynix_append("services.harmonia.settings.max_connection_rate", new_max_connection_rate)
2026-02-10 14:59:44 +01:00
2026-02-18 14:01:39 +01:00
# Max connection rate should have changed, and workers should be the same as before.
2026-02-10 14:59:44 +01:00
config_toml = get_config_file()
assert int(config_toml['max_connection_rate']) == new_max_connection_rate, f"{config_toml['max_connection_rate']=} != {new_max_connection_rate}"
2026-02-18 14:01:39 +01:00
assert int(config_toml['workers']) == new_workers, f"{config_toml['workers']=} != {new_workers}"
2026-02-10 14:59:44 +01:00
2026-03-22 17:15:04 +01:00
machine.log("Done with tests; stopping dynix-daemon")
machine.succeed("systemctl stop dynix-daemon.service")
2026-02-10 14:59:44 +01:00
# And this should set everything back.
machine.succeed("env PAGER= nixos-rebuild switch --log-format raw-with-logs --no-reexec -v --fallback")
2026-02-10 14:59:44 +01:00
machine.wait_for_unit("harmonia.service")
config_toml = get_config_file()
assert int(config_toml['max_connection_rate']) == 256, f'{config_toml["max_connection_rate"]=} != 256'
assert int(config_toml['workers']) == 4, f'{config_toml["workers"]=} != 4'