]> git.proxmox.com Git - mirror_zfs.git/blob - scripts/zfs-tests.sh
ZTS: Adopt OpenZFS test analysis script
[mirror_zfs.git] / scripts / zfs-tests.sh
1 #!/bin/bash
2 #
3 # CDDL HEADER START
4 #
5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License, Version 1.0 only
7 # (the "License"). You may not use this file except in compliance
8 # with the License.
9 #
10 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11 # or http://www.opensolaris.org/os/licensing.
12 # See the License for the specific language governing permissions
13 # and limitations under the License.
14 #
15 # When distributing Covered Code, include this CDDL HEADER in each
16 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17 # If applicable, add the following below this CDDL HEADER, with the
18 # fields enclosed by brackets "[]" replaced with your own identifying
19 # information: Portions Copyright [yyyy] [name of copyright owner]
20 #
21 # CDDL HEADER END
22 #
23
24 BASE_DIR=$(dirname "$0")
25 SCRIPT_COMMON=common.sh
26 if [ -f "${BASE_DIR}/${SCRIPT_COMMON}" ]; then
27 . "${BASE_DIR}/${SCRIPT_COMMON}"
28 else
29 echo "Missing helper script ${SCRIPT_COMMON}" && exit 1
30 fi
31
32 PROG=zfs-tests.sh
33 VERBOSE="no"
34 QUIET=
35 CLEANUP="yes"
36 CLEANUPALL="no"
37 LOOPBACK="yes"
38 FILESIZE="4G"
39 RUNFILE=${RUNFILE:-"linux.run"}
40 FILEDIR=${FILEDIR:-/var/tmp}
41 DISKS=${DISKS:-""}
42 SINGLETEST=()
43 SINGLETESTUSER="root"
44 TAGS=""
45 ITERATIONS=1
46 ZFS_DBGMSG="$STF_SUITE/callbacks/zfs_dbgmsg.ksh"
47 ZFS_DMESG="$STF_SUITE/callbacks/zfs_dmesg.ksh"
48 ZFS_MMP="$STF_SUITE/callbacks/zfs_mmp.ksh"
49 TESTFAIL_CALLBACKS=${TESTFAIL_CALLBACKS:-"$ZFS_DBGMSG:$ZFS_DMESG:$ZFS_MMP"}
50 LOSETUP=${LOSETUP:-/sbin/losetup}
51 DMSETUP=${DMSETUP:-/sbin/dmsetup}
52
53 #
54 # Log an informational message when additional verbosity is enabled.
55 #
56 msg() {
57 if [ "$VERBOSE" = "yes" ]; then
58 echo "$@"
59 fi
60 }
61
62 #
63 # Log a failure message, cleanup, and return an error.
64 #
65 fail() {
66 echo -e "$PROG: $1" >&2
67 cleanup
68 exit 1
69 }
70
71 #
72 # Attempt to remove loopback devices and files which where created earlier
73 # by this script to run the test framework. The '-k' option may be passed
74 # to the script to suppress cleanup for debugging purposes.
75 #
76 cleanup() {
77 if [ "$CLEANUP" = "no" ]; then
78 return 0
79 fi
80
81 if [ "$LOOPBACK" = "yes" ]; then
82 for TEST_LOOPBACK in ${LOOPBACKS}; do
83 LOOP_DEV=$(basename "$TEST_LOOPBACK")
84 DM_DEV=$(sudo "${DMSETUP}" ls 2>/dev/null | \
85 grep "${LOOP_DEV}" | cut -f1)
86
87 if [ -n "$DM_DEV" ]; then
88 sudo "${DMSETUP}" remove "${DM_DEV}" ||
89 echo "Failed to remove: ${DM_DEV}"
90 fi
91
92 if [ -n "${TEST_LOOPBACK}" ]; then
93 sudo "${LOSETUP}" -d "${TEST_LOOPBACK}" ||
94 echo "Failed to remove: ${TEST_LOOPBACK}"
95 fi
96 done
97 fi
98
99 for TEST_FILE in ${FILES}; do
100 rm -f "${TEST_FILE}" &>/dev/null
101 done
102
103 if [ "$STF_PATH_REMOVE" = "yes" ] && [ -d "$STF_PATH" ]; then
104 rm -Rf "$STF_PATH"
105 fi
106 }
107 trap cleanup EXIT
108
109 #
110 # Attempt to remove all testpools (testpool.XXX), unopened dm devices,
111 # loopback devices, and files. This is a useful way to cleanup a previous
112 # test run failure which has left the system in an unknown state. This can
113 # be dangerous and should only be used in a dedicated test environment.
114 #
115 cleanup_all() {
116 local TEST_POOLS
117 TEST_POOLS=$(sudo "$ZPOOL" list -H -o name | grep testpool)
118 local TEST_LOOPBACKS
119 TEST_LOOPBACKS=$(sudo "${LOSETUP}" -a|grep file-vdev|cut -f1 -d:)
120 local TEST_FILES
121 TEST_FILES=$(ls /var/tmp/file-vdev* 2>/dev/null)
122
123 msg
124 msg "--- Cleanup ---"
125 msg "Removing pool(s): $(echo "${TEST_POOLS}" | tr '\n' ' ')"
126 for TEST_POOL in $TEST_POOLS; do
127 sudo "$ZPOOL" destroy "${TEST_POOL}"
128 done
129
130 msg "Removing dm(s): $(sudo "${DMSETUP}" ls |
131 grep loop | tr '\n' ' ')"
132 sudo "${DMSETUP}" remove_all
133
134 msg "Removing loopback(s): $(echo "${TEST_LOOPBACKS}" | tr '\n' ' ')"
135 for TEST_LOOPBACK in $TEST_LOOPBACKS; do
136 sudo "${LOSETUP}" -d "${TEST_LOOPBACK}"
137 done
138
139 msg "Removing files(s): $(echo "${TEST_FILES}" | tr '\n' ' ')"
140 for TEST_FILE in $TEST_FILES; do
141 sudo rm -f "${TEST_FILE}"
142 done
143 }
144
145 #
146 # Takes a name as the only arguments and looks for the following variations
147 # on that name. If one is found it is returned.
148 #
149 # $RUNFILE_DIR/<name>
150 # $RUNFILE_DIR/<name>.run
151 # <name>
152 # <name>.run
153 #
154 find_runfile() {
155 local NAME=$1
156 local RESULT=""
157
158 if [ -f "$RUNFILE_DIR/$NAME" ]; then
159 RESULT="$RUNFILE_DIR/$NAME"
160 elif [ -f "$RUNFILE_DIR/$NAME.run" ]; then
161 RESULT="$RUNFILE_DIR/$NAME.run"
162 elif [ -f "$NAME" ]; then
163 RESULT="$NAME"
164 elif [ -f "$NAME.run" ]; then
165 RESULT="$NAME.run"
166 fi
167
168 echo "$RESULT"
169 }
170
171 #
172 # Symlink file if it appears under any of the given paths.
173 #
174 create_links() {
175 local dir_list="$1"
176 local file_list="$2"
177
178 [ -n "$STF_PATH" ] || fail "STF_PATH wasn't correctly set"
179
180 for i in $file_list; do
181 for j in $dir_list; do
182 [ ! -e "$STF_PATH/$i" ] || continue
183
184 if [ ! -d "$j/$i" ] && [ -e "$j/$i" ]; then
185 ln -s "$j/$i" "$STF_PATH/$i" || \
186 fail "Couldn't link $i"
187 break
188 fi
189 done
190
191 [ ! -e "$STF_PATH/$i" ] && STF_MISSING_BIN="$STF_MISSING_BIN$i "
192 done
193 }
194
195 #
196 # Constrain the path to limit the available binaries to a known set.
197 # When running in-tree a top level ./bin/ directory is created for
198 # convenience, otherwise a temporary directory is used.
199 #
200 constrain_path() {
201 . "$STF_SUITE/include/commands.cfg"
202
203 if [ "$INTREE" = "yes" ]; then
204 # Constrained path set to ./zfs/bin/
205 STF_PATH="$BIN_DIR"
206 STF_PATH_REMOVE="no"
207 STF_MISSING_BIN=""
208 if [ ! -d "$STF_PATH" ]; then
209 mkdir "$STF_PATH"
210 chmod 755 "$STF_PATH" || fail "Couldn't chmod $STF_PATH"
211 fi
212
213 # Special case links for standard zfs utilities
214 DIRS="$(find "$CMD_DIR" -type d \( ! -name .deps -a \
215 ! -name .libs \) -print | tr '\n' ' ')"
216 create_links "$DIRS" "$ZFS_FILES"
217
218 # Special case links for zfs test suite utilities
219 DIRS="$(find "$STF_SUITE" -type d \( ! -name .deps -a \
220 ! -name .libs \) -print | tr '\n' ' ')"
221 create_links "$DIRS" "$ZFSTEST_FILES"
222 else
223 # Constrained path set to /var/tmp/constrained_path.*
224 SYSTEMDIR=${SYSTEMDIR:-/var/tmp/constrained_path.XXXX}
225 STF_PATH=$(/bin/mktemp -d "$SYSTEMDIR")
226 STF_PATH_REMOVE="yes"
227 STF_MISSING_BIN=""
228
229 chmod 755 "$STF_PATH" || fail "Couldn't chmod $STF_PATH"
230
231 # Special case links for standard zfs utilities
232 create_links "/bin /usr/bin /sbin /usr/sbin" "$ZFS_FILES"
233
234 # Special case links for zfs test suite utilities
235 create_links "$STF_SUITE/bin" "$ZFSTEST_FILES"
236 fi
237
238 # Standard system utilities
239 create_links "/bin /usr/bin /sbin /usr/sbin" "$SYSTEM_FILES"
240
241 # Exceptions
242 ln -fs "$STF_PATH/awk" "$STF_PATH/nawk"
243 ln -fs /sbin/fsck.ext4 "$STF_PATH/fsck"
244 ln -fs /sbin/mkfs.ext4 "$STF_PATH/newfs"
245 ln -fs "$STF_PATH/gzip" "$STF_PATH/compress"
246 ln -fs "$STF_PATH/gunzip" "$STF_PATH/uncompress"
247 ln -fs "$STF_PATH/exportfs" "$STF_PATH/share"
248 ln -fs "$STF_PATH/exportfs" "$STF_PATH/unshare"
249 }
250
251 #
252 # Output a useful usage message.
253 #
254 usage() {
255 cat << EOF
256 USAGE:
257 $0 [hvqxkf] [-s SIZE] [-r RUNFILE] [-t PATH] [-u USER]
258
259 DESCRIPTION:
260 ZFS Test Suite launch script
261
262 OPTIONS:
263 -h Show this message
264 -v Verbose zfs-tests.sh output
265 -q Quiet test-runner output
266 -x Remove all testpools, dm, lo, and files (unsafe)
267 -k Disable cleanup after test failure
268 -f Use files only, disables block device tests
269 -c Only create and populate constrained path
270 -I NUM Number of iterations
271 -d DIR Use DIR for files and loopback devices
272 -s SIZE Use vdevs of SIZE (default: 4G)
273 -r RUNFILE Run tests in RUNFILE (default: linux.run)
274 -t PATH Run single test at PATH relative to test suite
275 -T TAGS Comma separated list of tags (default: 'functional')
276 -u USER Run single test as USER (default: root)
277
278 EXAMPLES:
279 # Run the default (linux) suite of tests and output the configuration used.
280 $0 -v
281
282 # Run a smaller suite of tests designed to run more quickly.
283 $0 -r linux-fast
284
285 # Cleanup a previous run of the test suite prior to testing, run the
286 # default (linux) suite of tests and perform no cleanup on exit.
287 $0 -x
288
289 EOF
290 }
291
292 while getopts 'hvqxkfcd:s:r:?t:T:u:I:' OPTION; do
293 case $OPTION in
294 h)
295 usage
296 exit 1
297 ;;
298 v)
299 # shellcheck disable=SC2034
300 VERBOSE="yes"
301 ;;
302 q)
303 QUIET="-q"
304 ;;
305 x)
306 CLEANUPALL="yes"
307 ;;
308 k)
309 CLEANUP="no"
310 ;;
311 f)
312 LOOPBACK="no"
313 ;;
314 c)
315 constrain_path
316 exit
317 ;;
318 d)
319 FILEDIR="$OPTARG"
320 ;;
321 I)
322 ITERATIONS="$OPTARG"
323 if [ "$ITERATIONS" -le 0 ]; then
324 fail "Iterations must be greater than 0."
325 fi
326 ;;
327 s)
328 FILESIZE="$OPTARG"
329 ;;
330 r)
331 RUNFILE="$OPTARG"
332 ;;
333 t)
334 if [ ${#SINGLETEST[@]} -ne 0 ]; then
335 fail "-t can only be provided once."
336 fi
337 SINGLETEST+=("$OPTARG")
338 ;;
339 T)
340 TAGS="$OPTARG"
341 ;;
342 u)
343 SINGLETESTUSER="$OPTARG"
344 ;;
345 ?)
346 usage
347 exit
348 ;;
349 esac
350 done
351
352 shift $((OPTIND-1))
353
354 FILES=${FILES:-"$FILEDIR/file-vdev0 $FILEDIR/file-vdev1 $FILEDIR/file-vdev2"}
355 LOOPBACKS=${LOOPBACKS:-""}
356
357 if [ ${#SINGLETEST[@]} -ne 0 ]; then
358 if [ -n "$TAGS" ]; then
359 fail "-t and -T are mutually exclusive."
360 fi
361 RUNFILE_DIR="/var/tmp"
362 RUNFILE="zfs-tests.$$.run"
363 SINGLEQUIET="False"
364
365 if [ -n "$QUIET" ]; then
366 SINGLEQUIET="True"
367 fi
368
369 cat >$RUNFILE_DIR/$RUNFILE << EOF
370 [DEFAULT]
371 pre =
372 quiet = $SINGLEQUIET
373 pre_user = root
374 user = $SINGLETESTUSER
375 timeout = 600
376 post_user = root
377 post =
378 outputdir = /var/tmp/test_results
379 EOF
380 for t in "${SINGLETEST[@]}"
381 do
382 SINGLETESTDIR=$(dirname "$t")
383 SINGLETESTFILE=$(basename "$t")
384 SETUPSCRIPT=
385 CLEANUPSCRIPT=
386
387 if [ -f "$STF_SUITE/$SINGLETESTDIR/setup.ksh" ]; then
388 SETUPSCRIPT="setup"
389 fi
390
391 if [ -f "$STF_SUITE/$SINGLETESTDIR/cleanup.ksh" ]; then
392 CLEANUPSCRIPT="cleanup"
393 fi
394
395 cat >>$RUNFILE_DIR/$RUNFILE << EOF
396
397 [$SINGLETESTDIR]
398 tests = ['$SINGLETESTFILE']
399 pre = $SETUPSCRIPT
400 post = $CLEANUPSCRIPT
401 tags = ['functional']
402 EOF
403 done
404 fi
405
406 #
407 # Use default tag if none was specified
408 #
409 TAGS=${TAGS:='functional'}
410
411 #
412 # Attempt to locate the runfile describing the test workload.
413 #
414 if [ -n "$RUNFILE" ]; then
415 SAVED_RUNFILE="$RUNFILE"
416 RUNFILE=$(find_runfile "$RUNFILE")
417 [ -z "$RUNFILE" ] && fail "Cannot find runfile: $SAVED_RUNFILE"
418 fi
419
420 if [ ! -r "$RUNFILE" ]; then
421 fail "Cannot read runfile: $RUNFILE"
422 fi
423
424 #
425 # This script should not be run as root. Instead the test user, which may
426 # be a normal user account, needs to be configured such that it can
427 # run commands via sudo passwordlessly.
428 #
429 if [ "$(id -u)" = "0" ]; then
430 fail "This script must not be run as root."
431 fi
432
433 if [ "$(sudo whoami)" != "root" ]; then
434 fail "Passwordless sudo access required."
435 fi
436
437 #
438 # Constrain the available binaries to a known set.
439 #
440 constrain_path
441
442 #
443 # Check if ksh exists
444 #
445 [ -e "$STF_PATH/ksh" ] || fail "This test suite requires ksh."
446 [ -e "$STF_SUITE/include/default.cfg" ] || fail \
447 "Missing $STF_SUITE/include/default.cfg file."
448
449 #
450 # Verify the ZFS module stack is loaded.
451 #
452 sudo "${ZFS_SH}" &>/dev/null
453
454 #
455 # Attempt to cleanup all previous state for a new test run.
456 #
457 if [ "$CLEANUPALL" = "yes" ]; then
458 cleanup_all
459 fi
460
461 #
462 # By default preserve any existing pools
463 # NOTE: Since 'zpool list' outputs a newline-delimited list convert $KEEP from
464 # space-delimited to newline-delimited.
465 #
466 if [ -z "${KEEP}" ]; then
467 KEEP="$(sudo "$ZPOOL" list -H -o name)"
468 if [ -z "${KEEP}" ]; then
469 KEEP="rpool"
470 fi
471 else
472 KEEP="$(echo -e "${KEEP//[[:blank:]]/\n}")"
473 fi
474
475 #
476 # NOTE: The following environment variables are undocumented
477 # and should be used for testing purposes only:
478 #
479 # __ZFS_POOL_EXCLUDE - don't iterate over the pools it lists
480 # __ZFS_POOL_RESTRICT - iterate only over the pools it lists
481 #
482 # See libzfs/libzfs_config.c for more information.
483 #
484 __ZFS_POOL_EXCLUDE="$(echo "$KEEP" | sed ':a;N;s/\n/ /g;ba')"
485
486 . "$STF_SUITE/include/default.cfg"
487
488 msg
489 msg "--- Configuration ---"
490 msg "Runfile: $RUNFILE"
491 msg "STF_TOOLS: $STF_TOOLS"
492 msg "STF_SUITE: $STF_SUITE"
493 msg "STF_PATH: $STF_PATH"
494
495 #
496 # No DISKS have been provided so a basic file or loopback based devices
497 # must be created for the test suite to use.
498 #
499 if [ -z "${DISKS}" ]; then
500 #
501 # Create sparse files for the test suite. These may be used
502 # directory or have loopback devices layered on them.
503 #
504 for TEST_FILE in ${FILES}; do
505 [ -f "$TEST_FILE" ] && fail "Failed file exists: ${TEST_FILE}"
506 truncate -s "${FILESIZE}" "${TEST_FILE}" ||
507 fail "Failed creating: ${TEST_FILE} ($?)"
508 if [[ "$DISKS" ]]; then
509 DISKS="$DISKS $TEST_FILE"
510 else
511 DISKS="$TEST_FILE"
512 fi
513 done
514
515 #
516 # If requested setup loopback devices backed by the sparse files.
517 #
518 if [ "$LOOPBACK" = "yes" ]; then
519 DISKS=""
520
521 test -x "$LOSETUP" || fail "$LOSETUP utility must be installed"
522
523 for TEST_FILE in ${FILES}; do
524 TEST_LOOPBACK=$(sudo "${LOSETUP}" -f)
525 sudo "${LOSETUP}" "${TEST_LOOPBACK}" "${TEST_FILE}" ||
526 fail "Failed: ${TEST_FILE} -> ${TEST_LOOPBACK}"
527 LOOPBACKS="${LOOPBACKS}${TEST_LOOPBACK} "
528 BASELOOPBACKS=$(basename "$TEST_LOOPBACK")
529 if [[ "$DISKS" ]]; then
530 DISKS="$DISKS $BASELOOPBACKS"
531 else
532 DISKS="$BASELOOPBACKS"
533 fi
534 done
535 fi
536 fi
537
538 NUM_DISKS=$(echo "${DISKS}" | awk '{print NF}')
539 [ "$NUM_DISKS" -lt 3 ] && fail "Not enough disks ($NUM_DISKS/3 minimum)"
540
541 #
542 # Disable SELinux until the ZFS Test Suite has been updated accordingly.
543 #
544 if [ -x "$STF_PATH/setenforce" ]; then
545 sudo setenforce permissive &>/dev/null
546 fi
547
548 #
549 # Enable internal ZFS debug log and clear it.
550 #
551 if [ -e /sys/module/zfs/parameters/zfs_dbgmsg_enable ]; then
552 sudo /bin/sh -c "echo 1 >/sys/module/zfs/parameters/zfs_dbgmsg_enable"
553 sudo /bin/sh -c "echo 0 >/proc/spl/kstat/zfs/dbgmsg"
554 fi
555
556 msg "FILEDIR: $FILEDIR"
557 msg "FILES: $FILES"
558 msg "LOOPBACKS: $LOOPBACKS"
559 msg "DISKS: $DISKS"
560 msg "NUM_DISKS: $NUM_DISKS"
561 msg "FILESIZE: $FILESIZE"
562 msg "ITERATIONS: $ITERATIONS"
563 msg "TAGS: $TAGS"
564 msg "Keep pool(s): $KEEP"
565 msg "Missing util(s): $STF_MISSING_BIN"
566 msg ""
567
568 export STF_TOOLS
569 export STF_SUITE
570 export STF_PATH
571 export DISKS
572 export FILEDIR
573 export KEEP
574 export __ZFS_POOL_EXCLUDE
575 export TESTFAIL_CALLBACKS
576 export PATH=$STF_PATH
577
578 RESULTS_FILE=$(mktemp -u -t zts-results.XXXX -p "$FILEDIR")
579 REPORT_FILE=$(mktemp -u -t zts-report.XXXX -p "$FILEDIR")
580
581 #
582 # Run all the tests as specified.
583 #
584 msg "${TEST_RUNNER} ${QUIET} -c ${RUNFILE} -T ${TAGS} -i ${STF_SUITE}" \
585 "-I ${ITERATIONS}"
586 ${TEST_RUNNER} ${QUIET} -c "${RUNFILE}" -T "${TAGS}" -i "${STF_SUITE}" \
587 -I "${ITERATIONS}" 2>&1 | tee "$RESULTS_FILE"
588
589 #
590 # Analyze the results.
591 #
592 set -o pipefail
593 ${ZTS_REPORT} "$RESULTS_FILE" | tee "$REPORT_FILE"
594 RESULT=$?
595 set +o pipefail
596
597 RESULTS_DIR=$(awk '/^Log directory/ { print $3 }' "$RESULTS_FILE")
598 if [ -d "$RESULTS_DIR" ]; then
599 cat "$RESULTS_FILE" "$REPORT_FILE" >"$RESULTS_DIR/results"
600 fi
601
602 rm -f "$RESULTS_FILE" "$REPORT_FILE"
603
604 if [ ${#SINGLETEST[@]} -ne 0 ]; then
605 rm -f "$RUNFILE" &>/dev/null
606 fi
607
608 exit ${RESULT}