Writing TestsA NixOS test is a Nix expression that has the following structure:
import ./make-test.nix {
# Either the configuration of a single machine:
machine =
{ config, pkgs, ... }:
{ configuration…
};
# Or a set of machines:
nodes =
{ machine1 =
{ config, pkgs, ... }: { … };
machine2 =
{ config, pkgs, ... }: { … };
…
};
testScript =
''
Perl code…
'';
}
The attribute testScript is a bit of Perl code that
executes the test (described below). During the test, it will start
one or more virtual machines, the configuration of which is described
by the attribute machine (if you need only one
machine in your test) or by the attribute nodes (if
you need multiple machines). For instance, login.nix
only needs a single machine to test whether users can log in on the
virtual console, whether device ownership is correctly maintained when
switching between consoles, and so on. On the other hand, nfs.nix,
which tests NFS client and server functionality in the Linux kernel
(including whether locks are maintained across server crashes),
requires three machines: a server and two clients.There are a few special NixOS configuration options for test
VMs:
The memory of the VM in
megabytes.The virtual networks to which the VM is
connected. See nat.nix
for an example.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.The test script is a sequence of Perl 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,
where name is the identifier of the machine
(which is just machine if you didn’t specify
multiple machines using the nodes attribute). For
instance, the following starts the machine, waits until it has
finished booting, then executes a command and checks that the output
is more-or-less correct:
$machine->start;
$machine->waitForUnit("default.target");
$machine->succeed("uname") =~ /Linux/;
The first line is actually unnecessary; machines are implicitly
started when you first execute an action on them (such as
waitForUnit or succeed). If you
have multiple machines, you can speed up the test by starting them in
parallel:
startAll;
The following methods are available on machine objects:
startStart the virtual machine. This method is
asynchronous — it does not wait for the machine to finish
booting.shutdownShut down the machine, waiting for the VM to
exit.crashSimulate a sudden power failure, by telling the VM
to exit immediately.blockSimulate unplugging the Ethernet cable that
connects the machine to the other machines.unblockUndo the effect of
block.screenshotTake a picture of the display of the virtual
machine, in PNG format. The screenshot is linked from the HTML
log.sendMonitorCommandSend a command to the QEMU monitor. This is rarely
used, but allows doing stuff such as attaching virtual USB disks
to a running machine.sendKeysSimulate pressing keys on the virtual keyboard,
e.g., sendKeys("ctrl-alt-delete").sendCharsSimulate typing a sequence of characters on the
virtual keyboard, e.g., sendKeys("foobar\n")
will type the string foobar followed by the
Enter key.executeExecute a shell command, returning a list
(status,
stdout).succeedExecute a shell command, raising an exception if
the exit status is not zero, otherwise returning the standard
output.failLike succeed, but raising
an exception if the command returns a zero status.waitUntilSucceedsRepeat a shell command with 1-second intervals
until it succeeds.waitUntilFailsRepeat a shell command with 1-second intervals
until it fails.waitForUnitWait until the specified systemd unit has reached
the “active” state.waitForFileWait until the specified file
exists.waitForOpenPortWait until a process is listening on the given TCP
port (on localhost, at least).waitForClosedPortWait until nobody is listening on the given TCP
port.waitForXWait until the X11 server is accepting
connections.waitForWindowWait until an X11 window has appeared whose name
matches the given regular expression, e.g.,
waitForWindow(qr/Terminal/).