dynix/tests/basic/test-script.py
2026-02-10 14:18:45 +01:00

90 lines
3.2 KiB
Python

from pathlib import Path
import shlex
import textwrap
from typing import 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"
@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() -> str:
machine.wait_for_unit("gotosocial.service")
gotosocial_pid = int(machine.get_unit_property("gotosocial.service", "MainPID"))
print(f"{gotosocial_pid=}")
cmdline = machine.succeed(f"cat /proc/{gotosocial_pid}/cmdline")
cmdline_args = cmdline.split("\0")
config_file_idx = cmdline_args.index("--config-path") + 1
config_file = Path(cmdline_args[config_file_idx])
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, "r") as f:
return f.read()
machine.wait_for_unit("default.target")
assert "lix" in machine.succeed("nix --version").lower()
machine.log("INIT")
machine.succeed("nix profile install -vv $(realpath /run/current-system/sw/share/nixos/modules/dynix)")
machine.succeed("nixos-generate-config")
machine.succeed("mkdir -vp /etc/nixos")
# Dereference is required since that configuration.nix is probably a symlink to the store.
machine.succeed("cp -rv --dereference /run/current-system/sw/share/nixos/configuration.nix /etc/nixos/")
machine.log("REBUILDING configuration inside VM")
machine.succeed("env PAGER= nixos-rebuild switch --log-format raw-with-logs --fallback")
config_text = get_config_file()
lines = config_text.splitlines()
application_name = next((line for line in lines if line.startswith("application-name:")), None)
assert application_name is not None, f"no 'application-name:' found in config file: {textwrap.indent(config_text, "")}"
assert "gotosocial-for-machine" in application_name, f"'gotosocial-for-machine' should be in {application_name=}"
new_app_name = "yay!"
expr = textwrap.dedent(f"""
let
nixos = import <nixpkgs/nixos> {{ }};
in nixos.config.dynamicism.doChange {{
option = "services.gotosocial.settings.application-name";
value = "{new_app_name}";
}}
""").strip()
machine.succeed(rf"""
nix run --show-trace --log-format raw-with-logs --impure -E {shlex.quote(expr)}
""".strip())
config_file_new = get_config_file()
lines = config_file_new.splitlines()
application_name = next(line for line in lines if line.startswith("application-name:"))
assert new_app_name in application_name, f"'{new_app_name}' should be in {application_name=}"