Merge pull request 'refactor(clanServices): add example of eval test' (#3337) from hsjobeki/clan-core:clan-services-1 into main
Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3337
This commit is contained in:
@@ -1,8 +1,5 @@
|
||||
{ self, lib, ... }:
|
||||
{
|
||||
clan.inventory.modules = {
|
||||
zerotier-redux = lib.modules.importApply ./zerotier-redux/default.nix {
|
||||
inherit (self) packages;
|
||||
};
|
||||
};
|
||||
imports = [
|
||||
./hello-world/flake-module.nix
|
||||
];
|
||||
}
|
||||
|
||||
8
clanServices/hello-world/default.nix
Normal file
8
clanServices/hello-world/default.nix
Normal file
@@ -0,0 +1,8 @@
|
||||
{ packages }:
|
||||
{ ... }:
|
||||
{
|
||||
_class = "clan.service";
|
||||
manifest.name = "clan-core/hello-word";
|
||||
|
||||
roles.peer = { };
|
||||
}
|
||||
37
clanServices/hello-world/flake-module.nix
Normal file
37
clanServices/hello-world/flake-module.nix
Normal file
@@ -0,0 +1,37 @@
|
||||
{
|
||||
self,
|
||||
inputs,
|
||||
lib,
|
||||
...
|
||||
}:
|
||||
let
|
||||
module = lib.modules.importApply ./default.nix {
|
||||
inherit (self) packages;
|
||||
};
|
||||
in
|
||||
{
|
||||
clan.inventory.modules = {
|
||||
hello-world = module;
|
||||
};
|
||||
perSystem =
|
||||
{ ... }:
|
||||
let
|
||||
# Module that contains the tests
|
||||
# This module adds:
|
||||
# - legacyPackages.<system>.eval-tests-hello-world
|
||||
# - checks.<system>.eval-tests-hello-world
|
||||
unit-test-module = (
|
||||
self.clanLib.test.flakeModules.makeEvalChecks {
|
||||
inherit module;
|
||||
inherit self inputs;
|
||||
testName = "hello-world";
|
||||
tests = ./tests/eval-tests.nix;
|
||||
# Optional arguments passed to the test
|
||||
testArgs = { };
|
||||
}
|
||||
);
|
||||
in
|
||||
{
|
||||
imports = [ unit-test-module ];
|
||||
};
|
||||
}
|
||||
53
clanServices/hello-world/tests/eval-tests.nix
Normal file
53
clanServices/hello-world/tests/eval-tests.nix
Normal file
@@ -0,0 +1,53 @@
|
||||
{
|
||||
module,
|
||||
clanLib,
|
||||
...
|
||||
}:
|
||||
let
|
||||
testFlake = clanLib.buildClan {
|
||||
# Point to the folder of the module
|
||||
# TODO: make this optional in buildClan
|
||||
directory = ./..;
|
||||
|
||||
# Create some test machines
|
||||
machines.jon = {
|
||||
nixpkgs.hostPlatform = "x86_64-linux";
|
||||
};
|
||||
machines.sara = {
|
||||
nixpkgs.hostPlatform = "x86_64-linux";
|
||||
};
|
||||
|
||||
# Register the module for the test
|
||||
inventory.modules.hello-world = module;
|
||||
|
||||
# Use the module in the test
|
||||
inventory.instances = {
|
||||
"hello" = {
|
||||
module.name = "hello-world";
|
||||
|
||||
roles.peer.machines.jon = { };
|
||||
};
|
||||
};
|
||||
};
|
||||
# NOTE:
|
||||
# If you wonder why 'self-zerotier-redux':
|
||||
# A local module has prefix 'self', otherwise it is the name of the 'input'
|
||||
# The rest is the name of the service as in the instance 'module.name';
|
||||
#
|
||||
# -> ${module.input}-${module.name}
|
||||
# In this case it is 'self-zerotier-redux'
|
||||
# This is usually only used internally, but we can use it to test the evaluation of service module in isolation
|
||||
# evaluatedService =
|
||||
# testFlake.clanInternals.inventoryClass.distributedServices.importedModulesEvaluated.self-zerotier-redux.config;
|
||||
in
|
||||
{
|
||||
test_simple = {
|
||||
inherit testFlake;
|
||||
|
||||
expr = {
|
||||
};
|
||||
expected = {
|
||||
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
{ packages }:
|
||||
{ ... }:
|
||||
{
|
||||
_class = "clan.service";
|
||||
manifest.name = "clan-core/zerotier";
|
||||
|
||||
# TODO: Migrate the behavior from nixosModules/clanCore/zerotier
|
||||
# Expose a flag, to disable the clanCore/zerotier module if this module is used
|
||||
# To ensure conflict free behavior
|
||||
roles.moon = { };
|
||||
roles.peer = { };
|
||||
roles.controller = { };
|
||||
}
|
||||
@@ -6,6 +6,9 @@ let
|
||||
;
|
||||
in
|
||||
{
|
||||
flakeModules = clanLib.callLib ./flakeModules.nix { };
|
||||
|
||||
#
|
||||
minifyModule = ./minify.nix;
|
||||
sopsModule = ./sops.nix;
|
||||
# A function that returns an extension to runTest
|
||||
|
||||
56
lib/test/flakeModules.nix
Normal file
56
lib/test/flakeModules.nix
Normal file
@@ -0,0 +1,56 @@
|
||||
{ lib, clanLib }:
|
||||
{
|
||||
|
||||
/**
|
||||
This function is used to create a module that can be imported into flake-parts
|
||||
and used to run eval tests.
|
||||
|
||||
Usage: `lib.modules.importApply makeEvalChecks { module = module-under-test; testArgs = { }; }`
|
||||
|
||||
returns a module to be imported into flake-parts
|
||||
|
||||
Which in turn adds to the flake:
|
||||
|
||||
- legacyPackages.<system>.eval-tests-<testName>: The attribute set passed to nix-unit. (Exposed for debugging i.e. via nix repl).
|
||||
- checks.<system>.eval-tests-<testName>: A derivation that can be built and fails if nix-unit fails
|
||||
*/
|
||||
makeEvalChecks =
|
||||
{
|
||||
self,
|
||||
inputs,
|
||||
testName,
|
||||
tests,
|
||||
module,
|
||||
testArgs ? { },
|
||||
}:
|
||||
let
|
||||
inputOverrides = builtins.concatStringsSep " " (
|
||||
builtins.map (input: " --override-input ${input} ${inputs.${input}}") (builtins.attrNames inputs)
|
||||
);
|
||||
attrName = "eval-tests-${testName}";
|
||||
in
|
||||
{
|
||||
pkgs,
|
||||
system,
|
||||
...
|
||||
}:
|
||||
{
|
||||
legacyPackages.${attrName} = import tests (
|
||||
{
|
||||
inherit clanLib lib module;
|
||||
}
|
||||
// testArgs
|
||||
);
|
||||
checks.${attrName} = pkgs.runCommand "tests" { nativeBuildInputs = [ pkgs.nix-unit ]; } ''
|
||||
export HOME="$(realpath .)"
|
||||
|
||||
nix-unit --eval-store "$HOME" \
|
||||
--extra-experimental-features flakes \
|
||||
--show-trace \
|
||||
${inputOverrides} \
|
||||
--flake ${self}#legacyPackages.${system}.${attrName}
|
||||
touch $out
|
||||
'';
|
||||
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user