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
|
#!/usr/bin/env bash
# constants
TEST_USER=sshcommand_user
TEST_KEY_NAME=test_key
TEST_KEY_DIR=/tmp/test_keys
# test functions
flunk() {
{ if [[ "$#" -eq 0 ]]; then cat -
else echo "$*"
fi
}
return 1
}
# ShellCheck doesn't know about $status from Bats
# shellcheck disable=SC2154
assert_success() {
if [[ "$status" -ne 0 ]]; then
flunk "command failed with exit status $status"
elif [[ "$#" -gt 0 ]]; then
assert_output "$1"
fi
}
assert_failure() {
if [[ "$status" -eq 0 ]]; then
flunk "expected failed exit status"
elif [[ "$#" -gt 0 ]]; then
assert_output "$1"
fi
}
assert_equal() {
if [[ "$1" != "$2" ]]; then
{ echo "expected: $1"
echo "actual: $2"
} | flunk
fi
}
# ShellCheck doesn't know about $output from Bats
# shellcheck disable=SC2154
assert_output() {
local expected
if [[ $# -eq 0 ]]; then
expected="$(cat -)"
else
expected="$1"
fi
assert_equal "$expected" "$output"
}
# ShellCheck doesn't know about $lines from Bats
# shellcheck disable=SC2154
assert_line() {
if [[ "$1" -ge 0 ]] 2>/dev/null; then
assert_equal "$2" "${lines[$1]}"
else
local line
for line in "${lines[@]}"; do
[[ "$line" = "$1" ]] && return 0
done
flunk "expected line \`$1'"
fi
}
refute_line() {
if [[ "$1" -ge 0 ]] 2>/dev/null; then
local num_lines="${#lines[@]}"
if [[ "$1" -lt "$num_lines" ]]; then
flunk "output has $num_lines lines"
fi
else
local line
for line in "${lines[@]}"; do
if [[ "$line" = "$1" ]]; then
flunk "expected to not find line \`$line'"
fi
done
fi
}
assert() {
if ! "$*"; then
flunk "failed: $*"
fi
}
assert_exit_status() {
assert_equal "$status" "$1"
}
# sshcommand helpers
create_user() {
sshcommand create $TEST_USER ls > /dev/null
}
delete_user() {
deluser --remove-home $TEST_USER
}
create_test_key() {
KEY_NAME="$1"; KEY_NAME=${KEY_NAME:=$TEST_KEY_NAME}
mkdir -p $TEST_KEY_DIR
ssh-keygen -N '' -q -f "$TEST_KEY_DIR/$KEY_NAME"
}
delete_test_keys() {
rm -rf $TEST_KEY_DIR
}
|