diff options
| -rw-r--r-- | README.md | 8 | ||||
| -rw-r--r-- | tests/default.nix | 20 | ||||
| -rw-r--r-- | tests/pure-deploy.nix | 22 | ||||
| -rw-r--r-- | tests/pure.nix | 144 | ||||
| -rw-r--r-- | tests/scenarios.nix | 36 |
5 files changed, 222 insertions, 8 deletions
@@ -355,15 +355,19 @@ automatically. `tests/` contains NixOS VM tests for krops, using the VM machinery and the scenarios of [populate's tests](pkgs/populate/README.md#tests): `deploy`, for the arguments `writeDeploy` passes to `nixos-rebuild`, which a stand-in -records, and `populate`, for `pkgs.populate`, running populate's scenarios +records, `populate`, for `pkgs.populate`, running populate's scenarios that expect populate to refuse its arguments, which `pkgs.populate` does -during evaluation, and those of populate's smoke test. The tests require +during evaluation, and those of populate's smoke test, and `pure`, for +evaluating krops' lib from a flake, and, exhaustively, deploying with +`writeDeploy` from a flake, which evaluates nixpkgs, and copies it, without +`.git`, into the store. The tests require KVM and nixpkgs with `pkgs.testers.runNixOSTest` (23.05 or later), and use `<nixpkgs>` by default. ```sh nix-build tests # all tests, except the exhaustive ones nix-build tests -A deploy # the test of writeDeploy +nix-build tests -A pure # the test of pure evaluation nix-build tests -A exhaustive # all exhaustive tests nix-build tests -A smoke # a few scenarios of each, in one VM nix-build tests -A only --argstr only 'lock-hooks-.*' # scenarios by name diff --git a/tests/default.nix b/tests/default.nix index 08f95a6..6c3bc59 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -1,10 +1,10 @@ # NixOS VM tests for krops, with the VM machinery and the scenarios of # populate's tests, in ../pkgs/populate/tests: deploy, for writeDeploy's -# arguments to nixos-rebuild, and populate, for pkgs.populate, running +# arguments to nixos-rebuild, populate, for pkgs.populate, running # populate's scenarios that expect populate to refuse its arguments, which # pkgs.populate does during evaluation here, and the scenarios of # populate's smoke test, with target attribute sets as mkTarget returns -# them. Build all with nix-build tests, or one with, e.g., nix-build tests +# them, and pure, for pure evaluation, see ./pure.nix. Build all with nix-build tests, or one with, e.g., nix-build tests # -A deploy. The exhaustive tests add the combinations of options that no # code path tells apart from the ones the others cover; build them with # nix-build tests -A exhaustive. The smoke test runs a few scenarios of @@ -38,8 +38,12 @@ let }; }; + pure = import ./pure.nix { inherit pkgs; }; + # A phase runs pkgs.populate, or writeDeploy with a stand-in for - # nixos-rebuild. + # nixos-rebuild, or, if deploy's pure is set, a pure evaluation: of + # krops' lib, if it's lib, or of writeDeploy, if it's deploy, running + # what that makes. run = { source, backup, force, prune, deploy, targetKind, targetPath, }: let @@ -47,10 +51,13 @@ let inherit source backup force prune; target = targets.${targetKind} targetPath; }; + deployArgs = args // { useNixOutputMonitor = false; } // deploy; in if deploy == null then pkgs.populate args - else pkgs.krops.writeDeploy "deploy" - (args // { useNixOutputMonitor = false; } // deploy); + else if deploy.pure or null == "lib" then pure.lib + else if deploy.pure or null == "deploy" then + pure.deploy (removeAttrs deployArgs [ "pure" ]) + else pkgs.krops.writeDeploy "deploy" deployArgs; builderArgs = { inherit lib run; @@ -70,13 +77,14 @@ let # Names of the scenarios of the smoke test, besides populate's. smokeNames = import (populateTests + "/smoke.nix") ++ [ + "pure-lib" "deploy-local-buildTarget0-etcFlake0-flakefile-targetString0" "deploy-remote-buildTarget0-etcFlake0-flakenone-targetString1" "deploy-sudo-allocateTTY" ]; scenarios = { - inherit (kropsScenarios) deploy; + inherit (kropsScenarios) deploy pure; populate = builtins.filter (scenario: scenario.refusals || builtins.elem scenario.name smokeNames) populateScenarios; diff --git a/tests/pure-deploy.nix b/tests/pure-deploy.nix new file mode 100644 index 0000000..6e34706 --- /dev/null +++ b/tests/pure-deploy.nix @@ -0,0 +1,22 @@ +# writeDeploy of krops' flake, with the arguments args, given krops' and +# nixpkgs' sources: what the flake ./pure.nix makes evaluates purely, and +# what the host builds. nixpkgs is a flake with lib and legacyPackages, +# without nixpkgs' configuration and overlays, which differ between the +# two, and which pure evaluation can't read anyway. +{ krops, nixpkgs, system, args }: + +let + nixpkgs' = { + lib = import "${nixpkgs}/lib"; + legacyPackages.${system} = import nixpkgs { + inherit system; + config = {}; + overlays = []; + }; + }; + kropsFlake = (import "${krops}/flake.nix").outputs { + self = kropsFlake; + nixpkgs = nixpkgs'; + }; +in + kropsFlake.lib.${system}.writeDeploy "deploy" args diff --git a/tests/pure.nix b/tests/pure.nix new file mode 100644 index 0000000..ce540b4 --- /dev/null +++ b/tests/pure.nix @@ -0,0 +1,144 @@ +# Flakes evaluating krops purely, as flakes evaluate by default, and scripts +# running them on the deploying machine. Their inputs are krops' and +# nixpkgs' sources in the store, without .git, locked as tarballs by their +# hashes, so Nix uses the store paths as they are, without fetching or +# reading them, and the VM shares them with the host's store. +{ pkgs }: + +let + inherit (pkgs) lib; + inherit (pkgs.stdenv.hostPlatform) system; + + kropsSrc = builtins.path { + name = "source"; + path = ../.; + filter = path: type: let name = baseNameOf path; in + name != ".git" && name != "target" && !lib.hasPrefix "result" name; + }; + nixpkgsSrc = builtins.path { + name = "source"; + path = pkgs.path; + filter = path: type: baseNameOf path != ".git"; + }; + nixpkgsLibSrc = builtins.path { + name = "source"; + path = pkgs.path + "/lib"; + }; + + # A directory with flake.nix, whose inputs are the store paths of inputs + # by name, not flakes, its lock file, and files, by name. + flake = { inputs, outputs, files ? {} }: pkgs.runCommand "pure-flake" { + nativeBuildInputs = [ pkgs.nix ]; + } '' + mkdir $out + cat > $out/flake.nix <<'EOF' + { + ${lib.concatStrings (lib.mapAttrsToList (name: _: '' + inputs.${name} = { url = "file:///${name}.tar.gz"; flake = false; }; + '') inputs)} + outputs = ${outputs}; + } + EOF + export HOME=$TMPDIR + { + echo '{ "nodes": { "root": { "inputs": {' + ${lib.concatStringsSep "\necho ,\n" (lib.mapAttrsToList (name: _: '' + printf '"%s": "%s"' ${name} ${name} + '') inputs)} + echo '} }' + ${lib.concatStrings (lib.mapAttrsToList (name: path: '' + printf ', "%s": { "flake": false, "locked": { "type": "tarball", "url": "file:///%s.tar.gz", "narHash": "%s" }, "original": { "type": "tarball", "url": "file:///%s.tar.gz" } }\n' \ + ${name} ${name} "$(nix-hash --type sha256 --sri ${path})" ${name} + '') inputs)} + echo '}, "root": "root", "version": 7 }' + } > $out/flake.lock + ${lib.concatStrings (lib.mapAttrsToList (name: file: '' + cp ${file} $out/${name} + '') files)} + ''; + + # A script evaluating what a flake's attribute attr is, purely, printing + # it, and referring to the store paths of the inputs, so the VM has them. + evaluate = name: dir: attr: inputs: pkgs.writers.writeDash name '' + set -efu + # ${toString (lib.attrValues inputs)} + exec nix eval --raw ${lib.escapeShellArg "path:${dir}#${attr}"} + ''; + + # krops' lib, from a flake with nixpkgs' lib only. mkTarget leaves the + # user and the host null, which pure evaluation can't tell, and the + # sources of each type evaluate. + libFlake = let + inputs = { krops = kropsSrc; lib = nixpkgsLibSrc; }; + in flake { + inherit inputs; + outputs = /* nix */ '' + { krops, lib, ... }: let + klib = import "''${krops}/lib/with.nix" { lib = import lib; }; + target = klib.mkTarget ""; + in { + check = + assert target.user == null && target.host == null; + assert target.path == "/var/src"; + builtins.toJSON (klib.evalSource [{ + a.derivation = "{}"; + b.file = "/fixtures/alt"; + c.flake = "/fixtures/top"; + d.flake-input = { url = "/fixtures/top"; name = "dep"; }; + e.git = { url = "git://deployer/gitsrc.git"; ref = "HEAD"; }; + f.pass = { dir = "/fixtures/pass"; name = "x"; }; + g.passage.dir = "/fixtures/passage/store"; + h.pipe = "/fixtures/pipe"; + i.symlink = "b"; + }]); + } + ''; + }; + + # What the host makes of writeDeploy's arguments args, as the flake. + deployOf = args: import ./pure-deploy.nix { + krops = kropsSrc; + nixpkgs = nixpkgsSrc; + inherit system args; + }; + +in { + lib = evaluate "pure-lib" libFlake "check" { inherit kropsSrc nixpkgsLibSrc; }; + + # A script deploying with writeDeploy's arguments args, which have to be + # JSON, from a flake evaluating it purely. Building it in the VM would + # need what building populate needs, so it runs what the host built of + # the same derivation, failing if pure evaluation makes another. + deploy = args: let + args' = builtins.fromJSON (builtins.toJSON args); + expected = deployOf args'; + dir = flake { + inputs = { krops = kropsSrc; nixpkgs = nixpkgsSrc; }; + outputs = /* nix */ '' + { krops, nixpkgs, ... }: { + deploy = import ./pure-deploy.nix { + inherit krops nixpkgs; + system = ${builtins.toJSON system}; + args = builtins.fromJSON (builtins.readFile ./args.json); + }; + } + ''; + files = { + "args.json" = pkgs.writeText "args.json" (builtins.toJSON args'); + "pure-deploy.nix" = ./pure-deploy.nix; + }; + }; + drvPath = evaluate "pure-deploy-drv" dir "deploy.drvPath" { + inherit kropsSrc nixpkgsSrc; + }; + in pkgs.writers.writeDash "pure-deploy" '' + set -efu + drv=$(${drvPath}) + if test "$drv" != ${builtins.unsafeDiscardStringContext expected.drvPath}; then + >&2 printf 'pure evaluation makes %s instead of %s\n' "$drv" \ + ${builtins.unsafeDiscardStringContext expected.drvPath} + exit 1 + fi + exec ${expected} + ''; +} diff --git a/tests/scenarios.nix b/tests/scenarios.nix index 0e90284..0f7a0f8 100644 --- a/tests/scenarios.nix +++ b/tests/scenarios.nix @@ -130,4 +130,40 @@ in }]; })) (if targetKind == "sudo" then [ false ] else [ false true ])) targetKinds; + + # Pure evaluation, as flakes evaluate by default: of krops' lib, and of + # writeDeploy, deploying what that makes, with a target string, which + # makes the same derivation as evaluating on the host, which builds it. + # That evaluates nixpkgs, so it's exhaustive. + pure = [ + (scenario { + name = "pure-lib"; + targetKind = "local"; + phases = [{ + sources.x.file = "/fixtures/alt"; + deploy.pure = "lib"; + }]; + }) + (scenario { + name = "pure-deploy"; + targetKind = "remote"; + exhaustive = true; + prepare = '' + rm -f /tmp/nixos-rebuild.log /etc/nixos/flake.nix + ''; + phases = [{ + sources.x.file = "/fixtures/alt"; + deploy = { + pure = "deploy"; + target = "root@${remoteHost}/var/krops/pure-deploy"; + }; + check = '' + test -e "$T"/x/flake.nix || fail "x/flake.nix missing" + expect_eq NIX_PATH "$(sed -n 2p /tmp/nixos-rebuild.log)" "$T" + expect_eq arguments "$(sed -n 3p /tmp/nixos-rebuild.log)" switch + ''; + }]; + }) + ]; + } |
