45713506d06c9d8cca97caa4acea3445d2e2c00d
[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                         &>> $RESULTS \
102                         || _fail "failed to restore image $image" >&2
103         fi
104
105         [ -f "$cleanme" ] && rm -f "$cleanme"
106
107         echo "$image.restored"
108 }
109
110 # Process all image dumps in a given directory
111 check_all_images()
112 {
113         local dir
114         local extracted
115
116         dir="$1"
117         for image in $(find $dir \( -iname '*.img' -o   \
118                                 -iname '*.img.xz' -o    \
119                                 -iname '*.raw' -o       \
120                                 -iname '*.raw.xz' \) | sort)
121         do
122                 extracted=$(extract_image "$image")
123                 check_image "$extracted"
124                 rm -f "$extracted"
125         done
126 }
127
128 # some tests need to mount the recovered image and do verifications call
129 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
130 # needs to fail otherwise; using sudo by default for now
131 SUDO_HELPER=
132 NEED_SUDO_VALIDATE=unknown
133 export SUDO_HELPER
134 export NEED_SUDO_VALIDATE
135 root_helper()
136 {
137         if [ $UID -eq 0 ]; then
138                 "$@"
139         else
140                 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
141                         sudo -v -n &>/dev/null || \
142                                 _not_run "Need to validate sudo credentials"
143                         sudo -n "$@"
144                 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
145                         sudo -n /bin/true &> /dev/null || \
146                                 _not_run "Need to validate sudo user settings"
147                         sudo -n "$@"
148                 else
149                         # should not happen
150                         _not_run "Need to validate root privileges"
151                 fi
152         fi
153 }
154
155 setup_root_helper()
156 {
157         if [ $UID -eq 0 ]; then
158                 return
159         fi
160
161         # Test for old sudo or special settings, which make sudo -v fail even
162         # if user setting is NOPASSWD
163         sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
164
165         # Newer sudo or default sudo setting
166         sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
167
168         if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
169                 _not_run "Need to validate root privileges"
170         fi
171         SUDO_HELPER=root_helper
172 }
173
174 prepare_test_dev()
175 {
176         # num[K/M/G/T...]
177         local size="$1"
178
179         [[ "$TEST_DEV" ]] && return
180         [[ "$size" ]] || size='1G'
181
182         echo "\$TEST_DEV not given, use $TOP/test/test.img as fallback" >> \
183                 $RESULTS
184         TEST_DEV="$TOP/tests/test.img"
185
186         truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
187 }
188
189 run_check_mount_test_dev()
190 {
191         setup_root_helper
192
193         local loop_opt
194         if [[ -b "$TEST_DEV" ]]; then
195                 loop_opt=""
196         elif [[ -f "$TEST_DEV" ]]; then
197                 loop_opt="-o loop"
198         else
199                 _fail "Invalid \$TEST_DEV: $TEST_DEV"
200         fi
201
202         [[ -d "$TEST_MNT" ]] || {
203                 _fail "Invalid \$TEST_MNT: $TEST_MNT"
204         }
205
206         run_check $SUDO_HELPER mount $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
207 }
208
209 run_check_umount_test_dev()
210 {
211         setup_root_helper
212         run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
213 }
214
215 init_env()
216 {
217         TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
218         export TEST_MNT
219         mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }
220 }
221 init_env