diff --git a/checks/installation/flake-module.nix b/checks/installation/flake-module.nix index e297d88ad..6041d082e 100644 --- a/checks/installation/flake-module.nix +++ b/checks/installation/flake-module.nix @@ -167,7 +167,7 @@ name = "installation"; nodes.target = (import ./test-helpers.nix { inherit lib pkgs self; }).target; extraPythonPackages = _p: [ - (import ./test-helpers.nix { inherit lib pkgs self; }).nixosTestLib + self.legacyPackages.${pkgs.system}.nixosTestLib ]; testScript = '' @@ -225,7 +225,7 @@ name = "update-hardware-configuration"; nodes.target = (import ./test-helpers.nix { inherit lib pkgs self; }).target; extraPythonPackages = _p: [ - (import ./test-helpers.nix { inherit lib pkgs self; }).nixosTestLib + self.legacyPackages.${pkgs.system}.nixosTestLib ]; testScript = '' diff --git a/checks/installation/nixos_test_lib/environment.py b/checks/installation/nixos_test_lib/environment.py deleted file mode 100644 index c66be466e..000000000 --- a/checks/installation/nixos_test_lib/environment.py +++ /dev/null @@ -1,13 +0,0 @@ -"""Environment setup utilities for VM tests""" - -import os - - -def setup_nix_environment(temp_dir: str, closure_info: str) -> None: - """Set up nix chroot store environment""" - if "NIX_REMOTE" in os.environ: - del os.environ["NIX_REMOTE"] # we don't have any nix daemon running - - os.environ["TMPDIR"] = temp_dir - # Set NIX_CONFIG globally to disable substituters for speed - os.environ["NIX_CONFIG"] = "substituters = \ntrusted-public-keys = " diff --git a/checks/service-dummy-test-from-flake/default.nix b/checks/service-dummy-test-from-flake/default.nix index aaf2444b6..324e2f087 100644 --- a/checks/service-dummy-test-from-flake/default.nix +++ b/checks/service-dummy-test-from-flake/default.nix @@ -23,7 +23,7 @@ nixosLib.runTest ( clan.test.fromFlake = ./.; extraPythonPackages = _p: [ - clan-core.legacyPackages.${hostPkgs.system}.setupNixInNixPythonPackage + clan-core.legacyPackages.${hostPkgs.system}.nixosTestLib ]; testScript = diff --git a/pkgs/testing/flake-module.nix b/pkgs/testing/flake-module.nix index 11c81bfd9..c30aaf383 100644 --- a/pkgs/testing/flake-module.nix +++ b/pkgs/testing/flake-module.nix @@ -1,6 +1,6 @@ { perSystem = - { pkgs, ... }: + { pkgs, lib, ... }: { legacyPackages = { setupNixInNix = '' @@ -21,6 +21,25 @@ fi ''; + # NixOS test library combining port utils and clan VM test utilities + nixosTestLib = pkgs.python3Packages.buildPythonPackage { + pname = "nixos-test-lib"; + version = "1.0.0"; + format = "pyproject"; + src = lib.fileset.toSource { + root = ./.; + fileset = lib.fileset.unions [ + ./pyproject.toml + ./nixos_test_lib + ]; + }; + nativeBuildInputs = with pkgs.python3Packages; [ + setuptools + wheel + ]; + doCheck = false; + }; + }; }; } diff --git a/checks/installation/nixos_test_lib/__init__.py b/pkgs/testing/nixos_test_lib/__init__.py similarity index 100% rename from checks/installation/nixos_test_lib/__init__.py rename to pkgs/testing/nixos_test_lib/__init__.py diff --git a/pkgs/testing/nixos_test_lib/machine.py b/pkgs/testing/nixos_test_lib/machine.py new file mode 100644 index 000000000..c20bad723 --- /dev/null +++ b/pkgs/testing/nixos_test_lib/machine.py @@ -0,0 +1,25 @@ +"""VM machine management utilities""" + + +def create_test_machine(oldmachine, qemu_test_bin: str, **kwargs): + """Create a new test machine from an installed disk image""" + start_command = [ + f"{qemu_test_bin}/bin/qemu-kvm", + "-cpu", + "max", + "-m", + "3048", + "-virtfs", + "local,path=/nix/store,security_model=none,mount_tag=nix-store", + "-drive", + f"file={oldmachine.state_dir}/target.qcow2,id=drive1,if=none,index=1,werror=report", + "-device", + "virtio-blk-pci,drive=drive1", + "-netdev", + "user,id=net0", + "-device", + "virtio-net-pci,netdev=net0", + ] + machine = create_machine(start_command=" ".join(start_command), **kwargs) + driver.machines.append(machine) + return machine diff --git a/checks/installation/nixos_test_lib/nix_setup.py b/pkgs/testing/nixos_test_lib/nix_setup.py similarity index 84% rename from checks/installation/nixos_test_lib/nix_setup.py rename to pkgs/testing/nixos_test_lib/nix_setup.py index 5e518bcfd..5fc9d1d69 100644 --- a/checks/installation/nixos_test_lib/nix_setup.py +++ b/pkgs/testing/nixos_test_lib/nix_setup.py @@ -12,7 +12,7 @@ def setup_nix_in_nix(closure_info: str) -> None: Args: closure_info: Path to closure info directory containing store-paths file """ - tmpdir = os.environ.get("TMPDIR", "/tmp") + tmpdir = Path(os.environ.get("TMPDIR", "/tmp")) # Remove NIX_REMOTE if present (we don't have any nix daemon running) if "NIX_REMOTE" in os.environ: @@ -35,10 +35,10 @@ def setup_nix_in_nix(closure_info: str) -> None: Path(f"{tmpdir}/store").mkdir(parents=True, exist_ok=True) # Set up Nix store if closure info is provided - if closure_info and os.path.exists(closure_info): - store_paths_file = os.path.join(closure_info, "store-paths") - if os.path.exists(store_paths_file): - with open(store_paths_file) as f: + if closure_info and Path(closure_info).exists(): + store_paths_file = Path(closure_info) / "store-paths" + if store_paths_file.exists(): + with store_paths_file.open() as f: store_paths = f.read().strip().split("\n") # Copy store paths to test store @@ -55,12 +55,12 @@ def setup_nix_in_nix(closure_info: str) -> None: shutil.copy2(store_path, dest_path) # Load Nix database - registration_file = os.path.join(closure_info, "registration") - if os.path.exists(registration_file): + registration_file = Path(closure_info) / "registration" + if registration_file.exists(): env = os.environ.copy() env["NIX_REMOTE"] = f"local?store={tmpdir}/store" - with open(registration_file) as f: + with registration_file.open() as f: subprocess.run( ["nix-store", "--load-db"], input=f.read(), diff --git a/checks/installation/nixos_test_lib/port.py b/pkgs/testing/nixos_test_lib/port.py similarity index 100% rename from checks/installation/nixos_test_lib/port.py rename to pkgs/testing/nixos_test_lib/port.py diff --git a/checks/installation/nixos_test_lib/py.typed b/pkgs/testing/nixos_test_lib/py.typed similarity index 100% rename from checks/installation/nixos_test_lib/py.typed rename to pkgs/testing/nixos_test_lib/py.typed diff --git a/checks/installation/nixos_test_lib/ssh.py b/pkgs/testing/nixos_test_lib/ssh.py similarity index 99% rename from checks/installation/nixos_test_lib/ssh.py rename to pkgs/testing/nixos_test_lib/ssh.py index 645a0769a..ae6a84f89 100644 --- a/checks/installation/nixos_test_lib/ssh.py +++ b/pkgs/testing/nixos_test_lib/ssh.py @@ -1,6 +1,5 @@ """SSH and test setup utilities""" -import os import subprocess from typing import NamedTuple diff --git a/pkgs/testing/pyproject.toml b/pkgs/testing/pyproject.toml new file mode 100644 index 000000000..b82e1bc08 --- /dev/null +++ b/pkgs/testing/pyproject.toml @@ -0,0 +1,44 @@ +[build-system] +requires = ["setuptools", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "nixos-test-lib" +version = "1.0.0" +description = "NixOS test utilities for clan VM testing" +authors = [ + {name = "Clan Core Team"} +] +dependencies = [] + +[project.optional-dependencies] +dev = [ + "mypy", + "ruff" +] + +[tool.setuptools.packages.find] +where = ["."] +include = ["nixos_test_lib*"] + +[tool.setuptools.package-data] +"nixos_test_lib" = ["py.typed"] + +[tool.mypy] +python_version = "3.12" +strict = true +warn_return_any = true +warn_unused_configs = true + +[tool.ruff] +target-version = "py312" +line-length = 88 + +[tool.ruff.lint] +select = ["ALL"] +ignore = [ + "D", # docstrings + "ANN", # type annotations + "COM812", # trailing comma + "ISC001", # string concatenation +] \ No newline at end of file