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 prerequisites: $1";
100 if [ $? -ne 0 ]; then
101 _fail "Failed system wide prerequisities: $1";
110 echo "testing image $(basename $image)" >> $RESULTS
111 $TOP/btrfs check $image >> $RESULTS 2>&1
112 [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
114 run_check $TOP/btrfs check --repair $image
115 run_check $TOP/btrfs check $image
118 # Extract a usable image from packed formats
119 # - raw btrfs filesystem images, suffix .raw
120 # - dtto compressed by XZ, suffix .raw.xz
121 # - meta-dump images with suffix .img
122 # - dtto compressed by XZ, suffix .img.xz
131 rm -f $image.restored
134 xz --decompress --keep "$image" || \
135 _fail "failed to decompress image $image" >&2
137 rm -f $image.restored
141 cp --sparse=auto $image $image.restored
144 xz --decompress --keep "$image" || \
145 _fail "failed to decompress image $image" >&2
147 mv "$image" "$image".restored
151 if ! [ -f $image.restored ]; then
152 echo "restoring image $(basename $image)" >> $RESULTS
153 $TOP/btrfs-image -r $image $image.restored \
155 || _fail "failed to restore image $image" >&2
158 [ -f "$cleanme" ] && rm -f "$cleanme"
160 echo "$image.restored"
163 # Process all image dumps in a given directory
170 for image in $(find $dir \( -iname '*.img' -o \
171 -iname '*.img.xz' -o \
173 -iname '*.raw.xz' \) | sort)
175 extracted=$(extract_image "$image")
176 check_image "$extracted"
181 # some tests need to mount the recovered image and do verifications call
182 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
183 # needs to fail otherwise; using sudo by default for now
185 NEED_SUDO_VALIDATE=unknown
187 export NEED_SUDO_VALIDATE
190 if [ $UID -eq 0 ]; then
193 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
194 sudo -v -n &>/dev/null || \
195 _not_run "Need to validate sudo credentials"
197 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
198 sudo -n /bin/true &> /dev/null || \
199 _not_run "Need to validate sudo user settings"
203 _not_run "Need to validate root privileges"
210 if [ $UID -eq 0 -o -n "$SUDO_HELPER" ]; then
214 # Test for old sudo or special settings, which make sudo -v fail even
215 # if user setting is NOPASSWD
216 sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
218 # Newer sudo or default sudo setting
219 sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
221 if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
222 _not_run "Need to validate root privileges"
224 SUDO_HELPER=root_helper
232 [[ "$TEST_DEV" ]] && return
233 [[ "$size" ]] || size='2G'
235 echo "\$TEST_DEV not given, use $TOP/test/test.img as fallback" >> \
237 TEST_DEV="$TOP/tests/test.img"
239 truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
242 run_check_mount_test_dev()
247 if [[ -b "$TEST_DEV" ]]; then
249 elif [[ -f "$TEST_DEV" ]]; then
252 _fail "Invalid \$TEST_DEV: $TEST_DEV"
255 [[ -d "$TEST_MNT" ]] || {
256 _fail "Invalid \$TEST_MNT: $TEST_MNT"
259 run_check $SUDO_HELPER mount $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
262 run_check_umount_test_dev()
265 run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
270 TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
272 mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }