# Helpers for the netssh test environment. Sourced by run.sh, the tests, and # shell.sh inside the client container. # shellcheck disable=SC1091 . /state/env RESULTS=${RESULTS:-/tmp/netssh-results} # new_home gives each test file its own netssh config, cache, notes, and rc files. new_home() { HOME=$(mktemp -d /tmp/home.XXXXXX) export HOME XDG_CONFIG_HOME="$HOME/.config" XDG_CACHE_HOME="$HOME/.cache" SHELL=/bin/bash mkdir -p "$XDG_CONFIG_HOME" "$XDG_CACHE_HOME" unset TMUX ZELLIJ NETSSH_NETBOX_URL NETSSH_NETBOX_TOKEN cd "$HOME" || exit 1 } # base_config prints both NetBox instances followed by stdin. base_config() { cat /state/netssh.yaml -; } # config writes the netssh config: both NetBox instances plus stdin. config() { base_config >"$XDG_CONFIG_HOME/netssh.yaml"; } # config_only writes stdin as the whole config (no NetBox). config_only() { cat >"$XDG_CONFIG_HOME/netssh.yaml"; } ok() { echo "ok $CURRENT: $1" >>"$RESULTS" printf ' \033[32mok\033[0m %s\n' "$1" } bad() { echo "FAIL $CURRENT: $1" >>"$RESULTS" printf ' \033[31mFAIL\033[0m %s\n' "$1" printf '%s\n' "$2" | sed 's/^/ /' } # check NAME CMD... — CMD must succeed. check() { local name=$1 out shift if out=$("$@" 2>&1); then ok "$name"; else bad "$name" "exit $?: $out"; fi } # expect_out NAME WANT CMD... — CMD must succeed and print WANT (substring). expect_out() { local name=$1 want=$2 out rc shift 2 out=$("$@" 2>&1) && rc=0 || rc=$? if [ "$rc" = 0 ] && [[ $out == *"$want"* ]]; then ok "$name"; else bad "$name" "want: $want got (exit $rc): $out"; fi } # expect_eq NAME WANT CMD... — CMD must succeed and print exactly WANT. expect_eq() { local name=$1 want=$2 out rc shift 2 out=$("$@" 2>&1) && rc=0 || rc=$? if [ "$rc" = 0 ] && [ "$out" = "$want" ]; then ok "$name"; else bad "$name" "want: $want got (exit $rc): $out"; fi } # expect_fail NAME WANT CMD... — CMD must fail and print WANT (substring). expect_fail() { local name=$1 want=$2 out rc shift 2 out=$("$@" 2>&1) && rc=0 || rc=$? if [ "$rc" != 0 ] && [[ $out == *"$want"* ]]; then ok "$name"; else bad "$name" "want failure with: $want got (exit $rc): $out"; fi } # wait_for TIMEOUT CMD... — retry CMD every 0.2 s until it succeeds. wait_for() { local deadline=$((SECONDS + $1)) shift until "$@" >/dev/null 2>&1; do [ $SECONDS -ge $deadline ] && return 1 sleep 0.2 done }