makeTestClan: simplify - move parameters into module

... in preparation of removing the makeTestClan wrapper and make this a module instead that can be imported into any test.
This commit is contained in:
DavHau
2025-06-02 12:14:06 +07:00
parent 9069421c7f
commit 9cb6382cec
5 changed files with 83 additions and 85 deletions

View File

@@ -6,8 +6,6 @@
}: }:
clanLib.test.makeTestClan { clanLib.test.makeTestClan {
inherit pkgs self; inherit pkgs self;
# TODO: container driver does not support: sleep, wait_for_window, send_chars, wait_for_text
useContainers = false;
nixosTest = ( nixosTest = (
{ lib, ... }: { lib, ... }:
let let
@@ -31,6 +29,8 @@ clanLib.test.makeTestClan {
clan = { clan = {
directory = ./.; directory = ./.;
# TODO: container driver does not support: sleep, wait_for_window, send_chars, wait_for_text
test.useContainers = false;
inventory = { inventory = {
machines = lib.genAttrs machines (_: { }); machines = lib.genAttrs machines (_: { });
services = { services = {

View File

@@ -6,8 +6,6 @@
}: }:
clanLib.test.makeTestClan { clanLib.test.makeTestClan {
inherit pkgs self; inherit pkgs self;
# TODO: container driver does not support wait_for_file() yet
useContainers = false;
nixosTest = ( nixosTest = (
{ lib, ... }: { lib, ... }:
{ {
@@ -15,6 +13,8 @@ clanLib.test.makeTestClan {
clan = { clan = {
directory = ./.; directory = ./.;
# TODO: container driver does not support wait_for_file() yet
test.useContainers = false;
inventory = { inventory = {
machines = lib.genAttrs [ machines = lib.genAttrs [
"introducer" "introducer"

View File

@@ -7,7 +7,6 @@
}: }:
clanLib.test.makeTestClan { clanLib.test.makeTestClan {
inherit pkgs self; inherit pkgs self;
useContainers = false;
nixosTest = ( nixosTest = (
{ ... }: { ... }:
{ {
@@ -15,6 +14,7 @@ clanLib.test.makeTestClan {
clan = { clan = {
directory = ./.; directory = ./.;
test.useContainers = false;
modules."@clan/wifi" = module; modules."@clan/wifi" = module;
inventory = { inventory = {

View File

@@ -36,7 +36,7 @@ let
machineNames = map (name: "${name}: Machine;") pythonizedNames; machineNames = map (name: "${name}: Machine;") pythonizedNames;
pythonizedNames = map pythonizeName nodeHostNames; pythonizedNames = map pythonizeName nodeHostNames;
in in
{ lib.mkIf (config.clan.test.useContainers or true) {
defaults.imports = [ defaults.imports = [
./nixos-module.nix ./nixos-module.nix
]; ];

View File

@@ -15,10 +15,9 @@ let
in in
{ {
#
containerTest = import ./container-test.nix; containerTest = import ./container-test.nix;
baseTest = import ./test-base.nix; baseTest = import ./test-base.nix;
#
flakeModules = clanLib.callLib ./flakeModules.nix { }; flakeModules = clanLib.callLib ./flakeModules.nix { };
minifyModule = ./minify.nix; minifyModule = ./minify.nix;
@@ -30,84 +29,82 @@ in
nixosTest, nixosTest,
pkgs, pkgs,
self, self,
useContainers ? true,
# Displayed for better error messages, otherwise the placeholder
attrName ? "<check_name>",
... ...
}: }:
let let
nixos-lib = import (pkgs.path + "/nixos/lib") { }; nixos-lib = import (pkgs.path + "/nixos/lib") { };
testName = test.config.name; test = (
nixos-lib.runTest (
update-vars-script = "${self.packages.${pkgs.system}.generate-test-vars}/bin/generate-test-vars";
relativeDir = removePrefix ("${self}/") (toString test.config.clan.directory);
update-vars = pkgs.writeShellScriptBin "update-vars" ''
${update-vars-script} $PRJ_ROOT/${relativeDir} ${testName}
'';
testSrc = lib.cleanSource test.config.clan.directory;
inputsForMachine =
machine:
flip mapAttrsToList machine.clan.core.vars.generators (_name: generator: generator.runtimeInputs);
generatorRuntimeInputs = unique (
flatten (flip mapAttrsToList test.config.nodes (_machineName: machine: inputsForMachine machine))
);
vars-check =
pkgs.runCommand "update-vars-check"
{
nativeBuildInputs = generatorRuntimeInputs ++ [
pkgs.nix
pkgs.git
pkgs.age
pkgs.sops
pkgs.bubblewrap
];
closureInfo = pkgs.closureInfo {
rootPaths = generatorRuntimeInputs ++ [
pkgs.bash
pkgs.coreutils
pkgs.jq.dev
pkgs.stdenv
pkgs.stdenvNoCC
pkgs.shellcheck-minimal
pkgs.age
pkgs.sops
];
};
}
''
${self.legacyPackages.${pkgs.system}.setupNixInNix}
cp -r ${testSrc} ./src
chmod +w -R ./src
find ./src/sops ./src/vars | sort > filesBefore
${update-vars-script} ./src ${testName} \
--repo-root ${self.packages.${pkgs.system}.clan-core-flake} \
--clean
find ./src/sops ./src/vars | sort > filesAfter
if ! diff -q filesBefore filesAfter; then
echo "The update-vars script changed the files in ${testSrc}."
echo "Diff:"
diff filesBefore filesAfter || true
exit 1
fi
touch $out
'';
test =
(nixos-lib.runTest (
{ config, ... }: { config, ... }:
let let
clanFlakeResult = config.clan; clanFlakeResult = config.clan;
testName = config.name;
update-vars-script = "${self.packages.${pkgs.system}.generate-test-vars}/bin/generate-test-vars";
relativeDir = removePrefix ("${self}/") (toString config.clan.directory);
update-vars = pkgs.writeShellScriptBin "update-vars" ''
${update-vars-script} $PRJ_ROOT/${relativeDir} ${testName}
'';
testSrc = lib.cleanSource config.clan.directory;
inputsForMachine =
machine:
flip mapAttrsToList machine.clan.core.vars.generators (_name: generator: generator.runtimeInputs);
generatorRuntimeInputs = unique (
flatten (flip mapAttrsToList config.nodes (_machineName: machine: inputsForMachine machine))
);
vars-check =
pkgs.runCommand "update-vars-check"
{
nativeBuildInputs = generatorRuntimeInputs ++ [
pkgs.nix
pkgs.git
pkgs.age
pkgs.sops
pkgs.bubblewrap
];
closureInfo = pkgs.closureInfo {
rootPaths = generatorRuntimeInputs ++ [
pkgs.bash
pkgs.coreutils
pkgs.jq.dev
pkgs.stdenv
pkgs.stdenvNoCC
pkgs.shellcheck-minimal
pkgs.age
pkgs.sops
];
};
}
''
${self.legacyPackages.${pkgs.system}.setupNixInNix}
cp -r ${testSrc} ./src
chmod +w -R ./src
find ./src/sops ./src/vars | sort > filesBefore
${update-vars-script} ./src ${testName} \
--repo-root ${self.packages.${pkgs.system}.clan-core-flake} \
--clean
find ./src/sops ./src/vars | sort > filesAfter
if ! diff -q filesBefore filesAfter; then
echo "The update-vars script changed the files in ${testSrc}."
echo "Diff:"
diff filesBefore filesAfter || true
exit 1
fi
touch $out
'';
in in
{ {
imports = [ imports = [
nixosTest nixosTest
] ++ lib.optionals useContainers [ ./container-test-driver/driver-module.nix ]; ./container-test-driver/driver-module.nix
];
options = { options = {
clanSettings = mkOption { clanSettings = mkOption {
default = { }; default = { };
@@ -136,10 +133,17 @@ in
_prefix = [ _prefix = [
"checks" "checks"
"<system>" "<system>"
attrName config.name
"config" "config"
"clan" "clan"
]; ];
options = {
test.useContainers = mkOption {
default = true;
type = types.bool;
description = "Whether to use containers for the test.";
};
};
} }
]; ];
}; };
@@ -201,17 +205,11 @@ in
} }
); );
# TODO: figure out if we really need this result = { inherit update-vars vars-check; };
# I am proposing for less magic in the test-framework
# People may add this in their own tests
# _module.args = { inherit self; };
# node.specialArgs.self = self;
}; };
} }
)).config.result; )
);
in in
test test;
// {
inherit update-vars vars-check;
};
} }