Testing a Resource Provider

NixOps4 resource providers can be tested in multiple ways.

Choose a Testing Environment

The choice of testing method depends on the desired trade-offs familiarity and convenience vs. robustness.

The following table provides an overview of the trade-offs, which are explained in more detail below.

Criteria:

๐Ÿ“ฆ: Is the test hermetic and reproducible?
โ„๏ธ: Is it easy to set up NixOS services?
โ˜๏ธ: Can the test access the network?
๐Ÿ—๏ธ: Can the test build derivations?
๐Ÿšš: Can the test use a Nix cache?
๐Ÿ๐Ÿ“ฆ: Can it test a macOS build of the application under test?
๐Ÿ๐Ÿง‘โ€๐Ÿ’ป: Can a macOS user test with it?

EnvironmentRunner๐Ÿ“ฆโ„๏ธโ˜๏ธ๐Ÿ—๏ธ๐Ÿšš๐Ÿ๐Ÿ“ฆ๐Ÿ๐Ÿง‘โ€๐Ÿ’ปnotes
Nix sandboxnixops4-resource-runnerโœ…โŒโŒโŒโŒโœ…โœ…
Nix sandbox with different storeDirnixops4โœ…โŒโŒโœ…โŒโœ…โœ…Impractical
Nix sandbox with relocated storenixops4โœ…โŒโŒโœ…โœ…ยนโŒโŒ๐Ÿšง Untested
Nix sandbox with recursive nixnixops4โš ๏ธยณโŒโŒโœ…โœ…โœ…โœ…โš ๏ธยณ
NixOS VM testnixops4โœ…โœ…โŒโœ…โœ…ยนโŒโœ…ยฒ๐Ÿšง In development, adds ~10s overhead
UnsandboxedeitherโŒโŒโœ…โœ…โœ…โœ…โœ…Not perfect, but can be good

ยน: Make sure to add expected build inputs to the check derivation or system.extraDependencies

ยฒ: Requires a "remote" builder, which can be provided by nix-darwin's nix.linux-builder.enable

ยณ: The recursive-nix experimental feature is not planned to be supported in the long term and has problems.

Environment

The main differentiator is the environment. The benefits of picking a more restrictive environment include

  • ability to run offline
  • hermeticity and the ability to git bisect

These tend to be lost when running outside the Nix sandbox.

If you are testing a provider that interacts with the network, you may have no choice.

Test runner

You may run your tests with nixops4 or nixops4-resource-runner. The latter is simpler and easy to call from a script, and is good for a "unit test" style of testing, whereas nixops4 proper makes it easy to test whole deployments.

Can build

If your test relies on building a derivation, this may be a deciding factor. The Nix sandbox does not normally allow building, but workarounds exist.

Many providers do not require building to test them.

Can use cache

This is only relevant if you are building derivations in the test. Depending on the workaround, you may be able to use pre-built dependencies.

MacOS support

We can distinguish between the ability to test a provider that is built for macOS, versus the ability to test using macOS at all.

A NixOS VM test can be run on a macOS host, but it will not test the provider on macOS.