dynix/modules/tests-main.py

77 lines
2.8 KiB
Python
Raw Normal View History

2026-02-06 17:12:02 +01:00
#import re
from pathlib import Path
from typing import cast, TYPE_CHECKING
from test_driver.machine import Machine
from test_driver.errors import RequestedAssertionFailed
DEFAULT_NIX = "@DEFAULT_NIX@"
CONFIGURATION_NIX = "@CONFIGURATION_NIX@"
DYNAMICISM = "@DYNAMICISM@"
if TYPE_CHECKING:
global machine
machine = cast(Machine, ...)
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
def get_config_file() -> Path:
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())
return machine.out_dir / config_file.name
machine.wait_for_unit("default.target")
assert "lix" in run_log(machine, "nix --version").lower()
print(f"{CONFIGURATION_NIX=}")
machine.succeed("mkdir -vp /etc/nixos")
machine.copy_from_host(CONFIGURATION_NIX, "/etc/nixos")
machine.copy_from_host(DYNAMICISM, "/etc/nixos")
run_log(machine, f"nix build --log-format multiline-with-logs --impure -E 'import <nixpkgs/nixos> {{ configuration = {CONFIGURATION_NIX}; }}'")
run_log(machine, f"nixos-rebuild switch --file {CONFIGURATION_NIX} --verbose --print-build-logs")
config_file_local = get_config_file()
machine.log(f"opening copied file: {config_file_local=}")
with open(config_file_local, "r") as f:
text = f.read()
lines = text.splitlines()
application_name = next(line for line in lines if line.startswith("application-name:"))
assert "gotosocial-for-machine" in application_name, f"'gotosocial-for-machine' should be in {application_name=}"
print(f"{DEFAULT_NIX=}")
run_log(machine, "eza -lah --color=always --group-directories-first --tree /etc/")
#exec_start = machine.succeed("systemctl show gotosocial.service --property=ExecStart --value")
#exec_start = machine.succeed("systemctl show gotosocial.service --property=ExecStart --value")
#service_text = machine.succeed("systemctl show gotosocial.service")
#service_props = dict(line.split("=", maxsplit=1) for line in service_text.splitlines())
#exec_start = service_props['ExecStart']
#print(f"{exec_start=}")