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