tests: update inventory tests to use whole clan modules

This commit is contained in:
Johannes Kirschbauer
2025-10-30 13:47:34 +01:00
parent be31b9ce21
commit 1953540d08
6 changed files with 245 additions and 237 deletions

View File

@@ -4,36 +4,8 @@
... ...
}: }:
let let
inherit (lib)
evalModules
;
evalInventory =
m:
(evalModules {
# Static modules
modules = [
clanLib.inventory.inventoryModule
{
_file = "test file";
tags.all = [ ];
tags.nixos = [ ];
tags.darwin = [ ];
}
{
modules.test = { };
}
m
];
}).config;
callInventoryAdapter =
inventoryModule:
let
inventory = evalInventory inventoryModule;
flakeInputsFixture = { flakeInputsFixture = {
self.clan.modules = inventoryModule.modules or { };
# Example upstream module
upstream.clan.modules = { upstream.clan.modules = {
uzzi = { uzzi = {
_class = "clan.service"; _class = "clan.service";
@@ -43,24 +15,42 @@ let
}; };
}; };
}; };
in
clanLib.inventory.mapInstances { createTestClan =
directory = ./.; testClan:
clanCoreModules = { }; let
flakeInputs = flakeInputsFixture; res = clanLib.clan ({
inherit inventory; # Static / mocked
exportsModule = { }; specialArgs = {
clan-core = {
clan.modules = { };
}; };
};
self.inputs = flakeInputsFixture // {
self.clan = res.config;
};
directory = ./.;
exportsModule = { };
imports = [
testClan
];
});
in
res;
in in
{ {
extraModules = import ./extraModules.nix { inherit clanLib; }; extraModules = import ./extraModules.nix { inherit clanLib; };
exports = import ./exports.nix { inherit lib clanLib; }; exports = import ./exports.nix { inherit lib clanLib; };
settings = import ./settings.nix { inherit lib callInventoryAdapter; }; settings = import ./settings.nix { inherit lib createTestClan; };
specialArgs = import ./specialArgs.nix { inherit lib callInventoryAdapter; }; specialArgs = import ./specialArgs.nix { inherit lib createTestClan; };
resolve_module_spec = import ./import_module_spec.nix { inherit lib callInventoryAdapter; }; resolve_module_spec = import ./import_module_spec.nix {
inherit lib createTestClan;
};
test_simple = test_simple =
let let
res = callInventoryAdapter { res = createTestClan {
# Authored module # Authored module
# A minimal module looks like this # A minimal module looks like this
# It isn't exactly doing anything but it's a valid module that produces an output # It isn't exactly doing anything but it's a valid module that produces an output
@@ -71,7 +61,7 @@ in
}; };
}; };
# User config # User config
instances."instance_foo" = { inventory.instances."instance_foo" = {
module = { module = {
name = "simple-module"; name = "simple-module";
}; };
@@ -81,7 +71,7 @@ in
{ {
# Test that the module is mapped into the output # Test that the module is mapped into the output
# We might change the attribute name in the future # We might change the attribute name in the future
expr = res.servicesEval.config.mappedServices ? "<clan-core>-simple-module"; expr = res.config._services.mappedServices ? "<clan-core>-simple-module";
expected = true; expected = true;
inherit res; inherit res;
}; };
@@ -92,7 +82,7 @@ in
# All instances should be included within one evaluation to make all of them available # All instances should be included within one evaluation to make all of them available
test_module_grouping = test_module_grouping =
let let
res = callInventoryAdapter { res = createTestClan {
# Authored module # Authored module
# A minimal module looks like this # A minimal module looks like this
# It isn't exactly doing anything but it's a valid module that produces an output # It isn't exactly doing anything but it's a valid module that produces an output
@@ -112,18 +102,19 @@ in
perMachine = { }: { }; perMachine = { }: { };
}; };
# User config # User config
instances."instance_foo" = { inventory.instances."instance_foo" = {
module = { module = {
name = "A"; name = "A";
}; };
}; };
instances."instance_bar" = { inventory.instances."instance_bar" = {
module = { module = {
name = "B"; name = "B";
}; };
}; };
instances."instance_baz" = { inventory.instances."instance_baz" = {
module = { module = {
name = "A"; name = "A";
}; };
@@ -133,16 +124,16 @@ in
{ {
# Test that the module is mapped into the output # Test that the module is mapped into the output
# We might change the attribute name in the future # We might change the attribute name in the future
expr = lib.mapAttrs (_n: v: builtins.length v) res.grouped; expr = lib.attrNames res.config._services.mappedServices;
expected = { expected = [
"<clan-core>-A" = 2; "<clan-core>-A"
"<clan-core>-B" = 1; "<clan-core>-B"
}; ];
}; };
test_creates_all_instances = test_creates_all_instances =
let let
res = callInventoryAdapter { res = createTestClan {
# Authored module # Authored module
# A minimal module looks like this # A minimal module looks like this
# It isn't exactly doing anything but it's a valid module that produces an output # It isn't exactly doing anything but it's a valid module that produces an output
@@ -154,6 +145,7 @@ in
perMachine = { }: { }; perMachine = { }: { };
}; };
inventory = {
instances."instance_foo" = { instances."instance_foo" = {
module = { module = {
name = "A"; name = "A";
@@ -173,11 +165,12 @@ in
}; };
}; };
}; };
};
in in
{ {
# Test that the module is mapped into the output # Test that the module is mapped into the output
# We might change the attribute name in the future # We might change the attribute name in the future
expr = lib.attrNames res.servicesEval.config.mappedServices.self-A.instances; expr = lib.attrNames res.config._services.mappedServices.self-A.instances;
expected = [ expected = [
"instance_bar" "instance_bar"
"instance_foo" "instance_foo"
@@ -187,7 +180,7 @@ in
# Membership via roles # Membership via roles
test_add_machines_directly = test_add_machines_directly =
let let
res = callInventoryAdapter { res = createTestClan {
# Authored module # Authored module
# A minimal module looks like this # A minimal module looks like this
# It isn't exactly doing anything but it's a valid module that produces an output # It isn't exactly doing anything but it's a valid module that produces an output
@@ -202,6 +195,7 @@ in
# perMachine = {}: {}; # perMachine = {}: {};
}; };
inventory = {
machines = { machines = {
jon = { }; jon = { };
sara = { }; sara = { };
@@ -229,11 +223,12 @@ in
roles.peer.tags.all = { }; roles.peer.tags.all = { };
}; };
}; };
};
in in
{ {
# Test that the module is mapped into the output # Test that the module is mapped into the output
# We might change the attribute name in the future # We might change the attribute name in the future
expr = lib.attrNames res.servicesEval.config.mappedServices.self-A.result.allMachines; expr = lib.attrNames res.config._services.mappedServices.self-A.result.allMachines;
expected = [ expected = [
"jon" "jon"
"sara" "sara"
@@ -243,7 +238,7 @@ in
# Membership via tags # Membership via tags
test_add_machines_via_tags = test_add_machines_via_tags =
let let
res = callInventoryAdapter { res = createTestClan {
# Authored module # Authored module
# A minimal module looks like this # A minimal module looks like this
# It isn't exactly doing anything but it's a valid module that produces an output # It isn't exactly doing anything but it's a valid module that produces an output
@@ -257,6 +252,7 @@ in
# perMachine = {}: {}; # perMachine = {}: {};
}; };
inventory = {
machines = { machines = {
jon = { jon = {
tags = [ "foo" ]; tags = [ "foo" ];
@@ -281,11 +277,12 @@ in
roles.peer.tags.all = { }; roles.peer.tags.all = { };
}; };
}; };
};
in in
{ {
# Test that the module is mapped into the output # Test that the module is mapped into the output
# We might change the attribute name in the future # We might change the attribute name in the future
expr = lib.attrNames res.servicesEval.config.mappedServices.self-A.result.allMachines; expr = lib.attrNames res.config._services.mappedServices.self-A.result.allMachines;
expected = [ expected = [
"jon" "jon"
"sara" "sara"
@@ -293,6 +290,9 @@ in
}; };
machine_imports = import ./machine_imports.nix { inherit lib clanLib; }; machine_imports = import ./machine_imports.nix { inherit lib clanLib; };
per_machine_args = import ./per_machine_args.nix { inherit lib callInventoryAdapter; }; per_machine_args = import ./per_machine_args.nix { inherit lib createTestClan; };
per_instance_args = import ./per_instance_args.nix { inherit lib callInventoryAdapter; }; per_instance_args = import ./per_instance_args.nix {
inherit lib;
callInventoryAdapter = createTestClan;
};
} }

View File

@@ -1,4 +1,4 @@
{ callInventoryAdapter, ... }: { createTestClan, ... }:
let let
# Authored module # Authored module
# A minimal module looks like this # A minimal module looks like this
@@ -23,12 +23,15 @@ let
resolve = resolve =
spec: spec:
callInventoryAdapter { createTestClan {
inherit modules machines; inherit modules;
inventory = {
inherit machines;
instances."instance_foo" = { instances."instance_foo" = {
module = spec; module = spec;
}; };
}; };
};
in in
{ {
test_import_local_module_by_name = { test_import_local_module_by_name = {
@@ -36,25 +39,16 @@ in
(resolve { (resolve {
name = "A"; name = "A";
input = "self"; input = "self";
}).importedModuleWithInstances.instance_foo.resolvedModule; }).config._services.mappedServices.self-A.manifest.name;
expected = { expected = "network";
_class = "clan.service";
manifest = {
name = "network";
};
};
}; };
test_import_remote_module_by_name = { test_import_remote_module_by_name = {
expr = expr =
(resolve { (resolve {
name = "uzzi"; name = "uzzi";
input = "upstream"; input = "upstream";
}).importedModuleWithInstances.instance_foo.resolvedModule; }).config._services.mappedServices.upstream-uzzi.manifest.name;
expected = { expected = "uzzi-from-upstream";
_class = "clan.service";
manifest = {
name = "uzzi-from-upstream";
};
};
}; };
} }

View File

@@ -58,7 +58,10 @@ let
sara = { }; sara = { };
}; };
res = callInventoryAdapter { res = callInventoryAdapter {
inherit modules machines; inherit modules;
inventory = {
inherit machines;
instances."instance_foo" = { instances."instance_foo" = {
module = { module = {
name = "A"; name = "A";
@@ -93,6 +96,7 @@ let
roles.peer.tags.all = { }; roles.peer.tags.all = { };
}; };
}; };
};
/* /*
1 { imports = [ { instanceName = "instance_foo"; machine = { name = "jon"; roles = [ "controller" "pe 1 null 1 { imports = [ { instanceName = "instance_foo"; machine = { name = "jon"; roles = [ "controller" "pe 1 null
@@ -105,9 +109,10 @@ in
{ {
# settings should evaluate # settings should evaluate
test_per_instance_arguments = { test_per_instance_arguments = {
inherit res;
expr = { expr = {
instanceName = instanceName =
res.servicesEval.config.mappedServices.self-A.result.allRoles.peer.allInstances."instance_foo".allMachines.jon.passthru.instanceName; res.config._services.mappedServices.self-A.result.allRoles.peer.allInstances."instance_foo".allMachines.jon.passthru.instanceName;
# settings are specific. # settings are specific.
# Below we access: # Below we access:
@@ -115,11 +120,11 @@ in
# roles = peer # roles = peer
# machines = jon # machines = jon
settings = settings =
res.servicesEval.config.mappedServices.self-A.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.passthru.settings; res.config._services.mappedServices.self-A.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.passthru.settings;
machine = machine =
res.servicesEval.config.mappedServices.self-A.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.passthru.machine; res.config._services.mappedServices.self-A.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.passthru.machine;
roles = roles =
res.servicesEval.config.mappedServices.self-A.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.passthru.roles; res.config._services.mappedServices.self-A.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.passthru.roles;
}; };
expected = { expected = {
instanceName = "instance_foo"; instanceName = "instance_foo";
@@ -160,9 +165,9 @@ in
# TODO: Cannot be tested like this anymore # TODO: Cannot be tested like this anymore
test_per_instance_settings_vendoring = { test_per_instance_settings_vendoring = {
x = res.servicesEval.config.mappedServices.self-A; x = res.config._services.mappedServices.self-A;
expr = expr =
res.servicesEval.config.mappedServices.self-A.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.passthru.vendoredSettings; res.config._services.mappedServices.self-A.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.passthru.vendoredSettings;
expected = { expected = {
timeout = "config.thing"; timeout = "config.thing";
}; };

View File

@@ -1,4 +1,4 @@
{ lib, callInventoryAdapter }: { lib, createTestClan }:
let let
# Authored module # Authored module
# A minimal module looks like this # A minimal module looks like this
@@ -39,8 +39,11 @@ let
jon = { }; jon = { };
sara = { }; sara = { };
}; };
res = callInventoryAdapter { res = createTestClan {
inherit modules machines; inherit modules;
inventory = {
inherit machines;
instances."instance_foo" = { instances."instance_foo" = {
module = { module = {
name = "A"; name = "A";
@@ -70,6 +73,7 @@ let
roles.peer.tags.all = { }; roles.peer.tags.all = { };
}; };
}; };
};
in in
{ {
@@ -79,7 +83,7 @@ in
inherit res; inherit res;
expr = { expr = {
hasMachineSettings = hasMachineSettings =
res.servicesEval.config.mappedServices.self-A.result.allMachines.jon.passthru.instances.instance_foo.roles.peer.machines.jon res.config._services.mappedServices.self-A.result.allMachines.jon.passthru.instances.instance_foo.roles.peer.machines.jon
? settings; ? settings;
# settings are specific. # settings are specific.
@@ -88,10 +92,10 @@ in
# roles = peer # roles = peer
# machines = jon # machines = jon
specificMachineSettings = specificMachineSettings =
res.servicesEval.config.mappedServices.self-A.result.allMachines.jon.passthru.instances.instance_foo.roles.peer.machines.jon.settings; res.config._services.mappedServices.self-A.result.allMachines.jon.passthru.instances.instance_foo.roles.peer.machines.jon.settings;
hasRoleSettings = hasRoleSettings =
res.servicesEval.config.mappedServices.self-A.result.allMachines.jon.passthru.instances.instance_foo.roles.peer res.config._services.mappedServices.self-A.result.allMachines.jon.passthru.instances.instance_foo.roles.peer
? settings; ? settings;
# settings are specific. # settings are specific.
@@ -100,7 +104,7 @@ in
# roles = peer # roles = peer
# machines = * # machines = *
specificRoleSettings = specificRoleSettings =
res.servicesEval.config.mappedServices.self-A.result.allMachines.jon.passthru.instances.instance_foo.roles.peer; res.config._services.mappedServices.self-A.result.allMachines.jon.passthru.instances.instance_foo.roles.peer;
}; };
expected = { expected = {
hasMachineSettings = true; hasMachineSettings = true;

View File

@@ -1,6 +1,6 @@
{ callInventoryAdapter, lib, ... }: { createTestClan, lib, ... }:
let let
res = callInventoryAdapter { res = createTestClan {
modules."A" = { modules."A" = {
_class = "clan.service"; _class = "clan.service";
manifest = { manifest = {
@@ -21,6 +21,8 @@ let
}; };
}; };
}; };
inventory = {
machines = { machines = {
jon = { }; jon = { };
sara = { }; sara = { };
@@ -41,8 +43,9 @@ let
roles.peer.machines.sara = { }; roles.peer.machines.sara = { };
}; };
}; };
};
config = res.servicesEval.config.mappedServices.self-A; config = res.config._services.mappedServices.self-A;
# #
applySettings = applySettings =

View File

@@ -1,6 +1,6 @@
{ callInventoryAdapter, lib, ... }: { createTestClan, lib, ... }:
let let
res = callInventoryAdapter { res = createTestClan {
modules."A" = m: { modules."A" = m: {
_class = "clan.service"; _class = "clan.service";
config = { config = {
@@ -14,6 +14,7 @@ let
default = m; default = m;
}; };
}; };
inventory = {
machines = { machines = {
jon = { }; jon = { };
}; };
@@ -25,8 +26,9 @@ let
roles.peer.machines.jon = { }; roles.peer.machines.jon = { };
}; };
}; };
};
specialArgs = lib.attrNames res.servicesEval.config.mappedServices.self-A.test.specialArgs; specialArgs = lib.attrNames res.config._services.mappedServices.self-A.test.specialArgs;
in in
{ {
test_simple = { test_simple = {