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"
70 # first argument is error message to print if it fails, otherwise
71 # same as run_check but expects the command to fail, output is logged
79 echo "############### $@" >> $RESULTS 2>&1
80 if [ "$TEST_LOG" = 'tty' ]; then echo "CMD(mustfail): $@" > /dev/tty; fi
81 if [ "$1" = 'root_helper' ]; then
84 $INSTRUMENT "$@" >> $RESULTS 2>&1
87 echo "failed (expected): $@" >> $RESULTS
90 echo "succeeded (unexpected!): $@" >> $RESULTS
91 _fail "unexpected success: $msg"
98 if ! [ -f $TOP/$1 ]; then
99 _fail "Failed prerequisites: $1";
103 check_global_prereq()
105 which $1 &> /dev/null
106 if [ $? -ne 0 ]; then
107 _fail "Failed system wide prerequisities: $1";
116 echo "testing image $(basename $image)" >> $RESULTS
117 $TOP/btrfs check $image >> $RESULTS 2>&1
118 [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
120 run_check $TOP/btrfs check --repair $image
121 run_check $TOP/btrfs check $image
124 # Extract a usable image from packed formats
125 # - raw btrfs filesystem images, suffix .raw
126 # - dtto compressed by XZ, suffix .raw.xz
127 # - meta-dump images with suffix .img
128 # - dtto compressed by XZ, suffix .img.xz
137 rm -f $image.restored
140 xz --decompress --keep "$image" || \
141 _fail "failed to decompress image $image" >&2
143 rm -f $image.restored
147 cp --sparse=auto $image $image.restored
150 xz --decompress --keep "$image" || \
151 _fail "failed to decompress image $image" >&2
153 mv "$image" "$image".restored
157 if ! [ -f $image.restored ]; then
158 echo "restoring image $(basename $image)" >> $RESULTS
159 $TOP/btrfs-image -r $image $image.restored \
161 || _fail "failed to restore image $image" >&2
164 [ -f "$cleanme" ] && rm -f "$cleanme"
166 echo "$image.restored"
169 # Process all image dumps in a given directory
176 for image in $(find $dir \( -iname '*.img' -o \
177 -iname '*.img.xz' -o \
179 -iname '*.raw.xz' \) | sort)
181 extracted=$(extract_image "$image")
182 check_image "$extracted"
187 # some tests need to mount the recovered image and do verifications call
188 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
189 # needs to fail otherwise; using sudo by default for now
191 NEED_SUDO_VALIDATE=unknown
193 export NEED_SUDO_VALIDATE
196 if [ $UID -eq 0 ]; then
199 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
200 sudo -v -n &>/dev/null || \
201 _not_run "Need to validate sudo credentials"
203 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
204 sudo -n /bin/true &> /dev/null || \
205 _not_run "Need to validate sudo user settings"
209 _not_run "Need to validate root privileges"
216 if [ $UID -eq 0 -o -n "$SUDO_HELPER" ]; then
220 # Test for old sudo or special settings, which make sudo -v fail even
221 # if user setting is NOPASSWD
222 sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
224 # Newer sudo or default sudo setting
225 sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
227 if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
228 _not_run "Need to validate root privileges"
230 SUDO_HELPER=root_helper
238 [[ "$TEST_DEV" ]] && return
239 [[ "$size" ]] || size='2G'
241 echo "\$TEST_DEV not given, use $TOP/test/test.img as fallback" >> \
243 TEST_DEV="$TOP/tests/test.img"
245 truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
248 run_check_mount_test_dev()
253 if [[ -b "$TEST_DEV" ]]; then
255 elif [[ -f "$TEST_DEV" ]]; then
258 _fail "Invalid \$TEST_DEV: $TEST_DEV"
261 [[ -d "$TEST_MNT" ]] || {
262 _fail "Invalid \$TEST_MNT: $TEST_MNT"
265 run_check $SUDO_HELPER mount $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
268 run_check_umount_test_dev()
271 run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
276 TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
278 mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }