add nixops4 data model test

This commit is contained in:
Kiara Grouwstra 2025-08-27 20:40:49 +02:00
parent b134464c6e
commit 2ca18eabe8
Signed by: kiara
SSH key fingerprint: SHA256:COspvLoLJ5WC5rFb9ZDe5urVCkK4LJZOsjfF4duRJFU
7 changed files with 194 additions and 113 deletions

View file

@ -163,7 +163,7 @@ in
'')} '')}
${ ${
if config.useFlake then if true then
'' ''
## NOTE: This is super slow. It could probably be optimised in Nix, for ## NOTE: This is super slow. It could probably be optimised in Nix, for
## instance by allowing to grab things directly from the host's store. ## instance by allowing to grab things directly from the host's store.

View file

@ -1,7 +1,9 @@
{ {
targetMachines = [ targetMachines = [
"hello" "ssh"
"nixops4"
]; ];
pathToRoot = ../../..; pathToRoot = ../../..;
pathFromRoot = ./.; pathFromRoot = ./.;
enableAcme = true;
} }

View file

@ -12,5 +12,10 @@ runNixOSTest {
./nixosTest.nix ./nixosTest.nix
]; ];
_module.args = { inherit inputs sources; }; _module.args = { inherit inputs sources; };
inherit (import ./constants.nix) targetMachines pathToRoot pathFromRoot; inherit (import ./constants.nix)
targetMachines
pathToRoot
pathFromRoot
enableAcme
;
} }

View file

@ -12,11 +12,12 @@ let
inherit (pkgs) lib; inherit (pkgs) lib;
deployment-config = config; deployment-config = config;
inherit (lib) mkOption types; inherit (lib) mkOption types;
inherit (import ./constants.nix) targetMachines pathToRoot pathFromRoot;
eval = eval =
module: module:
(lib.evalModules { (lib.evalModules {
specialArgs = { specialArgs = {
inherit inputs; inherit pkgs inputs;
}; };
modules = [ modules = [
module module
@ -98,17 +99,10 @@ let
}; };
}; };
}; };
environments.single-nixos-vm = environment: { environments =
resources."operator-environment".login-shell.username = "operator"; let
implementation = requests: { mkNixosConfiguration =
input = requests; environment: requests:
output.ssh-host = {
ssh = {
username = "root";
inherit (deployment-config) host;
key-file = null;
};
nixos-configuration =
{ ... }: { ... }:
{ {
imports = [ imports = [
@ -118,13 +112,6 @@ let
"${nixpkgs}/nixos/modules/profiles/qemu-guest.nix" "${nixpkgs}/nixos/modules/profiles/qemu-guest.nix"
]; ];
inherit (deployment-config) enableAcme;
acmeNodeIP =
if deployment-config.enableAcme then
deployment-config.nodes.acme.networking.primaryIPAddress
else
null;
users.users = environment.config.resources."operator-environment".login-shell.apply { users.users = environment.config.resources."operator-environment".login-shell.apply {
resources = lib.filterAttrs (_name: value: value ? login-shell) ( resources = lib.filterAttrs (_name: value: value ? login-shell) (
lib.concatMapAttrs ( lib.concatMapAttrs (
@ -133,6 +120,43 @@ let
); );
}; };
}; };
in
{
single-nixos-vm-ssh = environment: {
resources."operator-environment".login-shell.username = "operator";
implementation = requests: {
input = requests;
output.ssh-host = {
nixos-configuration = mkNixosConfiguration environment requests;
ssh = {
username = "root";
inherit (deployment-config) host;
key-file = null;
};
};
};
};
single-nixos-vm-nixops4 = environment: {
resources."operator-environment".login-shell.username = "operator";
implementation = requests: {
input = requests;
output.nixops4 =
{ providers, ... }:
{
providers = {
inherit (inputs.nixops4.modules.nixops4Provider) local;
};
resources = lib.genAttrs targetMachines (nodeName: {
type = providers.local.exec;
imports = [
inputs.nixops4-nixos.modules.nixops4Resource.nixos
../common/targetResource.nix
];
nixos.module = mkNixosConfiguration environment requests;
_module.args = { inherit inputs sources; };
inherit nodeName pathToRoot pathFromRoot;
});
};
}; };
}; };
}; };
@ -145,12 +169,24 @@ let
applications.hello.enable = true; applications.hello.enable = true;
}; };
}; };
"example-deployment" = mkOption { "ssh-deployment" =
type = config.environments.single-nixos-vm.resource-mapping.output-type; let
default = config.environments.single-nixos-vm.deployment config."example-configuration"; env = config.environments."single-nixos-vm-ssh";
in
mkOption {
type = env.resource-mapping.output-type;
default = env.deployment config."example-configuration";
};
"nixops4-deployment" =
let
env = config.environments."single-nixos-vm-nixops4";
in
mkOption {
type = env.resource-mapping.output-type;
default = env.deployment config."example-configuration";
}; };
}; };
} }
); );
in in
fediversity."example-deployment" fediversity

View file

@ -0,0 +1,26 @@
{
inputs = {
nixops4.follows = "nixops4-nixos/nixops4";
nixops4-nixos.url = "github:nixops4/nixops4-nixos";
};
outputs =
inputs:
import ./mkFlake.nix inputs (
{ inputs, ... }:
let
system = "x86_64-linux";
in
{
imports = [
inputs.nixops4.modules.flake.default
];
nixops4Deployments.check-deployment-model =
(import ./deployment/check/data-model/deployment.nix {
inherit system inputs;
config.host = "nixops4";
})."nixops4-deployment".nixops4;
}
);
}

View file

@ -2,11 +2,28 @@
lib, lib,
config, config,
pkgs, pkgs,
inputs,
... ...
}: }:
let let
inherit (import ./constants.nix) targetMachines pathToRoot; inherit (import ./constants.nix) pathToRoot;
escapedJson = v: lib.replaceStrings [ "\"" ] [ "\\\\\"" ] (lib.strings.toJSON v); escapedJson = v: lib.replaceStrings [ "\"" ] [ "\\\\\"" ] (lib.strings.toJSON v);
deployment-config = {
inherit (config) enableAcme;
acmeNodeIP = if config.enableAcme then config.nodes.acme.networking.primaryIPAddress else null;
host = "ssh";
};
inherit
((import ./deployment.nix {
inherit (pkgs) system;
inherit inputs;
config = deployment-config;
})."ssh-deployment".ssh-host.ssh
)
host
username
key-file
;
in in
{ {
_class = "nixosTest"; _class = "nixosTest";
@ -20,14 +37,24 @@ in
../../function.nix ../../function.nix
./constants.nix ./constants.nix
./deployment.nix ./deployment.nix
./options.nix
(config.pathToCwd + "/flake-under-test.nix")
]; ];
nodes.deployer = nodes.deployer =
{ pkgs, ... }: { pkgs, ... }:
{ {
environment.systemPackages = with pkgs; [ environment.systemPackages = with pkgs; [
inputs.nixops4.packages.${system}.default
jq jq
]; ];
# FIXME: sad times
system.extraDependencies = with pkgs; [
jq
jq.inputDerivation
];
system.extraDependenciesFromModule = system.extraDependenciesFromModule =
{ pkgs, ... }: { pkgs, ... }:
{ {
@ -38,31 +65,15 @@ in
}; };
extraTestScript = '' extraTestScript = ''
${lib.concatStringsSep "\n" ( with subtest("nixops4"):
lib.lists.map ( nixops4.fail("hello 1>&2")
nodeName: deployer.succeed("nixops4 apply check-deployment-model --show-trace --verbose --no-interactive 1>&2")
let nixops4.succeed("su - operator -c hello 1>&2")
deployment-config = {
inherit (config) enableAcme;
acmeNodeIP = if config.enableAcme then config.nodes.acme.networking.primaryIPAddress else null;
host = nodeName;
};
inherit
((import ./deployment.nix {
inherit (pkgs) system;
config = deployment-config;
}).ssh-host.ssh
)
host
username
key-file
;
in
''
with subtest("Check the status before deployment"):
${nodeName}.fail("${nodeName} 1>&2")
with subtest("Run the deployment for ${nodeName}"): with subtest("ssh: Check the status before deployment"):
ssh.fail("hello 1>&2")
with subtest("ssh: Run the deployment"):
deployer.succeed(""" deployer.succeed("""
set -euo pipefail set -euo pipefail
@ -78,7 +89,7 @@ in
inherit system; inherit system;
config = builtins.fromJSON "${escapedJson deployment-config}"; config = builtins.fromJSON "${escapedJson deployment-config}";
} }
).ssh-host.nixos-configuration; )."ssh-deployment".ssh-host.nixos-configuration;
} }
') ')
# DEPLOY # DEPLOY
@ -98,17 +109,14 @@ in
# switch the remote host to the config # switch the remote host to the config
output=$(ssh "''${sshOpts[@]}" "$host" "nix-env --profile /nix/var/nix/profiles/system --set $outPath; nohup $outPath/bin/switch-to-configuration switch &" 2>&1) || echo "status code: $?" output=$(ssh "''${sshOpts[@]}" "$host" "nix-env --profile /nix/var/nix/profiles/system --set $outPath; nohup $outPath/bin/switch-to-configuration switch &" 2>&1) || echo "status code: $?"
echo "output: $output" echo "output: $output"
if [[ $output != *"Timeout, server ${nodeName} not responding"* ]]; then if [[ $output != *"Timeout, server ssh not responding"* ]]; then
echo "non-timeout error: $output" echo "non-timeout error: $output"
exit 1 exit 1
else else
exit 0 exit 0
fi fi
""") """)
${nodeName}.wait_for_unit("multi-user.target") ssh.wait_for_unit("multi-user.target")
${nodeName}.succeed("su - operator -c ${nodeName} 1>&2") ssh.succeed("su - operator -c hello 1>&2")
''
) targetMachines
)}
''; '';
} }

View file

@ -159,6 +159,10 @@ let
description = "A NixOps4 NixOS deployment. For an example, see https://github.com/nixops4/nixops4-nixos/blob/main/example/deployment.nix."; description = "A NixOps4 NixOS deployment. For an example, see https://github.com/nixops4/nixops4-nixos/blob/main/example/deployment.nix.";
type = nixops4Deployment; type = nixops4Deployment;
}; };
nixops4 = mkOption {
description = "A NixOps4 NixOS deployment. For an example, see https://github.com/nixops4/nixops4-nixos/blob/main/example/deployment.nix.";
type = nixops4Deployment;
};
}; };
in in
{ {