about summary refs log tree commit diff stats
path: root/tests/infrastructure/default.nix
diff options
context:
space:
mode:
Diffstat (limited to 'tests/infrastructure/default.nix')
-rw-r--r--tests/infrastructure/default.nix136
1 files changed, 136 insertions, 0 deletions
diff --git a/tests/infrastructure/default.nix b/tests/infrastructure/default.nix
new file mode 100644
index 00000000..4f1ec7a2
--- /dev/null
+++ b/tests/infrastructure/default.nix
@@ -0,0 +1,136 @@
+{
+  pkgs,
+  myPkgs,
+  lib,
+  nixos-lib,
+  extraModules,
+  ...
+}: {
+  name,
+  configuration,
+  description,
+  hash,
+  testData,
+}:
+nixos-lib.runTest {
+  hostPkgs = pkgs; # the Nixpkgs package set used outside the VMs
+
+  inherit name;
+
+  node = {
+    specialArgs = {
+      inherit myPkgs;
+    };
+    # Use the nixpkgs as constructed by the `nixpkgs.*` options
+    pkgs = null;
+  };
+
+  nodes = {
+    machine = {config, ...}: {
+      imports =
+        [
+          extraModules.home-manager
+          ../../modules/by-name/us/users/module.nix
+        ]
+        ++ configuration.imports;
+
+      config = lib.modules.mkMerge [
+        {
+          soispha = {
+            users = {
+              enable = true;
+
+              # The password is 'test'.
+              hashedPassword = "$y$j9T$yYYUpE9OaxmEO8MaPI2jr0$WGpYYWaLySakI.Mwqz4sljGSOetAp4s5CIUa1VUU1l2";
+            };
+          };
+          home-manager.users.soispha.home.stateVersion = "24.11";
+        }
+        configuration.config
+      ];
+    };
+  };
+
+  testScript = {nodes, ...}: let
+    testDir = "${nodes.machine.home-manager.users.soispha.home.homeDirectory}/test";
+    goldenFile = "${testDir}/__test_golden";
+    logFile = "${testDir}/__test_log";
+  in
+    /*
+    python
+    */
+    ''
+      start_all()
+
+      machine.succeed("sudo -u soispha ${pkgs.writeShellScript "mkTestEnvironment" ''
+        set -e
+
+        mkdir --parents "${testDir}"
+        cd "${testDir}"
+
+        ${
+          lib.strings.concatStringsSep "\n\n" (
+            builtins.attrValues
+            (builtins.mapAttrs
+              (name: value: ''
+                mkdir --parents "$(dirname '${name}')"
+                cp --recursive '${value}' '${name}'
+              '')
+              testData)
+          )
+        }
+      ''}")
+
+      machine.succeed("sudo -u soispha ${pkgs.writeShellScript "mkGoldenRecord" ''
+        set -e
+
+        # HACK: Prevent zsh from complaining about missing configuration.
+        # TODO: These tests should probably just run under bash. <2024-11-22>
+        touch ~soispha/.zshrc
+
+        cd "${testDir}"
+
+        __TEST_TMUX="${lib.getExe pkgs.tmux}"
+        __TEST_TMUX_PANE="__TEST_TMUX_PANE"
+
+        __TEST_EVAL_AWK_CLEAN_FILE="${./clean.awk}"
+        __TEST_EVAL_LOG_FILE="${logFile}"
+
+        . ${./driver.sh}
+
+        "$__TEST_TMUX" new-session -d -s "$__TEST_TMUX_PANE"
+        "$__TEST_TMUX" pipe-pane -t "$__TEST_TMUX_PANE" -o 'cat >>${goldenFile}'
+
+        __test_eval "${description}"
+
+        # Clear the pipe again
+        "$__TEST_TMUX" pipe-pane -t "$__TEST_TMUX_PANE"
+      ''}")
+
+      machine.succeed("sudo -u soispha ${pkgs.writeShellScript "testHashOfGolden" ''
+          set -e
+
+          cd "${testDir}"
+        ''
+        + lib.optionalString (hash != null)
+        /*
+        bash
+        */
+        ''
+          golden_hash="$(sha256sum ${goldenFile} | awk '{print $1}')"
+
+          if [ "$golden_hash" != "${hash}" ]; then
+            echo "Hash mismatch."
+            echo "Expected '${hash}',"
+            echo "but got '$golden_hash'"
+            exit 1
+          else
+            echo "Hash was successfully checked."
+          fi
+        ''}")
+
+
+      machine.copy_from_vm("${goldenFile}", "golden")
+      machine.copy_from_vm("${logFile}", "log")
+    '';
+}