summaryrefslogtreecommitdiff
path: root/tests/prelude.sh
blob: bd062d74ddd58933cf9fee246dcf5eee3e70c6a1 (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
# Basic libs for ktest tests:

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

if [[ ! -v ktest_verbose ]]; then
    ktest_verbose=0
    ktest_priority=0
    ktest_cpus=$(nproc)
    ktest_mem=""
    ktest_timeout=""
    ktest_kernel_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=ahci

    ktest_images=()
    ktest_scratch_devs=()
    ktest_make_install=()
    ktest_kernel_config_require=()
    ktest_qemu_append=()

    NEXT_SCRATCH_DEV="b"
    BUILD_ON_HOST=""
fi

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-qemu-append()
{
    local OLDIFS=$IFS
    IFS=','

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

    IFS=$OLDIFS
}

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

config-scratch-devs()
{
    ktest_scratch_devs+=("$1")
}

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

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

config-cpus()
{
    ktest_cpus=$1
}

config-mem()
{
    ktest_mem=$1
}

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

config-arch()
{
    ktest_arch=$1
}

set_watchdog()
{
    echo WATCHDOG $1
}

run_test()
{
    local test=test_$1

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

    $test
}

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

    echo
    echo "Running tests $@"
    echo

    for i in $@; do
	echo "========= TEST   $i"
	echo

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

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

	# XXX: check dmesg for warnings, oopses, slab corruption, etc. before
	# signaling success

	echo

	if [[ $ret = 0 ]]; then
	    echo "========= PASSED $i in $(($finish - $start))s"
	    tests_passed+=($i)
	else
	    echo "========= FAILED $i in $(($finish - $start))s"
	    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

	    for mnt in $(awk '{print $2}' /proc/mounts|grep ^/mnt|sort -r); 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'
}

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

    local arg=$1
    shift

    case $arg in
	deps)
	    echo "ktest_arch=$ktest_arch"
	    echo "ktest_cpus=$ktest_cpus"
	    echo "ktest_mem=$ktest_mem"
	    echo "ktest_timeout=$ktest_timeout"
	    echo "ktest_kernel_append=(${ktest_kernel_append[@]})"
	    echo "ktest_storage_bus=$ktest_storage_bus"
	    echo "ktest_images=(${ktest_images[@]})"
	    echo "ktest_scratch_devs=(${ktest_scratch_devs[@]})"
	    echo "ktest_make_install=(${ktest_make_install[@]})"
	    echo "ktest_kernel_config_require=(${ktest_kernel_config_require[@]})"
	    echo "ktest_qemu_append=(${ktest_qemu_append[@]})"
	    ;;
	list-tests)
	    list_tests
	    ;;
	run-tests)
	    run_tests "$@"
	    ;;
	*)
	    usage
	    exit 1
	    ;;
    esac
}