From fee5c7e1c2b7395b61d7436503292df584e52840 Mon Sep 17 00:00:00 2001 From: Alois Wohlschlager Date: Sat, 16 Dec 2023 19:02:17 +0100 Subject: [PATCH] replaceDependencies: add tests The tests cannot be directly built by Hydra, because replaceDependencies relies on IFD. Instead, they are put inside a NixOS test where they are built on the guest. --- nixos/tests/all-tests.nix | 1 + nixos/tests/replace-dependencies/default.nix | 18 +++ nixos/tests/replace-dependencies/guest.nix | 132 +++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 nixos/tests/replace-dependencies/default.nix create mode 100644 nixos/tests/replace-dependencies/guest.nix diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 2917a8ed4887..15efe3a06b56 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -857,6 +857,7 @@ in { redlib = handleTest ./redlib.nix {}; redmine = handleTest ./redmine.nix {}; renovate = handleTest ./renovate.nix {}; + replace-dependencies = handleTest ./replace-dependencies {}; restartByActivationScript = handleTest ./restart-by-activation-script.nix {}; restic-rest-server = handleTest ./restic-rest-server.nix {}; restic = handleTest ./restic.nix {}; diff --git a/nixos/tests/replace-dependencies/default.nix b/nixos/tests/replace-dependencies/default.nix new file mode 100644 index 000000000000..822b0fbecade --- /dev/null +++ b/nixos/tests/replace-dependencies/default.nix @@ -0,0 +1,18 @@ +import ../make-test-python.nix ( + { pkgs, ... }: + { + name = "replace-dependencies"; + meta.maintainers = with pkgs.lib.maintainers; [ alois31 ]; + + nodes.machine = + { ... }: + { + system.extraDependencies = [ pkgs.stdenvNoCC ]; + }; + + testScript = '' + start_all() + machine.succeed("nix-build --option substitute false ${pkgs.path}/nixos/tests/replace-dependencies/guest.nix") + ''; + } +) diff --git a/nixos/tests/replace-dependencies/guest.nix b/nixos/tests/replace-dependencies/guest.nix new file mode 100644 index 000000000000..32376e0a5636 --- /dev/null +++ b/nixos/tests/replace-dependencies/guest.nix @@ -0,0 +1,132 @@ +# This needs to be run in a NixOS test, because Hydra cannot do IFD. +let + pkgs = import ../../.. { }; + inherit (pkgs) + runCommand + writeShellScriptBin + replaceDependency + replaceDependencies + ; + inherit (pkgs.lib) escapeShellArg; + mkCheckOutput = + name: test: output: + runCommand name { } '' + actualOutput="$(${escapeShellArg "${test}/bin/test"})" + if [ "$(${escapeShellArg "${test}/bin/test"})" != ${escapeShellArg output} ]; then + echo >&2 "mismatched output: expected \""${escapeShellArg output}"\", got \"$actualOutput\"" + exit 1 + fi + touch "$out" + ''; + oldDependency = writeShellScriptBin "dependency" '' + echo "got old dependency" + ''; + newDependency = writeShellScriptBin "dependency" '' + echo "got new dependency" + ''; + basic = writeShellScriptBin "test" '' + ${oldDependency}/bin/dependency + ''; + transitive = writeShellScriptBin "test" '' + ${basic}/bin/test + ''; + weirdDependency = writeShellScriptBin "dependency" '' + echo "got weird dependency" + ${basic}/bin/test + ''; + oldDependency1 = writeShellScriptBin "dependency1" '' + echo "got old dependency 1" + ''; + newDependency1 = writeShellScriptBin "dependency1" '' + echo "got new dependency 1" + ''; + oldDependency2 = writeShellScriptBin "dependency2" '' + ${oldDependency1}/bin/dependency1 + echo "got old dependency 2" + ''; + newDependency2 = writeShellScriptBin "dependency2" '' + ${oldDependency1}/bin/dependency1 + echo "got new dependency 2" + ''; + deep = writeShellScriptBin "test" '' + ${oldDependency2}/bin/dependency2 + ''; +in +{ + replacedependency-basic = mkCheckOutput "replacedependency-basic" (replaceDependency { + drv = basic; + inherit oldDependency newDependency; + }) "got new dependency"; + + replacedependency-transitive = mkCheckOutput "replacedependency-transitive" (replaceDependency { + drv = transitive; + inherit oldDependency newDependency; + }) "got new dependency"; + + replacedependency-weird = + mkCheckOutput "replacedependency-weird" + (replaceDependency { + drv = basic; + inherit oldDependency; + newDependency = weirdDependency; + }) + '' + got weird dependency + got old dependency''; + + replacedependencies-precedence = mkCheckOutput "replacedependencies-precedence" (replaceDependencies + { + drv = basic; + replacements = [ { inherit oldDependency newDependency; } ]; + cutoffPackages = [ oldDependency ]; + } + ) "got new dependency"; + + replacedependencies-self = mkCheckOutput "replacedependencies-self" (replaceDependencies { + drv = basic; + replacements = [ + { + inherit oldDependency; + newDependency = oldDependency; + } + ]; + }) "got old dependency"; + + replacedependencies-deep-order1 = + mkCheckOutput "replacedependencies-deep-order1" + (replaceDependencies { + drv = deep; + replacements = [ + { + oldDependency = oldDependency1; + newDependency = newDependency1; + } + { + oldDependency = oldDependency2; + newDependency = newDependency2; + } + ]; + }) + '' + got new dependency 1 + got new dependency 2''; + + replacedependencies-deep-order2 = + mkCheckOutput "replacedependencies-deep-order2" + (replaceDependencies { + drv = deep; + replacements = [ + { + oldDependency = oldDependency2; + newDependency = newDependency2; + } + { + oldDependency = oldDependency1; + newDependency = newDependency1; + } + ]; + }) + '' + got new dependency 1 + got new dependency 2''; +}