From a8e73f02605f334876d38b2104e30415b41fd8c3 Mon Sep 17 00:00:00 2001 From: DavHau Date: Mon, 7 Apr 2025 21:00:09 +0700 Subject: [PATCH] inventory tests: generalize update-vars.py ... and move to pkgs/scripts/ --- .../scripts}/update-vars.py | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) rename {checks/dummy-inventory-test/dummy-module => pkgs/scripts}/update-vars.py (78%) diff --git a/checks/dummy-inventory-test/dummy-module/update-vars.py b/pkgs/scripts/update-vars.py similarity index 78% rename from checks/dummy-inventory-test/dummy-module/update-vars.py rename to pkgs/scripts/update-vars.py index cd632dbac..c7f28cc50 100755 --- a/checks/dummy-inventory-test/dummy-module/update-vars.py +++ b/pkgs/scripts/update-vars.py @@ -21,10 +21,25 @@ else: test_name = "dummy-inventory-test" -def test_dir(test_name: str) -> Path: +def _test_dir(test_name: str) -> Path: return Path(clan_core_dir / "checks" / test_name) +def machine_names(test_name: str) -> list[str]: + """ + Get the machine names from the test flake + """ + cmd = nix_eval( + [ + f"{clan_core_dir}#checks.{nix_config()['system']}.{test_name}.nodes", + "--apply", + "builtins.attrNames", + ] + ) + out = subprocess.run(cmd, check=True, text=True, stdout=subprocess.PIPE) + return json.loads(out.stdout.strip()) + + class TestMachine(Machine): """ Machine class which is able to deal with not having an actual flake. @@ -38,7 +53,7 @@ class TestMachine(Machine): return self._deployment cmd = nix_build( [ - f"{clan_core_dir}#checks.{nix_config()['system']}.{test_name}.nodes.{self.name}.config.system.clan.deployment.file" + f"{clan_core_dir}#checks.{nix_config()['system']}.{test_name}.nodes.{self.name}.system.clan.deployment.file" ] ) out = subprocess.run(cmd, check=True, text=True, stdout=subprocess.PIPE) @@ -100,11 +115,13 @@ class TestMachine(Machine): if __name__ == "__main__": os.environ["CLAN_NO_COMMIT"] = "1" - flake = Flake(str(test_dir(test_name))) - flake._path = test_dir(test_name) # noqa SLF001 + test_dir = _test_dir(test_name) + subprocess.run(["rm", "-rf", f"{test_dir}/vars", f"{test_dir}/sops"]) + flake = Flake(str(test_dir)) + flake._path = test_dir # noqa SLF001 flake._is_local = True # noqa SLF001 - machine = TestMachine("admin1", flake) + machines = [TestMachine(name, flake) for name in machine_names(test_name)] user = "admin" if not Path(flake.path / "sops" / "users" / user / "key.json").exists(): keygen(user, flake, False) - generate_vars([machine]) + generate_vars(list(machines))