generate_test_vars: add test

This commit is contained in:
DavHau
2025-09-16 16:36:52 +07:00
parent ea32fdbd93
commit c936830bc6
4 changed files with 56 additions and 14 deletions

View File

@@ -206,13 +206,13 @@ def parse_args() -> Options:
)
def main() -> None:
logging.basicConfig(level=logging.DEBUG)
os.environ["CLAN_NO_COMMIT"] = "1"
opts = parse_args()
test_dir = opts.test_dir
if opts.clean:
def generate_test_vars(
clean: bool,
repo_root: Path,
test_dir: Path,
check_attr: str,
) -> None:
if clean:
shutil.rmtree(test_dir / "vars", ignore_errors=True)
shutil.rmtree(test_dir / "sops", ignore_errors=True)
@@ -222,10 +222,10 @@ def main() -> None:
if system.endswith("-darwin"):
test_system = system.rstrip("darwin") + "linux"
flake = TestFlake(opts.check_attr, test_dir, str(opts.repo_root))
flake = TestFlake(check_attr, test_dir, str(repo_root))
machine_names = get_machine_names(
opts.repo_root,
opts.check_attr,
repo_root,
check_attr,
test_system,
)
@@ -233,16 +233,16 @@ def main() -> None:
flake.precache(
[
f"checks.{test_system}.{opts.check_attr}.machinesCross.{system}.{{{','.join(machine_names)}}}.config.clan.core.vars.generators.*.validationHash",
f"checks.{test_system}.{check_attr}.machinesCross.{system}.{{{','.join(machine_names)}}}.config.clan.core.vars.generators.*.validationHash",
],
)
# This hack is necessary because the sops store uses flake.path to find the machine keys
# This hack does not work because flake.invalidate_cache resets _path
flake._path = opts.test_dir # noqa: SLF001
flake._path = test_dir # noqa: SLF001
machines = [
TestMachine(name, flake, test_dir, opts.check_attr) for name in machine_names
TestMachine(name, flake, test_dir, check_attr) for name in machine_names
]
user = "admin"
admin_key_path = Path(test_dir.resolve() / "sops" / "users" / user / "key.json")
@@ -287,5 +287,17 @@ def main() -> None:
run_generators(list(machines), prompt_values=mocked_prompts)
def main() -> None:
logging.basicConfig(level=logging.DEBUG)
os.environ["CLAN_NO_COMMIT"] = "1"
args = parse_args()
generate_test_vars(
clean=args.clean,
repo_root=args.repo_root,
test_dir=args.repo_root / args.test_dir,
check_attr=args.check_attr,
)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,26 @@
import shutil
import subprocess
from pathlib import Path
import pytest
from clan_cli.generate_test_vars.cli import generate_test_vars
@pytest.mark.with_core
def test_generate_test_vars(
clan_core: Path,
temp_dir: Path,
) -> None:
test_dir_original = clan_core / "checks/service-dummy-test"
service_dir = temp_dir / "service-dummy-test"
shutil.copytree(test_dir_original, service_dir)
# Make the copied tree writable
subprocess.run(["chmod", "-R", "+w", str(service_dir)], check=True)
generate_test_vars(
clean=True,
repo_root=clan_core,
test_dir=service_dir,
check_attr="service-dummy-test",
)

View File

@@ -28,6 +28,10 @@
"pkgs/zerotierone"
"pkgs/minifakeroot"
"pkgs/clan-cli/clan_cli/tests/flake-module.nix"
# needed for test_generate_test_vars.py
"checks/service-dummy-test"
"checks/flake-module.nix"
];
};
};

View File

@@ -18,7 +18,7 @@ Documentation = "https://docs.clan.lol/"
Repository = "https://git.clan.lol/clan/clan-core"
[tool.setuptools.packages.find]
include = ["clan_lib*", "clan_cli*", "generate_test_vars*"]
include = ["clan_lib*", "clan_cli*"]
exclude = ["clan_lib.nixpkgs*", "result"]
[tool.setuptools.package-data]