6178048633c5711f919311ce38e082c160afa887
[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 check_prereq()
65 {
66         if ! [ -f $TOP/$1 ]; then
67                 _fail "Failed prerequisities: $1";
68         fi
69 }
70
71 check_image()
72 {
73         local image
74
75         image=$1
76         echo "testing image $(basename $image)" >> $RESULTS
77         $TOP/btrfs check $image >> $RESULTS 2>&1
78         [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
79
80         run_check $TOP/btrfs check --repair $image
81         run_check $TOP/btrfs check $image
82 }
83
84 # Extract a usable image from packed formats
85 # - raw btrfs filesystem images, suffix .raw
86 # - dtto compressed by XZ, suffix .raw.xz
87 # - meta-dump images with suffix .img
88 # - dtto compressed by XZ, suffix .img.xz
89 extract_image()
90 {
91         local image
92         local cleanme
93
94         image="$1"
95         case "$image" in
96         *.img)
97                 rm -f $image.restored
98                 : ;;
99         *.img.xz)
100                 xz --decompress --keep "$image" || \
101                         _fail "failed to decompress image $image" >&2
102                 image=${image%%.xz}
103                 rm -f $image.restored
104                 cleanme=$image
105                 ;;
106         *.raw)
107                 cp --sparse=auto $image $image.restored
108                 ;;
109         *.raw.xz)
110                 xz --decompress --keep "$image" || \
111                         _fail "failed to decompress image $image" >&2
112                 image=${image%%.xz}
113                 mv "$image" "$image".restored
114                 ;;
115         esac
116
117         if ! [ -f $image.restored ]; then
118                 echo "restoring image $(basename $image)" >> $RESULTS
119                 $TOP/btrfs-image -r $image $image.restored \
120                         &>> $RESULTS \
121                         || _fail "failed to restore image $image" >&2
122         fi
123
124         [ -f "$cleanme" ] && rm -f "$cleanme"
125
126         echo "$image.restored"
127 }
128
129 # Process all image dumps in a given directory
130 check_all_images()
131 {
132         local dir
133         local extracted
134
135         dir="$1"
136         for image in $(find $dir \( -iname '*.img' -o   \
137                                 -iname '*.img.xz' -o    \
138                                 -iname '*.raw' -o       \
139                                 -iname '*.raw.xz' \) | sort)
140         do
141                 extracted=$(extract_image "$image")
142                 check_image "$extracted"
143                 rm -f "$extracted"
144         done
145 }
146
147 # some tests need to mount the recovered image and do verifications call
148 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
149 # needs to fail otherwise; using sudo by default for now
150 SUDO_HELPER=
151 NEED_SUDO_VALIDATE=unknown
152 export SUDO_HELPER
153 export NEED_SUDO_VALIDATE
154 root_helper()
155 {
156         if [ $UID -eq 0 ]; then
157                 "$@"
158         else
159                 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
160                         sudo -v -n &>/dev/null || \
161                                 _not_run "Need to validate sudo credentials"
162                         sudo -n "$@"
163                 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
164                         sudo -n /bin/true &> /dev/null || \
165                                 _not_run "Need to validate sudo user settings"
166                         sudo -n "$@"
167                 else
168                         # should not happen
169                         _not_run "Need to validate root privileges"
170                 fi
171         fi
172 }
173
174 setup_root_helper()
175 {
176         if [ $UID -eq 0 -o -n "$SUDO_HELPER" ]; then
177                 return
178         fi
179
180         # Test for old sudo or special settings, which make sudo -v fail even
181         # if user setting is NOPASSWD
182         sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
183
184         # Newer sudo or default sudo setting
185         sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
186
187         if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
188                 _not_run "Need to validate root privileges"
189         fi
190         SUDO_HELPER=root_helper
191 }
192
193 prepare_test_dev()
194 {
195         # num[K/M/G/T...]
196         local size="$1"
197
198         [[ "$TEST_DEV" ]] && return
199         [[ "$size" ]] || size='2G'
200
201         echo "\$TEST_DEV not given, use $TOP/test/test.img as fallback" >> \
202                 $RESULTS
203         TEST_DEV="$TOP/tests/test.img"
204
205         truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
206 }
207
208 run_check_mount_test_dev()
209 {
210         setup_root_helper
211
212         local loop_opt
213         if [[ -b "$TEST_DEV" ]]; then
214                 loop_opt=""
215         elif [[ -f "$TEST_DEV" ]]; then
216                 loop_opt="-o loop"
217         else
218                 _fail "Invalid \$TEST_DEV: $TEST_DEV"
219         fi
220
221         [[ -d "$TEST_MNT" ]] || {
222                 _fail "Invalid \$TEST_MNT: $TEST_MNT"
223         }
224
225         run_check $SUDO_HELPER mount $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
226 }
227
228 run_check_umount_test_dev()
229 {
230         setup_root_helper
231         run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
232 }
233
234 init_env()
235 {
236         TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
237         export TEST_MNT
238         mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }
239 }
240 init_env