tests: test vars generation for all clan service tests
This commit is contained in:
@@ -1,4 +1,7 @@
|
|||||||
{ lib, clanLib }:
|
{
|
||||||
|
lib,
|
||||||
|
clanLib,
|
||||||
|
}:
|
||||||
let
|
let
|
||||||
inherit (lib)
|
inherit (lib)
|
||||||
mkOption
|
mkOption
|
||||||
@@ -16,6 +19,7 @@ in
|
|||||||
minifyModule = ./minify.nix;
|
minifyModule = ./minify.nix;
|
||||||
sopsModule = ./sops.nix;
|
sopsModule = ./sops.nix;
|
||||||
# A function that returns an extension to runTest
|
# A function that returns an extension to runTest
|
||||||
|
# TODO: remove this from clanLib, add to legacyPackages, simplify signature
|
||||||
makeTestClan =
|
makeTestClan =
|
||||||
{
|
{
|
||||||
nixosTest,
|
nixosTest,
|
||||||
@@ -23,13 +27,64 @@ in
|
|||||||
self,
|
self,
|
||||||
useContainers ? true,
|
useContainers ? true,
|
||||||
# Displayed for better error messages, otherwise the placeholder
|
# Displayed for better error messages, otherwise the placeholder
|
||||||
system ? "<system>",
|
|
||||||
attrName ? "<check_name>",
|
attrName ? "<check_name>",
|
||||||
...
|
...
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
nixos-lib = import (pkgs.path + "/nixos/lib") { };
|
nixos-lib = import (pkgs.path + "/nixos/lib") { };
|
||||||
in
|
|
||||||
|
testName = test.config.name;
|
||||||
|
|
||||||
|
update-vars-script = "${self.packages.${pkgs.system}.generate-test-vars}/bin/generate-test-vars";
|
||||||
|
|
||||||
|
update-vars = pkgs.writeShellScriptBin "update-vars" ''
|
||||||
|
${update-vars-script} $PRJ_ROOT/checks/${testName} ${testName}
|
||||||
|
'';
|
||||||
|
|
||||||
|
testSrc = lib.cleanSource (self + "/checks/${testName}");
|
||||||
|
|
||||||
|
vars-check =
|
||||||
|
pkgs.runCommand "update-vars-check"
|
||||||
|
{
|
||||||
|
nativeBuildInputs = [
|
||||||
|
pkgs.nix
|
||||||
|
pkgs.git
|
||||||
|
pkgs.age
|
||||||
|
pkgs.sops
|
||||||
|
pkgs.bubblewrap
|
||||||
|
];
|
||||||
|
closureInfo = pkgs.closureInfo {
|
||||||
|
rootPaths = [
|
||||||
|
pkgs.bash
|
||||||
|
pkgs.coreutils
|
||||||
|
pkgs.jq.dev
|
||||||
|
pkgs.stdenv
|
||||||
|
pkgs.stdenvNoCC
|
||||||
|
pkgs.shellcheck-minimal
|
||||||
|
pkgs.age
|
||||||
|
pkgs.sops
|
||||||
|
];
|
||||||
|
};
|
||||||
|
}
|
||||||
|
''
|
||||||
|
# make the test depend on its vars-check derivation
|
||||||
|
echo ${vars-check} >/dev/null
|
||||||
|
|
||||||
|
${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}
|
||||||
|
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 (
|
(nixos-lib.runTest (
|
||||||
{ config, ... }:
|
{ config, ... }:
|
||||||
let
|
let
|
||||||
@@ -66,7 +121,7 @@ in
|
|||||||
{
|
{
|
||||||
_prefix = [
|
_prefix = [
|
||||||
"checks"
|
"checks"
|
||||||
system
|
"<system>"
|
||||||
attrName
|
attrName
|
||||||
"config"
|
"config"
|
||||||
"clan"
|
"clan"
|
||||||
@@ -138,4 +193,9 @@ in
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
)).config.result;
|
)).config.result;
|
||||||
|
in
|
||||||
|
test
|
||||||
|
// {
|
||||||
|
inherit update-vars vars-check;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,10 +10,11 @@
|
|||||||
stdenv,
|
stdenv,
|
||||||
# custom args
|
# custom args
|
||||||
clan-core-path,
|
clan-core-path,
|
||||||
nixpkgs,
|
|
||||||
nix-select,
|
|
||||||
includedRuntimeDeps,
|
includedRuntimeDeps,
|
||||||
|
nix-select,
|
||||||
|
nixpkgs,
|
||||||
pythonRuntime,
|
pythonRuntime,
|
||||||
|
setupNixInNix,
|
||||||
templateDerivation,
|
templateDerivation,
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
@@ -211,17 +212,10 @@ pythonRuntime.pkgs.buildPythonApplication {
|
|||||||
chmod +w -R ./src
|
chmod +w -R ./src
|
||||||
cd ./src
|
cd ./src
|
||||||
|
|
||||||
|
${setupNixInNix}
|
||||||
|
|
||||||
export CLAN_CORE_PATH=${clan-core-path}
|
export CLAN_CORE_PATH=${clan-core-path}
|
||||||
export NIX_STATE_DIR=$TMPDIR/nix
|
|
||||||
export IN_NIX_SANDBOX=1
|
|
||||||
export PYTHONWARNINGS=error
|
export PYTHONWARNINGS=error
|
||||||
export CLAN_TEST_STORE=$TMPDIR/store
|
|
||||||
# required to prevent concurrent 'nix flake lock' operations
|
|
||||||
export LOCK_NIX=$TMPDIR/nix_lock
|
|
||||||
mkdir -p "$CLAN_TEST_STORE/nix/store"
|
|
||||||
mkdir -p "$CLAN_TEST_STORE/nix/var/nix/gcroots"
|
|
||||||
xargs cp --recursive --target "$CLAN_TEST_STORE/nix/store" < "$closureInfo/store-paths"
|
|
||||||
nix-store --load-db --store "$CLAN_TEST_STORE" < "$closureInfo/registration"
|
|
||||||
|
|
||||||
# limit build cores to 16
|
# limit build cores to 16
|
||||||
jobs="$((NIX_BUILD_CORES>16 ? 16 : NIX_BUILD_CORES))"
|
jobs="$((NIX_BUILD_CORES>16 ? 16 : NIX_BUILD_CORES))"
|
||||||
@@ -264,17 +258,10 @@ pythonRuntime.pkgs.buildPythonApplication {
|
|||||||
chmod +w -R ./src
|
chmod +w -R ./src
|
||||||
cd ./src
|
cd ./src
|
||||||
|
|
||||||
|
${setupNixInNix}
|
||||||
|
|
||||||
export CLAN_CORE_PATH=${clan-core-path}
|
export CLAN_CORE_PATH=${clan-core-path}
|
||||||
export NIX_STATE_DIR=$TMPDIR/nix
|
|
||||||
export IN_NIX_SANDBOX=1
|
|
||||||
export PYTHONWARNINGS=error
|
export PYTHONWARNINGS=error
|
||||||
export CLAN_TEST_STORE=$TMPDIR/store
|
|
||||||
# required to prevent concurrent 'nix flake lock' operations
|
|
||||||
export LOCK_NIX=$TMPDIR/nix_lock
|
|
||||||
mkdir -p "$CLAN_TEST_STORE/nix/store"
|
|
||||||
mkdir -p "$CLAN_TEST_STORE/nix/var/nix/gcroots"
|
|
||||||
xargs cp --recursive --target "$CLAN_TEST_STORE/nix/store" < "$closureInfo/store-paths"
|
|
||||||
nix-store --load-db --store "$CLAN_TEST_STORE" < "$closureInfo/registration"
|
|
||||||
|
|
||||||
# used for tests without flakes
|
# used for tests without flakes
|
||||||
export NIXPKGS=${nixpkgs}
|
export NIXPKGS=${nixpkgs}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
{
|
{
|
||||||
self',
|
self',
|
||||||
pkgs,
|
pkgs,
|
||||||
|
system,
|
||||||
...
|
...
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
@@ -41,6 +42,7 @@
|
|||||||
packages = {
|
packages = {
|
||||||
clan-cli = pkgs.callPackage ./default.nix {
|
clan-cli = pkgs.callPackage ./default.nix {
|
||||||
inherit (inputs) nixpkgs nix-select;
|
inherit (inputs) nixpkgs nix-select;
|
||||||
|
inherit (self.legacyPackages.${system}) setupNixInNix;
|
||||||
templateDerivation = templateDerivation;
|
templateDerivation = templateDerivation;
|
||||||
pythonRuntime = pkgs.python3;
|
pythonRuntime = pkgs.python3;
|
||||||
clan-core-path = clanCoreWithVendoredDeps;
|
clan-core-path = clanCoreWithVendoredDeps;
|
||||||
@@ -51,6 +53,7 @@
|
|||||||
};
|
};
|
||||||
clan-cli-full = pkgs.callPackage ./default.nix {
|
clan-cli-full = pkgs.callPackage ./default.nix {
|
||||||
inherit (inputs) nixpkgs nix-select;
|
inherit (inputs) nixpkgs nix-select;
|
||||||
|
inherit (self.legacyPackages.${system}) setupNixInNix;
|
||||||
clan-core-path = clanCoreWithVendoredDeps;
|
clan-core-path = clanCoreWithVendoredDeps;
|
||||||
templateDerivation = templateDerivation;
|
templateDerivation = templateDerivation;
|
||||||
pythonRuntime = pkgs.python3;
|
pythonRuntime = pkgs.python3;
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
./generate-test-vars/flake-module.nix
|
./generate-test-vars/flake-module.nix
|
||||||
./clan-core-flake/flake-module.nix
|
./clan-core-flake/flake-module.nix
|
||||||
./clan-app/flake-module.nix
|
./clan-app/flake-module.nix
|
||||||
|
./testing/flake-module.nix
|
||||||
];
|
];
|
||||||
|
|
||||||
flake.packages.x86_64-linux =
|
flake.packages.x86_64-linux =
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ from clan_cli.vars.generate import generate_vars
|
|||||||
from clan_lib.dirs import find_git_repo_root
|
from clan_lib.dirs import find_git_repo_root
|
||||||
from clan_lib.flake.flake import Flake
|
from clan_lib.flake.flake import Flake
|
||||||
from clan_lib.machines.machines import Machine
|
from clan_lib.machines.machines import Machine
|
||||||
from clan_lib.nix import nix_config, nix_eval
|
from clan_lib.nix import nix_config, nix_eval, nix_test_store
|
||||||
|
|
||||||
sops_priv_key = (
|
sops_priv_key = (
|
||||||
"AGE-SECRET-KEY-1PL0M9CWRCG3PZ9DXRTTLMCVD57U6JDFE8K7DNVQ35F4JENZ6G3MQ0RQLRV"
|
"AGE-SECRET-KEY-1PL0M9CWRCG3PZ9DXRTTLMCVD57U6JDFE8K7DNVQ35F4JENZ6G3MQ0RQLRV"
|
||||||
@@ -26,11 +26,15 @@ def get_machine_names(repo_root: Path, check_attr: str) -> list[str]:
|
|||||||
"""
|
"""
|
||||||
Get the machine names from the test flake
|
Get the machine names from the test flake
|
||||||
"""
|
"""
|
||||||
|
nix_options = []
|
||||||
|
if tmp_store := nix_test_store():
|
||||||
|
nix_options += ["--store", str(tmp_store)]
|
||||||
cmd = nix_eval(
|
cmd = nix_eval(
|
||||||
[
|
[
|
||||||
f"git+file://{repo_root}#checks.{nix_config()['system']}.{check_attr}.nodes",
|
f"path://{repo_root}#checks.{nix_config()['system']}.{check_attr}.nodes",
|
||||||
"--apply",
|
"--apply",
|
||||||
"builtins.attrNames",
|
"builtins.attrNames",
|
||||||
|
*nix_options,
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
out = subprocess.run(cmd, check=True, text=True, stdout=subprocess.PIPE)
|
out = subprocess.run(cmd, check=True, text=True, stdout=subprocess.PIPE)
|
||||||
@@ -93,7 +97,7 @@ def parse_args() -> Options:
|
|||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--repo_root",
|
"--repo-root",
|
||||||
type=Path,
|
type=Path,
|
||||||
help="""
|
help="""
|
||||||
Should be an absolute path to the repo root.
|
Should be an absolute path to the repo root.
|
||||||
@@ -127,7 +131,7 @@ def parse_args() -> Options:
|
|||||||
def main() -> None:
|
def main() -> None:
|
||||||
os.environ["CLAN_NO_COMMIT"] = "1"
|
os.environ["CLAN_NO_COMMIT"] = "1"
|
||||||
opts = parse_args()
|
opts = parse_args()
|
||||||
test_dir = opts.repo_root / opts.test_dir
|
test_dir = opts.test_dir
|
||||||
|
|
||||||
shutil.rmtree(test_dir / "vars", ignore_errors=True)
|
shutil.rmtree(test_dir / "vars", ignore_errors=True)
|
||||||
shutil.rmtree(test_dir / "sops", ignore_errors=True)
|
shutil.rmtree(test_dir / "sops", ignore_errors=True)
|
||||||
|
|||||||
18
pkgs/testing/flake-module.nix
Normal file
18
pkgs/testing/flake-module.nix
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
perSystem = {
|
||||||
|
legacyPackages.setupNixInNix = ''
|
||||||
|
export HOME=$TMPDIR
|
||||||
|
export NIX_STATE_DIR=$TMPDIR/nix
|
||||||
|
export IN_NIX_SANDBOX=1
|
||||||
|
export CLAN_TEST_STORE=$TMPDIR/store
|
||||||
|
# required to prevent concurrent 'nix flake lock' operations
|
||||||
|
export LOCK_NIX=$TMPDIR/nix_lock
|
||||||
|
mkdir -p "$CLAN_TEST_STORE/nix/store"
|
||||||
|
mkdir -p "$CLAN_TEST_STORE/nix/var/nix/gcroots"
|
||||||
|
if [[ -n "''${closureInfo-}" ]]; then
|
||||||
|
xargs cp --recursive --target "$CLAN_TEST_STORE/nix/store" < "$closureInfo/store-paths"
|
||||||
|
nix-store --load-db --store "$CLAN_TEST_STORE" < "$closureInfo/registration"
|
||||||
|
fi
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user