-
Notifications
You must be signed in to change notification settings - Fork 11
/
test
executable file
·139 lines (115 loc) · 5.11 KB
/
test
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
#!/usr/bin/env bash
# Copyright (C) 2011-2024 Bearstech - http://bearstech.com
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This is a trivial test harness : command to run, expected exit status,
# expected stdout (extended regex), expected stderr (extended regex), and
# a custom optional test (a shell fragment).
#
# Only non-empty fields are checked.
#
# Use .test-* files for input, they will be cleaned up for you.
#
assert() {
cmd="$1"
ex="$2"
out="$3"
err="$4"
more="$5"
eval "$cmd" >.test-stdout 2>.test-stderr
status=$?
_failed=$failed
if [ -n "$ex" -a "$ex" -ne $status ]; then
echo "FAIL: $cmd: expected status $ex, got $status"
let failed=$failed+1
fi
if [ -n "$out" ] && ! grep -qE "$out" .test-stdout; then
echo "FAIL: $cmd: expected '$out' in stdout"
let failed=$failed+1
fi
if [ -n "$err" ] && ! grep -qE "$err" .test-stderr; then
echo "FAIL: $cmd: expected '$err' in stdout"
let failed=$failed+1
fi
if [ -n "$more" ] && ! eval "$more"; then
echo "FAIL: $cmd: '$more' custom test failed"
let failed=$failed+1
fi
if [ $opt_stop = y -a $_failed != $failed ]; then
exit $failed
fi
rm -f .test-*
}
pussh=./pussh
opt_stop=n
case "$1" in
-s|--stop-on-error) opt_stop=y;;
-*) echo "Usage: $0 [-s|--stop-on-errorl]" >&2; exit 1
esac
failed=0
assert "$pussh" 1 '' 'missing command'
assert "$pussh --help" 0 '^Usage:'
assert "$pussh --version" 0 '^pussh [0-9]'
# Basic usage (-h/--hosts, -q/--quiet)
assert "$pussh -h localhost echo _TEST_" 0 '^localhost: _TEST_'
assert "$pussh --hosts localhost echo _TEST_" 0 '^localhost: _TEST_'
assert "$pussh -q -h localhost echo _TEST_" 0 '^_TEST_'
assert "$pussh --quiet -h localhost echo _TEST_" 0 '^_TEST_'
# -r/--rate
assert "$pussh -q -h localhost -r 100 echo _TEST_" 0 '^_TEST_'
assert "$pussh -q -h localhost --rate 100 echo _TEST_" 0 '^_TEST_'
# -s/--ssh-opt
assert "$pussh -q -h localhost -s '-p22' echo _TEST_" 0 '^_TEST_'
assert "$pussh -q -h localhost --ssh-opt '-p22' echo _TEST_" 0 '^_TEST_'
# -l/--login
assert "$pussh -q -h localhost -l $LOGNAME echo _TEST_" 0 '^_TEST_'
assert "$pussh -q -h localhost --login $LOGNAME echo _TEST_" 0 '^_TEST_'
# -n/--dry-run
assert "$pussh -q -h localhost -n echo _TEST_" 0 '^\[test\] localhost'
assert "$pussh -q -h localhost --dry-run echo _TEST_" 0 '^\[test\] localhost'
# -h/--hosts (multiple hosts)
assert "$pussh -h localhost,127.0.0.1 echo _TEST_" 0 '^(localhost|127\.0\.0\.1): _TEST_'
assert "$pussh -h localhost -h 127.0.0.1 echo _TEST_" 0 '^(localhost|127\.0\.0\.1): _TEST_'
assert "$pussh -h $LOGNAME@localhost echo _TEST_" 0 '^localhost: _TEST_'
assert "$pussh -h $LOGNAME@localhost:22 echo _TEST_" 0 '^localhost: _TEST_'
assert "$pussh -h localhost:22 echo _TEST_" 0 '^localhost: _TEST_'
# -f/--from-file
assert "echo localhost | $pussh -q -f - echo _TEST_" 0 '^_TEST_'
assert "echo localhost | $pussh -q --from-file - echo _TEST_" 0 '^_TEST_'
echo "localhost 127.0.0.1" >.test-in
assert "$pussh -f .test-in echo _TEST_" 0 '^(localhost|127\.0\.0\.1): _TEST_'
echo -e "localhost\n#nowhere" >.test-in
assert "$pussh -f .test-in echo _TEST_" 0 '^localhost: _TEST_'
# -u/--upload
assert "$pussh -q -h localhost -u /bin/echo arg1 arg2" 0 '^arg1 arg2'
assert "$pussh -q -h localhost --upload /bin/echo arg1 arg2" 0 '^arg1 arg2'
# -i/--input
echo "_TEST_" >.test-in
assert "$pussh -q -h localhost -i .test-in cat" 0 '^_TEST_'
echo "_TEST_" >.test-in
assert "$pussh -q -h localhost --input .test-in cat" 0 '^_TEST_'
echo "_TEST_" >.test-in-localhost
echo "_TEST_" >.test-in-127.0.0.1
assert "$pussh -h localhost,127.0.0.1 -i .test-in-%h cat" 0 '^(localhost|127\.0\.0\.1): _TEST_'
assert "$pussh -q -h localhost -i 'echo _TEST_|' cat" 0 '_TEST_'
assert "$pussh -q -h localhost,127.0.0.1 -i 'echo %h= _TEST_|' cat" 0 '^(localhost|127\.0\.0\.1)= _TEST_'
# -o/--output
assert "$pussh -q -h localhost -o .test-out echo _TEST_" 0 '' '' 'grep -qE ^_TEST_ .test-out'
assert "$pussh -q -h localhost --output .test-out echo _TEST_" 0 '' '' 'grep -qE ^_TEST_ .test-out'
assert "$pussh -q -h localhost,127.0.0.1 -o .test-out-%h echo _TEST_" 0 '' '' \
'grep -qE ^_TEST_ .test-out-localhost && grep -qE ^_TEST_ .test-out-127.0.0.1'
assert "$pussh -q -h localhost -o '|cat >.test-out' echo _TEST_" 0 '' '' 'grep -qE ^_TEST_ .test-out'
assert "$pussh -q -h localhost,127.0.0.1 -o '|cat >.test-out-%h' echo _TEST_" 0 '' '' \
'grep -qE ^_TEST_ .test-out-localhost && grep -qE ^_TEST_ .test-out-127.0.0.1'
exit $failed