Statistics
| Branch: | Tag: | Revision:

root / test / import-export_unittest.bash @ 043f2292

History | View | Annotate | Download (5.1 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 --connect-timeout=1 --connect-retries=1"
27

    
28
# Add "-d" for debugging
29
#impexpd+=' -d'
30

    
31
err() {
32
  echo "$@"
33
  echo 'Aborting'
34
  exit 1
35
}
36

    
37
checkpids() {
38
  local result=0
39

    
40
  # Unlike combining the "wait" commands using || or &&, this ensures we
41
  # actually wait for all PIDs.
42
  for pid in "$@"; do
43
    if ! wait $pid; then
44
      result=1
45
    fi
46
  done
47

    
48
  return $result
49
}
50

    
51
get_testpath() {
52
  echo "${TOP_SRCDIR:-.}/test"
53
}
54

    
55
get_testfile() {
56
  echo "$(get_testpath)/data/$1"
57
}
58

    
59
statusdir=$(mktemp -d)
60
trap "rm -rf $statusdir" EXIT
61

    
62
src_statusfile=$statusdir/src.status
63
src_x509=$statusdir/src.pem
64

    
65
dst_statusfile=$statusdir/dst.status
66
dst_x509=$statusdir/dst.pem
67
dst_portfile=$statusdir/dst.port
68

    
69
other_x509=$statusdir/other.pem
70

    
71
testdata=$statusdir/data1
72

    
73
cmd_prefix=
74
cmd_suffix=
75

    
76
$impexpd >/dev/null 2>&1 &&
77
  err "daemon-util succeeded without parameters"
78

    
79
$impexpd foo bar baz moo boo >/dev/null 2>&1 &&
80
  err "daemon-util succeeded with wrong parameters"
81

    
82
$impexpd $src_statusfile >/dev/null 2>&1 &&
83
  err "daemon-util succeeded with insufficient parameters"
84

    
85
$impexpd $src_statusfile invalidmode >/dev/null 2>&1 &&
86
  err "daemon-util succeeded with invalid mode"
87

    
88
cat $(get_testfile proc_drbd8.txt) $(get_testfile cert1.pem) > $testdata
89

    
90
impexpd_helper() {
91
  $PYTHON $(get_testpath)/import-export_unittest-helper "$@"
92
}
93

    
94
reset_status() {
95
  rm -f $src_statusfile $dst_statusfile $dst_portfile
96
}
97

    
98
write_data() {
99
  # Wait for connection to be established
100
  impexpd_helper $dst_statusfile connected
101

    
102
  cat $testdata
103
}
104

    
105
do_export() {
106
  # Wait for listening port
107
  impexpd_helper $dst_statusfile listen-port > $dst_portfile
108

    
109
  local port=$(< $dst_portfile)
110

    
111
  test -n "$port" || err 'Empty port file'
112

    
113
  do_export_to_port $port
114
}
115

    
116
do_export_to_port() {
117
  local port=$1
118

    
119
  $impexpd $src_statusfile export --bind=127.0.0.1 \
120
    --host=127.0.0.1 --port=$port \
121
    --key=$src_x509 --cert=$src_x509 --ca=$dst_x509 \
122
    --cmd-prefix="$cmd_prefix" --cmd-suffix="$cmd_suffix"
123
}
124

    
125
do_import() {
126
  $impexpd $dst_statusfile import --bind=127.0.0.1 \
127
    --host=127.0.0.1 \
128
    --key=$dst_x509 --cert=$dst_x509 --ca=$src_x509 \
129
    --cmd-prefix="$cmd_prefix" --cmd-suffix="$cmd_suffix"
130
}
131

    
132
# Generate X509 certificates and keys
133
impexpd_helper $src_x509 gencert
134
impexpd_helper $dst_x509 gencert
135
impexpd_helper $other_x509 gencert
136

    
137
# Normal case
138
reset_status
139
do_import > $statusdir/recv1 & imppid=$!
140
write_data | do_export & exppid=$!
141
checkpids $exppid $imppid || err 'An error occurred'
142
cmp $testdata $statusdir/recv1 || err 'Received data does not match input'
143

    
144
# Export using wrong CA
145
reset_status
146
do_import > /dev/null 2>&1 & imppid=$!
147
: | dst_x509=$other_x509 do_export 2>/dev/null & exppid=$!
148
checkpids $exppid $imppid && err 'Export did not fail when using wrong CA'
149

    
150
# Import using wrong CA
151
reset_status
152
src_x509=$other_x509 do_import > /dev/null 2>&1 & imppid=$!
153
: | do_export 2> /dev/null & exppid=$!
154
checkpids $exppid $imppid && err 'Import did not fail when using wrong CA'
155

    
156
# Suffix command on import
157
reset_status
158
cmd_suffix="| cksum > $statusdir/recv2" do_import & imppid=$!
159
write_data | do_export & exppid=$!
160
checkpids $exppid $imppid || err 'Testing additional commands failed'
161
cmp $statusdir/recv2 <(cksum < $testdata) || \
162
  err 'Checksum of received data does not match'
163

    
164
# Prefix command on export
165
reset_status
166
do_import > $statusdir/recv3 & imppid=$!
167
write_data | cmd_prefix="cksum |" do_export & exppid=$!
168
checkpids $exppid $imppid || err 'Testing additional commands failed'
169
cmp $statusdir/recv3 <(cksum < $testdata) || \
170
  err 'Received checksum does not match'
171

    
172
# Failing prefix command on export
173
reset_status
174
: | cmd_prefix='exit 1;' do_export_to_port 0 & exppid=$!
175
checkpids $exppid && err 'Prefix command on export did not fail when it should'
176

    
177
# Failing suffix command on export
178
reset_status
179
do_import > /dev/null & imppid=$!
180
: | cmd_suffix='| exit 1' do_export & exppid=$!
181
checkpids $imppid $exppid && \
182
  err 'Suffix command on export did not fail when it should'
183

    
184
# Failing prefix command on import
185
reset_status
186
cmd_prefix='exit 1;' do_import > /dev/null & imppid=$!
187
checkpids $imppid && err 'Prefix command on import did not fail when it should'
188

    
189
# Failing suffix command on import
190
reset_status
191
cmd_suffix='| exit 1' do_import > /dev/null & imppid=$!
192
: | do_export & exppid=$!
193
checkpids $imppid $exppid && \
194
  err 'Suffix command on import did not fail when it should'
195

    
196
exit 0