summaryrefslogtreecommitdiff
path: root/tests/prelude.sh
blob: 9b0fd61005a5dabd5d3da5a40ec59d09f8eccd19 (plain)
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
#!/usr/bin/env bash
# Basic libs for ktest tests:

. $(dirname $(readlink -e "${BASH_SOURCE[0]}"))/../lib/common.sh

if [[ ! -v ktest_interactive ]]; then
    ktest_failfast=false
    ktest_interactive=false
    ktest_verbose=false
    ktest_priority=0
fi

if [[ ! -v ktest_cpus ]]; then
    ktest_cpus=$(nproc)
    ktest_mem=""
    ktest_timeout=""
    ktest_timeout_multiplier=1
    ktest_mem_multiplier=1
    ktest_kernel_append=()
    ktest_kernel_make_append=()

    # virtio-scsi-pci semes to be buggy: reading the superblock on the root
    # filesystem randomly returns zeroes
    #ktest_storage_bus=virtio-scsi-pci
    ktest_storage_bus=virtio-blk
    ktest_images=()
    ktest_rw_images=()

    ktest_scratch_dev=()
    ktest_scratch_dev_sizes=()
    ktest_scratch_dev_count=0

    ktest_make_install=()
    ktest_kernel_config_require=()
    ktest_kernel_config_require_soft=()
    ktest_qemu_append=()
    ktest_compiler="${CC:-gcc}"
    ktest_allow_taint=false

    ktest_tests_unknown=false
    ktest_kconfig_base=

    BUILD_ON_HOST=""
fi

case $ktest_storage_bus in
    virtio-blk)
        ktest_dev_prefix="vd"
        ;;
    *)
        ktest_dev_prefix="sd"
        ;;
esac

require-git()
{
    local req="$1"
    local dir=$(basename $req)
    dir=${dir%%.git}

    if [[ $# -ge 2 ]]; then
	dir=$2
    fi

    dir=$(dirname $(readlink -e "${BASH_SOURCE[1]}"))/$dir

    if [[ ! -d $dir ]]; then
	git clone $req $dir
    fi
}

do-build-deb()
{
    local path=$(readlink -e "$1")
    local name=$(basename $path)

    get_tmpdir

    make -C "$path"

    cp -drl $path $ktest_tmp
    pushd "$ktest_tmp/$name" > /dev/null

    # make -nc actually work:
    rm -f debian/*.debhelper.log

    debuild --no-lintian -b -i -I -us -uc -nc
    popd > /dev/null
}

# $1 is a source repository, which will be built (with make) and then turned
# into a dpkg
require-build-deb()
{
    local req=$1

    if ! [[ -d $req ]]; then
	echo "build-deb dependency $req not found"
	exit 1
    fi

    checkdep debuild devscripts

    run_quiet "building $(basename $req)" do-build-deb $req
}

require-make()
{
    local req=$(dirname $(readlink -e ${BASH_SOURCE[1]}))/$1

    if [[ ! -d $req ]]; then
	echo "require-make: $req not found"
	exit 1
    fi

    ktest_make_install+=("$req")

    if [[ -n $BUILD_ON_HOST ]]; then
	run_quiet "building $1" make -C "$req"
    fi
}

require-kernel-config()
{
    local OLDIFS=$IFS
    IFS=','

    for i in $1; do
	ktest_kernel_config_require+=("$i")
    done

    IFS=$OLDIFS
}

require-kernel-config-soft()
{
    ktest_kernel_config_require_soft+=("$1")
}

require-qemu-append()
{
    ktest_qemu_append+=("$@")
}

require-qemu-prepend()
{
    ktest_qemu_prepend+=("$@")
}

require-kernel-append()
{
    ktest_kernel_append+=("$1")
}

require-kernel-make-append()
{
    ktest_kernel_make_append+=("$1")
}

require-gcov()
{
    local dir=$(echo "${1%/}"|tr / _)

    require-kernel-make-append "GCOV_PROFILE_$dir=y"
    require-kernel-config GCOV_KERNEL
}

config-scratch-devs()
{
    local chars=( {b..z} )

    ktest_scratch_dev+=("/dev/${ktest_dev_prefix}${chars[$ktest_scratch_dev_count]}")
    ktest_scratch_dev_count=$((ktest_scratch_dev_count + 1))

    ktest_scratch_dev_sizes+=("$1")
}

config-pmem-devs()
{
    ktest_pmem_devs+=("$1")
}

config-image()
{
    ktest_images+=("$1")
}

config-rw-image()
{
    ktest_rw_images+=("$1")
}

config-cpus()
{
    ktest_cpus=$1
}

config-mem()
{
    local bytes=$(echo $1|numfmt --from=iec)
    ktest_mem=$((bytes / 1048576))
}

config-mem-multiplier()
{
    ktest_mem_multiplier=$(($ktest_mem_multiplier * $1))
}

config-timeout()
{
    n=$1
    if [ "${EXTENDED_DEBUG:-0}" == 1 ]; then
	n=$((n * 2))
    fi
    ktest_timeout=$n
}

config-timeout-multiplier()
{
    ktest_timeout_multiplier=$(($ktest_timeout_multiplier * $1))
}

config-arch()
{
    ktest_arch=$1
}

config-compiler()
{
    ktest_compiler=$1
}

allow_taint()
{
    ktest_allow_taint=true
}

create_ktest_user()
{
    groupadd -g 1000 ktest_group	>& /dev/null || true
    useradd -u 1000 -g 1000 ktest_user	>& /dev/null || true
}

set_watchdog()
{
    ktest_timeout=$1
    ktest_timeout=$((ktest_timeout * ktest_timeout_multiplier))

    echo WATCHDOG $ktest_timeout
}

check_dmesg()
{
    ! dmesg |
	awk '/========= TEST/{last=NR} {if(NR>last && last!=0) print}'|
	grep -E -q -e "kernel BUG at" \
	    -e "WARNING:" \
	    -e "\bBUG:" \
	    -e "Oops:" \
	    -e "possible recursive locking detected" \
	    -e "Internal error" \
	    -e "(INFO|ERR): suspicious RCU usage" \
	    -e "INFO: possible circular locking dependency detected" \
	    -e "general protection fault:" \
	    -e "BUG .* remaining" \
	    -e "UBSAN:"
}

run_test()
{
    local test_file=$(basename -s .ktest $0)
    local test_name=$1
    local test_fn=test_$test_name
    local test_output=/ktest-out/out/$test_file.$test_name

    if [[ $(type -t $test_fn) != function ]]; then
	echo "test $test_name does not exist"
	exit 1
    fi

    mkdir -p $test_output
    echo "|/bin/cp --sparse=always /dev/stdin $test_output/core.%e.PID%p.SIG%s.TIME%t" > /proc/sys/kernel/core_pattern

    $test_fn

    check_dmesg
}

run_tests()
{
    local tests_passed=()
    local tests_failed=()

    echo
    echo "Running tests $@"
    echo

    for i in $@; do
	echo "========= TEST   $i" > /dev/kmsg
	echo

	local start=$(date '+%s')
	local ret=0
	(set -e; run_test $i)
	ret=$?
	local finish=$(date '+%s')

	pkill -P $$ >/dev/null || true

	echo

	if [[ $ret = 0 ]]; then
	    echo "========= PASSED $i in $(($finish - $start))s" > /dev/kmsg
	    tests_passed+=($i)
	else
	    echo "========= FAILED $i in $(($finish - $start))s" > /dev/kmsg
	    tests_failed+=($i)

	    # Try to clean up after a failed test so we can run the rest of
	    # the tests - unless failfast is enabled, or there was only one
	    # test to run:

	    $ktest_failfast  && break
	    [[ $# = 1 ]] && break

	    awk '{print $2}' /proc/mounts | grep ^/mnt | sort -r 2>/dev/null | while read -r mnt; do
		while [[ -n $(fuser -k -M -m $mnt) ]]; do
		    sleep 1
		done
		umount $mnt
	    done
	fi
    done

    echo
    echo "Passed: ${tests_passed[@]}"
    echo "Failed: ${tests_failed[@]}"

    return ${#tests_failed[@]}
}

list_tests()
{
    declare -F|sed -ne '/ test_/ s/.*test_// p'
}

# must have at least one init function to avoid errors below:
init_noop()
{
    true
}

run_init_hooks()
{
    for h in `declare -F|grep -Eo '\<init_.*'`; do
	echo "hook $h"
	$h
    done
}

main()
{
    if [[ $# = 0 ]]; then
	exit 0
    fi

    local arg=$1
    shift

    case $arg in
	deps)
	    echo "ktest_arch=$ktest_arch"
	    echo "ktest_compiler=$ktest_compiler"
	    echo "ktest_cpus=$ktest_cpus"
	    echo "ktest_mem=$((ktest_mem * ktest_mem_multiplier))"
	    echo "ktest_timeout=$((ktest_timeout * ktest_timeout_multiplier))"
	    echo "ktest_kernel_append=(${ktest_kernel_append[@]})"
	    echo "ktest_kernel_make_append=(${ktest_kernel_make_append[@]})"
	    echo "ktest_storage_bus=$ktest_storage_bus"
	    echo "ktest_images=(${ktest_images[@]})"
	    echo "ktest_rw_images=(${ktest_rw_images[@]})"
	    echo "ktest_scratch_dev_sizes=(${ktest_scratch_dev_sizes[@]})"
	    echo "ktest_make_install=(${ktest_make_install[@]})"
	    echo "ktest_kernel_config_require=(${ktest_kernel_config_require[@]})"
	    echo "ktest_kernel_config_require_soft=(${ktest_kernel_config_require_soft[@]})"
	    echo "ktest_qemu_append=(${ktest_qemu_append[@]})"
	    echo "ktest_qemu_prepend=(${ktest_qemu_prepend[@]})"
	    echo "ktest_allow_taint=$ktest_allow_taint"
	    echo "ktest_tests_unknown=$ktest_tests_unknown"
	    echo "ktest_kconfig_base=$ktest_kconfig_base"
	    ;;
	init)
	    create_ktest_user
	    run_init_hooks
	    ;;
	list-tests)
	    list_tests
	    ;;
	run-tests)
	    run_tests "$@"
	    ;;
	*)
	    usage
	    exit 1
	    ;;
    esac
}