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, output is logged
71 echo "############### $@" >> "$RESULTS" 2>&1
72 if [ "$TEST_LOG" = 'tty' ]; then echo "CMD(mayfail): $@" > /dev/tty; fi
73 if [ "$1" = 'root_helper' ]; then
76 $INSTRUMENT "$@" >> "$RESULTS" 2>&1
79 if [ $ret != 0 ]; then
80 echo "failed (ignored, ret=$ret): $@" >> "$RESULTS"
81 if [ $ret == 139 ]; then
82 _fail "mayfail: returned code 139 (SEGFAULT), not ignored"
83 elif [ $ret == 134 ]; then
84 _fail "mayfail: returned code 134 (SIGABRT), not ignored"
90 # first argument is error message to print if it fails, otherwise
91 # same as run_check but expects the command to fail, output is logged
99 echo "############### $@" >> "$RESULTS" 2>&1
100 if [ "$TEST_LOG" = 'tty' ]; then echo "CMD(mustfail): $@" > /dev/tty; fi
101 if [ "$1" = 'root_helper' ]; then
102 "$@" >> "$RESULTS" 2>&1
104 $INSTRUMENT "$@" >> "$RESULTS" 2>&1
107 echo "failed (expected): $@" >> "$RESULTS"
110 echo "succeeded (unexpected!): $@" >> "$RESULTS"
111 _fail "unexpected success: $msg"
118 if ! [ -f "$TOP/$1" ]; then
119 _fail "Failed prerequisites: $1";
123 check_global_prereq()
125 which $1 &> /dev/null
126 if [ $? -ne 0 ]; then
127 _fail "Failed system wide prerequisities: $1";
136 echo "testing image $(basename $image)" >> "$RESULTS"
137 $TOP/btrfs check "$image" >> "$RESULTS" 2>&1
138 [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
140 run_check $TOP/btrfs check --repair "$image"
141 run_check $TOP/btrfs check "$image"
144 # Extract a usable image from packed formats
145 # - raw btrfs filesystem images, suffix .raw
146 # - dtto compressed by XZ, suffix .raw.xz
147 # - meta-dump images with suffix .img
148 # - dtto compressed by XZ, suffix .img.xz
149 # - compressed send stream, .stream.xz
158 rm -f "$image.restored"
161 xz --decompress --keep "$image" || \
162 _fail "failed to decompress image $image" >&2
164 rm -f "$image.restored"
168 cp --sparse=auto "$image" "$image.restored"
171 xz --decompress --keep "$image" || \
172 _fail "failed to decompress image $image" >&2
174 mv "$image" "$image.restored"
177 xz --decompress --keep "$image" || \
178 _fail "failed to decompress file $image" >&2
180 mv "$image" "$image.restored"
184 if ! [ -f "$image.restored" ]; then
185 echo "restoring image $(basename $image)" >> "$RESULTS"
186 "$TOP/btrfs-image" -r "$image" "$image.restored" \
188 || _fail "failed to restore image $image" >&2
191 [ -f "$cleanme" ] && rm -f "$cleanme"
193 echo "$image.restored"
196 # Process all image dumps in a given directory
203 for image in $(find "$dir" \( -iname '*.img' -o \
204 -iname '*.img.xz' -o \
206 -iname '*.raw.xz' \) | sort)
208 extracted=$(extract_image "$image")
209 check_image "$extracted"
214 # some tests need to mount the recovered image and do verifications call
215 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
216 # needs to fail otherwise; using sudo by default for now
218 NEED_SUDO_VALIDATE=unknown
220 export NEED_SUDO_VALIDATE
223 if [ $UID -eq 0 ]; then
226 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
227 sudo -v -n &>/dev/null || \
228 _not_run "Need to validate sudo credentials"
230 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
231 sudo -n /bin/true &> /dev/null || \
232 _not_run "Need to validate sudo user settings"
236 _not_run "Need to validate root privileges"
243 if [ $UID -eq 0 -o -n "$SUDO_HELPER" ]; then
247 # Test for old sudo or special settings, which make sudo -v fail even
248 # if user setting is NOPASSWD
249 sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
251 # Newer sudo or default sudo setting
252 sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
254 if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
255 _not_run "Need to validate root privileges"
257 SUDO_HELPER=root_helper
265 [[ "$TEST_DEV" ]] && return
266 [[ "$size" ]] || size='2G'
268 echo "\$TEST_DEV not given, use $TOP/test/test.img as fallback" >> \
270 TEST_DEV="$TOP/tests/test.img"
272 truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
275 run_check_mount_test_dev()
280 if [[ -b "$TEST_DEV" ]]; then
282 elif [[ -f "$TEST_DEV" ]]; then
285 _fail "Invalid \$TEST_DEV: $TEST_DEV"
288 [[ -d "$TEST_MNT" ]] || {
289 _fail "Invalid \$TEST_MNT: $TEST_MNT"
292 run_check $SUDO_HELPER mount $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
295 run_check_umount_test_dev()
298 run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
301 check_kernel_support()
303 if ! grep -iq 'btrfs' /proc/filesystems; then
304 echo "WARNING: btrfs filesystem not listed in /proc/filesystems, some tests might fail"
312 TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
314 mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }