-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.sh
executable file
·153 lines (126 loc) · 3.9 KB
/
test.sh
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
#!/bin/bash
binPath=$1
[ ! -x "${binPath}" ] && { echo "error: required 'bin-path' argument is either missing or not executable" >&2; exit 1; }
command -v jq >/dev/null || { echo "error: required dependency 'jq' is missing." >&2; exit 1; }
failCount=0
givenScript() {
local script="$1"
rm ./script ./output >/dev/null 2>&1
echo -e "#!/bin/sh\n${script}" > ./script
chmod +x ./script
}
assertEqual() {
local case="$1"
local expected="$2"
local actual="$3"
if [[ "${expected}" = "${actual}" ]]; then
echo -e "\033[32m✔ ${case}\033[0m"
else
echo -e "\033[31m✘ ${case}\033[0m"
echo " Failed asserting that values match:"
echo " Expected: ${expected}"
echo " Actual: ${actual}"
failCount=$((failCount + 1))
fi
}
assertExitCode() {
local case="$1"
local expected="$2"
local command="$3"
${command} >/dev/null 2>&1
local exitCode=$?
assertEqual "${case}" "${expected}" ${exitCode}
}
#
# Given: Invocation with `--version`
# Then: It reports the version configured in Cargo.toml
#
assertEqual \
"It reports the version configured in Cargo.toml" \
"$(cargo metadata 2>/dev/null | jq -r '.packages[] | select(.name == "retry-cli") | .version')" \
"$(${binPath} --version | cut -d' ' -f2)"
#
# Given: The child exit code is zero
# Then: The exit code is zero
#
givenScript "exit 0"
assertExitCode "The exit code is zero when the child exit code is zero" 0 "${binPath} -- ./script"
#
# Given: The child exit code is non-zero
# Then: The exit code matches the child exit code
#
givenScript "exit 2"
assertExitCode "The exit code matches the child exit code when the child exit code is non-zero" 2 "${binPath} --attempts 1 -- ./script"
#
# Given: The child exit code is non-zero
# Then: The exit code matches the last child exit code
#
givenScript "echo . >>output; exit \"\$(wc -l output | awk '{print \$1}')\""
assertExitCode "The exit code matches the last child exit code when the child exit code is non-zero" 3 "${binPath} --attempts 3 --delay 0s -- ./script"
#
# Given: The child is running
# When: A stop signal is received
# Then: The stop signal is sent to the child
#
givenScript "
trap 'echo \"child received TERM signal\" && exit 0' TERM
i=0
while [ \$i -lt 3 ]; do
i=\$((i+1))
sleep 1
done
"
${binPath} -- ./script >output &
processId=$!
sleep 1 # Give the child time to start; TODO: find a better way
kill -s TERM ${processId}
wait ${processId} 2>/dev/null
assertEqual \
"Stop signals are sent to the child" \
"child received TERM signal" \
"$(cat ./output)"
#
# Given: The child is running,
# and there are attempts remaining
# When: The child exits non-zero,
# and a stop signal is received
# Then: No further attempts occur
#
givenScript "exit 1"
${binPath} --attempts 3 --delay 1s -- ./script >output 2>&1 &
processId=$!
sleep 1 # Give the child time to fail at least once
outputBeforeStop=$(cat ./output)
kill -s TERM ${processId}
wait ${processId} 2>/dev/null
assertEqual \
"No further attempts occur when the child exits non-zero and a stop signal is received" \
"${outputBeforeStop}" \
"$(cat ./output)"
#
# Given: The child is running,
# and there are attempts remaining
# When: A stop signal is received,
# and the child exits non-zero while handling the signal
# Then: No further attempts occur
#
givenScript "
trap 'echo \"child received TERM signal\" && exit 1' TERM
echo .
i=0
while [ \$i -lt 3 ]; do
i=\$((i+1))
sleep 1
done
"
${binPath} --attempts 2 -- ./script >output 2>/dev/null &
processId=$!
sleep 1 # Give the child time to start; TODO: find a better way
kill -s TERM ${processId}
wait ${processId} 2>/dev/null
assertEqual \
"No further attempts occur when a stop signal is received and the child exits non-zero while handling the signal" \
"child received TERM signal" \
"$(tail -n 1 ./output)"
rm ./script ./output >/dev/null 2>&1
[ ${failCount} = 0 ] && exit 0 || exit 1