diff options
Diffstat (limited to '')
-rw-r--r-- | tests/infrastructure/clean.awk | 13 | ||||
-rw-r--r-- | tests/infrastructure/default.nix | 136 | ||||
-rw-r--r-- | tests/infrastructure/driver.sh | 72 | ||||
-rw-r--r-- | tests/infrastructure/run.nix | 34 |
4 files changed, 255 insertions, 0 deletions
diff --git a/tests/infrastructure/clean.awk b/tests/infrastructure/clean.awk new file mode 100644 index 00000000..1208b1ef --- /dev/null +++ b/tests/infrastructure/clean.awk @@ -0,0 +1,13 @@ +{ + # Shell like comments + gsub(/^#.*$/, "", $0) + + # Strip leading and trailing white space + gsub(/^[[:blank:]]*/, "", $0) + gsub(/[[:blank:]]*$/, "", $0) + + # Only accept the line, if it contains something + if (NF) { + print $0 + } +} 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") + ''; +} diff --git a/tests/infrastructure/driver.sh b/tests/infrastructure/driver.sh new file mode 100644 index 00000000..52f7d3ad --- /dev/null +++ b/tests/infrastructure/driver.sh @@ -0,0 +1,72 @@ +#! /usr/bin/env sh +set -e + +msg() { + if [ "$#" -ne 0 ]; then + echo "$@" | tee --append "$__TEST_EVAL_LOG_FILE" >&2 + else + cat | tee --append "$__TEST_EVAL_LOG_FILE" >&2 + fi +} + +__test_eval() { + tmux="$__TEST_TMUX" + tpane="$__TEST_TMUX_PANE" + file="$1" + + awk --file "$__TEST_EVAL_AWK_CLEAN_FILE" "$file" | while read -r cmd args; do + case "$cmd" in + "Type") + msg "Sending keys to application '$args'.." + "$tmux" send-keys -t "$tpane": "$args" + ;; + "Sleep") + msg "Sleeping for '$args' seconds.." + sleep "$args" + ;; + "Expect" | "ExpectNot") + msg "Trying to match regex ('$args') for currently visible content.." + + matched="" + if "$tmux" capture-pane -t "$tpane" -p -S 0 -E - | grep "$args"; then + matched=true + else + matched=false + fi + + case "$cmd" in + "Expect") + if [ "$matched" = true ]; then + msg "Regex matched." + else + msg "Failed to find string, matched by regex '$args' on the screen" + msg current screen: + "$tmux" capture-pane -t "$tpane" -p -S 0 -E - | msg + + exit 1 + fi + ;; + "ExpectNot") + if [ "$matched" = false ]; then + msg "Regex successfully not matched." + else + msg "Found to find string, matched by regex '$args' on the screen. But expected none" + msg current screen: + "$tmux" capture-pane -t "$tpane" -p -S 0 -E - | msg + + exit 1 + fi + ;; + *) + msg "Entered unrechable code. This is a bug." + exit 1 + ;; + esac + ;; + *) + msg "Unrecognized command: '$cmd'" + exit 1 + ;; + esac + done +} diff --git a/tests/infrastructure/run.nix b/tests/infrastructure/run.nix new file mode 100644 index 00000000..91120ef4 --- /dev/null +++ b/tests/infrastructure/run.nix @@ -0,0 +1,34 @@ +{ + pkgs, + lib, +}: +pkgs.writeShellScript "run_test_description" '' + set -e + + [ "$#" -ne 1 ] && { + echo "Usage: $0 <Test description file>"; + exit 2 + } + description="$1" + + __TEST_TMUX="${lib.getExe pkgs.tmux}" + __TEST_TMUX_PANE="__TEST_TMUX_PANE" + __TEST_AWK_CLEAN_FILE="${./clean.awk}" + + . ${./driver.sh} + + echo "Setting up a session.." + if "$__TEST_TMUX" has-session -t "$__TEST_TMUX_PANE"; then + echo "Killing old '$__TEST_TMUX_PANE'" + "$__TEST_TMUX" kill-session -t "$__TEST_TMUX_PANE" + fi + "$__TEST_TMUX" new-session -d -s "$__TEST_TMUX_PANE" + + echo "Initializing pipe.." + "$__TEST_TMUX" pipe-pane -t "$__TEST_TMUX_PANE" -o 'cat >>./test.golden' + + echo "Evaluating description.." + __test_eval "$description" + + "$__TEST_TMUX" pipe-pane -t "$__TEST_TMUX_PANE" +'' |