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 "$@" >> $RESULTS 2>&1 || _fail "failed: $@"
31 # same as run_check but the stderr+stdout output is duplicated on stdout and
32 # can be processed further
35 echo "############### $@" >> $RESULTS 2>&1
36 if [ "$TEST_LOG" = 'tty' ]; then echo "CMD(stdout): $@" > /dev/tty; fi
37 "$@" 2>&1 | tee -a $RESULTS || _fail "failed: $@"
40 # same as run_check but does not fail the test, output is logged
43 echo "############### $@" >> $RESULTS 2>&1
44 if [ "$TEST_LOG" = 'tty' ]; then echo "CMD(mayfail): $@" > /dev/tty; fi
45 "$@" >> $RESULTS 2>&1 || _log "failed (ignored): $@"
50 if ! [ -f $TOP/$1 ]; then
51 _fail "Failed prerequisities: $1";
60 echo "testing image $(basename $image)" >> $RESULTS
61 $TOP/btrfs check $image >> $RESULTS 2>&1
62 [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
64 run_check $TOP/btrfs check --repair $image
65 run_check $TOP/btrfs check $image
68 # Extract a usable image from packed formats
69 # - raw btrfs filesystem images, suffix .raw
70 # - dtto compressed by XZ, suffix .raw.xz
71 # - meta-dump images with suffix .img
72 # - dtto compressed by XZ, suffix .img.xz
84 xz --decompress --keep "$image" || \
85 _fail "failed to decompress image $image" >&2
91 cp --sparse=auto $image $image.restored
94 xz --decompress --keep "$image" || \
95 _fail "failed to decompress image $image" >&2
97 mv "$image" "$image".restored
101 if ! [ -f $image.restored ]; then
102 echo "restoring image $(basename $image)" >> $RESULTS
103 $TOP/btrfs-image -r $image $image.restored \
105 || _fail "failed to restore image $image" >&2
108 [ -f "$cleanme" ] && rm -f "$cleanme"
110 echo "$image.restored"
113 # Process all image dumps in a given directory
120 for image in $(find $dir \( -iname '*.img' -o \
121 -iname '*.img.xz' -o \
123 -iname '*.raw.xz' \) | sort)
125 extracted=$(extract_image "$image")
126 check_image "$extracted"
131 # some tests need to mount the recovered image and do verifications call
132 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
133 # needs to fail otherwise; using sudo by default for now
135 NEED_SUDO_VALIDATE=unknown
137 export NEED_SUDO_VALIDATE
140 if [ $UID -eq 0 ]; then
143 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
144 sudo -v -n &>/dev/null || \
145 _not_run "Need to validate sudo credentials"
147 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
148 sudo -n /bin/true &> /dev/null || \
149 _not_run "Need to validate sudo user settings"
153 _not_run "Need to validate root privileges"
160 if [ $UID -eq 0 ]; then
164 # Test for old sudo or special settings, which make sudo -v fail even
165 # if user setting is NOPASSWD
166 sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
168 # Newer sudo or default sudo setting
169 sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
171 if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
172 _not_run "Need to validate root privileges"
174 SUDO_HELPER=root_helper
182 [[ "$TEST_DEV" ]] && return
183 [[ "$size" ]] || size='2G'
185 echo "\$TEST_DEV not given, use $TOP/test/test.img as fallback" >> \
187 TEST_DEV="$TOP/tests/test.img"
189 truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
192 run_check_mount_test_dev()
197 if [[ -b "$TEST_DEV" ]]; then
199 elif [[ -f "$TEST_DEV" ]]; then
202 _fail "Invalid \$TEST_DEV: $TEST_DEV"
205 [[ -d "$TEST_MNT" ]] || {
206 _fail "Invalid \$TEST_MNT: $TEST_MNT"
209 run_check $SUDO_HELPER mount $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
212 run_check_umount_test_dev()
215 run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
220 TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
222 mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }