summaryrefslogtreecommitdiff
path: root/lib/testrunner
blob: c64a617c5d62b8b002ca3b2edc0aed3b42b112d8 (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
#!/bin/bash
#
# Test wrapper run inside the VM

set -o nounset
set -o errexit
set -o errtrace

export PS4='+`basename ${BASH_SOURCE[0]}`:${LINENO}:${FUNCNAME[0]:+${FUNCNAME[0]}()}+ '

KERNEL_ARCH=""

. /host/$ktest_env

ktest_dir="/host/$ktest_dir"
ktest_tmp="/host/$ktest_tmp"

ln -sf $ktest_dir /ktest
ln -sf /host/$home $home

. "$ktest_dir/lib/util.sh"
. "$ktest_dir/lib/parse-test.sh"

ktest_no_cleanup_tmpdir=1

log_verbose "Testrunner starting"

mkdir -p /root/.ssh
cat $home/.ssh/id*.pub > /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys

ln -sf "/host/$ktest_kernel_binary/lib/modules" /lib/modules
run_quiet depmod depmod -a

dmesg --console-on
dmesg --console-level 8

echo 1 > /proc/sys/kernel/sysrq
ulimit -c unlimited

# Log file system visible to host

LOGDIR=/ktest-out
ln -sf "/host/$ktest_out" $LOGDIR

# Core dump settings
echo 1 > /proc/sys/fs/suid_dumpable
echo "|/bin/cp --sparse=always /dev/stdin $LOGDIR/core.%e.PID%p.SIG%s.TIME%t" > /proc/sys/kernel/core_pattern
ulimit -c unlimited
rm -f /ktest-out/core.*

# Virtual block device tweaks
echo none | tee /sys/block/sd*/queue/scheduler >/dev/null 2>&1 || true

# Check if we are running the crashdump kernel
if [[ -s /proc/vmcore ]]; then
    echo "Collecting crash dump..."
    rm -f "$LOGDIR/vmcore"
    cp --sparse=always /proc/vmcore "$LOGDIR/vmcore" || true
    sync
    poweroff
fi

# If debugging crash dumps, add "console=hvc0" to the append line
# below:
if [[ $ktest_crashdump = 1 ]]; then
    kexec -p /host/$ktest_kernel_binary/vmlinuz --append="root=/dev/sda rw maxcpus=1" || true
fi

NR_REBOOTS=0
EXPECTED_REBOOT=0
[[ -e /NR_REBOOTS ]]		&& NR_REBOOTS=$(</NR_REBOOTS)
[[ -e /EXPECTED_REBOOT ]]	&& EXPECTED_REBOOT=$(</EXPECTED_REBOOT)

if [[ $NR_REBOOTS != $EXPECTED_REBOOT ]]; then
    echo "UNEXPECTED REBOOT: got $NR_REBOOTS expected $EXPECTED_REBOOT"
    echo "TEST FAILED"
    exit 1
fi

echo $((NR_REBOOTS + 1)) | dd of=/NR_REBOOTS oflag=direct 2> /dev/null

if compgen -G "$ktest_tmp/*.deb" > /dev/null; then
    if ! output=$(dpkg -i $ktest_tmp/*.deb); then
	echo $output
	exit 1
    fi
fi

for i in "${ktest_make_install[@]}"; do
    pushd "/host/$i" > /dev/null
    if [[ -f autogen.sh && ! -f configure ]]; then
	run_quiet "autogen $(basename $i)" ./autogen.sh
    fi
    if [[ -f configure && ! -f Makefile ]]; then
	run_quiet "configure $(basename $i)" ./configure
    fi
    run_quiet "building $(basename $i)" make -j $ktest_cpus
    run_quiet "installing $(basename $i)" make -j $ktest_cpus install
    popd > /dev/null
done

get_stratch_devs()
{
    echo
    sfdisk -X gpt /dev/sdb 
}

copy_to_host()
{
    cat /sys/kernel/debug/tracing/trace >> $LOGDIR/trace.txt

    # Code coverage
    gcov_dir=/sys/kernel/debug/gcov

    if [[ -d $gcov_dir ]]; then
	# find a destination dir that doesn't exist, so we can copy multiple
	# sets of gcov data from different tests/reboots and merge them later

	for i in {0..99}; do
	    dst=$LOGDIR/gcov.$i

	    if [[ ! -d $dst ]]; then
		cp -dR $gcov_dir $dst
		break
	    fi
	done
    fi

    sync
}

check_taint()
{
    read taint < /proc/sys/kernel/tainted

    if [[ $taint != 0 ]]; then
	echo "Failure because kernel tainted - check log for warnings"
	echo "TEST FAILED"
	exit 0
    fi
}

do_reboot()
{
    copy_to_host
    check_taint

    echo $((NR_REBOOTS + 1)) | dd of=/EXPECTED_REBOOT oflag=direct 2> /dev/null
    echo b > /proc/sysrq-trigger
}

for h in $(declare -F|sed -ne '/ hook_/ s/.*hook_// p'); do
    run_quiet "running test hook $h" hook_$h
done

echo -n "Kernel version: "
uname -r

if [[ $(type -t list_tests) == function ]]; then
    tests=$(list_tests)
else
    tests=$(declare -F|sed -ne '/ test_/ s/.*test_// p')
fi

# may be overridden by test:
if [[ $(type -t run_test) != function ]]; then
    run_test()
    {
	local test=test_$1

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

	$test
    }
fi

# may be overridden by test:
if [[ $(type -t run_tests) != function ]]; then
    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

	    # 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 = 1 ]] && 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[@]}
    }
fi

if [[ -z $tests ]]; then
    echo "No tests found"
    echo "TEST FAILED"
    exit 1
fi

[[ -n $ktest_testargs ]] && tests="$ktest_testargs"

tests=$(echo $tests)

if [[ $tests = "none" ]]; then
    echo "No tests to run"
    echo "TEST FAILED"
    exit 0
fi

if [[ $ktest_exit_on_success = 0 && $ktest_interactive = 0 ]]; then
    (
	sleep $ktest_timeout
	echo "TEST TIMEOUT - triggering crash"
	echo c > /proc/sysrq-trigger
    ) &
fi

trap 'pkill -P $$ >/dev/null' EXIT
cd /root

set +e
ret=0
iterations=0
while [[ $ret = 0 ]]; do
    run_tests $tests
    ret=$?
    pkill -P $$ >/dev/null || true
    [[ $ret != 0 ]]	    && break
    [[ $ktest_loop = 1 ]]   || break

    iterations=$((iterations + 1))
    echo "SUCCESSFUL ITERATIONS $iterations"
done

copy_to_host
check_taint

echo -n "Kernel version: "
uname -r

if [[ $ret = 0 ]]; then
    echo "TEST SUCCESS"
else
    echo "TEST FAILED"
fi

exit 0