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
51 echo "############### $@" >> $RESULTS 2>&1
52 if [ "$TEST_LOG" = 'tty' ]; then echo "CMD(mayfail): $@" > /dev/tty; fi
53 if [ "$1" = 'root_helper' ]; then
56 $INSTRUMENT "$@" >> $RESULTS 2>&1
59 echo "failed (ignored): $@" >> $RESULTS
64 # first argument is error message to print if it fails, otherwise
65 # same as run_check but expects the command to fail, output is logged
73 echo "############### $@" >> $RESULTS 2>&1
74 if [ "$TEST_LOG" = 'tty' ]; then echo "CMD(mustfail): $@" > /dev/tty; fi
75 if [ "$1" = 'root_helper' ]; then
78 $INSTRUMENT "$@" >> $RESULTS 2>&1
81 echo "failed (expected): $@" >> $RESULTS
84 echo "succeeded (unexpected!): $@" >> $RESULTS
85 _fail "unexpected success: $msg"
92 if ! [ -f $TOP/$1 ]; then
93 _fail "Failed prerequisities: $1";
102 echo "testing image $(basename $image)" >> $RESULTS
103 $TOP/btrfs check $image >> $RESULTS 2>&1
104 [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
106 run_check $TOP/btrfs check --repair $image
107 run_check $TOP/btrfs check $image
110 # Extract a usable image from packed formats
111 # - raw btrfs filesystem images, suffix .raw
112 # - dtto compressed by XZ, suffix .raw.xz
113 # - meta-dump images with suffix .img
114 # - dtto compressed by XZ, suffix .img.xz
123 rm -f $image.restored
126 xz --decompress --keep "$image" || \
127 _fail "failed to decompress image $image" >&2
129 rm -f $image.restored
133 cp --sparse=auto $image $image.restored
136 xz --decompress --keep "$image" || \
137 _fail "failed to decompress image $image" >&2
139 mv "$image" "$image".restored
143 if ! [ -f $image.restored ]; then
144 echo "restoring image $(basename $image)" >> $RESULTS
145 $TOP/btrfs-image -r $image $image.restored \
147 || _fail "failed to restore image $image" >&2
150 [ -f "$cleanme" ] && rm -f "$cleanme"
152 echo "$image.restored"
155 # Process all image dumps in a given directory
162 for image in $(find $dir \( -iname '*.img' -o \
163 -iname '*.img.xz' -o \
165 -iname '*.raw.xz' \) | sort)
167 extracted=$(extract_image "$image")
168 check_image "$extracted"
173 # some tests need to mount the recovered image and do verifications call
174 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
175 # needs to fail otherwise; using sudo by default for now
177 NEED_SUDO_VALIDATE=unknown
179 export NEED_SUDO_VALIDATE
182 if [ $UID -eq 0 ]; then
185 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
186 sudo -v -n &>/dev/null || \
187 _not_run "Need to validate sudo credentials"
189 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
190 sudo -n /bin/true &> /dev/null || \
191 _not_run "Need to validate sudo user settings"
195 _not_run "Need to validate root privileges"
202 if [ $UID -eq 0 -o -n "$SUDO_HELPER" ]; then
206 # Test for old sudo or special settings, which make sudo -v fail even
207 # if user setting is NOPASSWD
208 sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
210 # Newer sudo or default sudo setting
211 sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
213 if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
214 _not_run "Need to validate root privileges"
216 SUDO_HELPER=root_helper
224 [[ "$TEST_DEV" ]] && return
225 [[ "$size" ]] || size='2G'
227 echo "\$TEST_DEV not given, use $TOP/test/test.img as fallback" >> \
229 TEST_DEV="$TOP/tests/test.img"
231 truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
234 run_check_mount_test_dev()
239 if [[ -b "$TEST_DEV" ]]; then
241 elif [[ -f "$TEST_DEV" ]]; then
244 _fail "Invalid \$TEST_DEV: $TEST_DEV"
247 [[ -d "$TEST_MNT" ]] || {
248 _fail "Invalid \$TEST_MNT: $TEST_MNT"
251 run_check $SUDO_HELPER mount $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
254 run_check_umount_test_dev()
257 run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
262 TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
264 mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }