daemon tests for gotosocial

This commit is contained in:
Qyriad 2026-03-25 19:12:45 +01:00
parent 7f4a5a35ca
commit ccd37ee169
7 changed files with 201 additions and 129 deletions

View file

@ -21,6 +21,7 @@ if TYPE_CHECKING:
assert machine.shell is not None
ls = "eza -lah --color=always --group-directories-first"
testing_client = "/root/.nix-profile/libexec/dynix-testing-client.py"
indent = functools.partial(textwrap.indent, prefix=' ')
@ -65,9 +66,6 @@ def get_config_file() -> dict[str, Any]:
@beartype
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(
@ -76,12 +74,10 @@ def dynix_append_daemon(option: str, value: Any):
),
))
machine.succeed(f'''
echo '{payload}' | socat -T10 -,ignoreeof /run/user/0/dynix.sock
''')
machine.succeed(f"echo '{payload}' | {testing_client} /run/user/0/dynix.sock")
@beartype
def dynix_append_traditional(option: str, value: Any):
def dynix_append_cli(option: str, value: Any):
machine.succeed(f'''
dynix append {shlex.quote(option)} {shlex.quote(str(value))}
'''.strip())
@ -94,76 +90,81 @@ def dynix_append_traditional(option: str, value: Any):
""".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)
def run_all_tests(machine: Machine, *, use_daemon: bool):
dynix_append = dynix_append_daemon if use_daemon else dynix_append_cli
machine.log("Doing test initialization and checks")
#
# Setup.
#
dynix_out = machine.succeed("dynix --version")
assert "dynix" in dynix_out, f"dynix not in {dynix_out=}"
machine.wait_for_unit("default.target")
machine.wait_for_unit("install-dynix.service")
machine.succeed("systemctl start user@0.service")
machine.wait_for_unit("user@0.service")
dynix_out = machine.succeed("dynix --version")
assert "dynix" in dynix_out, f"dynix not in {dynix_out=}"
run_log(machine, "systemctl start dynix-daemon.service")
machine.wait_for_unit("dynix-daemon.service")
machine.succeed("systemctl start user@0.service")
machine.wait_for_unit("user@0.service")
machine.log("Checking initial harmonia.service conditions")
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")
# 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"
machine.log("Checking initial harmonia.service conditions")
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")
# 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"
# 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"
with machine.nested("must succeed: initial nixos-rebuild switch"):
machine.log("Testing dynamic workers=20")
new_workers = 20
dynix_append("services.harmonia.settings.workers", new_workers)
machine.log("Testing that workers, but not max_connection_rate, changed")
# Workers, but not max connection rate, should have changed.
config_toml = get_config_file()
from pprint import pformat
machine.log(pformat(config_toml))
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)
# Max connection rate should have changed, and workers should be the same as before.
config_toml = get_config_file()
print(f"checking connection rate, {use_daemon=}")
assert int(config_toml['max_connection_rate']) == new_max_connection_rate, f"{config_toml['max_connection_rate']=} != {new_max_connection_rate}"
print(f"checking workers, {use_daemon=}")
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")
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'
# 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"
machine.start(allow_reboot=True)
machine.wait_for_unit("install-dynix.service")
try:
run_all_tests(machine, use_daemon=False)
except Exception as e:
machine.log(f"ERROR running CLI tests: {e}")
machine.log("Testing dynamic workers=20")
new_workers = 20
dynix_append("services.harmonia.settings.workers", new_workers)
machine.reboot()
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)
# 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")
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'
machine.wait_for_unit("install-dynix.service")
try:
run_all_tests(machine, use_daemon=True)
except Exception as e:
machine.log(f"ERROR running DAEMON tests: {e}")
raise