NixOS 101 Course / Section 6: NixOS integration tests
Premium

6.2 Your first NixOS integration test

Lesson 23 of 28 · 11:08

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Running the NixOS Test Driver
  2. 2:25 Inspecting Test Output and KVM Performance
  3. 5:18 Refactoring the Test Script
  4. 7:35 Guest Packages vs Host Packages
  5. 9:20 Switching from VMs to Containers
Transcript

Running the NixOS Test Driver

0:00 Okay, so enough talk about how amazing the test driver is and let's simply test it out. I took this code here from the slide and put it into a few files and I changed a few little things here. So the thing is general, of course we need some kind of packages attribute. Of course you could do that in flakes too, but flakes would be not these three lines of boilerplate, but 15 or something and this part here packages.testers.runNixOSTest.

0:33 That is the function that you would also call from flake context, right? So that's the part that we concentrate on and what we do here is we import the actual test file, which is test.nix here and as the defaults we are disabling DHCP in all the VMs. You don't have to. You just make the test slightly faster. Just as an example what you can do with like defaults. And then the test, it must have like a name, right?

1:04 Because that will be part of your derivation output path. It needs kind of machines, right? So machine one and two are defined here and an empty pair of brackets constitutes a standard default config that doesn't really contain anything, but gives you a bootable system, a root shell without a password and also the instrumentation that we need so that we can control the machines from the Python script. And then the test script, which is the biggest part of this one here, I added some Python annotation here because Tree-sitter syntax highlighters can then syntax highlight this as Python instead of Nix.

The full transcript comes with the lesson.

About this lesson

Two machines that ping each other: the smallest useful NixOS integration test. In this lesson you run it with runNixOSTest, read its output, change it, and then switch the machines from VMs to containers and watch the runtime drop.

In this video

  • The few lines of boilerplate for a test without flakes, and what a test needs at minimum
  • Reading the interleaved VM logs and the driver's own messages
  • The KVM permission problem that makes tests slow instead of failing
  • Host packages versus guest packages: two ways to add a package, and one of them is wrong
  • The one word that turns VMs into containers

After this lesson

You can write, run, and modify a basic multi-machine test, and you know the first thing to check when tests run slowly.

Links: