Statistics
| Branch: | Tag: | Revision:

root / test / import-export_unittest.bash @ c08d76f5

History | View | Annotate | Download (8.4 kB)

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