Skip to content

nixosTest reusable test setup via module system #171396

@roberth

Description

@roberth

Describe the problem

Test setup isn't reusable. For example, if you want to have a dns server in my network, you have to duplicate configuration from existing dns server tests. Testing should be easy!

This also brings nixosTest closer to NixOps 2. Interop is feasible (as long as user is ok not to access resources directly but via user-declared network-level options).

Implementation

Refactor nixosTest aka makeTest to take a module instead of regular attrset arguments.
Module composition will allow the creation of modules that affect multiple nodes and/or the test script.

Example module for adding and configuring a DNS server:

{ config, lib, options, pkgs, ... }:

{
  nodes = {
    ns = { nodes, ... }: {
      networking.firewall.allowedUDPPorts = [ 53 ];
      services.bind.enable = true;
      services.bind.extraOptions = "empty-zones-enable no;";
      services.bind.zones = [{
        name = ".";
        master = true;
        file = writeText "root.zone" ''
          $TTL 3600
          . IN SOA ns. ns. ( 1 8 2 4 1 )
          . IN NS ns.
          ${concatMapStringsSep "\n"
            (node: "${node.config.networking.hostName}. IN A ${node.config.networking.primaryIPAddress}")
            (builtins.attrValues nodes)
          }
        '';
      }];
    };
  };
  # `default` is added to all `nodes.*`, type: deferredModule
  default = { config, ... }: {
      networking.dhcpcd.enable = false;
      environment.etc."resolv.conf".text = ''
        nameserver ${nodes.ns.config.networking.primaryIPAddress}
      '';
  };
  # prefixed to testScript, type: lines
  testScriptPrelude = ''
    def wait_dns():
      ns.wait_for_unit("bind.service")
      ns.wait_for_open_port(53)
  '';
}

Example usage:

nixosTest {
  imports = [ nixosTestModules.bind ];
  nodes.foo = /* ... */;
  testScript = ''
    start_all();
    wait_dns();

    # ...
  '';
}

Other module ideas:

  • Set up a mock mail server and adds assertion methods for checking / waiting for mail arrival
  • A module that sets up the host sandbox as a binary cache via 9p + local. (might need a tweak in Nix itself, or recursive Nix)
  • Clean up nixosTest implementation by splitting existing code into modules.

Notify maintainers

@tfc

Metadata

Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

[user@system:~]$ nix-shell -p nix-info --run "nix-info -m"
output here

Metadata

Metadata

Assignees

No one assigned

    Labels

    0.kind: enhancementAdd something new or improve an existing system.6.topic: nixosIssues or PRs affecting NixOS modules, or package usability issues specific to NixOS6.topic: testingTooling for automated testing of packages and modules6.topic: user experienceNixOS / Nixpkgs end user experience

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions