Running Tests
You can run tests using nix-build. For
example, to run the test login.nix,
you just do:
$ nix-build '<nixpkgs/nixos/tests/login.nix>'
or, if you don’t want to rely on NIX_PATH:
$ cd /my/nixpkgs/nixos/tests
$ nix-build login.nix
…
running the VM test script
machine: QEMU running (pid 8841)
…
6 out of 6 tests succeeded
After building/downloading all required dependencies, this will
perform a build that starts a QEMU/KVM virtual machine containing a
NixOS system. The virtual machine mounts the Nix store of the host;
this makes VM creation very fast, as no disk image needs to be
created. Afterwards, you can view a pretty-printed log of the test:
$ firefox result/log.html
Running Tests interactively
The test itself can be run interactively. This is
particularly useful when developing or debugging a test:
$ nix-build nixos/tests/login.nix -A driver
$ ./result/bin/nixos-test-driver
starting VDE switch for network 1
>
You can then take any Perl statement, e.g.
> startAll
> testScript
> $machine->succeed("touch /tmp/foo")
The function testScript executes the entire test
script and drops you back into the test driver command line upon its
completion. This allows you to inspect the state of the VMs after the
test (e.g. to debug the test script).
To just start and experiment with the VMs, run:
$ nix-build nixos/tests/login.nix -A driver
$ ./result/bin/nixos-run-vms
The script nixos-run-vms starts the virtual
machines defined by test. The root file system of the VMs is created
on the fly and kept across VM restarts in
./hostname.qcow2.