Handle ESRCH when sending signals
[ganeti-local] / test / import-export_unittest.bash
1 #!/bin/bash
2 #
3
4 # Copyright (C) 2010 Google Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21 set -e
22 set -o pipefail
23
24 export PYTHON=${PYTHON:=python}
25
26 impexpd="$PYTHON daemons/import-export -d"
27
28 err() {
29   echo "$@"
30   echo 'Aborting'
31   show_output
32   exit 1
33 }
34
35 show_output() {
36   if [[ -s "$gencert_output" ]]; then
37     echo
38     echo 'Generating certificates:'
39     cat $gencert_output
40   fi
41   if [[ -s "$dst_output" ]]; then
42     echo
43     echo 'Import output:'
44     cat $dst_output
45   fi
46   if [[ -s "$src_output" ]]; then
47     echo
48     echo 'Export output:'
49     cat $src_output
50   fi
51 }
52
53 checkpids() {
54   local result=0
55
56   # Unlike combining the "wait" commands using || or &&, this ensures we
57   # actually wait for all PIDs.
58   for pid in "$@"; do
59     if ! wait $pid; then
60       result=1
61     fi
62   done
63
64   return $result
65 }
66
67 get_testpath() {
68   echo "${TOP_SRCDIR:-.}/test"
69 }
70
71 get_testfile() {
72   echo "$(get_testpath)/data/$1"
73 }
74
75 upto() {
76   echo "$(date '+%F %T'):" "$@" '...'
77 }
78
79 statusdir=$(mktemp -d)
80 trap "rm -rf $statusdir" EXIT
81
82 gencert_output=$statusdir/gencert.output
83
84 src_statusfile=$statusdir/src.status
85 src_output=$statusdir/src.output
86 src_x509=$statusdir/src.pem
87
88 dst_statusfile=$statusdir/dst.status
89 dst_output=$statusdir/dst.output
90 dst_x509=$statusdir/dst.pem
91
92 other_x509=$statusdir/other.pem
93
94 testdata=$statusdir/data1
95 largetestdata=$statusdir/data2
96
97 upto 'Command line parameter tests'
98
99 $impexpd >/dev/null 2>&1 &&
100   err "daemon-util succeeded without parameters"
101
102 $impexpd foo bar baz moo boo >/dev/null 2>&1 &&
103   err "daemon-util succeeded with wrong parameters"
104
105 $impexpd $src_statusfile >/dev/null 2>&1 &&
106   err "daemon-util succeeded with insufficient parameters"
107
108 $impexpd $src_statusfile invalidmode >/dev/null 2>&1 &&
109   err "daemon-util succeeded with invalid mode"
110
111 $impexpd $src_statusfile import --compression=rot13 >/dev/null 2>&1 &&
112   err "daemon-util succeeded with invalid compression"
113
114 upto 'Generate test data'
115 cat $(get_testfile proc_drbd8.txt) $(get_testfile cert1.pem) > $testdata
116
117 # Generate about 7.5 MB of test data
118 { tmp="$(<$testdata)"
119   for (( i=0; i < 100; ++i )); do
120     echo "$tmp $tmp $tmp $tmp $tmp $tmp"
121   done
122   dd if=/dev/zero bs=1024 count=4096 2>/dev/null
123   for (( i=0; i < 100; ++i )); do
124     echo "$tmp $tmp $tmp $tmp $tmp $tmp"
125   done
126 } > $largetestdata
127
128 impexpd_helper() {
129   $PYTHON $(get_testpath)/import-export_unittest-helper "$@"
130 }
131
132 start_test() {
133   upto "$@"
134
135   rm -f $src_statusfile $dst_output $dst_statusfile $dst_output
136   rm -f $gencert_output
137
138   imppid=
139   exppid=
140
141   cmd_prefix=
142   cmd_suffix=
143   connect_timeout=30
144   connect_retries=1
145   compress=gzip
146 }
147
148 wait_import_ready() {
149   # Wait for listening port
150   impexpd_helper $dst_statusfile listen-port
151 }
152
153 do_export() {
154   local port=$1
155
156   $impexpd $src_statusfile export --bind=127.0.0.1 \
157     --host=127.0.0.1 --port=$port \
158     --key=$src_x509 --cert=$src_x509 --ca=$dst_x509 \
159     --cmd-prefix="$cmd_prefix" --cmd-suffix="$cmd_suffix" \
160     --connect-timeout=$connect_timeout \
161     --connect-retries=$connect_retries \
162     --compress=$compress
163 }
164
165 do_import() {
166   $impexpd $dst_statusfile import --bind=127.0.0.1 \
167     --host=127.0.0.1 \
168     --key=$dst_x509 --cert=$dst_x509 --ca=$src_x509 \
169     --cmd-prefix="$cmd_prefix" --cmd-suffix="$cmd_suffix" \
170     --connect-timeout=$connect_timeout \
171     --connect-retries=$connect_retries \
172     --compress=$compress
173 }
174
175 upto 'Generate X509 certificates and keys'
176 impexpd_helper $src_x509 gencert 2>$gencert_output & srccertpid=$!
177 impexpd_helper $dst_x509 gencert 2>$gencert_output & dstcertpid=$!
178 impexpd_helper $other_x509 gencert 2>$gencert_output & othercertpid=$!
179 checkpids $srccertpid $dstcertpid $othercertpid || \
180   err 'Failed to generate certificates'
181
182 start_test 'Normal case'
183 do_import > $statusdir/recv1 2>$dst_output & imppid=$!
184 if port=$(wait_import_ready 2>$src_output); then
185   do_export $port < $testdata >>$src_output 2>&1 & exppid=$!
186 fi
187 checkpids $exppid $imppid || err 'An error occurred'
188 cmp $testdata $statusdir/recv1 || err 'Received data does not match input'
189
190 start_test 'Export using wrong CA'
191 # Setting lower timeout to not wait for too long
192 connect_timeout=1 do_import &>$dst_output & imppid=$!
193 if port=$(wait_import_ready 2>$src_output); then
194   : | dst_x509=$other_x509 do_export $port >>$src_output 2>&1 & exppid=$!
195 fi
196 checkpids $exppid $imppid && err 'Export did not fail when using wrong CA'
197
198 start_test 'Import using wrong CA'
199 # Setting lower timeout to not wait for too long
200 src_x509=$other_x509 connect_timeout=1 do_import &>$dst_output & imppid=$!
201 if port=$(wait_import_ready 2>$src_output); then
202   : | do_export $port >>$src_output 2>&1 & exppid=$!
203 fi
204 checkpids $exppid $imppid && err 'Import did not fail when using wrong CA'
205
206 start_test 'Suffix command on import'
207 cmd_suffix="| cksum > $statusdir/recv2" do_import &>$dst_output & imppid=$!
208 if port=$(wait_import_ready 2>$src_output); then
209   do_export $port < $testdata >>$src_output 2>&1 & exppid=$!
210 fi
211 checkpids $exppid $imppid || err 'Testing additional commands failed'
212 cmp $statusdir/recv2 <(cksum < $testdata) || \
213   err 'Checksum of received data does not match'
214
215 start_test 'Prefix command on export'
216 do_import > $statusdir/recv3 2>$dst_output & imppid=$!
217 if port=$(wait_import_ready 2>$src_output); then
218   cmd_prefix='cksum |' do_export $port <$testdata >>$src_output 2>&1 & exppid=$!
219 fi
220 checkpids $exppid $imppid || err 'Testing additional commands failed'
221 cmp $statusdir/recv3 <(cksum < $testdata) || \
222   err 'Received checksum does not match'
223
224 start_test 'Failing prefix command on export'
225 : | cmd_prefix='exit 1;' do_export 0 &>$src_output & exppid=$!
226 checkpids $exppid && err 'Prefix command on export did not fail when it should'
227
228 start_test 'Failing suffix command on export'
229 do_import >&$dst_output & imppid=$!
230 if port=$(wait_import_ready 2>$src_output); then
231   : | cmd_suffix='| exit 1' do_export $port >>$src_output 2>&1 & exppid=$!
232 fi
233 checkpids $imppid $exppid && \
234   err 'Suffix command on export did not fail when it should'
235
236 start_test 'Failing prefix command on import'
237 cmd_prefix='exit 1;' do_import &>$dst_output & imppid=$!
238 checkpids $imppid && err 'Prefix command on import did not fail when it should'
239
240 start_test 'Failing suffix command on import'
241 cmd_suffix='| exit 1' do_import &>$dst_output & imppid=$!
242 if port=$(wait_import_ready 2>$src_output); then
243   : | do_export $port >>$src_output 2>&1 & exppid=$!
244 fi
245 checkpids $imppid $exppid && \
246   err 'Suffix command on import did not fail when it should'
247
248 start_test 'Listen timeout A'
249 # Setting lower timeout to not wait too long (there won't be anything trying to
250 # connect)
251 connect_timeout=1 do_import &>$dst_output & imppid=$!
252 checkpids $imppid && \
253   err 'Listening with timeout did not fail when it should'
254
255 start_test 'Listen timeout B'
256 do_import &>$dst_output & imppid=$!
257 if port=$(wait_import_ready 2>$src_output); then
258   { sleep 1; : | do_export $port; } >>$src_output 2>&1 & exppid=$!
259 fi
260 checkpids $exppid $imppid || \
261   err 'Listening with timeout failed when it should not'
262
263 start_test 'Connect timeout'
264 # Setting lower timeout as nothing will be listening on port 0
265 : | connect_timeout=1 do_export 0 &>$src_output & exppid=$!
266 checkpids $exppid && err 'Connection did not time out when it should'
267
268 start_test 'No compression'
269 compress=none do_import > $statusdir/recv-nocompr 2>$dst_output & imppid=$!
270 if port=$(wait_import_ready 2>$src_output); then
271   compress=none do_export $port < $testdata >>$src_output 2>&1 & exppid=$!
272 fi
273 checkpids $exppid $imppid || err 'An error occurred'
274 cmp $testdata $statusdir/recv-nocompr || \
275   err 'Received data does not match input'
276
277 start_test 'Compression mismatch A'
278 compress=none do_import > $statusdir/recv-miscompr 2>$dst_output & imppid=$!
279 if port=$(wait_import_ready 2>$src_output); then
280   compress=gzip do_export $port < $testdata >>$src_output 2>&1 & exppid=$!
281 fi
282 checkpids $exppid $imppid || err 'An error occurred'
283 cmp -s $testdata $statusdir/recv-miscompr && \
284   err 'Received data matches input when it should not'
285
286 start_test 'Compression mismatch B'
287 compress=gzip do_import > $statusdir/recv-miscompr2 2>$dst_output & imppid=$!
288 if port=$(wait_import_ready 2>$src_output); then
289   compress=none do_export $port < $testdata >>$src_output 2>&1 & exppid=$!
290 fi
291 checkpids $exppid $imppid && err 'Did not fail when it should'
292 cmp -s $testdata $statusdir/recv-miscompr2 && \
293   err 'Received data matches input when it should not'
294
295 start_test 'Large transfer'
296 do_import > $statusdir/recv-large 2>$dst_output & imppid=$!
297 if port=$(wait_import_ready 2>$src_output); then
298   do_export $port < $largetestdata >>$src_output 2>&1 & exppid=$!
299 fi
300 checkpids $exppid $imppid || err 'An error occurred'
301 cmp $largetestdata $statusdir/recv-large || \
302   err 'Received data does not match input'
303
304 exit 0