daemon w/ tests PoC

This commit is contained in:
Qyriad 2026-03-22 17:15:04 +01:00
parent 68fc04a6d2
commit 7f4a5a35ca
13 changed files with 498 additions and 81 deletions

View file

@ -4,6 +4,7 @@
import functools
from pathlib import Path
import json
import shlex
import textwrap
import tomllib
@ -55,31 +56,76 @@ def get_config_file() -> dict[str, Any]:
with open(config_file_path, "rb") as f:
config_data = tomllib.load(f)
config_file_path.unlink()
try:
config_file_path.unlink()
except Exception as e:
machine.log(f"Couldn't unlike path {config_file_path}: {e}")
raise
return config_data
@beartype
def dynix_append(option: str, value: Any):
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):
machine.succeed(f'''
dynix append {shlex.quote(option)} {shlex.quote(str(value))}
'''.strip())
@beartype
def do_apply():
expr = textwrap.dedent("""
(import <nixpkgs/nixos> { }).config.dynamicism.applyDynamicConfiguration { }
""").strip()
machine.succeed(rf"""
nix run --show-trace --log-format raw-with-logs --impure -E {shlex.quote(expr)}
""".strip())
@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")
machine.wait_for_unit("default.target")
machine.wait_for_unit("install-dynix.service")
dynix_out = machine.succeed("dynix --version")
assert "dynix" in dynix_out, f"dynix not in {dynix_out=}"
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")
# Config should have our initial values.
config_toml = get_config_file()
assert int(config_toml['workers']) == 4, f"{config_toml['workers']=} != 4"
@ -93,24 +139,28 @@ 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"
machine.log("Testing dynamic workers=20")
new_workers = 20
dynix_append("services.harmonia.settings.workers", new_workers)
do_apply()
machine.log("Testing that workers, but not max_connectin_rate, changed")
# 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"
machine.log("Testing dynamic max_connection_rate=100")
new_max_connection_rate = 100
dynix_append("services.harmonia.settings.max_connection_rate", new_max_connection_rate)
do_apply()
# Max connection rate should have changed, and workers should be the same as before.
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}"
assert int(config_toml['workers']) == new_workers, f"{config_toml['workers']=} != {new_workers}"
machine.log("Done with tests; stopping dynix-daemon")
machine.succeed("systemctl stop dynix-daemon.service")
# And this should set everything back.
machine.succeed("env PAGER= nixos-rebuild switch --log-format raw-with-logs --no-reexec -v --fallback")
machine.wait_for_unit("harmonia.service")