3 # Common routines for all tests
6 # assert that argument is not empty and is an existing path (file or directory)
12 if [ -z "$path" ]; then
13 echo "ASSERTION FAIL: $path is not valid"
17 if [ -f "$path" -o -d "$path" -o -b "$path" ]; then
20 echo "ASSERTION FAIL: $path is not valid"
26 echo "$*" | tee -a "$RESULTS"
30 # log a message to the results file
33 echo "$*" | tee -a "$RESULTS"
44 echo "############### $@" >> "$RESULTS" 2>&1
45 if [[ $TEST_LOG =~ tty ]]; then echo "CMD: $@" > /dev/tty; fi
46 if [ "$1" = 'root_helper' ]; then
47 "$@" >> "$RESULTS" 2>&1 || _fail "failed: $@"
49 $INSTRUMENT "$@" >> "$RESULTS" 2>&1 || _fail "failed: $@"
53 # same as run_check but the stderr+stdout output is duplicated on stdout and
54 # can be processed further
57 echo "############### $@" >> "$RESULTS" 2>&1
58 if [[ $TEST_LOG =~ tty ]]; then echo "CMD(stdout): $@" > /dev/tty; fi
59 if [ "$1" = 'root_helper' ]; then
60 "$@" 2>&1 | tee -a "$RESULTS" || _fail "failed: $@"
62 $INSTRUMENT "$@" 2>&1 | tee -a "$RESULTS" || _fail "failed: $@"
66 # same as run_check but does not fail the test if it's handled gracefully by
67 # the tool, unexpected failure like segfault or abor will exit forcibly
73 echo "############### $@" >> "$RESULTS" 2>&1
74 if [[ $TEST_LOG =~ tty ]]; then echo "CMD(mayfail): $@" > /dev/tty; fi
75 if [ "$1" = 'root_helper' ]; then
78 $INSTRUMENT "$@" >> "$RESULTS" 2>&1
81 if [ $ret != 0 ]; then
82 echo "failed (ignored, ret=$ret): $@" >> "$RESULTS"
83 if [ $ret == 139 ]; then
84 _fail "mayfail: returned code 139 (SEGFAULT), not ignored"
85 elif [ $ret == 134 ]; then
86 _fail "mayfail: returned code 134 (SIGABRT), not ignored"
92 # first argument is error message to print if it fails, otherwise
93 # same as run_check but expects the command to fail, output is logged
101 echo "############### $@" >> "$RESULTS" 2>&1
102 if [[ $TEST_LOG =~ tty ]]; then echo "CMD(mustfail): $@" > /dev/tty; fi
103 if [ "$1" = 'root_helper' ]; then
104 "$@" >> "$RESULTS" 2>&1
106 $INSTRUMENT "$@" >> "$RESULTS" 2>&1
109 echo "failed (expected): $@" >> "$RESULTS"
112 echo "succeeded (unexpected!): $@" >> "$RESULTS"
113 _fail "unexpected success: $msg"
120 if ! [ -f "$TOP/$1" ]; then
121 _fail "Failed prerequisites: $1";
125 check_global_prereq()
127 which $1 &> /dev/null
128 if [ $? -ne 0 ]; then
129 _fail "Failed system wide prerequisities: $1";
138 echo "testing image $(basename $image)" >> "$RESULTS"
139 $TOP/btrfs check "$image" >> "$RESULTS" 2>&1
140 [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
142 run_check $TOP/btrfs check --repair "$image"
143 run_check $TOP/btrfs check "$image"
146 # Extract a usable image from packed formats
147 # - raw btrfs filesystem images, suffix .raw
148 # - dtto compressed by XZ, suffix .raw.xz
149 # - meta-dump images with suffix .img
150 # - dtto compressed by XZ, suffix .img.xz
151 # - compressed send stream, .stream.xz
160 rm -f "$image.restored"
163 xz --decompress --keep "$image" || \
164 _fail "failed to decompress image $image" >&2
166 rm -f "$image.restored"
170 cp --sparse=auto "$image" "$image.restored"
173 xz --decompress --keep "$image" || \
174 _fail "failed to decompress image $image" >&2
176 mv "$image" "$image.restored"
179 xz --decompress --keep "$image" || \
180 _fail "failed to decompress file $image" >&2
182 mv "$image" "$image.restored"
186 if ! [ -f "$image.restored" ]; then
187 echo "restoring image $(basename $image)" >> "$RESULTS"
188 "$TOP/btrfs-image" -r "$image" "$image.restored" \
190 || _fail "failed to restore image $image" >&2
193 [ -f "$cleanme" ] && rm -f "$cleanme"
195 echo "$image.restored"
198 # Process all image dumps in a given directory
205 if [ -z "$dir" ]; then
209 for image in $(find "$dir" \( -iname '*.img' -o \
210 -iname '*.img.xz' -o \
212 -iname '*.raw.xz' \) | sort)
214 extracted=$(extract_image "$image")
215 check_image "$extracted"
220 # some tests need to mount the recovered image and do verifications call
221 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
222 # needs to fail otherwise; using sudo by default for now
224 NEED_SUDO_VALIDATE=unknown
226 export NEED_SUDO_VALIDATE
229 if [ $UID -eq 0 ]; then
232 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
233 sudo -v -n &>/dev/null || \
234 _not_run "Need to validate sudo credentials"
236 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
237 sudo -n /bin/true &> /dev/null || \
238 _not_run "Need to validate sudo user settings"
242 _not_run "Need to validate root privileges"
249 if [ $UID -eq 0 -o -n "$SUDO_HELPER" ]; then
253 # Test for old sudo or special settings, which make sudo -v fail even
254 # if user setting is NOPASSWD
255 sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
257 # Newer sudo or default sudo setting
258 sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
260 if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
261 _not_run "Need to validate root privileges"
263 SUDO_HELPER=root_helper
271 [[ "$TEST_DEV" ]] && return
272 [[ "$size" ]] || size='2G'
274 echo "\$TEST_DEV not given, use $TOP/test/test.img as fallback" >> \
276 TEST_DEV="$TOP/tests/test.img"
278 truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
281 run_check_mount_test_dev()
286 if [[ -b "$TEST_DEV" ]]; then
288 elif [[ -f "$TEST_DEV" ]]; then
291 _fail "Invalid \$TEST_DEV: $TEST_DEV"
294 [[ -d "$TEST_MNT" ]] || {
295 _fail "Invalid \$TEST_MNT: $TEST_MNT"
298 run_check $SUDO_HELPER mount $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
301 run_check_umount_test_dev()
304 run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
307 check_kernel_support()
309 if ! grep -iq 'btrfs' /proc/filesystems; then
310 echo "WARNING: btrfs filesystem not listed in /proc/filesystems, some tests might fail"
318 TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
320 mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }