1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
#!/bin/sh
#
# SPDX-FileCopyrightText: Peter Pentchev <roam@ringlet.net>
# SPDX-License-Identifier: BSD-2-Clause
set -e
ensure_empty()
{
local filename="$1"
if [ -s "$filename" ]; then
echo "Did not expect anything in $filename" 1>&2
cat -- "$filename"
exit 1
fi
}
check_output()
{
local pattern="$1" filename="$2"
local prefix='^@REMOTE_TEMP_DIR@'
local pattern_stripped="${pattern#$prefix}"
if [ "$pattern_stripped" != "$pattern" ]; then
echo "Replacing $prefix by ^$remote_temp_dir_pattern in $pattern"
pattern="^$remote_temp_dir_pattern$pattern_stripped"
echo "- got $pattern"
fi
if ! grep -Eqe "$pattern" -- "$filename"; then
echo 'Unexpected output:' 1>&2
cat -- "$filename" 1>&2
exit 1
fi
}
test_bad()
{
local filename="$1" pattern="$2" prog_opts="$3"
shift 3
: > "$tempf"
# shellcheck disable=SC2086 # prog_opts may be more than one argument
if "$remrun" \
${REMRUN_TEST_LOCAL_CKSUM_CMD:+-C "$REMRUN_TEST_LOCAL_CKSUM_CMD"} \
${REMRUN_TEST_REMOTE_CKSUM_CMD:+-c "$REMRUN_TEST_REMOTE_CKSUM_CMD"} \
${REMRUN_TEST_REMOTE_TMP:+-T "$REMRUN_TEST_REMOTE_TMP"} \
"$@" -- "$REMRUN_TEST_HOSTSPEC" "$filename" $prog_opts > "$tempf"; then
echo 'Unexpected success; here is the output:' 1>&2
cat -- "$tempf" 1>&2
exit 1
fi
if [ -z "$pattern" ]; then
ensure_empty "$tempf"
else
check_output "$pattern" "$tempf"
fi
}
test_good()
{
local filename="$1" pattern="$2" prog_opts="$3"
shift 3
: > "$tempf"
# shellcheck disable=SC2086 # prog_opts may be more than one argument
if ! "$remrun" \
${REMRUN_TEST_LOCAL_CKSUM_CMD:+-C "$REMRUN_TEST_LOCAL_CKSUM_CMD"} \
${REMRUN_TEST_REMOTE_CKSUM_CMD:+-c "$REMRUN_TEST_REMOTE_CKSUM_CMD"} \
${REMRUN_TEST_REMOTE_TMP:+-T "$REMRUN_TEST_REMOTE_TMP"} \
"$@" -- "$REMRUN_TEST_HOSTSPEC" "$filename" $prog_opts > "$tempf"; then
echo 'Unexpected failure; here is the output:' 1>&2
cat -- "$tempf" 1>&2
exit 1
fi
if [ -z "$pattern" ]; then
ensure_empty "$tempf"
else
check_output "$pattern" "$tempf"
fi
}
if [ "$#" -ne 1 ]; then
echo 'Usage: run-test.sh /path/to/remrun' 1>&2
exit 1
fi
remrun="$1"
if [ -z "$REMRUN_TEST_HOSTSPEC" ]; then
echo 'No REMRUN_TEST_HOSTSPEC in the environment, skipping the test suite'
exit 0
fi
datadir="$(dirname -- "$0")/test-data"
if [ ! -d "$datadir" ] || [ ! -f "$datadir/whoami.sh" ]; then
echo "Invalid datadir detected: $datadir" 1>&2
exit 1
fi
tempf="$(mktemp remrun-test.XXXXXX)"
# shellcheck disable=SC2064 # we *mean* to not expand it later
trap "rm -f -- '$tempf'" HUP INT EXIT QUIT TERM
if [ -z "$REMRUN_TEST_REMOTE_TMP" ]; then
remote_temp_dir_pattern='\.'
else
remote_temp_dir_pattern="$(printf -- "$REMRUN_TEST_REMOTE_TMP" | sed -e 's/[^A-Za-z0-9_-]/\\&/g')"
fi
printf '\n=== Fail with a nonexistent local file\n\n'
test_bad /nonexistent '' ''
printf '\n=== Do not run "whoami" on the remote host\n\n'
test_good "$datadir/whoami.sh" '^@REMOTE_TEMP_DIR@/remrun\.[A-Za-z0-9._-]{6}$' '' -v -N
printf '\n=== Run "whoami" on the remote host\n\n'
test_good "$datadir/whoami.sh" '^[A-Za-z0-9._-]+$' ''
printf '\n=== Fail with mismatched local and remote checksum commands\n\n'
test_bad "$datadir/whoami.sh" '' '' -C cksum
printf '\n=== Fail again with mismatched local and remote checksum commands\n\n'
test_bad "$datadir/whoami.sh" '' '' -c cksum
printf '\n=== Use the same non-default checksum command on both sides\n\n'
test_good "$datadir/whoami.sh" '^[A-Za-z0-9._-]+$' '' -C cksum -c cksum
printf '\n=== Run "wc" on an empty stream, expected: 0 0 0 -\n\n'
test_good "$datadir/wc.sh" '^[[:space:]]*0[[:space:]]+0[[:space:]]+0$' '' -v
printf '\n=== Run "wc" on a non-empty stream\n\n'
test_good "$datadir/wc.sh" '^[[:space:]]*2[[:space:]]+9[[:space:]]+37$' '' -v -s < "$datadir/testfile.txt"
printf '\n=== Run a script that will fail\n\n'
test_bad "$datadir/fail.sh" 'I am failing' ''
printf '\n=== Run a script read from the standard input stream\n\n'
test_good - '^[A-Za-z0-9._-]+$' '' < "$datadir/whoami.sh"
printf '\n=== Do not allow -s and - together\n\n'
test_bad - '' '' -s < "$datadir/whoami.sh"
printf '\n=== Pass command-line arguments to the executed program\n\n'
test_good "$datadir/count.sh" '^12345$' 5
printf '\n=== Pass command-line arguments to the executed program 2\n\n'
test_good "$datadir/count.sh" '^0$' ''
printf '\n=== Pass command-line arguments to the executed program 3\n\n'
test_bad "$datadir/count.sh" '^Something weird' '5 6'
printf '\n=== All fine\n\n'
|