Statistics
| Branch: | Tag: | Revision:

root / test / import-export_unittest.bash @ 53dbf14c

History | View | Annotate | Download (8.4 kB)

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 "$dst_output" ]]; then
37
    echo
38
    echo 'Import output:'
39
    cat $dst_output
40
  fi
41
  if [[ -s "$src_output" ]]; then
42
    echo
43
    echo 'Export output:'
44
    cat $src_output
45
  fi
46
}
47

    
48
checkpids() {
49
  local result=0
50

    
51
  # Unlike combining the "wait" commands using || or &&, this ensures we
52
  # actually wait for all PIDs.
53
  for pid in "$@"; do
54
    if ! wait $pid; then
55
      result=1
56
    fi
57
  done
58

    
59
  return $result
60
}
61

    
62
get_testpath() {
63
  echo "${TOP_SRCDIR:-.}/test"
64
}
65

    
66
get_testfile() {
67
  echo "$(get_testpath)/data/$1"
68
}
69

    
70
upto() {
71
  echo "$(date '+%F %T'):" "$@" '...'
72
}
73

    
74
statusdir=$(mktemp -d)
75
trap "rm -rf $statusdir" EXIT
76

    
77
src_statusfile=$statusdir/src.status
78
src_output=$statusdir/src.output
79
src_x509=$statusdir/src.pem
80

    
81
dst_statusfile=$statusdir/dst.status
82
dst_output=$statusdir/dst.output
83
dst_x509=$statusdir/dst.pem
84
dst_portfile=$statusdir/dst.port
85

    
86
other_x509=$statusdir/other.pem
87

    
88
testdata=$statusdir/data1
89
largetestdata=$statusdir/data2
90

    
91
cmd_prefix=
92
cmd_suffix=
93
connect_timeout=10
94
connect_retries=1
95
compress=gzip
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
cat $(get_testfile proc_drbd8.txt) $(get_testfile cert1.pem) > $testdata
115

    
116
# Generate about 7.5 MB of test data
117
{ tmp="$(<$testdata)"
118
  for (( i=0; i < 100; ++i )); do
119
    echo "$tmp $tmp $tmp $tmp $tmp $tmp"
120
  done
121
  dd if=/dev/zero bs=1024 count=4096 2>/dev/null
122
  for (( i=0; i < 100; ++i )); do
123
    echo "$tmp $tmp $tmp $tmp $tmp $tmp"
124
  done
125
} > $largetestdata
126

    
127
impexpd_helper() {
128
  $PYTHON $(get_testpath)/import-export_unittest-helper "$@"
129
}
130

    
131
reset_status() {
132
  rm -f $src_statusfile $dst_output $dst_statusfile $dst_output $dst_portfile
133
}
134

    
135
write_data() {
136
  local fname=${1:-$testdata}
137

    
138
  # Wait for connection to be established
139
  impexpd_helper $src_statusfile connected
140

    
141
  # And just to be sure, also wait for destination to report as connected
142
  impexpd_helper $dst_statusfile connected
143

    
144
  cat $fname
145
}
146

    
147
do_export() {
148
  # Wait for listening port
149
  impexpd_helper $dst_statusfile listen-port > $dst_portfile
150

    
151
  local port=$(< $dst_portfile)
152

    
153
  test -n "$port" || err 'Empty port file'
154
  test "$port" != None || err 'Missing port'
155

    
156
  do_export_to_port $port
157
}
158

    
159
do_export_to_port() {
160
  local port=$1
161

    
162
  $impexpd $src_statusfile export --bind=127.0.0.1 \
163
    --host=127.0.0.1 --port=$port \
164
    --key=$src_x509 --cert=$src_x509 --ca=$dst_x509 \
165
    --cmd-prefix="$cmd_prefix" --cmd-suffix="$cmd_suffix" \
166
    --connect-timeout=$connect_timeout \
167
    --connect-retries=$connect_retries \
168
    --compress=$compress
169
}
170

    
171
do_import() {
172
  $impexpd $dst_statusfile import --bind=127.0.0.1 \
173
    --host=127.0.0.1 \
174
    --key=$dst_x509 --cert=$dst_x509 --ca=$src_x509 \
175
    --cmd-prefix="$cmd_prefix" --cmd-suffix="$cmd_suffix" \
176
    --connect-timeout=$connect_timeout \
177
    --connect-retries=$connect_retries \
178
    --compress=$compress
179
}
180

    
181
upto 'Generate X509 certificates and keys'
182
impexpd_helper $src_x509 gencert
183
impexpd_helper $dst_x509 gencert
184
impexpd_helper $other_x509 gencert
185

    
186
upto 'Normal case'
187
reset_status
188
do_import > $statusdir/recv1 2>$dst_output & imppid=$!
189
{ write_data | do_export; } &>$src_output & exppid=$!
190
checkpids $exppid $imppid || err 'An error occurred'
191
cmp $testdata $statusdir/recv1 || err 'Received data does not match input'
192

    
193
upto 'Export using wrong CA'
194
reset_status
195
# Setting lower timeout to not wait for too long
196
connect_timeout=1 do_import &>$dst_output & imppid=$!
197
: | dst_x509=$other_x509 do_export &>$src_output & exppid=$!
198
checkpids $exppid $imppid && err 'Export did not fail when using wrong CA'
199

    
200
upto 'Import using wrong CA'
201
reset_status
202
# Setting lower timeout to not wait for too long
203
src_x509=$other_x509 connect_timeout=1 do_import &>$dst_output & imppid=$!
204
: | do_export &>$src_output & exppid=$!
205
checkpids $exppid $imppid && err 'Import did not fail when using wrong CA'
206

    
207
upto 'Suffix command on import'
208
reset_status
209
cmd_suffix="| cksum > $statusdir/recv2" do_import &>$dst_output & imppid=$!
210
{ write_data | do_export; } &>$src_output & exppid=$!
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
upto 'Prefix command on export'
216
reset_status
217
do_import > $statusdir/recv3 2>$dst_output & imppid=$!
218
{ write_data | cmd_prefix="cksum |" do_export; } &>$src_output & exppid=$!
219
checkpids $exppid $imppid || err 'Testing additional commands failed'
220
cmp $statusdir/recv3 <(cksum < $testdata) || \
221
  err 'Received checksum does not match'
222

    
223
upto 'Failing prefix command on export'
224
reset_status
225
: | cmd_prefix='exit 1;' do_export_to_port 0 &>$src_output & exppid=$!
226
checkpids $exppid && err 'Prefix command on export did not fail when it should'
227

    
228
upto 'Failing suffix command on export'
229
reset_status
230
do_import >&$src_output & imppid=$!
231
: | cmd_suffix='| exit 1' do_export &>$dst_output & exppid=$!
232
checkpids $imppid $exppid && \
233
  err 'Suffix command on export did not fail when it should'
234

    
235
upto 'Failing prefix command on import'
236
reset_status
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
upto 'Failing suffix command on import'
241
reset_status
242
cmd_suffix='| exit 1' do_import &>$dst_output & imppid=$!
243
: | do_export &>$src_output & exppid=$!
244
checkpids $imppid $exppid && \
245
  err 'Suffix command on import did not fail when it should'
246

    
247
upto 'Listen timeout A'
248
reset_status
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
upto 'Listen timeout B'
256
reset_status
257
do_import &>$dst_output & imppid=$!
258
{ sleep 1; : | do_export; } &>$src_output & exppid=$!
259
checkpids $exppid $imppid || \
260
  err 'Listening with timeout failed when it should not'
261

    
262
upto 'No compression'
263
reset_status
264
compress=none do_import > $statusdir/recv-nocompr 2>$dst_output & imppid=$!
265
{ write_data | compress=none do_export; } &>$src_output & exppid=$!
266
checkpids $exppid $imppid || err 'An error occurred'
267
cmp $testdata $statusdir/recv-nocompr || \
268
  err 'Received data does not match input'
269

    
270
upto 'Compression mismatch A'
271
reset_status
272
compress=none do_import > $statusdir/recv-miscompr 2>$dst_output & imppid=$!
273
{ write_data | compress=gzip do_export; } &>$src_output & exppid=$!
274
checkpids $exppid $imppid || err 'An error occurred'
275
cmp -s $testdata $statusdir/recv-miscompr && \
276
  err 'Received data matches input when it should not'
277

    
278
upto 'Compression mismatch B'
279
reset_status
280
compress=gzip do_import > $statusdir/recv-miscompr2 2>$dst_output & imppid=$!
281
{ write_data | compress=none do_export; } &>$src_output & exppid=$!
282
checkpids $exppid $imppid && err 'Did not fail when it should'
283
cmp -s $testdata $statusdir/recv-miscompr2 && \
284
  err 'Received data matches input when it should not'
285

    
286
upto 'Large transfer'
287
reset_status
288
do_import > $statusdir/recv-large 2>$dst_output & imppid=$!
289
{ write_data $largetestdata | do_export; } &>$src_output & exppid=$!
290
checkpids $exppid $imppid || err 'An error occurred'
291
cmp $largetestdata $statusdir/recv-large || \
292
  err 'Received data does not match input'
293

    
294
exit 0