From cdca66d7e8af90daa03fab564fa358df16b7421c Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 27 Sep 2018 17:03:23 +0200 Subject: [PATCH 1/6] Add pkgs.nixosTest --- nixos/lib/build-vms.nix | 14 +++++++-- nixos/lib/testing.nix | 11 +++++-- pkgs/top-level/all-packages.nix | 51 +++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/nixos/lib/build-vms.nix b/nixos/lib/build-vms.nix index 933f8139249..024f4414ebe 100644 --- a/nixos/lib/build-vms.nix +++ b/nixos/lib/build-vms.nix @@ -1,4 +1,13 @@ -{ system, pkgs, minimal ? false, config ? {} }: +{ system +, # Use a minimal kernel? + minimal ? false +, # Ignored + config ? null + # Nixpkgs, for qemu, lib and more +, pkgs +, # NixOS configuration to add to the VMs + extraConfigurations ? [] +}: with pkgs.lib; with import ../lib/qemu-flags.nix { inherit pkgs; }; @@ -28,7 +37,8 @@ rec { ../modules/testing/test-instrumentation.nix # !!! should only get added for automated test runs { key = "no-manual"; documentation.nixos.enable = false; } { key = "qemu"; system.build.qemu = qemu; } - ] ++ optional minimal ../modules/testing/minimal-kernel.nix; + ] ++ optional minimal ../modules/testing/minimal-kernel.nix + ++ extraConfigurations; extraArgs = { inherit nodes; }; }; diff --git a/nixos/lib/testing.nix b/nixos/lib/testing.nix index f90fc9f7df0..690f7dfd5fa 100644 --- a/nixos/lib/testing.nix +++ b/nixos/lib/testing.nix @@ -1,6 +1,13 @@ -{ system, pkgs, minimal ? false, config ? {} }: +{ system +, pkgs + # Use a minimal kernel? +, minimal ? false + # Ignored +, config ? null + # Modules to add to each VM +, extraConfigurations ? [] }: -with import ./build-vms.nix { inherit system pkgs minimal config; }; +with import ./build-vms.nix { inherit system pkgs minimal extraConfigurations; }; with pkgs; let diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index ad68a42f3f2..0a86c8ba516 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -22243,6 +22243,57 @@ with pkgs; ); }).config.system.build; + + /* + * Run a NixOS VM network test. + * + * This is equivalent to import ./make-test.nix from the NixOS manual + * For details see https://nixos.org/nixos/manual/index.html#sec-nixos-tests + * + * Parameter: + * A NixOS VM test network, or path to it. Example: + * + * { lib, ... }: + * { name = "my-test"; + * nodes = { + * machine-1 = someNixOSConfiguration; + * machine-2 = ...; + * } + * } + * + * Result: + * A derivation that runs the VM test. + * + * For the interaction between Nixpkgs and NixOS configuration + * consult the pkgs.nixos function documentation. + */ + nixosTest = + let + /* The nixos/lib/testing.nix module, preapplied with arguments that + * make sense for this evaluation of Nixpkgs. + */ + nixosTesting = + (import ../../nixos/lib/testing.nix { + inherit (pkgs.stdenv.hostPlatform) system; + inherit pkgs; + extraConfigurations = [( + { lib, ... }: { + config.nixpkgs.pkgs = lib.mkDefault pkgs; + } + )]; + }); + in + test: + let + loadedTest = if builtins.typeOf test == "path" + then import test + else test; + calledTest = if pkgs.lib.isFunction loadedTest + then callPackage loadedTest {} + else loadedTest; + in + nixosTesting.makeTest calledTest; + nixui = callPackage ../tools/package-management/nixui { node_webkit = nwjs_0_12; }; nixdoc = callPackage ../tools/nix/nixdoc {}; From 933c95c0f40973bd72f1e46767d001a8f1860f40 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 2 Oct 2018 21:49:54 +0200 Subject: [PATCH 2/6] Add tests for pkgs.nixos and pkgs.nixosTest --- pkgs/test/default.nix | 2 ++ pkgs/test/nixos-functions/default.nix | 32 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 pkgs/test/nixos-functions/default.nix diff --git a/pkgs/test/default.nix b/pkgs/test/default.nix index 24948ce9aa4..9315595f8ec 100644 --- a/pkgs/test/default.nix +++ b/pkgs/test/default.nix @@ -30,5 +30,7 @@ with pkgs; cross = callPackage ./cross {}; + nixos-functions = callPackage ./nixos-functions {}; + patch-shebangs = callPackage ./patch-shebangs {}; } diff --git a/pkgs/test/nixos-functions/default.nix b/pkgs/test/nixos-functions/default.nix new file mode 100644 index 00000000000..3f345152539 --- /dev/null +++ b/pkgs/test/nixos-functions/default.nix @@ -0,0 +1,32 @@ +{ pkgs, lib, stdenv, ... }: + +lib.optionalAttrs stdenv.hostPlatform.isLinux ( + pkgs.recurseIntoAttrs { + + nixos-test = (pkgs.nixos { + boot.loader.grub.enable = false; + fileSystems."/".device = "/dev/null"; + }).toplevel; + + nixosTest-test = let + # extend pkgs with an extra overlay to make sure pkgs is passed along properly to machines. + altPkgs = pkgs.extend (self: super: { + # To test pkgs in machine + hello_s9e8ghsi = self.hello; + # To test lib in test + lib = super.lib // { testSubject_dohra8w = "nixosTest"; }; + # To test pkgs in test + dash-test_ny3dseg = "-test"; + }); + in altPkgs.nixosTest ({ lib, pkgs, ... }: { + name = "${lib.testSubject_dohra8w}${pkgs.dash-test_ny3dseg}"; # These would fail if it's the wrong pkgs or lib + machine = { pkgs, ... }: { + environment.systemPackages = [ pkgs.hello_s9e8ghsi ]; + }; + testScript = '' + $machine->succeed("hello"); + ''; + }); + + } +) From 5d594d764ebc488075596fbfccdc9baf95925bb1 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 23 Oct 2018 17:32:59 +0200 Subject: [PATCH 3/6] tests: Don't use pkgs.extend. OfBorg will reject it. The good news is that it worked as expected. --- pkgs/test/nixos-functions/default.nix | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/pkgs/test/nixos-functions/default.nix b/pkgs/test/nixos-functions/default.nix index 3f345152539..a53157066bb 100644 --- a/pkgs/test/nixos-functions/default.nix +++ b/pkgs/test/nixos-functions/default.nix @@ -8,20 +8,10 @@ lib.optionalAttrs stdenv.hostPlatform.isLinux ( fileSystems."/".device = "/dev/null"; }).toplevel; - nixosTest-test = let - # extend pkgs with an extra overlay to make sure pkgs is passed along properly to machines. - altPkgs = pkgs.extend (self: super: { - # To test pkgs in machine - hello_s9e8ghsi = self.hello; - # To test lib in test - lib = super.lib // { testSubject_dohra8w = "nixosTest"; }; - # To test pkgs in test - dash-test_ny3dseg = "-test"; - }); - in altPkgs.nixosTest ({ lib, pkgs, ... }: { - name = "${lib.testSubject_dohra8w}${pkgs.dash-test_ny3dseg}"; # These would fail if it's the wrong pkgs or lib + nixosTest-test = pkgs.nixosTest ({ lib, pkgs, ... }: { + name = "nixosTest-test"; machine = { pkgs, ... }: { - environment.systemPackages = [ pkgs.hello_s9e8ghsi ]; + environment.systemPackages = [ pkgs.hello ]; }; testScript = '' $machine->succeed("hello"); From d82e1528f866b27fadee020e101458cd6a14d5c4 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 25 Oct 2018 15:37:51 +0200 Subject: [PATCH 4/6] pkgs.nixosTest: format --- pkgs/top-level/all-packages.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 0a86c8ba516..d25b0807e9c 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -22247,7 +22247,7 @@ with pkgs; /* * Run a NixOS VM network test. * - * This is equivalent to import ./make-test.nix from the NixOS manual + * This is equivalent to `import ./make-test.nix` from the NixOS manual * For details see https://nixos.org/nixos/manual/index.html#sec-nixos-tests * * Parameter: From 3783f2d5105412cec727816b385631047b2af01b Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Thu, 25 Oct 2018 15:58:11 +0200 Subject: [PATCH 5/6] pkgs/test/nixos-functions: Add inline doc --- pkgs/test/nixos-functions/default.nix | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pkgs/test/nixos-functions/default.nix b/pkgs/test/nixos-functions/default.nix index a53157066bb..c8f7122006f 100644 --- a/pkgs/test/nixos-functions/default.nix +++ b/pkgs/test/nixos-functions/default.nix @@ -1,3 +1,14 @@ +/* + +This file is a test that makes sure that the `pkgs.nixos` and +`pkgs.nixosTest` functions work. It's far from a perfect test suite, +but better than not checking them at all on hydra. + +To run this test: + + nixpkgs$ nix-build -A tests.nixos-functions + + */ { pkgs, lib, stdenv, ... }: lib.optionalAttrs stdenv.hostPlatform.isLinux ( From 5a8bddf16c60a97668c30077fc084510de6935c4 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sat, 27 Oct 2018 11:09:46 +0200 Subject: [PATCH 6/6] pkgs.nixosTest: Emphasize propagation of pkgs --- pkgs/top-level/all-packages.nix | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index d25b0807e9c..fb124cbcaeb 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -22245,10 +22245,14 @@ with pkgs; /* - * Run a NixOS VM network test. + * Run a NixOS VM network test using this evaluation of Nixpkgs. * - * This is equivalent to `import ./make-test.nix` from the NixOS manual - * For details see https://nixos.org/nixos/manual/index.html#sec-nixos-tests + * It is mostly equivalent to `import ./make-test.nix` from the + * NixOS manual[1], except that your `pkgs` will be used instead of + * letting NixOS invoke Nixpkgs again. If a test machine needs to + * set NixOS options under `nixpkgs`, it must set only the + * `nixpkgs.pkgs` option. For the details, see the Nixpkgs + * `pkgs.nixos` documentation. * * Parameter: * A NixOS VM test network, or path to it. Example: @@ -22264,8 +22268,8 @@ with pkgs; * Result: * A derivation that runs the VM test. * - * For the interaction between Nixpkgs and NixOS configuration - * consult the pkgs.nixos function documentation. + * [1]: For writing NixOS tests, see + * https://nixos.org/nixos/manual/index.html#sec-nixos-tests */ nixosTest = let