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 if [ -f $(type -p "$msg") ]; then
46 echo "$*" | tee -a "$RESULTS"
50 # log a message to the results file
53 echo "$*" | tee -a "$RESULTS"
68 echo "DUMP args for ${FUNCNAME[1]}:"
69 while [ $# -gt 0 ]; do
76 # read arguments, look if we're calling btrfs and if there's a known
77 # subcommand, return argument index to insert, taking root helper into
78 # consideration, returns 2 for unknown subcommand
81 if [ "$1" = 'root_helper' ]; then
82 if [[ $2 =~ /btrfs$ ]]; then
87 if [[ $1 =~ /btrfs$ ]]; then
95 # return command-specific arguments if enabled
98 if [ "$TEST_ENABLE_OVERRIDE" = 'true' ]; then
99 # if defined via common.local, use it, otherwise pass make
101 if [ "$(type -t _skip_spec)" = 'function' ]; then
102 if _skip_spec "$@"; then
107 check) echo -n "$TEST_ARGS_CHECK" ;;
112 # Argument passing magic:
113 # the command passed to run_* helpers is inspected, if there's 'btrfs command'
114 # found and there are defined additional arguments, they're inserted just after
115 # the command name, ie. any arguments in the test could override them.
117 # The root helper is recognized. Unrecognized subcommands or external tools
126 ins=$(_get_spec_ins "$@")
128 cmd=$(eval echo "\${$spec}")
129 spec=$(_cmd_spec "${@:$spec}")
130 set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
131 echo "############### $@" >> "$RESULTS" 2>&1
132 if [[ $TEST_LOG =~ tty ]]; then echo "CMD: $@" > /dev/tty; fi
133 if [ "$1" = 'root_helper' ]; then
134 "$@" >> "$RESULTS" 2>&1 || _fail "failed: $@"
136 $INSTRUMENT "$@" >> "$RESULTS" 2>&1 || _fail "failed: $@"
140 # same as run_check but the stderr+stdout output is duplicated on stdout and
141 # can be processed further
148 ins=$(_get_spec_ins "$@")
150 cmd=$(eval echo "\${$spec}")
151 spec=$(_cmd_spec "${@:$spec}")
152 set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
153 echo "############### $@" >> "$RESULTS" 2>&1
154 if [[ $TEST_LOG =~ tty ]]; then echo "CMD(stdout): $@" > /dev/tty; fi
155 if [ "$1" = 'root_helper' ]; then
156 "$@" 2>&1 | tee -a "$RESULTS" || _fail "failed: $@"
158 $INSTRUMENT "$@" 2>&1 | tee -a "$RESULTS" || _fail "failed: $@"
162 # same as run_check but does not fail the test if it's handled gracefully by
163 # the tool, unexpected failure like segfault or abor will exit forcibly
172 ins=$(_get_spec_ins "$@")
174 cmd=$(eval echo "\${$spec}")
175 spec=$(_cmd_spec "${@:$spec}")
176 set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
177 echo "############### $@" >> "$RESULTS" 2>&1
178 if [[ $TEST_LOG =~ tty ]]; then echo "CMD(mayfail): $@" > /dev/tty; fi
179 if [ "$1" = 'root_helper' ]; then
180 "$@" >> "$RESULTS" 2>&1
182 $INSTRUMENT "$@" >> "$RESULTS" 2>&1
185 if [ $ret != 0 ]; then
186 echo "failed (ignored, ret=$ret): $@" >> "$RESULTS"
187 if [ $ret == 139 ]; then
188 _fail "mayfail: returned code 139 (SEGFAULT), not ignored"
189 elif [ $ret == 134 ]; then
190 _fail "mayfail: returned code 134 (SIGABRT), not ignored"
196 # first argument is error message to print if it fails, otherwise
197 # same as run_check but expects the command to fail, output is logged
208 if _is_file_or_command "$msg"; then
209 echo "ASSERTION FAIL: 1st argument of run_mustfail must be a message"
213 ins=$(_get_spec_ins "$@")
215 cmd=$(eval echo "\${$spec}")
216 spec=$(_cmd_spec "${@:$spec}")
217 set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
218 echo "############### $@" >> "$RESULTS" 2>&1
219 if [[ $TEST_LOG =~ tty ]]; then echo "CMD(mustfail): $@" > /dev/tty; fi
220 if [ "$1" = 'root_helper' ]; then
221 "$@" >> "$RESULTS" 2>&1
223 $INSTRUMENT "$@" >> "$RESULTS" 2>&1
226 echo "failed (expected): $@" >> "$RESULTS"
229 echo "succeeded (unexpected!): $@" >> "$RESULTS"
230 _fail "unexpected success: $msg"
237 if ! [ -f "$TOP/$1" ]; then
238 _fail "Failed prerequisites: $1";
242 check_global_prereq()
244 which $1 &> /dev/null
245 if [ $? -ne 0 ]; then
246 _fail "Failed system wide prerequisities: $1";
255 echo "testing image $(basename $image)" >> "$RESULTS"
256 "$TOP/btrfs" check "$image" >> "$RESULTS" 2>&1
257 [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
259 run_check "$TOP/btrfs" check --repair "$image"
260 run_check "$TOP/btrfs" check "$image"
263 # Extract a usable image from packed formats
264 # - raw btrfs filesystem images, suffix .raw
265 # - dtto compressed by XZ, suffix .raw.xz
266 # - meta-dump images with suffix .img
267 # - dtto compressed by XZ, suffix .img.xz
268 # - compressed send stream, .stream.xz
277 rm -f "$image.restored"
280 xz --decompress --keep "$image" || \
281 _fail "failed to decompress image $image" >&2
283 rm -f "$image.restored"
287 cp --sparse=auto "$image" "$image.restored"
290 xz --decompress --keep "$image" || \
291 _fail "failed to decompress image $image" >&2
293 mv "$image" "$image.restored"
296 xz --decompress --keep "$image" || \
297 _fail "failed to decompress file $image" >&2
299 mv "$image" "$image.restored"
303 if ! [ -f "$image.restored" ]; then
304 echo "restoring image $(basename $image)" >> "$RESULTS"
305 "$TOP/btrfs-image" -r "$image" "$image.restored" \
307 || _fail "failed to restore image $image" >&2
310 [ -f "$cleanme" ] && rm -f "$cleanme"
312 echo "$image.restored"
315 # Process all image dumps in a given directory
322 if [ -z "$dir" ]; then
326 for image in $(find "$dir" \( -iname '*.img' -o \
327 -iname '*.img.xz' -o \
329 -iname '*.raw.xz' \) | sort)
331 extracted=$(extract_image "$image")
332 check_image "$extracted"
337 # some tests need to mount the recovered image and do verifications call
338 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
339 # needs to fail otherwise; using sudo by default for now
341 NEED_SUDO_VALIDATE=unknown
343 export NEED_SUDO_VALIDATE
346 if [ $UID -eq 0 ]; then
349 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
350 sudo -v -n &>/dev/null || \
351 _not_run "Need to validate sudo credentials"
353 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
354 sudo -n /bin/true &> /dev/null || \
355 _not_run "Need to validate sudo user settings"
359 _not_run "Need to validate root privileges"
366 if [ $UID -eq 0 -o -n "$SUDO_HELPER" ]; then
370 # Test for old sudo or special settings, which make sudo -v fail even
371 # if user setting is NOPASSWD
372 sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
374 # Newer sudo or default sudo setting
375 sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
377 if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
378 _not_run "Need to validate root privileges"
380 SUDO_HELPER=root_helper
388 [[ "$TEST_DEV" ]] && return
389 [[ "$size" ]] || size='2G'
391 echo "\$TEST_DEV not given, use $TOP/test/test.img as fallback" >> \
393 TEST_DEV="$TOP/tests/test.img"
395 truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
398 run_check_mount_test_dev()
403 if [[ -b "$TEST_DEV" ]]; then
405 elif [[ -f "$TEST_DEV" ]]; then
408 _fail "Invalid \$TEST_DEV: $TEST_DEV"
411 [[ -d "$TEST_MNT" ]] || {
412 _fail "Invalid \$TEST_MNT: $TEST_MNT"
415 run_check $SUDO_HELPER mount -t btrfs $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
418 run_check_umount_test_dev()
421 run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
424 check_kernel_support()
426 if ! grep -iq 'btrfs' /proc/filesystems; then
427 echo "WARNING: btrfs filesystem not listed in /proc/filesystems, some tests might fail"
433 # how many files to create.
439 dirpath=$TEST_MNT/$dataset_type
440 run_check $SUDO_HELPER mkdir -p "$dirpath"
442 case "$dataset_type" in
444 for num in $(seq 1 "$DATASET_SIZE"); do
445 run_check $SUDO_HELPER dd if=/dev/urandom of="$dirpath/$dataset_type.$num" bs=10K \
446 count=1 >/dev/null 2>&1
451 for num in $(seq 1 "$DATASET_SIZE"); do
452 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$num"
453 run_check $SUDO_HELPER ln "$dirpath/$dataset_type.$num" "$dirpath/hlink.$num"
458 for num in $(seq 1 "$DATASET_SIZE"); do
459 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$num"
460 run_check cd "$dirpath" && \
461 $SUDO_HELPER ln -s "$dataset_type.$num" "$dirpath/slink.$num" && \
467 for num in $(seq 1 "$DATASET_SIZE"); do
468 run_check $SUDO_HELPER ln -s "$dirpath/$dataset_type.$num" "$dirpath/blink.$num"
473 for modes in 777 775 755 750 700 666 664 644 640 600 444 440 400 000 \
474 1777 1775 1755 1750 1700 1666 1664 1644 1640 1600 1444 1440 1400 1000 \
475 2777 2775 2755 2750 2700 2666 2664 2644 2640 2600 2444 2440 2400 2000 \
476 4777 4775 4755 4750 4700 4666 4664 4644 4640 4600 4444 4440 4400 4000; do
477 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$modes"
478 run_check $SUDO_HELPER chmod "$modes" "$dirpath/$dataset_type.$modes"
483 for num in $(seq 1 "$DATASET_SIZE"); do
484 run_check $SUDO_HELPER dd if=/dev/urandom of="$dirpath/$dataset_type.$num" bs=10K \
485 count=1 >/dev/null 2>&1
486 run_check $SUDO_HELPER truncate -s 500K "$dirpath/$dataset_type.$num"
487 run_check $SUDO_HELPER dd if=/dev/urandom of="$dirpath/$dataset_type.$num" bs=10K \
488 oflag=append conv=notrunc count=1 >/dev/null 2>&1
489 run_check $SUDO_HELPER truncate -s 800K "$dirpath/$dataset_type.$num"
494 for num in $(seq 1 "$DATASET_SIZE"); do
495 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$num"
496 run_check $SUDO_HELPER setfacl -m "u:root:x" "$dirpath/$dataset_type.$num"
497 run_check $SUDO_HELPER setfattr -n user.foo -v "bar$num" "$dirpath/$dataset_type.$num"
502 for num in $(seq 1 "$DATASET_SIZE"); do
503 run_check $SUDO_HELPER mkfifo "$dirpath/$dataset_type.$num"
508 long_filename=`date +%s | sha256sum | cut -f1 -d'-'`
509 run_check $SUDO_HELPER touch "$dirpath/$long_filename"
510 for num in $(seq 1 "$DATASET_SIZE"); do
511 run_check $SUDO_HELPER ln -s "$dirpath/$long_filename" "$dirpath/slow_slink.$num"
515 run_check $SUDO_HELPER dd if=/dev/urandom bs=32M count=1 \
516 of="$dirpath/$dataset_type" >/dev/null 2>&1
523 TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
525 mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }
527 source $TOP/tests/common.local
529 if [ "$TEST_ENABLE_OVERRIDE" = 'true' -a -n "$RESULTS" ]; then
530 echo "INCLUDE common.local" >> "$RESULTS"
531 echo " check: $TEST_ARGS_CHECK" >> "$RESULTS"