p2p proxy tests: make robust against timing
Instead of repeatedly starting the netcat server, start it once and wait for it to fully start. Then, feed responses in using a fifo. License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
Steven Allen committed
Nov 28, 2018 at 22:08 UTC
9a443adada9e1cebc61985504c2b4a991d21ddde
1 file changed
+40
-28
test/sharness/t0184-http-proxy-over-p2p.sh
+40
-28
@@ -24,23 +24,32 @@ function show_logs() {
24
cat $REMOTE_SERVER_LOG
25
}
26
27
-function serve_http_once() {
28
- #
29
- # one shot http server (via nc) with static body
30
- #
31
- local body=$1
32
- local status_code=${2:-"200 OK"}
33
- local length=$((1 + ${#body}))
27
+function start_http_server() {
28
REMOTE_SERVER_LOG="server.log"
35
- rm $REMOTE_SERVER_LOG
36
- echo -e "HTTP/1.1 $status_code\nContent-length: $length\n\n$body" | nc -l $WEB_SERVE_PORT 2>&1 > $REMOTE_SERVER_LOG &
37
- test_wait_for_file 30 100ms $REMOTE_SERVER_LOG
29
+ rm -f $REMOTE_SERVER_LOG server_stdin
30
+
31
+ mkfifo server_stdin
32
+ nc -k -l 127.0.0.1 $WEB_SERVE_PORT 2>&1 > $REMOTE_SERVER_LOG < server_stdin &
33
REMOTE_SERVER_PID=$!
34
+ exec 7>server_stdin
35
+ rm server_stdin
36
+
37
+ while ! nc -z 127.0.0.1 $WEB_SERVE_PORT; do
38
+ go-sleep 100ms
39
+ done
40
}
41
42
function teardown_remote_server() {
42
- kill -9 $REMOTE_SERVER_PID > /dev/null 2>&1
43
- sleep 5
43
+ exec 7<&-
44
+ kill $REMOTE_SERVER_PID > /dev/null 2>&1
45
+ wait $REMOTE_SERVER_PID || true
46
+}
47
+
48
+function serve_content() {
49
+ local body=$1
50
+ local status_code=${2:-"200 OK"}
51
+ local length=$((1 + ${#body}))
52
+ echo -e "HTTP/1.1 $status_code\nContent-length: $length\n\n$body" >&7
53
}
54
55
function curl_check_response_code() {
@@ -97,7 +106,7 @@ function curl_send_multipart_form_request() {
106
#
107
# send multipart form request
108
#
100
- STATUS_CODE="$(curl -v -F file=@$FILE_PATH $SENDER_GATEWAY/p2p/$RECEIVER_ID/http/index.txt)"
109
+ STATUS_CODE="$(curl -o /dev/null -s -F file=@$FILE_PATH --write-out %{http_code} $SENDER_GATEWAY/p2p/$RECEIVER_ID/http/index.txt)"
110
#
111
# check status code
112
#
@@ -148,21 +157,23 @@ test_expect_success 'setup environment' '
157
RECEIVER_ID="$(iptb attr get 1 id)"
158
'
159
151
-test_expect_success 'handle proxy http request propogates error response from remote' '
152
- serve_http_once "SORRY GUYS, I LOST IT" "404 Not Found" &&
153
- curl_send_proxy_request_and_check_response 404 "SORRY GUYS, I LOST IT"
154
-'
155
-teardown_remote_server
156
-
160
test_expect_success 'handle proxy http request sends bad-gateway when remote server not available ' '
161
curl_send_proxy_request_and_check_response 502 ""
162
'
163
164
+test_expect_success 'start http server' '
165
+ start_http_server
166
+'
167
+
168
+test_expect_success 'handle proxy http request propogates error response from remote' '
169
+ serve_content "SORRY GUYS, I LOST IT" "404 Not Found" &&
170
+ curl_send_proxy_request_and_check_response 404 "SORRY GUYS, I LOST IT"
171
+'
172
+
173
test_expect_success 'handle proxy http request ' '
162
- serve_http_once "THE WOODS ARE LOVELY DARK AND DEEP" &&
174
+ serve_content "THE WOODS ARE LOVELY DARK AND DEEP" &&
175
curl_send_proxy_request_and_check_response 200 "THE WOODS ARE LOVELY DARK AND DEEP"
176
'
165
-teardown_remote_server
177
178
test_expect_success 'handle proxy http request invalid request' '
179
curl_check_response_code 400 p2p/DERPDERPDERP
@@ -173,26 +184,27 @@ test_expect_success 'handle proxy http request unknown proxy peer ' '
184
'
185
186
test_expect_success 'handle proxy http request to custom protocol' '
176
- serve_http_once "THE WOODS ARE LOVELY DARK AND DEEP" &&
187
+ serve_content "THE WOODS ARE LOVELY DARK AND DEEP" &&
188
curl_check_response_code 200 p2p/$RECEIVER_ID/x/custom/http/index.txt
189
'
179
-teardown_remote_server
190
191
test_expect_success 'handle proxy http request to missing protocol' '
182
- serve_http_once "THE WOODS ARE LOVELY DARK AND DEEP" &&
192
+ serve_content "THE WOODS ARE LOVELY DARK AND DEEP" &&
193
curl_check_response_code 502 p2p/$RECEIVER_ID/x/missing/http/index.txt
194
'
185
-teardown_remote_server
195
196
test_expect_success 'handle proxy http request missing the /http' '
197
curl_check_response_code 400 p2p/$RECEIVER_ID/x/custom/index.txt
198
'
199
200
test_expect_success 'handle multipart/form-data http request' '
192
- serve_http_once "OK" &&
193
- curl_send_multipart_form_request
201
+ serve_content "OK" &&
202
+ curl_send_multipart_form_request 200
203
+'
204
+
205
+test_expect_success 'stop http server' '
206
+ teardown_remote_server
207
'
195
-teardown_remote_server
208
209
test_expect_success 'stop nodes' '
210
iptb stop