btrfs-progs: tests: Move extract_image out of check_all_images for common use
[platform/upstream/btrfs-progs.git] / tests / common
1 #!/bin/bash
2 #
3 # Common routines for all tests
4 #
5
6 _fail()
7 {
8         echo "$*" | tee -a $RESULTS
9         exit 1
10 }
11
12 # log a message to the results file
13 _log()
14 {
15         echo "$*" | tee -a $RESULTS
16 }
17
18 _not_run()
19 {
20         echo "    [NOTRUN] $*"
21         exit 0
22 }
23
24 run_check()
25 {
26         echo "############### $@" >> $RESULTS 2>&1
27         "$@" >> $RESULTS 2>&1 || _fail "failed: $@"
28 }
29
30 # same as run_check but the stderr+stdout output is duplicated on stdout and
31 # can be processed further
32 run_check_stdout()
33 {
34         echo "############### $@" >> $RESULTS 2>&1
35         "$@" 2>&1 | tee -a $RESULTS || _fail "failed: $@"
36 }
37
38 # same as run_check but does not fail the test, output is logged
39 run_mayfail()
40 {
41         echo "############### $@" >> $RESULTS 2>&1
42         "$@" >> $RESULTS 2>&1 || _log "failed (ignored): $@"
43 }
44
45 check_prereq()
46 {
47         if ! [ -f $TOP/$1 ]; then
48                 _fail "Failed prerequisities: $1";
49         fi
50 }
51
52 check_image()
53 {
54         local image
55
56         image=$1
57         echo "testing image $(basename $image)" >> $RESULTS
58         $TOP/btrfs check $image >> $RESULTS 2>&1
59         [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
60
61         run_check $TOP/btrfs check --repair $image
62         run_check $TOP/btrfs check $image
63 }
64
65 # Extract a usable image from packed formats
66 # - raw btrfs filesystem images, suffix .raw
67 # - dtto compressed by XZ, suffix .raw.xz
68 # - meta-dump images with suffix .img
69 # - dtto compressed by XZ, suffix .img.xz
70 extract_image()
71 {
72         local image
73         local cleanme
74
75         image="$1"
76         case "$image" in
77         *.img)
78                 rm -f $image.restored
79                 : ;;
80         *.img.xz)
81                 xz --decompress --keep "$image" || \
82                         _fail "failed to decompress image $image" >&2
83                 image=${image%%.xz}
84                 rm -f $image.restored
85                 cleanme=$image
86                 ;;
87         *.raw)
88                 cp --sparse=auto $image $image.restored
89                 ;;
90         *.raw.xz)
91                 xz --decompress --keep "$image" || \
92                         _fail "failed to decompress image $image" >&2
93                 image=${image%%.xz}
94                 mv "$image" "$image".restored
95                 ;;
96         esac
97
98         if ! [ -f $image.restored ]; then
99                 echo "restoring image $(basename $image)" >> $RESULTS
100                 $TOP/btrfs-image -r $image $image.restored || \
101                         _fail "failed to restore image $image" >&2
102         fi
103
104         [ -f "$cleanme" ] && rm -f "$cleanme"
105
106         echo "$image.restored"
107 }
108
109 # Process all image dumps in a given directory
110 check_all_images()
111 {
112         local dir
113         local extracted
114
115         dir="$1"
116         for image in $(find $dir \( -iname '*.img' -o   \
117                                 -iname '*.img.xz' -o    \
118                                 -iname '*.raw' -o       \
119                                 -iname '*.raw.xz' \) | sort)
120         do
121                 extracted=$(extract_image "$image")
122                 check_image "$extracted"
123                 rm -f "$extracted"
124         done
125 }
126
127 # some tests need to mount the recovered image and do verifications call
128 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
129 # needs to fail otherwise; using sudo by default for now
130 SUDO_HELPER=
131 NEED_SUDO_VALIDATE=unknown
132 export SUDO_HELPER
133 export NEED_SUDO_VALIDATE
134 root_helper()
135 {
136         if [ $UID -eq 0 ]; then
137                 "$@"
138         else
139                 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
140                         sudo -v -n &>/dev/null || \
141                                 _not_run "Need to validate sudo credentials"
142                         sudo -n "$@"
143                 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
144                         sudo -n /bin/true &> /dev/null || \
145                                 _not_run "Need to validate sudo user settings"
146                         sudo -n "$@"
147                 else
148                         # should not happen
149                         _not_run "Need to validate root privileges"
150                 fi
151         fi
152 }
153
154 setup_root_helper()
155 {
156         if [ $UID -eq 0 ]; then
157                 return
158         fi
159
160         # Test for old sudo or special settings, which make sudo -v fail even
161         # if user setting is NOPASSWD
162         sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
163
164         # Newer sudo or default sudo setting
165         sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
166
167         if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
168                 _not_run "Need to validate root privileges"
169         fi
170         SUDO_HELPER=root_helper
171 }
172
173 prepare_test_dev()
174 {
175         # num[K/M/G/T...]
176         local size="$1"
177
178         [[ "$TEST_DEV" ]] && return
179         [[ "$size" ]] || size='1G'
180
181         echo "\$TEST_DEV not given, use $TOP/test/test.img as fallback" >> \
182                 $RESULTS
183         TEST_DEV="$TOP/tests/test.img"
184
185         truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
186 }
187
188 run_check_mount_test_dev()
189 {
190         setup_root_helper
191
192         local loop_opt
193         if [[ -b "$TEST_DEV" ]]; then
194                 loop_opt=""
195         elif [[ -f "$TEST_DEV" ]]; then
196                 loop_opt="-o loop"
197         else
198                 _fail "Invalid \$TEST_DEV: $TEST_DEV"
199         fi
200
201         [[ -d "$TEST_MNT" ]] || {
202                 _fail "Invalid \$TEST_MNT: $TEST_MNT"
203         }
204
205         run_check $SUDO_HELPER mount $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
206 }
207
208 run_check_umount_test_dev()
209 {
210         setup_root_helper
211         run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
212 }
213
214 init_env()
215 {
216         TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
217         export TEST_MNT
218         mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }
219 }
220 init_env