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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
#!/bin/sh
#
# Copyright (c) Linux Test Project, 2014-2017
#
# 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 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Written by Cyril Hrubis <chrubis@suse.cz>
#
# This is a LTP test library for shell.
#
export LTP_RET_VAL=0
export TST_COUNT=1
export TST_PASS_COUNT=0
export TST_LIB_LOADED=1
export TST_TMPDIR_RHOST=0
. tst_ansi_color.sh
# Exit values map
tst_flag2mask()
{
case "$1" in
TPASS) return 0;;
TFAIL) return 1;;
TBROK) return 2;;
TWARN) return 4;;
TINFO) return 16;;
TCONF) return 32;;
*) tst_brkm TBROK "Invalid resm type '$1'";;
esac
}
tst_resm()
{
local ttype="$1"
tst_flag2mask "$ttype"
local mask=$?
LTP_RET_VAL=$((LTP_RET_VAL|mask))
local ret=$1
shift
printf "$TCID $TST_COUNT "
tst_print_colored $ret "$ret:"
echo " $@"
case "$ret" in
TPASS|TFAIL|TCONF) TST_COUNT=$((TST_COUNT+1));;
esac
if [ "$ret" = TPASS ]; then
TST_PASS_COUNT=$((TST_PASS_COUNT+1))
fi
}
tst_brkm()
{
case "$1" in
TFAIL) ;;
TBROK) ;;
TCONF) ;;
*) tst_brkm TBROK "Invalid tst_brkm type '$1'";;
esac
local ret=$1
shift
tst_resm "$ret" "$@"
tst_exit
}
tst_record_childstatus()
{
if [ $# -ne 1 ]; then
tst_brkm TBROK "Requires child pid as parameter"
fi
local child_pid=$1
local ret=0
wait $child_pid
ret=$?
if [ $ret -eq 127 ]; then
tst_brkm TBROK "Child process pid='$child_pid' does not exist"
fi
LTP_RET_VAL=$((LTP_RET_VAL|ret))
}
tst_require_root()
{
if [ "$(id -ru)" != 0 ]; then
tst_brkm TCONF "Must be super/root for this test!"
fi
}
tst_exit()
{
if [ -n "${TST_CLEANUP:-}" -a -z "${TST_NO_CLEANUP:-}" ]; then
$TST_CLEANUP
fi
if [ -n "${LTP_IPC_PATH:-}" -a -f "${LTP_IPC_PATH:-}" ]; then
rm -f "$LTP_IPC_PATH"
fi
# Mask out TCONF if no TFAIL/TBROK/TWARN but has TPASS
if [ $((LTP_RET_VAL & 7)) -eq 0 -a $TST_PASS_COUNT -gt 0 ]; then
LTP_RET_VAL=$((LTP_RET_VAL & ~32))
fi
# Mask out TINFO
exit $((LTP_RET_VAL & ~16))
}
tst_tmpdir()
{
if [ -z "$TMPDIR" ]; then
export TMPDIR="/tmp"
fi
TST_TMPDIR=$(mktemp -d "$TMPDIR/$TCID.XXXXXXXXXX")
chmod 777 "$TST_TMPDIR"
TST_STARTWD=$(pwd)
cd "$TST_TMPDIR"
}
tst_rmdir()
{
if [ -n "$TST_TMPDIR" ]; then
cd "$LTPROOT"
rm -r "$TST_TMPDIR"
[ "$TST_TMPDIR_RHOST" = 1 ] && tst_cleanup_rhost
fi
}
#
# Checks if commands passed as arguments exists
#
tst_require_cmds()
{
local cmd
for cmd in $*; do
if ! command -v $cmd > /dev/null 2>&1; then
tst_brkm TCONF "'$cmd' not found"
fi
done
}
# tst_retry "command" [times]
# try run command for specified times, default is 3.
# Function returns 0 if succeed in RETRIES times or the last retcode the cmd
# returned
tst_retry()
{
local cmd="$1"
local RETRIES=${2:-"3"}
local i=$RETRIES
while [ $i -gt 0 ]; do
eval "$cmd"
ret=$?
if [ $ret -eq 0 ]; then
break
fi
i=$((i-1))
sleep 1
done
if [ $ret -ne 0 ]; then
tst_resm TINFO "Failed to execute '$cmd' after $RETRIES retries"
fi
return $ret
}
# tst_timeout "command arg1 arg2 ..." timeout
# Runs command for specified timeout (in seconds).
# Function returns retcode of command or 1 if arguments are invalid.
tst_timeout()
{
local command=$1
local timeout=$(echo $2 | grep -o "^[0-9]\+$")
# command must be non-empty string with command to run
if [ -z "$command" ]; then
echo "first argument must be non-empty string"
return 1
fi
# accept only numbers as timeout
if [ -z "$timeout" ]; then
echo "only numbers as second argument"
return 1
fi
setsid sh -c "eval $command" 2>&1 &
local pid=$!
while [ $timeout -gt 0 ]; do
kill -s 0 $pid 2>/dev/null
if [ $? -ne 0 ]; then
break
fi
timeout=$((timeout - 1))
sleep 1
done
local ret=0
if [ $timeout -le 0 ]; then
ret=128
kill -TERM -- -$pid
fi
wait $pid
ret=$((ret | $?))
return $ret
}
ROD_SILENT()
{
local tst_out
tst_out="$($@ 2>&1)"
if [ $? -ne 0 ]; then
echo "$tst_out"
tst_brkm TBROK "$@ failed"
fi
}
ROD_BASE()
{
local cmd
local arg
local file
local flag
for arg; do
file="${arg#\>}"
if [ "$file" != "$arg" ]; then
flag=1
if [ -n "$file" ]; then
break
fi
continue
fi
if [ -n "$flag" ]; then
file="$arg"
break
fi
cmd="$cmd $arg"
done
if [ -n "$flag" ]; then
$cmd > $file
else
$@
fi
}
ROD()
{
ROD_BASE "$@"
if [ $? -ne 0 ]; then
tst_brkm TBROK "$@ failed"
fi
}
EXPECT_PASS()
{
ROD_BASE "$@"
if [ $? -eq 0 ]; then
tst_resm TPASS "$@ passed as expected"
else
tst_resm TFAIL "$@ failed unexpectedly"
fi
}
EXPECT_FAIL()
{
# redirect stderr since we expect the command to fail
ROD_BASE "$@" 2> /dev/null
if [ $? -ne 0 ]; then
tst_resm TPASS "$@ failed as expected"
else
tst_resm TFAIL "$@ passed unexpectedly"
fi
}
tst_mkfs()
{
local fs_type=$1
local device=$2
shift 2
local fs_opts="$@"
if [ -z "$fs_type" ]; then
tst_brkm TBROK "No fs_type specified"
fi
if [ -z "$device" ]; then
tst_brkm TBROK "No device specified"
fi
tst_resm TINFO "Formatting $device with $fs_type extra opts='$fs_opts'"
ROD_SILENT mkfs.$fs_type $fs_opts $device
}
# Detect whether running under hypervisor: Microsoft Hyper-V
# Return 0: running under Hyper-V
# Return 1: not running under Hyper-V (bare metal, other hypervisor or
# failure of detection)
tst_virt_hyperv()
{
local v
v="$(systemd-detect-virt)"
[ $? -eq 0 ] || return 1
[ "$v" = "microsoft" ] || return 1
return 0
}
tst_umount()
{
local device="$1"
local i=0
if ! grep -q "$device" /proc/mounts; then
tst_resm TINFO "The $device is not mounted, skipping umount"
return
fi
while [ "$i" -lt 50 ]; do
if umount "$device" > /dev/null; then
return
fi
i=$((i+1))
tst_resm TINFO "umount($device) failed, try $i ..."
tst_resm TINFO "Likely gvfsd-trash is probing newly mounted "\
"fs, kill it to speed up tests."
tst_sleep 100ms
done
tst_resm TWARN "Failed to umount($device) after 50 retries"
}
# Check a module file existence
# Should be called after tst_tmpdir()
tst_module_exists()
{
local mod_name="$1"
if [ -f "$mod_name" ]; then
TST_MODPATH="$mod_name"
return
fi
local mod_path="$LTPROOT/testcases/bin/$mod_name"
if [ -f "$mod_path" ]; then
TST_MODPATH="$mod_path"
return
fi
if [ -n "$TST_TMPDIR" ]; then
mod_path="$TST_STARTWD/$mod_name"
if [ -f "$mod_path" ]; then
TST_MODPATH="$mod_path"
return
fi
fi
tst_brkm TCONF "Failed to find module '$mod_name'"
}
TST_CHECKPOINT_WAIT()
{
ROD tst_checkpoint wait 10000 "$1"
}
TST_CHECKPOINT_WAKE()
{
ROD tst_checkpoint wake 10000 "$1" 1
}
TST_CHECKPOINT_WAKE2()
{
ROD tst_checkpoint wake 10000 "$1" "$2"
}
TST_CHECKPOINT_WAKE_AND_WAIT()
{
TST_CHECKPOINT_WAKE "$1"
TST_CHECKPOINT_WAIT "$1"
}
# Check that test name is set
if [ -z "$TCID" ]; then
tst_brkm TBROK "TCID is not defined"
fi
if [ -z "$TST_TOTAL" ]; then
tst_brkm TBROK "TST_TOTAL is not defined"
fi
export TCID="$TCID"
export TST_TOTAL="$TST_TOTAL"
# Setup LTPROOT, default to current directory if not set
if [ -z "$LTPROOT" ]; then
export LTPROOT="$PWD"
export LTP_DATAROOT="$LTPROOT/datafiles"
else
export LTP_DATAROOT="$LTPROOT/testcases/data/$TCID"
fi
if [ "$TST_NEEDS_CHECKPOINTS" = "1" ]; then
LTP_IPC_PATH="/dev/shm/ltp_${TCID}_$$"
LTP_IPC_SIZE=$(tst_getconf PAGESIZE)
if [ $? -ne 0 ]; then
tst_brkm TBROK "tst_getconf PAGESIZE failed"
fi
ROD_SILENT dd if=/dev/zero of="$LTP_IPC_PATH" bs="$LTP_IPC_SIZE" count=1
ROD_SILENT chmod 600 "$LTP_IPC_PATH"
export LTP_IPC_PATH
fi
|