move nixosTestLib to pkgs/testing

This commit is contained in:
Jörg Thalheim
2025-07-02 16:03:01 +02:00
parent 3059f10da3
commit f5b54443da
11 changed files with 100 additions and 26 deletions

View File

@@ -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 = ''

View File

@@ -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 = "

View File

@@ -23,7 +23,7 @@ nixosLib.runTest (
clan.test.fromFlake = ./.;
extraPythonPackages = _p: [
clan-core.legacyPackages.${hostPkgs.system}.setupNixInNixPythonPackage
clan-core.legacyPackages.${hostPkgs.system}.nixosTestLib
];
testScript =

View File

@@ -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;
};
};
};
}

View File

@@ -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

View File

@@ -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(),

View File

@@ -1,6 +1,5 @@
"""SSH and test setup utilities"""
import os
import subprocess
from typing import NamedTuple

View File

@@ -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
]