diff options
| author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-10-30 15:15:04 +0100 |
|---|---|---|
| committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-10-30 15:15:04 +0100 |
| commit | 43dac390967e2e85da0b3fcd84043a77cfaddfd0 (patch) | |
| tree | 93f09b804bcded8b59db6f619e18ec4b6265b8c2 /tests/old/infrastructure | |
| parent | treewide: Align files with the same files in the `nixos-server` repo (diff) | |
| download | nixos-config-43dac390967e2e85da0b3fcd84043a77cfaddfd0.zip | |
treewide: Align the rest of the files with the server repository
Diffstat (limited to 'tests/old/infrastructure')
| -rw-r--r-- | tests/old/infrastructure/clean.awk | 23 | ||||
| -rw-r--r-- | tests/old/infrastructure/default.nix | 177 | ||||
| -rw-r--r-- | tests/old/infrastructure/driver.sh | 176 | ||||
| -rw-r--r-- | tests/old/infrastructure/run.nix | 54 |
4 files changed, 430 insertions, 0 deletions
diff --git a/tests/old/infrastructure/clean.awk b/tests/old/infrastructure/clean.awk new file mode 100644 index 00000000..0362d38b --- /dev/null +++ b/tests/old/infrastructure/clean.awk @@ -0,0 +1,23 @@ +# nixos-config - My current NixOS configuration +# +# Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de> +# SPDX-License-Identifier: GPL-3.0-or-later +# +# This file is part of my nixos-config. +# +# You should have received a copy of the License along with this program. +# If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>. + +{ + # 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/old/infrastructure/default.nix b/tests/old/infrastructure/default.nix new file mode 100644 index 00000000..cd21198c --- /dev/null +++ b/tests/old/infrastructure/default.nix @@ -0,0 +1,177 @@ +# nixos-config - My current NixOS configuration +# +# Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de> +# SPDX-License-Identifier: GPL-3.0-or-later +# +# This file is part of my nixos-config. +# +# You should have received a copy of the License along with this program. +# If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>. +{ + pkgs, + myPkgs, + lib, + nixos-lib, + extraModules, + sysLib, + nixpkgs_open_prs, + system, +}: { + name, + configuration, + description, + hash, + testData, + testShell ? pkgs.dash, + alternateScreen ? false, +}: +nixos-lib.runTest { + hostPkgs = pkgs; # the Nixpkgs package set used outside the VMs + + inherit name; + + node = { + specialArgs = { + inherit + myPkgs + sysLib + nixpkgs_open_prs + system + ; + }; + # 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"; + testTmux = lib.getExe pkgs.tmux; + in + /* + python + */ + '' + start_all() + + __TEST_TMUX="${testTmux}" + + 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="${testTmux}" + __TEST_SHELL="${lib.getExe testShell}" + __TEST_TMUX_PANE="__TEST_TMUX_PANE" + + __TEST_EVAL_USE_ALTERNATE_SCREEN="${ + if alternateScreen + then "true" + else "false" + }" + __TEST_EVAL_AWK_CLEAN_FILE="${./clean.awk}" + __TEST_EVAL_LOG_FILE="${logFile}" + __TEST_EVAL_GOLDEN_FILE="$(mktemp)" + printf "%s" "${goldenFile}" >"$__TEST_EVAL_GOLDEN_FILE" + + . ${./driver.sh} + + "$__TEST_TMUX" new-session -d -s "$__TEST_TMUX_PANE" "$__TEST_SHELL" + + # Warm up the shell in the tmux sesssion + "$__TEST_TMUX" send-keys -t "$__TEST_TMUX_PANE" "echo hi" "Enter" "clear" "Enter" + sleep 2 + + "$__TEST_TMUX" pipe-pane -t "$__TEST_TMUX_PANE" -o 'cat >>${goldenFile}' + + __test_eval "${description}" + + # Remove the pipe + "$__TEST_TMUX" pipe-pane -t "$__TEST_TMUX_PANE" + + # Check if the golden file was changed. + if [ "$(cat "$__TEST_EVAL_GOLDEN_FILE")" != "${goldenFile}" ]; then + mv "$(cat "$__TEST_EVAL_GOLDEN_FILE")" "${goldenFile}" + fi + + rm "$__TEST_EVAL_GOLDEN_FILE" + ''}") + + 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/old/infrastructure/driver.sh b/tests/old/infrastructure/driver.sh new file mode 100644 index 00000000..a8b241d2 --- /dev/null +++ b/tests/old/infrastructure/driver.sh @@ -0,0 +1,176 @@ +#! /usr/bin/env bash + +# nixos-config - My current NixOS configuration +# +# Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de> +# SPDX-License-Identifier: GPL-3.0-or-later +# +# This file is part of my nixos-config. +# +# You should have received a copy of the License along with this program. +# If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>. + +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 +} + +# Use bash built-ins to trim a string +# source: https://stackoverflow.com/a/3352015 +trim() { + local var="$*" + # remove leading whitespace characters + var="${var#"${var%%[![:space:]]*}"}" + # remove trailing whitespace characters + var="${var%"${var##*[![:space:]]}"}" + printf '%s' "$var" +} + +# contains(string, substring) +# +# Returns 0 if the specified string contains the specified substring, +# otherwise returns 1. +contains() { + string="$1" + substring="$2" + if [ "${string#*"$substring"}" != "$string" ]; then + return 0 # $substring is in $string + else + return 1 # $substring is not in $string + fi +} + +__test_wait_for_pid() { + local pre_pids_file="$1" + + exec_pids="$(mktemp)" + ps -eo tty,pid | awk "--assign=tmuxTty=$tmux_tty" '{if ($1 == tmuxTty) {print $2}}' >"$exec_pids" + + while read -r pid; do + sed --in-place "s/$pid//" "$exec_pids" + done <"$pre_pids_file" + + gawk --include inplace '{if (NF) {print $0}}' "$exec_pids" + + if [ "$(wc -l <"$exec_pids")" -eq 0 ]; then + # No further spawned processes left + return 0 + else + # Some other program is still running + pid="$(tail -n 1 "$exec_pids")" + rm "$exec_pids" + name="$(trim "$(tr '\0' ' ' <"/proc/$pid/cmdline")")" + + msg "Waiting until command ('$name') finishes (has pid: $pid).." + + # This allows waiting for non-children of the current shell + # source: https://stackoverflow.com/a/76046235 + tail --pid "$pid" --follow /dev/null & + wait $! + + # Give the `testShell` some time, to process the next command from a chained command. + sleep 0.2 + __test_wait_for_pid "$pre_pids_file" + 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" + ;; + "Exec") + local pre_exec_pids + local tmux_tty + + msg "Executing command '$args'.." + tmux_tty="$("$tmux" list-panes -t "$tpane" -F "#{pane_tty}" | sed 's|/dev/||')" + + pre_exec_pids="$(mktemp)" + ps -eo tty,pid | awk "--assign=tmuxTty=$tmux_tty" '{if ($1 == tmuxTty) {print $2}}' >"$pre_exec_pids" + + "$tmux" send-keys -t "$tpane": "$args" "Enter" + sleep 1 + + __test_wait_for_pid "$pre_exec_pids" + + rm "$pre_exec_pids" + msg "Finished command '$args'." + ;; + "Expect" | "ExpectNot") + msg "Trying to match regex ('$args') for currently visible content.." + + get_plane_text() { + alternate="" + [ "$__TEST_EVAL_USE_ALTERNATE_SCREEN" = "true" ] && alternate="-a" + + "$tmux" capture-pane -t "$tpane" -p $alternate -S 0 -E - + } + + matched="" + if get_plane_text | 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: + get_plane_text | 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: + get_plane_text | msg + + exit 1 + fi + ;; + *) + msg "Entered unrechable code. This is a bug." + exit 1 + ;; + esac + ;; + "SetGolden") + msg "Trying to set '$args' as golden file." + [ -f "$args" ] || { + msg "Argument is not a file!" + exit 1 + } + printf "%s" "$args" >"$__TEST_EVAL_GOLDEN_FILE" + msg "Set golden file to: '$args'" + ;; + *) + msg "Unrecognized command: '$cmd'" + exit 1 + ;; + esac + done +} diff --git a/tests/old/infrastructure/run.nix b/tests/old/infrastructure/run.nix new file mode 100644 index 00000000..3148a2d9 --- /dev/null +++ b/tests/old/infrastructure/run.nix @@ -0,0 +1,54 @@ +# nixos-config - My current NixOS configuration +# +# Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de> +# SPDX-License-Identifier: GPL-3.0-or-later +# +# This file is part of my nixos-config. +# +# You should have received a copy of the License along with this program. +# If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>. +{ + pkgs, + lib, +}: +pkgs.writeShellScriptBin "run_test_description" '' + set -eu + + [ "$#" -ne 1 ] && { + echo "Usage: $0 <Test description file>"; + exit 2 + } + description="$(realpath "$1")" + + base="$(mktemp -d)" + cd "$base" + + __TEST_TMUX="${lib.getExe pkgs.tmux}" + __TEST_SHELL="${lib.getExe pkgs.dash}" + __TEST_TMUX_PANE="__TEST_TMUX_PANE" + + __TEST_EVAL_USE_ALTERNATE_SCREEN="false" + __TEST_EVAL_AWK_CLEAN_FILE="${./clean.awk}" + __TEST_EVAL_LOG_FILE="__test_log" + __TEST_EVAL_GOLDEN_FILE="$(mktemp)" + printf "%s" "__test_golden" >"$__TEST_EVAL_GOLDEN_FILE" + + . ${./driver.sh} + + echo "Setting up a session.." + if "$__TEST_TMUX" has-session -t "$__TEST_TMUX_PANE"; then + echo "Killing old '$__TEST_TMUX_PANE' session." + "$__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" + + echo "Base dir: $base" +'' |
