forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell_recorder.sh
executable file
·330 lines (273 loc) · 8.14 KB
/
shell_recorder.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/bin/sh
@INCLUDE_COMMON@
set -f
# -- Functions -----------------------------------------------------------------------------------------------------------------------------
cleanup() {
rm -f ./stdout ./stderr
rm -rf "$EXPORT_DIR"
}
execute() {
proto="$*"
if [ -z "$Mountpoint" ]; then
printf 'Error: no mountpoint specified in script\n'
exit 1
fi
if [ "$BACKUP" -eq '1' ]; then
if ! "$KDB" export "$MountpointRoot" dump > "$TMPFILE" 2> /dev/null; then
printf 'ERROR: Failed to backup %s\nStopping test case.\n' "$MountpointRoot"
exit 1
fi
BACKUP=0
"$KDB" rm -r "$MountpointRoot" 2> /dev/null
fi
[ -z "$Storage" ] && Storage="dump"
command=$(printf '%s' "$proto" | sed \
-e "s~\$Mountpoint~${Mountpoint}~g" \
-e "s~\$File~${DBFile}~g" \
-e "s~\$Storage~${Storage}~g" \
-e "s~\$MountArgs~${MountArgs}~g")
printf '%s\n' "$command"
[ -s "$OutFile" ] && printf '\n' >> "$OutFile"
printf 'CMD: %s\n' "$command" >> "$OutFile"
command=$(printf '%s' "$command" | sed "s~kdb\ ~\"$KDB\" ~g")
# ===============
# = INTERACTIVE =
# ===============
if [ x"$(first "$command")" = xinteractive ]; then
S=$(second "$command")
if [ -z "$S" ]; then
S=$SHELL
fi
if [ -z "$S" ]; then
S=bash
fi
echo "spawning interactive shell $S"
$S -i /dev/tty
return
fi
# ========
# = EXEC =
# ========
sh -c "$command" 2> stderr 1> stdout
RETVAL="$?"
# =======
# = RET =
# =======
printf 'RET: %s\n' "$RETVAL" >> "$OutFile"
if [ -n "$RETCMP" ]; then
executedTests=$((executedTests + 1))
if ! printf '%s' "$RETVAL" | grep -Ewq "$RETCMP"; then
printerr '\nERROR - RET:\nReturn value “%s” does not match “%s”\n\n' "$RETVAL" "$RETCMP"
printf '=== FAILED return value does not match expected pattern %s\n' "$RETCMP" >> "$OutFile"
numberErrors=$((numberErrors + 1))
fi
fi
# ==========
# = STDERR =
# ==========
STDERR=$(cat ./stderr)
[ -n "$STDERR" ] && printf 'STDERR: %s\n' "$STDERR" >> "$OutFile"
if [ -n "${STDERRCMP+unset}" ]; then
executedTests=$((executedTests + 1))
if ! printf '%s' "$STDERR" | replace_newline_return | grep -Eq "^$STDERRCMP\$"; then
printerr '\nERROR - STDERR:\n“%s”\ndoes not match\n“%s”\n\n' "$STDERR" "$STDERRCMP"
printf '=== FAILED stderr does not match expected pattern %s\n' "$STDERRCMP" >> "$OutFile"
numberErrors=$((numberErrors + 1))
fi
fi
# ==========
# = STDOUT =
# ==========
STDOUT=$(cat ./stdout)
[ -n "$STDOUT" ] && printf '%s\n' "STDOUT: $STDOUT" >> "$OutFile"
if [ -n "${STDOUTCMP+unset}" ]; then
executedTests=$((executedTests + 1))
if ! printf '%s' "$STDOUT" | replace_newline_return | grep -Fqx -- "$STDOUTCMP" ||
test -z "$STDOUTCMP" -a -n "$STDOUT"; then
printerr '\nERROR - STDOUT:\n“%s”\ndoes not match\n“%s”\n\n' "$STDOUT" "$STDOUTCMP"
printf '=== FAILED stdout does not match expected pattern %s\n' "$STDOUTCMP" >> "$OutFile"
numberErrors=$((numberErrors + 1))
fi
fi
# ================
# = STDOUT-REGEX =
# ================
if [ -n "$STDOUTRECMP" ]; then
executedTests=$((executedTests + 1))
if ! printf '%s' "$STDOUT" | replace_newline_return | grep -Eq -- "$STDOUTRECMP"; then
printerr '\nERROR - STDOUT:\n“%s”\ndoes not match\n“%s”\n\n' "$STDOUT" "$STDOUTRECMP"
printf '=== FAILED stdout does not match expected pattern %s\n' "$STDOUTRECMP" >> "$OutFile"
numberErrors=$((numberErrors + 1))
fi
fi
# ============
# = WARNINGS =
# ============
WARNINGS=$(printf '%s' "$STDERR" | sed -nE 's/.*warning (C[A-Z0-9]+):/\1/p' | tr '\n' ',' | sed 's/.$//')
[ -n "$WARNINGS" ] && printf 'WARNINGS: %s\n' "$WARNINGS" >> "$OutFile"
if [ -n "$WARNINGSCMP" ]; then
executedTests=$((executedTests + 1))
if ! printf '%s' "$WARNINGS" | replace_newline_return | grep -Eq -- "$WARNINGSCMP"; then
printerr '\nERROR - WARNINGS:\n“%s”\ndoes not match\n“%s”\n\n' "$WARNINGS" "$WARNINGSCMP"
printf '=== FAILED Warnings do not match expected pattern %s\n' "$WARNINGSCMP" >> "$OutFile"
numberErrors=$((numberErrors + 1))
fi
fi
# ==========
# = ERROR =
# ==========
ERROR=$(printf '%s' "$STDERR" | sed -nE 's/.*error (C[A-Z0-9]+):/\1/p')
[ -n "$ERROR" ] && printf 'ERROR: %s\n' "$ERROR" >> "$OutFile"
if [ -n "$ERRORCMP" ]; then
executedTests=$((executedTests + 1))
if ! printf '%s' "$ERROR" | replace_newline_return | grep -Eq -- "$ERRORCMP"; then
printerr '\nERROR - ERROR:\n“%s”\ndoes not match\n“%s”\n\n' "$ERROR" "$ERRORCMP"
printf '=== FAILED Errors do not match expected pattern %s\n' "$ERRORCMP" >> "$OutFile"
numberErrors=$((numberErrors + 1))
fi
fi
}
tail() {
printf '%s' "$*" | cut -sd ' ' -f2-
}
first() {
printf '%s' "$*" | cut -d ' ' -f1
}
second() {
printf '%s' "$*" | cut -sd ' ' -f2
}
run_script() {
while [ -n "$continuation" ] && line="$continuation" && continuation= || read -r line; do
OP=
cmd=$(first "$line")
case "$cmd" in
Mountpoint:)
Mountpoint=$(second "$line")
MountpointRoot=$(printf "$Mountpoint" | sed -E 's~(/?[^/]+).*~\1/~')
;;
File:)
DBFile=$(second "$line")
if [ "$DBFile" = "File:" ] || [ -z "$DBFile" ]; then
DBFile=$(mktempfile_elektra)
fi
;;
Storage:)
Storage=$(second "$line")
;;
MountArgs:)
MountArgs=$(tail "$line")
;;
RET:)
RETCMP=$(tail "$line")
;;
ERROR:)
ERRORCMP=$(tail "$line")
;;
WARNINGS:)
WARNINGSCMP=$(tail "$line")
;;
STDOUT:)
STDOUTCMP=$(tail "$line")
;;
STDOUT-REGEX:)
STDOUTRECMP=$(tail "$line")
;;
STDERR:)
STDERRCMP=$(printf '%s' "$line" | sed -E 's/[^:]+: ?(.*)/\1/')
;;
\<)
OP="$cmd"
[ "$ARG" ] && ARG="$ARG$NEWLINE"
ARG="$ARG$(tail "$line")"
read -r continuation
# Check for multiline commands
first "$continuation" | grep -q '<' && continue
;;
esac
if [ "$OP" = "<" ]; then
execute "$ARG"
RETCMP=
ERRORCMP=
WARNINGSCMP=
unset STDOUTCMP
STDOUTRECMP=
unset STDERRCMP
ARG=
fi
done < "$FILE"
}
# -- Main ----------------------------------------------------------------------------------------------------------------------------------
trap cleanup EXIT INT QUIT TERM
# Parse optional argument `-p`
OPTIND=1
printProtocol='false'
while getopts "p" opt; do
case "$opt" in
p)
printProtocol='true'
;;
esac
done
shift $((OPTIND - 1))
FILE=$1
Mountpoint=
DBFile=
Storage=
MountArgs=
DiffType=File
OutFile=$(mktempfile_elektra)
RETCMP=
ERRORCMP=
WARNINGSCMP=
unset STDOUTCMP
STDOUTRECMP=
BACKUP=0
TMPFILE=$(mktempfile_elektra)
# variables to count up errors and tests
numberErrors=0
executedTests=0
if [ "$#" -lt '1' ] || [ "$#" -gt '2' ]; then
printf 'Usage: %s [-p] input_script [protocol to compare]\n\n' "$0"
printf ' -p print protocol file\n' "$0"
rm "$OutFile"
exit 0
fi
if [ ! -f "$FILE" ]; then
printf 'File not found: %s' "$FILE"
rm "$OutFile"
exit 1
fi
BACKUP=1
EXPORT_DIR="$(mktempdir_elektra)"
export_config "$EXPORT_DIR"
MOUNTPOINTS_BACKUP=$("$KDB" mount)
run_script
"$KDB" rm -r "$MountpointRoot" 2> /dev/null
"$KDB" import "$MountpointRoot" dump 2> /dev/null < "$TMPFILE"
EVAL=0
if [ "$#" -eq '1' ]; then
printf 'shell_recorder %s RESULTS: %s test(s) done %s error(s).' "$1" "$executedTests" "$numberErrors"
EVAL=$numberErrors
fi
if [ "$#" -eq '2' ]; then
RESULT=$(diff -N --text "$2" "$OutFile" 2> /dev/null)
if [ "$?" -ne '0' ]; then
printerr '=======================================\nReplay test failed, protocols differ\n%s\n\n\n\n' "$RESULT"
EVAL=1
else
printf '=======================================\nReplay test succeeded\n'
fi
fi
if [ "$EVAL" -ne 0 ] || [ $printProtocol = 'true' ]; then
printerr '\n\n—— Protocol ————————————————————————————————————————————————————\n'
cat >&2 "$OutFile"
printerr '————————————————————————————————————————————————————————————————\n'
fi
rm -f "$OutFile"
# We disable the cleanup procedure temporarily, since we still need the exported configuration,
# if the tests changed the configuration permanently.
trap - EXIT
export_check "$EXPORT_DIR" 'Test'
trap cleanup EXIT
rm "${TMPFILE}"
exit "$EVAL"