3 # Common routines for all tests
6 # assert that argument is not empty and is an existing path (file or directory)
12 if [ -z "$path" ]; then
13 echo "ASSERTION FAIL: $path is not valid"
17 if [ -f "$path" -o -d "$path" -o -b "$path" ]; then
20 echo "ASSERTION FAIL: $path is not valid"
24 # $1: this string gets matched to files, absolute or relative path, or a
25 # systemwide command available via $PATH
31 if [ -z "$msg" ]; then
35 if [ -f "$msg" -o -d "$msg" -o -b "$msg" ]; then
38 msg=$(type -p -- "$msg")
39 if [ -f "$msg" ]; then
47 echo "$*" | tee -a "$RESULTS"
51 # log a message to the results file
54 echo "$*" | tee -a "$RESULTS"
69 echo "DUMP args for ${FUNCNAME[1]}:"
70 while [ $# -gt 0 ]; do
77 # read arguments, look if we're calling btrfs and if there's a known
78 # subcommand, return argument index to insert, taking root helper into
79 # consideration, returns 2 for unknown subcommand
82 if [ "$1" = 'root_helper' ]; then
83 if [[ $2 =~ /btrfs$ ]]; then
88 if [[ $1 =~ /btrfs$ ]]; then
96 # return command-specific arguments if enabled
99 if [ "$TEST_ENABLE_OVERRIDE" = 'true' ]; then
100 # if defined via common.local, use it, otherwise pass make
102 if [ "$(type -t _skip_spec)" = 'function' ]; then
103 if _skip_spec "$@"; then
108 check) echo -n "$TEST_ARGS_CHECK" ;;
113 # Argument passing magic:
114 # the command passed to run_* helpers is inspected, if there's 'btrfs command'
115 # found and there are defined additional arguments, they're inserted just after
116 # the command name, ie. any arguments in the test could override them.
118 # The root helper is recognized. Unrecognized subcommands or external tools
127 ins=$(_get_spec_ins "$@")
129 cmd=$(eval echo "\${$spec}")
130 spec=$(_cmd_spec "${@:$spec}")
131 set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
132 echo "############### $@" >> "$RESULTS" 2>&1
133 if [[ $TEST_LOG =~ tty ]]; then echo "CMD: $@" > /dev/tty; fi
134 if [ "$1" = 'root_helper' ]; then
135 "$@" >> "$RESULTS" 2>&1 || _fail "failed: $@"
137 $INSTRUMENT "$@" >> "$RESULTS" 2>&1 || _fail "failed: $@"
141 # same as run_check but the stderr+stdout output is duplicated on stdout and
142 # can be processed further
149 ins=$(_get_spec_ins "$@")
151 cmd=$(eval echo "\${$spec}")
152 spec=$(_cmd_spec "${@:$spec}")
153 set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
154 echo "############### $@" >> "$RESULTS" 2>&1
155 if [[ $TEST_LOG =~ tty ]]; then echo "CMD(stdout): $@" > /dev/tty; fi
156 if [ "$1" = 'root_helper' ]; then
157 "$@" 2>&1 | tee -a "$RESULTS"
159 $INSTRUMENT "$@" 2>&1 | tee -a "$RESULTS"
161 if [ ${PIPESTATUS[0]} -ne 0 ]; then
166 # same as run_check but does not fail the test if it's handled gracefully by
167 # the tool, unexpected failure like segfault or abor will exit forcibly
176 ins=$(_get_spec_ins "$@")
178 cmd=$(eval echo "\${$spec}")
179 spec=$(_cmd_spec "${@:$spec}")
180 set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
181 echo "############### $@" >> "$RESULTS" 2>&1
182 if [[ $TEST_LOG =~ tty ]]; then echo "CMD(mayfail): $@" > /dev/tty; fi
183 if [ "$1" = 'root_helper' ]; then
184 "$@" >> "$RESULTS" 2>&1
186 $INSTRUMENT "$@" >> "$RESULTS" 2>&1
189 if [ $ret != 0 ]; then
190 echo "failed (ignored, ret=$ret): $@" >> "$RESULTS"
191 if [ $ret == 139 ]; then
192 _fail "mayfail: returned code 139 (SEGFAULT), not ignored"
193 elif [ $ret == 134 ]; then
194 _fail "mayfail: returned code 134 (SIGABRT), not ignored"
200 # first argument is error message to print if it fails, otherwise
201 # same as run_check but expects the command to fail, output is logged
212 if _is_file_or_command "$msg"; then
213 echo "ASSERTION FAIL: 1st argument of run_mustfail must be a message"
217 ins=$(_get_spec_ins "$@")
219 cmd=$(eval echo "\${$spec}")
220 spec=$(_cmd_spec "${@:$spec}")
221 set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
222 echo "############### $@" >> "$RESULTS" 2>&1
223 if [[ $TEST_LOG =~ tty ]]; then echo "CMD(mustfail): $@" > /dev/tty; fi
224 if [ "$1" = 'root_helper' ]; then
225 "$@" >> "$RESULTS" 2>&1
227 $INSTRUMENT "$@" >> "$RESULTS" 2>&1
230 echo "failed (expected): $@" >> "$RESULTS"
233 echo "succeeded (unexpected!): $@" >> "$RESULTS"
234 _fail "unexpected success: $msg"
239 # The first parameter is error message to print if it fails, just like
241 # NOTE: we don't use pipefail to avoid disturbing other script, so here we
242 # use a temporary output file.
243 # So it doesn't support pipeline in the @cmd
244 run_mustfail_stdout()
253 tmp_output=$(mktemp --tmpdir btrfs-progs-test--mustfail-stdtout.XXXXXX)
258 if _is_file_or_command "$msg"; then
259 echo "ASSERTION FAIL: 1st argument of run_mustfail_stdout must be a message"
263 ins=$(_get_spec_ins "$@")
265 cmd=$(eval echo "\${$spec}")
266 spec=$(_cmd_spec "${@:$spec}")
267 set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
268 echo "############### $@" >> "$RESULTS" 2>&1
269 if [[ $TEST_LOG =~ tty ]]; then echo "CMD(mustfail): $@" > /dev/tty; fi
270 if [ "$1" = 'root_helper' ]; then
271 "$@" 2>&1 > "$tmp_output"
273 $INSTRUMENT "$@" 2>&1 > "$tmp_output"
277 cat "$tmp_output" >> "$RESULTS"
281 if [ "$ret" != 0 ]; then
282 echo "failed (expected): $@" >> "$RESULTS"
285 echo "succeeded (unexpected!): $@" >> "$RESULTS"
286 _fail "unexpected success: $msg"
293 if ! [ -f "$TOP/$1" ]; then
294 _fail "Failed prerequisites: $1";
298 check_global_prereq()
300 which $1 &> /dev/null
301 if [ $? -ne 0 ]; then
302 _fail "Failed system wide prerequisities: $1";
311 echo "testing image $(basename $image)" >> "$RESULTS"
312 "$TOP/btrfs" check "$image" >> "$RESULTS" 2>&1
313 [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
315 run_check "$TOP/btrfs" check --repair "$image"
316 run_check "$TOP/btrfs" check "$image"
319 # Extract a usable image from packed formats
320 # - raw btrfs filesystem images, suffix .raw
321 # - dtto compressed by XZ, suffix .raw.xz
322 # - meta-dump images with suffix .img
323 # - dtto compressed by XZ, suffix .img.xz
324 # - compressed send stream, .stream.xz
333 rm -f "$image.restored"
336 xz --decompress --keep "$image" || \
337 _fail "failed to decompress image $image" >&2
339 rm -f "$image.restored"
343 cp --sparse=auto "$image" "$image.restored"
346 xz --decompress --keep "$image" || \
347 _fail "failed to decompress image $image" >&2
349 mv "$image" "$image.restored"
352 xz --decompress --keep "$image" || \
353 _fail "failed to decompress file $image" >&2
355 mv "$image" "$image.restored"
359 if ! [ -f "$image.restored" ]; then
360 echo "restoring image $(basename $image)" >> "$RESULTS"
361 "$TOP/btrfs-image" -r "$image" "$image.restored" \
363 || _fail "failed to restore image $image" >&2
366 [ -f "$cleanme" ] && rm -f "$cleanme"
368 echo "$image.restored"
371 # Process all image dumps in a given directory
378 if [ -z "$dir" ]; then
382 for image in $(find "$dir" \( -iname '*.img' -o \
383 -iname '*.img.xz' -o \
385 -iname '*.raw.xz' \) | sort)
387 extracted=$(extract_image "$image")
388 check_image "$extracted"
393 # some tests need to mount the recovered image and do verifications call
394 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
395 # needs to fail otherwise; using sudo by default for now
397 NEED_SUDO_VALIDATE=unknown
399 export NEED_SUDO_VALIDATE
402 if [ $UID -eq 0 ]; then
405 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
406 sudo -v -n &>/dev/null || \
407 _not_run "Need to validate sudo credentials"
409 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
410 sudo -n /bin/true &> /dev/null || \
411 _not_run "Need to validate sudo user settings"
415 _not_run "Need to validate root privileges"
422 if [ $UID -eq 0 -o -n "$SUDO_HELPER" ]; then
426 # Test for old sudo or special settings, which make sudo -v fail even
427 # if user setting is NOPASSWD
428 sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
430 # Newer sudo or default sudo setting
431 sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
433 if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
434 _not_run "Need to validate root privileges"
436 SUDO_HELPER=root_helper
444 [[ "$size" ]] || size='2G'
445 # Still truncate it to new size
446 if [ -n "$TEST_DEV" ]; then
447 truncate -s 0 "$TEST_DEV"
448 truncate -s "$size" "$TEST_DEV"
452 echo "\$TEST_DEV not given, using $TOP/tests/test.img as fallback" >> \
454 TEST_DEV="$TOP/tests/test.img"
456 truncate -s 0 "$TEST_DEV"
457 truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
460 run_check_mount_test_dev()
465 if [[ -b "$TEST_DEV" ]]; then
467 elif [[ -f "$TEST_DEV" ]]; then
470 _fail "Invalid \$TEST_DEV: $TEST_DEV"
473 [[ -d "$TEST_MNT" ]] || {
474 _fail "Invalid \$TEST_MNT: $TEST_MNT"
477 run_check $SUDO_HELPER mount -t btrfs $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
480 run_check_umount_test_dev()
483 run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
486 check_kernel_support()
488 if ! grep -iq 'btrfs' /proc/filesystems; then
489 run_check $SUDO_HELPER modprobe btrfs
490 if ! grep -iq 'btrfs' /proc/filesystems; then
492 "WARNING: btrfs filesystem not found in /proc/filesystems, some tests might fail"
499 # how many files to create.
505 dirpath=$TEST_MNT/$dataset_type
506 run_check $SUDO_HELPER mkdir -p "$dirpath"
508 case "$dataset_type" in
510 for num in $(seq 1 "$DATASET_SIZE"); do
511 run_check $SUDO_HELPER dd if=/dev/urandom of="$dirpath/$dataset_type.$num" bs=10K \
512 count=1 >/dev/null 2>&1
517 for num in $(seq 1 "$DATASET_SIZE"); do
518 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$num"
519 run_check $SUDO_HELPER ln "$dirpath/$dataset_type.$num" "$dirpath/hlink.$num"
524 for num in $(seq 1 "$DATASET_SIZE"); do
525 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$num"
526 run_check cd "$dirpath" && \
527 $SUDO_HELPER ln -s "$dataset_type.$num" "$dirpath/slink.$num" && \
533 for num in $(seq 1 "$DATASET_SIZE"); do
534 run_check $SUDO_HELPER ln -s "$dirpath/$dataset_type.$num" "$dirpath/blink.$num"
539 for modes in 777 775 755 750 700 666 664 644 640 600 444 440 400 000 \
540 1777 1775 1755 1750 1700 1666 1664 1644 1640 1600 1444 1440 1400 1000 \
541 2777 2775 2755 2750 2700 2666 2664 2644 2640 2600 2444 2440 2400 2000 \
542 4777 4775 4755 4750 4700 4666 4664 4644 4640 4600 4444 4440 4400 4000; do
543 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$modes"
544 run_check $SUDO_HELPER chmod "$modes" "$dirpath/$dataset_type.$modes"
549 for num in $(seq 1 "$DATASET_SIZE"); do
550 run_check $SUDO_HELPER dd if=/dev/urandom of="$dirpath/$dataset_type.$num" bs=10K \
551 count=1 >/dev/null 2>&1
552 run_check $SUDO_HELPER truncate -s 500K "$dirpath/$dataset_type.$num"
553 run_check $SUDO_HELPER dd if=/dev/urandom of="$dirpath/$dataset_type.$num" bs=10K \
554 oflag=append conv=notrunc count=1 >/dev/null 2>&1
555 run_check $SUDO_HELPER truncate -s 800K "$dirpath/$dataset_type.$num"
560 for num in $(seq 1 "$DATASET_SIZE"); do
561 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$num"
562 run_check $SUDO_HELPER setfacl -m "u:root:x" "$dirpath/$dataset_type.$num"
563 run_check $SUDO_HELPER setfattr -n user.foo -v "bar$num" "$dirpath/$dataset_type.$num"
568 for num in $(seq 1 "$DATASET_SIZE"); do
569 run_check $SUDO_HELPER mkfifo "$dirpath/$dataset_type.$num"
574 long_filename=`date +%s | sha256sum | cut -f1 -d ' '`
575 run_check $SUDO_HELPER touch "$dirpath/$long_filename"
576 for num in $(seq 1 "$DATASET_SIZE"); do
577 run_check $SUDO_HELPER ln -s "$dirpath/$long_filename" "$dirpath/slow_slink.$num"
581 run_check $SUDO_HELPER dd if=/dev/urandom bs=32M count=1 \
582 of="$dirpath/$dataset_type" >/dev/null 2>&1
587 # prepare environment for loop devices, set up the following variables
588 # - nloopdevs -- number of desired devices
589 # - loopdevs -- array containing paths to all devices (after prepare is called)
590 # - loopdev_prefix -- file backed images starting with this string, 'img' by default
592 # $1: number of loop devices to be set up
596 _fail "setup_loopdevs needs a number"
604 # create all loop devices from a given loopdev environment
607 for i in `seq $nloopdevs`; do
608 touch $loopdev_prefix$i
609 chmod a+rw $loopdev_prefix$i
610 truncate -s0 $loopdev_prefix$i
611 truncate -s2g $loopdev_prefix$i
612 loopdevs[$i]=`run_check_stdout $SUDO_HELPER losetup --find --show $loopdev_prefix$i`
616 # detach loop devices and reset their size to 0, delete the files afterwards
619 for dev in ${loopdevs[@]}; do
620 run_check $SUDO_HELPER losetup -d $dev
622 for i in `seq $nloopdevs`; do
623 truncate -s0 $loopdev_prefix$i
624 rm -- "$loopdev_prefix$i"
626 run_check $SUDO_HELPER losetup --all
631 TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
633 mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }
635 source $TOP/tests/common.local
637 if [ "$TEST_ENABLE_OVERRIDE" = 'true' -a -n "$RESULTS" ]; then
638 echo "INCLUDE common.local" >> "$RESULTS"
639 echo " check: $TEST_ARGS_CHECK" >> "$RESULTS"