btrfs-progs: typo review of strings and comments
[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         if [ "$TEST_LOG" = 'tty' ]; then echo "CMD: $@" > /dev/tty; fi
28         if [ "$1" = 'root_helper' ]; then
29                 "$@" >> $RESULTS 2>&1 || _fail "failed: $@"
30         else
31                 $INSTRUMENT "$@" >> $RESULTS 2>&1 || _fail "failed: $@"
32         fi
33 }
34
35 # same as run_check but the stderr+stdout output is duplicated on stdout and
36 # can be processed further
37 run_check_stdout()
38 {
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: $@"
43         else
44                 $INSTRUMENT "$@" 2>&1 | tee -a $RESULTS || _fail "failed: $@"
45         fi
46 }
47
48 # same as run_check but does not fail the test, output is logged
49 run_mayfail()
50 {
51         echo "############### $@" >> $RESULTS 2>&1
52         if [ "$TEST_LOG" = 'tty' ]; then echo "CMD(mayfail): $@" > /dev/tty; fi
53         if [ "$1" = 'root_helper' ]; then
54                 "$@" >> $RESULTS 2>&1
55         else
56                 $INSTRUMENT "$@" >> $RESULTS 2>&1
57         fi
58         if [ $? != 0 ]; then
59                 echo "failed (ignored): $@" >> $RESULTS
60                 return 1
61         fi
62 }
63
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
66 run_mustfail()
67 {
68         local msg
69
70         msg="$1"
71         shift
72
73         echo "############### $@" >> $RESULTS 2>&1
74         if [ "$TEST_LOG" = 'tty' ]; then echo "CMD(mustfail): $@" > /dev/tty; fi
75         if [ "$1" = 'root_helper' ]; then
76                 "$@" >> $RESULTS 2>&1
77         else
78                 $INSTRUMENT "$@" >> $RESULTS 2>&1
79         fi
80         if [ $? != 0 ]; then
81                 echo "failed (expected): $@" >> $RESULTS
82                 return 0
83         else
84                 echo "succeeded (unexpected!): $@" >> $RESULTS
85                 _fail "unexpected success: $msg"
86                 return 1
87         fi
88 }
89
90 check_prereq()
91 {
92         if ! [ -f $TOP/$1 ]; then
93                 _fail "Failed prerequisites: $1";
94         fi
95 }
96
97 check_image()
98 {
99         local image
100
101         image=$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"
105
106         run_check $TOP/btrfs check --repair $image
107         run_check $TOP/btrfs check $image
108 }
109
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
115 extract_image()
116 {
117         local image
118         local cleanme
119
120         image="$1"
121         case "$image" in
122         *.img)
123                 rm -f $image.restored
124                 : ;;
125         *.img.xz)
126                 xz --decompress --keep "$image" || \
127                         _fail "failed to decompress image $image" >&2
128                 image=${image%%.xz}
129                 rm -f $image.restored
130                 cleanme=$image
131                 ;;
132         *.raw)
133                 cp --sparse=auto $image $image.restored
134                 ;;
135         *.raw.xz)
136                 xz --decompress --keep "$image" || \
137                         _fail "failed to decompress image $image" >&2
138                 image=${image%%.xz}
139                 mv "$image" "$image".restored
140                 ;;
141         esac
142
143         if ! [ -f $image.restored ]; then
144                 echo "restoring image $(basename $image)" >> $RESULTS
145                 $TOP/btrfs-image -r $image $image.restored \
146                         &>> $RESULTS \
147                         || _fail "failed to restore image $image" >&2
148         fi
149
150         [ -f "$cleanme" ] && rm -f "$cleanme"
151
152         echo "$image.restored"
153 }
154
155 # Process all image dumps in a given directory
156 check_all_images()
157 {
158         local dir
159         local extracted
160
161         dir="$1"
162         for image in $(find $dir \( -iname '*.img' -o   \
163                                 -iname '*.img.xz' -o    \
164                                 -iname '*.raw' -o       \
165                                 -iname '*.raw.xz' \) | sort)
166         do
167                 extracted=$(extract_image "$image")
168                 check_image "$extracted"
169                 rm -f "$extracted"
170         done
171 }
172
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
176 SUDO_HELPER=
177 NEED_SUDO_VALIDATE=unknown
178 export SUDO_HELPER
179 export NEED_SUDO_VALIDATE
180 root_helper()
181 {
182         if [ $UID -eq 0 ]; then
183                 "$@"
184         else
185                 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
186                         sudo -v -n &>/dev/null || \
187                                 _not_run "Need to validate sudo credentials"
188                         sudo -n "$@"
189                 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
190                         sudo -n /bin/true &> /dev/null || \
191                                 _not_run "Need to validate sudo user settings"
192                         sudo -n "$@"
193                 else
194                         # should not happen
195                         _not_run "Need to validate root privileges"
196                 fi
197         fi
198 }
199
200 setup_root_helper()
201 {
202         if [ $UID -eq 0 -o -n "$SUDO_HELPER" ]; then
203                 return
204         fi
205
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
209
210         # Newer sudo or default sudo setting
211         sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
212
213         if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
214                 _not_run "Need to validate root privileges"
215         fi
216         SUDO_HELPER=root_helper
217 }
218
219 prepare_test_dev()
220 {
221         # num[K/M/G/T...]
222         local size="$1"
223
224         [[ "$TEST_DEV" ]] && return
225         [[ "$size" ]] || size='2G'
226
227         echo "\$TEST_DEV not given, use $TOP/test/test.img as fallback" >> \
228                 $RESULTS
229         TEST_DEV="$TOP/tests/test.img"
230
231         truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
232 }
233
234 run_check_mount_test_dev()
235 {
236         setup_root_helper
237
238         local loop_opt
239         if [[ -b "$TEST_DEV" ]]; then
240                 loop_opt=""
241         elif [[ -f "$TEST_DEV" ]]; then
242                 loop_opt="-o loop"
243         else
244                 _fail "Invalid \$TEST_DEV: $TEST_DEV"
245         fi
246
247         [[ -d "$TEST_MNT" ]] || {
248                 _fail "Invalid \$TEST_MNT: $TEST_MNT"
249         }
250
251         run_check $SUDO_HELPER mount $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
252 }
253
254 run_check_umount_test_dev()
255 {
256         setup_root_helper
257         run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
258 }
259
260 init_env()
261 {
262         TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
263         export TEST_MNT
264         mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }
265 }
266 init_env