pkgs/clan: Fix state list and add regression tests

Fix the `clan state list` subcommands, it now correctly propagates the
flake argument.
Also adds regression tests.
This commit is contained in:
a-kenji
2025-07-09 10:50:30 +02:00
parent db0f01a20a
commit 83c3abc225
2 changed files with 46 additions and 12 deletions

View File

@@ -1,11 +1,11 @@
import argparse import argparse
import json import json
import logging import logging
from pathlib import Path
from clan_lib.cmd import RunOpts, run from clan_lib.cmd import RunOpts, run
from clan_lib.dirs import get_clan_flake_toplevel_or_env from clan_lib.dirs import get_clan_flake_toplevel_or_env
from clan_lib.errors import ClanCmdError, ClanError from clan_lib.errors import ClanCmdError, ClanError
from clan_lib.flake import Flake
from clan_lib.machines.machines import Machine from clan_lib.machines.machines import Machine
from clan_lib.nix import nix_eval from clan_lib.nix import nix_eval
@@ -19,11 +19,8 @@ log = logging.getLogger(__name__)
def list_state_folders(machine: Machine, service: None | str = None) -> None: def list_state_folders(machine: Machine, service: None | str = None) -> None:
uri = "TODO" # Use the flake from the machine object (which comes from CLI --flake argument)
if (clan_dir_result := get_clan_flake_toplevel_or_env()) is not None: flake = machine.flake.path
flake = clan_dir_result
else:
flake = Path()
cmd = nix_eval( cmd = nix_eval(
[ [
f"{flake}#nixosConfigurations.{machine.name}.config.clan.core.state", f"{flake}#nixosConfigurations.{machine.name}.config.clan.core.state",
@@ -36,11 +33,11 @@ def list_state_folders(machine: Machine, service: None | str = None) -> None:
proc = run(cmd, RunOpts(prefix=machine.name)) proc = run(cmd, RunOpts(prefix=machine.name))
res = proc.stdout.strip() res = proc.stdout.strip()
except ClanCmdError as e: except ClanCmdError as e:
msg = "Clan might not have meta attributes" msg = "Failed to evaluate machine state configuration"
raise ClanError( raise ClanError(
msg, msg,
location=f"show_clan {uri}", location=f"clan state list {machine.name}",
description="Evaluation failed on clanInternals.meta attribute", description="Evaluation failed on clan.core.state attribute",
) from e ) from e
state = json.loads(res) state = json.loads(res)
@@ -87,9 +84,20 @@ def list_state_folders(machine: Machine, service: None | str = None) -> None:
def list_command(args: argparse.Namespace) -> None: def list_command(args: argparse.Namespace) -> None:
list_state_folders( if args.flake:
Machine(name=args.machine, flake=args.flake), service=args.service flake = args.flake
else:
tmp = get_clan_flake_toplevel_or_env()
flake = Flake(str(tmp)) if tmp else None
if not flake:
msg = "No clan found."
description = (
"Run this command in a clan directory or specify the --flake option"
) )
raise ClanError(msg, description=description)
list_state_folders(Machine(name=args.machine, flake=flake), service=args.service)
def register_state_parser(parser: argparse.ArgumentParser) -> None: def register_state_parser(parser: argparse.ArgumentParser) -> None:

View File

@@ -0,0 +1,26 @@
import pytest
from clan_cli.tests.fixtures_flakes import FlakeForTest
from clan_cli.tests.helpers import cli
from clan_cli.tests.stdout import CaptureOutput
@pytest.mark.with_core
def test_state_list_vm1(
test_flake_with_core: FlakeForTest, capture_output: CaptureOutput
) -> None:
with capture_output as output:
cli.run(["state", "list", "vm1", "--flake", str(test_flake_with_core.path)])
assert output.out is not None
assert "service: zerotier" in output.out
assert "folders:" in output.out
assert "/var/lib/zerotier-one" in output.out
@pytest.mark.with_core
def test_state_list_vm2(
test_flake_with_core: FlakeForTest, capture_output: CaptureOutput
) -> None:
with capture_output as output:
cli.run(["state", "list", "vm2", "--flake", str(test_flake_with_core.path)])
assert output.out is not None