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