3 # Common routines for all tests
8 echo "$*" | tee -a $RESULTS
12 # log a message to the results file
15 echo "$*" | tee -a $RESULTS
26 echo "############### $@" >> $RESULTS 2>&1
27 if [ "$TEST_LOG" = 'tty' ]; then echo "CMD: $@" > /dev/tty; fi
28 if [ "$1" = 'root_helper' ]; then
29 "$@" >> $RESULTS 2>&1 || _fail "failed: $@"
31 $INSTRUMENT "$@" >> $RESULTS 2>&1 || _fail "failed: $@"
35 # same as run_check but the stderr+stdout output is duplicated on stdout and
36 # can be processed further
39 echo "############### $@" >> $RESULTS 2>&1
40 if [ "$TEST_LOG" = 'tty' ]; then echo "CMD(stdout): $@" > /dev/tty; fi
41 if [ "$1" = 'root_helper' ]; then
42 "$@" 2>&1 | tee -a $RESULTS || _fail "failed: $@"
44 $INSTRUMENT "$@" 2>&1 | tee -a $RESULTS || _fail "failed: $@"
48 # same as run_check but does not fail the test, output is logged
53 echo "############### $@" >> $RESULTS 2>&1
54 if [ "$TEST_LOG" = 'tty' ]; then echo "CMD(mayfail): $@" > /dev/tty; fi
55 if [ "$1" = 'root_helper' ]; then
58 $INSTRUMENT "$@" >> $RESULTS 2>&1
61 if [ $ret != 0 ]; then
62 echo "failed (ignored, ret=$ret): $@" >> $RESULTS
63 if [ $ret == 139 ]; then
64 _fail "mayfail: returned code 139 (SEGFAULT), not ignored"
65 elif [ $ret == 134 ]; then
66 _fail "mayfail: returned code 134 (SIGABRT), not ignored"
72 # first argument is error message to print if it fails, otherwise
73 # same as run_check but expects the command to fail, output is logged
81 echo "############### $@" >> $RESULTS 2>&1
82 if [ "$TEST_LOG" = 'tty' ]; then echo "CMD(mustfail): $@" > /dev/tty; fi
83 if [ "$1" = 'root_helper' ]; then
86 $INSTRUMENT "$@" >> $RESULTS 2>&1
89 echo "failed (expected): $@" >> $RESULTS
92 echo "succeeded (unexpected!): $@" >> $RESULTS
93 _fail "unexpected success: $msg"
100 if ! [ -f $TOP/$1 ]; then
101 _fail "Failed prerequisites: $1";
105 check_global_prereq()
107 which $1 &> /dev/null
108 if [ $? -ne 0 ]; then
109 _fail "Failed system wide prerequisities: $1";
118 echo "testing image $(basename $image)" >> $RESULTS
119 $TOP/btrfs check $image >> $RESULTS 2>&1
120 [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
122 run_check $TOP/btrfs check --repair $image
123 run_check $TOP/btrfs check $image
126 # Extract a usable image from packed formats
127 # - raw btrfs filesystem images, suffix .raw
128 # - dtto compressed by XZ, suffix .raw.xz
129 # - meta-dump images with suffix .img
130 # - dtto compressed by XZ, suffix .img.xz
131 # - compressed send stream, .stream.xz
140 rm -f $image.restored
143 xz --decompress --keep "$image" || \
144 _fail "failed to decompress image $image" >&2
146 rm -f $image.restored
150 cp --sparse=auto $image $image.restored
153 xz --decompress --keep "$image" || \
154 _fail "failed to decompress image $image" >&2
156 mv "$image" "$image".restored
159 xz --decompress --keep "$image" || \
160 _fail "failed to decompress file $image" >&2
162 mv "$image" "$image".restored
166 if ! [ -f $image.restored ]; then
167 echo "restoring image $(basename $image)" >> $RESULTS
168 $TOP/btrfs-image -r $image $image.restored \
170 || _fail "failed to restore image $image" >&2
173 [ -f "$cleanme" ] && rm -f "$cleanme"
175 echo "$image.restored"
178 # Process all image dumps in a given directory
185 for image in $(find $dir \( -iname '*.img' -o \
186 -iname '*.img.xz' -o \
188 -iname '*.raw.xz' \) | sort)
190 extracted=$(extract_image "$image")
191 check_image "$extracted"
196 # some tests need to mount the recovered image and do verifications call
197 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
198 # needs to fail otherwise; using sudo by default for now
200 NEED_SUDO_VALIDATE=unknown
202 export NEED_SUDO_VALIDATE
205 if [ $UID -eq 0 ]; then
208 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
209 sudo -v -n &>/dev/null || \
210 _not_run "Need to validate sudo credentials"
212 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
213 sudo -n /bin/true &> /dev/null || \
214 _not_run "Need to validate sudo user settings"
218 _not_run "Need to validate root privileges"
225 if [ $UID -eq 0 -o -n "$SUDO_HELPER" ]; then
229 # Test for old sudo or special settings, which make sudo -v fail even
230 # if user setting is NOPASSWD
231 sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
233 # Newer sudo or default sudo setting
234 sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
236 if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
237 _not_run "Need to validate root privileges"
239 SUDO_HELPER=root_helper
247 [[ "$TEST_DEV" ]] && return
248 [[ "$size" ]] || size='2G'
250 echo "\$TEST_DEV not given, use $TOP/test/test.img as fallback" >> \
252 TEST_DEV="$TOP/tests/test.img"
254 truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
257 run_check_mount_test_dev()
262 if [[ -b "$TEST_DEV" ]]; then
264 elif [[ -f "$TEST_DEV" ]]; then
267 _fail "Invalid \$TEST_DEV: $TEST_DEV"
270 [[ -d "$TEST_MNT" ]] || {
271 _fail "Invalid \$TEST_MNT: $TEST_MNT"
274 run_check $SUDO_HELPER mount $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
277 run_check_umount_test_dev()
280 run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
283 check_kernel_support()
285 if ! grep -iq 'btrfs' /proc/filesystems; then
286 echo "WARNING: btrfs filesystem not listed in /proc/filesystems, some tests might fail"
294 TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
296 mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }