From ac1134f321b60f9e27ab0d973a4544480acc0709 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sat, 8 Apr 2023 18:44:24 +0200 Subject: [PATCH] testers.runNixOSTest: init An up to date alternative to pkgs.nixosTest --- doc/builders/testers.chapter.md | 20 ++++++++++++++++++++ pkgs/build-support/testers/default.nix | 14 ++++++++++++++ pkgs/build-support/testers/test/default.nix | 11 +++++++++++ 3 files changed, 45 insertions(+) diff --git a/doc/builders/testers.chapter.md b/doc/builders/testers.chapter.md index a644262fd9c9..928a57673e77 100644 --- a/doc/builders/testers.chapter.md +++ b/doc/builders/testers.chapter.md @@ -164,6 +164,26 @@ tests.fetchgit = testers.invalidateFetcherByDrvHash fetchgit { }; ``` +## `runNixOSTest` {#tester-runNixOSTest} + +A helper function that behaves exactly like the NixOS `runTest`, except it also assigns this Nixpkgs package set as the `pkgs` of the test and makes the `nixpkgs.*` options read-only. + +If your test is part of the Nixpkgs repository, or if you need a more general entrypoint, see ["Calling a test" in the NixOS manual](https://nixos.org/manual/nixos/stable/index.html#sec-calling-nixos-tests). + +Example: + +```nix +pkgs.testers.runNixOSTest ({ lib, ... }: { + name = "hello"; + nodes.machine = { pkgs, ... }: { + environment.systemPackages = [ pkgs.hello ]; + }; + testScript = '' + machine.succeed("hello") + ''; +}) +``` + ## `nixosTest` {#tester-nixosTest} Run a NixOS VM network test using this evaluation of Nixpkgs. diff --git a/pkgs/build-support/testers/default.nix b/pkgs/build-support/testers/default.nix index 542133dd959a..190ce72d0e63 100644 --- a/pkgs/build-support/testers/default.nix +++ b/pkgs/build-support/testers/default.nix @@ -94,6 +94,20 @@ else salted; in checked; + # See doc/builders/testers.chapter.md or + # https://nixos.org/manual/nixpkgs/unstable/#tester-runNixOSTest + runNixOSTest = + let nixos = import ../../../nixos/lib {}; + in testModule: + nixos.runTest { + _file = "pkgs.runNixOSTest implementation"; + imports = [ + (lib.setDefaultModuleLocation "the argument that was passed to pkgs.runNixOSTest" testModule) + ]; + hostPkgs = pkgs; + node.pkgs = pkgs; + }; + # See doc/builders/testers.chapter.md or # https://nixos.org/manual/nixpkgs/unstable/#tester-invalidateFetcherByDrvHash nixosTest = diff --git a/pkgs/build-support/testers/test/default.nix b/pkgs/build-support/testers/test/default.nix index 313c556737fb..fc4df4964f39 100644 --- a/pkgs/build-support/testers/test/default.nix +++ b/pkgs/build-support/testers/test/default.nix @@ -14,6 +14,17 @@ in lib.recurseIntoAttrs { hasPkgConfigModule = pkgs.callPackage ../hasPkgConfigModule/tests.nix { }; + runNixOSTest-example = pkgs-with-overlay.testers.runNixOSTest ({ lib, ... }: { + name = "runNixOSTest-test"; + nodes.machine = { pkgs, ... }: { + system.nixos = dummyVersioning; + environment.systemPackages = [ pkgs.proof-of-overlay-hello pkgs.figlet ]; + }; + testScript = '' + machine.succeed("hello | figlet >/dev/console") + ''; + }); + # Check that the wiring of nixosTest is correct. # Correct operation of the NixOS test driver should be asserted elsewhere. nixosTest-example = pkgs-with-overlay.testers.nixosTest ({ lib, pkgs, figlet, ... }: {