Test modules and defaults¶
A test is typically defined as an attribute set (See also the minimal example).
Outside of nixpkgs, the pkgs.testers.runNixOSTest function calls either an attribute set directly or a file that contains such an attribute set.
Inside the test attribute set, the rules of the NixOS module system apply.
Defaults¶
The top-level attribute set defaults (see also official NixOS manual) accepts a NixOS configuration snippet and applies it to all nodes.
Let's imagine a test with many nodes where it is desirable that they all share some common configuration, like a disabled firewall.
Instead of setting networking.firewall.enable = false; in every node configuration, we can set it in defaults:
{
name = "some test";
nodes = {
machine1 = { };
machine2 = { };
machine3 = { };
};
defaults = {
networking.firewall.enable = false;
};
testScript = ''
# some testing
'';
}
Everything that is set in defaults will be merged into every node's configuration.
If you are running a hybrid test, you can narrow this scope using containerDefaults and nodeDefaults. Configurations placed in these attributes will ignore the other type entirely, applying only across all containers or all VMs respectively.
Imports composition¶
A test module can import another test module:
{
name = "some test";
nodes.machine = {
# some configuration
networking.firewall.enable = true; # this is also the NixOS default
};
testScript = ''
# some testing
'';
}
Whatever this test does, it works with the firewall being enabled in the test VM.
To run the same test but with disabled firewall, we can write the following:
{
imports = [
./test-generic.nix
];
nodes.machine = { lib, ... }: {
networking.firewall.enable = lib.mkForce false;
};
}
Running pkgs.testers.runNixOSTest test-without-firewall.nix runs essentially the same test, but with a disabled firewall.