Home Explore Blog CI



nixpkgs

2nd chunk of `nixos/doc/manual/development/writing-nixos-tests.section.md`
8c422f10250ba11d950d76cc39da11524b2ba3b1f5fa29250000000100000974
    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
config. If you specified a node `nodes.machine`, the following example starts the
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")
t.assertIn("Linux", machine.succeed("uname"), "Wrong OS")
```

The first line is technically unnecessary; machines are implicitly started
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()
```

Under the variable `t`, all assertions from [`unittest.TestCase`](https://docs.python.org/3/library/unittest.html) are available.

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`.

## Machine objects {#ssec-machine-objects}

The following methods are available on machine objects:

@PYTHON_MACHINE_METHODS@

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`.

For faster dev cycles it's also possible to disable the code-linters
(this shouldn't be committed though):

```nix
{
  skipLint = true;
  nodes.machine =
    { config, pkgs, ... }:
    { # configuration…
    };

  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

Title: Test Scripts, Machine Objects, and Development Options
Summary
This section details how to write test scripts using Python to interact with virtual machines defined in the test configuration. It describes how to start VMs, execute commands, and check outputs. It introduces machine objects, which represent the virtual machines in the Python script, and their associated methods. It also mentions the availability of assertions from `unittest.TestCase` and how to handle hostnames with invalid characters. Additionally, it describes how to test user units declared by `systemd.user.services`. For faster development cycles, it discusses how to disable code linters, although this is discouraged for commits.