2021-07-03 03:11:41 +00:00
# Writing Tests {#sec-writing-nixos-tests}
2022-09-30 11:47:45 +00:00
A NixOS test is a module that has the following structure:
2021-07-03 03:11:41 +00:00
```nix
2022-09-30 11:47:45 +00:00
{
2021-07-03 03:11:41 +00:00
2022-04-03 18:54:34 +00:00
# One or more machines:
2021-07-03 03:11:41 +00:00
nodes =
2022-04-03 18:54:34 +00:00
{ machine =
2024-04-21 15:54:59 +00:00
{ config, pkgs, ... }: { /* ... */ };
2021-07-03 03:11:41 +00:00
machine2 =
2024-04-21 15:54:59 +00:00
{ config, pkgs, ... }: { /* ... */ };
# …
2021-07-03 03:11:41 +00:00
};
testScript =
''
Python code…
'';
}
```
2022-09-30 11:47:45 +00:00
We refer to the whole test above as a test module, whereas the values
2022-10-06 18:32:54 +00:00
in [`nodes.<name>` ](#test-opt-nodes ) are NixOS modules themselves.
2022-09-30 11:47:45 +00:00
2022-10-06 18:32:54 +00:00
The option [`testScript` ](#test-opt-testScript ) is a piece of Python code that executes the
2021-07-03 03:11:41 +00:00
test (described below). During the test, it will start one or more
2022-04-03 18:54:34 +00:00
virtual machines, the configuration of which is described by
2022-10-06 18:32:54 +00:00
the option [`nodes` ](#test-opt-nodes ).
2022-04-03 18:54:34 +00:00
An example of a single-node test is
[`login.nix` ](https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/login.nix ).
It only needs a single machine to test whether users can log in
2021-07-03 03:11:41 +00:00
on the virtual console, whether device ownership is correctly maintained
2022-04-03 18:54:34 +00:00
when switching between consoles, and so on. An interesting multi-node test is
[`nfs/simple.nix` ](https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/nfs/simple.nix ).
It uses two client nodes to test correct locking across server crashes.
2021-07-03 03:11:41 +00:00
2022-09-30 11:47:45 +00:00
## Calling a test {#sec-calling-nixos-tests}
Tests are invoked differently depending on whether the test is part of NixOS or lives in a different project.
### Testing within NixOS {#sec-call-nixos-test-in-nixos}
Tests that are part of NixOS are added to [`nixos/tests/all-tests.nix` ](https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/all-tests.nix ).
```nix
2024-04-21 15:54:59 +00:00
{
2022-09-30 11:47:45 +00:00
hostname = runTest ./hostname.nix;
2024-04-21 15:54:59 +00:00
}
2022-09-30 11:47:45 +00:00
```
Overrides can be added by defining an anonymous module in `all-tests.nix` .
```nix
2024-04-21 15:54:59 +00:00
{
2022-09-30 11:47:45 +00:00
hostname = runTest {
imports = [ ./hostname.nix ];
defaults.networking.firewall.enable = false;
};
2024-04-21 15:54:59 +00:00
}
2022-09-30 11:47:45 +00:00
```
You can run a test with attribute name `hostname` in `nixos/tests/all-tests.nix` by invoking:
```shell
cd /my/git/clone/of/nixpkgs
nix-build -A nixosTests.hostname
```
### Testing outside the NixOS project {#sec-call-nixos-test-outside-nixos}
2024-10-29 11:11:06 +00:00
Outside the `nixpkgs` repository, you can use the `runNixOSTest` function from
`pkgs.testers` :
2022-09-30 11:47:45 +00:00
```nix
2024-10-29 11:11:06 +00:00
let pkgs = import < nixpkgs > {};
2022-09-30 11:47:45 +00:00
in
2024-10-29 11:11:06 +00:00
pkgs.testers.runNixOSTest {
2022-09-30 11:47:45 +00:00
imports = [ ./test.nix ];
defaults.services.foo.package = mypkg;
}
```
2024-10-29 11:11:06 +00:00
`runNixOSTest` returns a derivation that runs the test.
2022-09-30 11:47:45 +00:00
## Configuring the nodes {#sec-nixos-test-nodes}
There are a few special NixOS options for test VMs:
2021-07-03 03:11:41 +00:00
`virtualisation.memorySize`
: The memory of the VM in megabytes.
`virtualisation.vlans`
: The virtual networks to which the VM is connected. See
[`nat.nix` ](https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/nat.nix )
for an example.
`virtualisation.writableStore`
: By default, the Nix store in the VM is not writable. If you enable
this option, a writable union file system is mounted on top of the
Nix store to make it appear writable. This is necessary for tests
that run Nix operations that modify the store.
For more options, see the module
[`qemu-vm.nix` ](https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/virtualisation/qemu-vm.nix ).
The test script is a sequence of Python statements that perform various
actions, such as starting VMs, executing commands in the VMs, and so on.
Each virtual machine is represented as an object stored in the variable
`name` if this is also the identifier of the machine in the declarative
2022-04-03 18:54:34 +00:00
config. If you specified a node `nodes.machine` , the following example starts the
2021-07-03 03:11:41 +00:00
machine, waits until it has finished booting, then executes a command
and checks that the output is more-or-less correct:
```py
machine.start()
machine.wait_for_unit("default.target")
if not "Linux" in machine.succeed("uname"):
raise Exception("Wrong OS")
```
2022-04-03 18:54:34 +00:00
The first line is technically unnecessary; machines are implicitly started
2021-07-03 03:11:41 +00:00
when you first execute an action on them (such as `wait_for_unit` or
`succeed` ). If you have multiple machines, you can speed up the test by
starting them in parallel:
```py
start_all()
```
2023-05-24 13:37:59 +00:00
If the hostname of a node contains characters that can't be used in a
Python variable name, those characters will be replaced with
underscores in the variable name, so `nodes.machine-a` will be exposed
to Python as `machine_a` .
2022-01-13 20:06:32 +00:00
## Machine objects {#ssec-machine-objects}
2021-07-03 03:11:41 +00:00
The following methods are available on machine objects:
2023-08-04 22:07:22 +00:00
@PYTHON_MACHINE_METHODS @
2022-03-30 09:31:56 +00:00
2021-07-03 03:11:41 +00:00
To test user units declared by `systemd.user.services` the optional
`user` argument can be used:
```py
machine.start()
machine.wait_for_x()
machine.wait_for_unit("xautolock.service", "x-session-user")
```
This applies to `systemctl` , `get_unit_info` , `wait_for_unit` ,
`start_job` and `stop_job` .
2022-12-28 21:21:41 +00:00
For faster dev cycles it's also possible to disable the code-linters
(this shouldn't be committed though):
2021-07-03 03:11:41 +00:00
```nix
2022-09-30 11:47:45 +00:00
{
2021-07-03 03:11:41 +00:00
skipLint = true;
2022-04-03 18:54:34 +00:00
nodes.machine =
2021-07-03 03:11:41 +00:00
{ config, pkgs, ... }:
2024-04-21 15:54:59 +00:00
{ # configuration…
2021-07-03 03:11:41 +00:00
};
testScript =
''
Python code…
'';
}
```
This will produce a Nix warning at evaluation time. To fully disable the
linter, wrap the test script in comment directives to disable the Black
2022-12-28 21:21:41 +00:00
linter directly (again, don't commit this within the Nixpkgs
2021-07-03 03:11:41 +00:00
repository):
```nix
2024-04-21 15:54:59 +00:00
{
2021-07-03 03:11:41 +00:00
testScript =
''
# fmt: off
Python code…
# fmt: on
'';
2024-04-21 15:54:59 +00:00
}
2021-07-03 03:11:41 +00:00
```
2022-01-13 20:06:32 +00:00
2022-06-16 17:23:12 +00:00
Similarly, the type checking of test scripts can be disabled in the following
way:
```nix
2022-09-30 11:47:45 +00:00
{
2022-06-16 17:23:12 +00:00
skipTypeCheck = true;
nodes.machine =
{ config, pkgs, ... }:
2024-04-21 15:54:59 +00:00
{ # configuration…
2022-06-16 17:23:12 +00:00
};
}
```
2022-01-13 20:06:32 +00:00
## Failing tests early {#ssec-failing-tests-early}
2022-08-12 12:06:08 +00:00
To fail tests early when certain invariants are no longer met (instead of waiting for the build to time out), the decorator `polling_condition` is provided. For example, if we are testing a program `foo` that should not quit after being started, we might write the following:
2022-01-13 20:06:32 +00:00
```py
@polling_condition
def foo_running():
machine.succeed("pgrep -x foo")
machine.succeed("foo --start")
machine.wait_until_succeeds("pgrep -x foo")
with foo_running:
... # Put `foo` through its paces
```
`polling_condition` takes the following (optional) arguments:
`seconds_interval`
2023-02-16 17:41:37 +00:00
: specifies how often the condition should be polled:
2022-01-13 20:06:32 +00:00
2022-08-12 12:06:08 +00:00
```py
@polling_condition (seconds_interval=10)
def foo_running():
machine.succeed("pgrep -x foo")
```
2022-01-13 20:06:32 +00:00
`description`
2023-02-16 17:41:37 +00:00
: is used in the log when the condition is checked. If this is not provided, the description is pulled from the docstring of the function. These two are therefore equivalent:
2022-01-13 20:06:32 +00:00
2022-08-12 12:06:08 +00:00
```py
@polling_condition
def foo_running():
"check that foo is running"
machine.succeed("pgrep -x foo")
```
2022-01-13 20:06:32 +00:00
2022-08-12 12:06:08 +00:00
```py
@polling_condition (description="check that foo is running")
def foo_running():
machine.succeed("pgrep -x foo")
```
2022-06-16 17:23:12 +00:00
## Adding Python packages to the test script {#ssec-python-packages-in-test-script}
When additional Python libraries are required in the test script, they can be
added using the parameter `extraPythonPackages` . For example, you could add
`numpy` like this:
```nix
{
extraPythonPackages = p: [ p.numpy ];
nodes = { };
2022-07-18 16:21:45 +00:00
# Type checking on extra packages doesn't work yet
skipTypeCheck = true;
2022-06-16 17:23:12 +00:00
testScript = ''
import numpy as np
2024-04-21 15:54:59 +00:00
assert str(np.zeros(4)) == "[0. 0. 0. 0.]"
2022-06-16 17:23:12 +00:00
'';
}
```
In that case, `numpy` is chosen from the generic `python3Packages` .
2022-09-30 11:47:45 +00:00
## Test Options Reference {#sec-test-options-reference}
The following options can be used when writing tests.
2023-02-22 10:55:15 +00:00
```{=include=} options
id-prefix: test-opt-
list-id: test-options-list
source: @NIXOS_TEST_OPTIONS_JSON @
2022-09-30 11:47:45 +00:00
```