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"
26 echo "$*" | tee -a "$RESULTS"
30 # log a message to the results file
33 echo "$*" | tee -a "$RESULTS"
48 echo "DUMP args for ${FUNCNAME[1]}:"
49 while [ $# -gt 0 ]; do
56 # read arguments, look if we're calling btrfs and if there's a known
57 # subcommand, return argument index to insert, taking root helper into
58 # consideration, returns 2 for unknown subcommand
61 if [ "$1" = 'root_helper' ]; then
62 if [[ $2 =~ /btrfs$ ]]; then
67 if [[ $1 =~ /btrfs$ ]]; then
75 # return command-specific arguments if enabled
78 if [ "$TEST_ENABLE_OVERRIDE" = 'true' ]; then
79 # if defined via common.local, use it, otherwise pass make
81 if [ "$(type -t _skip_spec)" = 'function' ]; then
82 if _skip_spec "$@"; then
87 check) echo -n "$TEST_ARGS_CHECK" ;;
92 # Argument passing magic:
93 # the command passed to run_* helpers is inspected, if there's 'btrfs command'
94 # found and there are defined additional arguments, they're inserted just after
95 # the command name, ie. any arguments in the test could override them.
97 # The root helper is recognized. Unrecognized subcommands or external tools
106 ins=$(_get_spec_ins "$@")
108 cmd=$(eval echo "\${$spec}")
109 spec=$(_cmd_spec "${@:$spec}")
110 set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
111 echo "############### $@" >> "$RESULTS" 2>&1
112 if [[ $TEST_LOG =~ tty ]]; then echo "CMD: $@" > /dev/tty; fi
113 if [ "$1" = 'root_helper' ]; then
114 "$@" >> "$RESULTS" 2>&1 || _fail "failed: $@"
116 $INSTRUMENT "$@" >> "$RESULTS" 2>&1 || _fail "failed: $@"
120 # same as run_check but the stderr+stdout output is duplicated on stdout and
121 # can be processed further
128 ins=$(_get_spec_ins "$@")
130 cmd=$(eval echo "\${$spec}")
131 spec=$(_cmd_spec "${@:$spec}")
132 set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
133 echo "############### $@" >> "$RESULTS" 2>&1
134 if [[ $TEST_LOG =~ tty ]]; then echo "CMD(stdout): $@" > /dev/tty; fi
135 if [ "$1" = 'root_helper' ]; then
136 "$@" 2>&1 | tee -a "$RESULTS" || _fail "failed: $@"
138 $INSTRUMENT "$@" 2>&1 | tee -a "$RESULTS" || _fail "failed: $@"
142 # same as run_check but does not fail the test if it's handled gracefully by
143 # the tool, unexpected failure like segfault or abor will exit forcibly
152 ins=$(_get_spec_ins "$@")
154 cmd=$(eval echo "\${$spec}")
155 spec=$(_cmd_spec "${@:$spec}")
156 set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
157 echo "############### $@" >> "$RESULTS" 2>&1
158 if [[ $TEST_LOG =~ tty ]]; then echo "CMD(mayfail): $@" > /dev/tty; fi
159 if [ "$1" = 'root_helper' ]; then
160 "$@" >> "$RESULTS" 2>&1
162 $INSTRUMENT "$@" >> "$RESULTS" 2>&1
165 if [ $ret != 0 ]; then
166 echo "failed (ignored, ret=$ret): $@" >> "$RESULTS"
167 if [ $ret == 139 ]; then
168 _fail "mayfail: returned code 139 (SEGFAULT), not ignored"
169 elif [ $ret == 134 ]; then
170 _fail "mayfail: returned code 134 (SIGABRT), not ignored"
176 # first argument is error message to print if it fails, otherwise
177 # same as run_check but expects the command to fail, output is logged
188 ins=$(_get_spec_ins "$@")
190 cmd=$(eval echo "\${$spec}")
191 spec=$(_cmd_spec "${@:$spec}")
192 set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}"
193 echo "############### $@" >> "$RESULTS" 2>&1
194 if [[ $TEST_LOG =~ tty ]]; then echo "CMD(mustfail): $@" > /dev/tty; fi
195 if [ "$1" = 'root_helper' ]; then
196 "$@" >> "$RESULTS" 2>&1
198 $INSTRUMENT "$@" >> "$RESULTS" 2>&1
201 echo "failed (expected): $@" >> "$RESULTS"
204 echo "succeeded (unexpected!): $@" >> "$RESULTS"
205 _fail "unexpected success: $msg"
212 if ! [ -f "$TOP/$1" ]; then
213 _fail "Failed prerequisites: $1";
217 check_global_prereq()
219 which $1 &> /dev/null
220 if [ $? -ne 0 ]; then
221 _fail "Failed system wide prerequisities: $1";
230 echo "testing image $(basename $image)" >> "$RESULTS"
231 "$TOP/btrfs" check "$image" >> "$RESULTS" 2>&1
232 [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
234 run_check "$TOP/btrfs" check --repair "$image"
235 run_check "$TOP/btrfs" check "$image"
238 # Extract a usable image from packed formats
239 # - raw btrfs filesystem images, suffix .raw
240 # - dtto compressed by XZ, suffix .raw.xz
241 # - meta-dump images with suffix .img
242 # - dtto compressed by XZ, suffix .img.xz
243 # - compressed send stream, .stream.xz
252 rm -f "$image.restored"
255 xz --decompress --keep "$image" || \
256 _fail "failed to decompress image $image" >&2
258 rm -f "$image.restored"
262 cp --sparse=auto "$image" "$image.restored"
265 xz --decompress --keep "$image" || \
266 _fail "failed to decompress image $image" >&2
268 mv "$image" "$image.restored"
271 xz --decompress --keep "$image" || \
272 _fail "failed to decompress file $image" >&2
274 mv "$image" "$image.restored"
278 if ! [ -f "$image.restored" ]; then
279 echo "restoring image $(basename $image)" >> "$RESULTS"
280 "$TOP/btrfs-image" -r "$image" "$image.restored" \
282 || _fail "failed to restore image $image" >&2
285 [ -f "$cleanme" ] && rm -f "$cleanme"
287 echo "$image.restored"
290 # Process all image dumps in a given directory
297 if [ -z "$dir" ]; then
301 for image in $(find "$dir" \( -iname '*.img' -o \
302 -iname '*.img.xz' -o \
304 -iname '*.raw.xz' \) | sort)
306 extracted=$(extract_image "$image")
307 check_image "$extracted"
312 # some tests need to mount the recovered image and do verifications call
313 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
314 # needs to fail otherwise; using sudo by default for now
316 NEED_SUDO_VALIDATE=unknown
318 export NEED_SUDO_VALIDATE
321 if [ $UID -eq 0 ]; then
324 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
325 sudo -v -n &>/dev/null || \
326 _not_run "Need to validate sudo credentials"
328 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
329 sudo -n /bin/true &> /dev/null || \
330 _not_run "Need to validate sudo user settings"
334 _not_run "Need to validate root privileges"
341 if [ $UID -eq 0 -o -n "$SUDO_HELPER" ]; then
345 # Test for old sudo or special settings, which make sudo -v fail even
346 # if user setting is NOPASSWD
347 sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
349 # Newer sudo or default sudo setting
350 sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
352 if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
353 _not_run "Need to validate root privileges"
355 SUDO_HELPER=root_helper
363 [[ "$TEST_DEV" ]] && return
364 [[ "$size" ]] || size='2G'
366 echo "\$TEST_DEV not given, use $TOP/test/test.img as fallback" >> \
368 TEST_DEV="$TOP/tests/test.img"
370 truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
373 run_check_mount_test_dev()
378 if [[ -b "$TEST_DEV" ]]; then
380 elif [[ -f "$TEST_DEV" ]]; then
383 _fail "Invalid \$TEST_DEV: $TEST_DEV"
386 [[ -d "$TEST_MNT" ]] || {
387 _fail "Invalid \$TEST_MNT: $TEST_MNT"
390 run_check $SUDO_HELPER mount $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
393 run_check_umount_test_dev()
396 run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
399 check_kernel_support()
401 if ! grep -iq 'btrfs' /proc/filesystems; then
402 echo "WARNING: btrfs filesystem not listed in /proc/filesystems, some tests might fail"
408 # how many files to create.
414 dirpath=$TEST_MNT/$dataset_type
415 run_check $SUDO_HELPER mkdir -p "$dirpath"
417 case "$dataset_type" in
419 for num in $(seq 1 "$DATASET_SIZE"); do
420 run_check $SUDO_HELPER dd if=/dev/urandom of="$dirpath/$dataset_type.$num" bs=10K \
421 count=1 >/dev/null 2>&1
426 for num in $(seq 1 "$DATASET_SIZE"); do
427 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$num"
428 run_check $SUDO_HELPER ln "$dirpath/$dataset_type.$num" "$dirpath/hlink.$num"
433 for num in $(seq 1 "$DATASET_SIZE"); do
434 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$num"
435 run_check cd "$dirpath" && \
436 $SUDO_HELPER ln -s "$dataset_type.$num" "$dirpath/slink.$num" && \
442 for num in $(seq 1 "$DATASET_SIZE"); do
443 run_check $SUDO_HELPER ln -s "$dirpath/$dataset_type.$num" "$dirpath/blink.$num"
448 for modes in 777 775 755 750 700 666 664 644 640 600 444 440 400 000 \
449 1777 1775 1755 1750 1700 1666 1664 1644 1640 1600 1444 1440 1400 1000 \
450 2777 2775 2755 2750 2700 2666 2664 2644 2640 2600 2444 2440 2400 2000 \
451 4777 4775 4755 4750 4700 4666 4664 4644 4640 4600 4444 4440 4400 4000; do
452 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$modes"
453 run_check $SUDO_HELPER chmod "$modes" "$dirpath/$dataset_type.$modes"
458 for num in $(seq 1 "$DATASET_SIZE"); do
459 run_check $SUDO_HELPER dd if=/dev/urandom of="$dirpath/$dataset_type.$num" bs=10K \
460 count=1 >/dev/null 2>&1
461 run_check $SUDO_HELPER truncate -s 500K "$dirpath/$dataset_type.$num"
462 run_check $SUDO_HELPER dd if=/dev/urandom of="$dirpath/$dataset_type.$num" bs=10K \
463 oflag=append conv=notrunc count=1 >/dev/null 2>&1
464 run_check $SUDO_HELPER truncate -s 800K "$dirpath/$dataset_type.$num"
469 for num in $(seq 1 "$DATASET_SIZE"); do
470 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$num"
471 run_check $SUDO_HELPER setfacl -m "u:root:x" "$dirpath/$dataset_type.$num"
472 run_check $SUDO_HELPER setfattr -n user.foo -v "bar$num" "$dirpath/$dataset_type.$num"
477 for num in $(seq 1 "$DATASET_SIZE"); do
478 run_check $SUDO_HELPER mkfifo "$dirpath/$dataset_type.$num"
483 long_filename=`date +%s | sha256sum | cut -f1 -d'-'`
484 run_check $SUDO_HELPER touch "$dirpath/$long_filename"
485 for num in $(seq 1 "$DATASET_SIZE"); do
486 run_check $SUDO_HELPER ln -s "$dirpath/$long_filename" "$dirpath/slow_slink.$num"
490 run_check $SUDO_HELPER dd if=/dev/urandom bs=32M count=1 \
491 of="$dirpath/$dataset_type" >/dev/null 2>&1
498 TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
500 mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }
502 source $TOP/tests/common.local
504 if [ "$TEST_ENABLE_OVERRIDE" = 'true' -a -n "$RESULTS" ]; then
505 echo "INCLUDE common.local" >> "$RESULTS"
506 echo " check: $TEST_ARGS_CHECK" >> "$RESULTS"