inventory test framework: init

Add nixos test module checks/lib/test-inventory-nix

accepts new option `inventory` and creates machines accordingly with correct imports
This commit is contained in:
DavHau
2025-03-31 13:39:23 +07:00
parent 566441d580
commit 8479e6c5c3
7 changed files with 116 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
(import ../lib/test-inventory.nix) (
{ ... }:
{
name = "dummy-inventory-test";
inventory = {
machines.peer1 = { };
machines.admin1 = { };
services = {
dummy-module.default = {
roles.peer.machines = [ "peer1" ];
roles.admin.machines = [ "admin1" ];
};
};
modules = {
dummy-module = ./dummy-module;
};
};
testScript = ''
start_all()
admin1.wait_for_unit("dummy-service")
peer1.wait_for_unit("dummy-service")
'';
}
)

View File

@@ -0,0 +1,10 @@
---
description = "Set up dummy-module"
categories = ["System"]
features = [ "inventory" ]
[constraints]
roles.admin.min = 1
roles.admin.max = 1
---

View File

@@ -0,0 +1,5 @@
{
imports = [
./shared.nix
];
}

View File

@@ -0,0 +1,5 @@
{
imports = [
./shared.nix
];
}

View File

@@ -0,0 +1,13 @@
{ pkgs, ... }:
{
systemd.services.dummy-service = {
enable = true;
description = "Dummy service";
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
ExecStart = "${pkgs.coreutils}/bin/true";
RemainAfterExit = true;
};
};
}

View File

@@ -41,6 +41,7 @@ in
borgbackup = import ./borgbackup nixosTestArgs;
matrix-synapse = import ./matrix-synapse nixosTestArgs;
mumble = import ./mumble nixosTestArgs;
dummy-inventory-test = import ./dummy-inventory-test nixosTestArgs;
data-mesher = import ./data-mesher nixosTestArgs;
syncthing = import ./syncthing nixosTestArgs;
zt-tcp-relay = import ./zt-tcp-relay nixosTestArgs;

View File

@@ -0,0 +1,56 @@
test:
{ pkgs, self, ... }:
let
inherit (pkgs) lib;
inherit (lib) mkOption flip mapAttrs;
inherit (lib.types) raw;
inherit (self.lib.inventory) buildInventory;
nixos-lib = import (pkgs.path + "/nixos/lib") { };
in
(nixos-lib.runTest (
{ config, ... }:
{
imports = [ test ];
options = {
inventory = mkOption {
description = "Inventory of machines and services";
type = raw;
};
serviceConfigs = mkOption {
description = "Result of the evaluated inventory";
type = raw;
readOnly = true;
};
};
config = {
serviceConfigs = buildInventory {
inventory = config.inventory;
# TODO: make directory argument optional in buildInventory
directory = ./.;
};
nodes = flip mapAttrs config.serviceConfigs.machines (
_machineName: attrs: {
imports = attrs.machineImports ++ [ self.nixosModules.clanCore ];
clan.core.settings.directory = builtins.toString ./.;
}
);
hostPkgs = pkgs;
# speed-up evaluation
defaults = (
{ config, ... }:
{
imports = [
./minify.nix
];
documentation.enable = lib.mkDefault false;
nix.settings.min-free = 0;
system.stateVersion = config.system.nixos.release;
}
);
_module.args = { inherit self; };
# to accept external dependencies such as disko
node.specialArgs.self = self;
};
}
)).config.result