Skip to content

Commit

Permalink
p2p proxy tests: make robust against timing
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Stebalien committed Nov 29, 2018
1 parent 4994305 commit 181aa64
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions test/sharness/t0184-http-proxy-over-p2p.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,32 @@ function show_logs() {
cat $REMOTE_SERVER_LOG
}

function serve_http_once() {
#
# one shot http server (via nc) with static body
#
local body=$1
local status_code=${2:-"200 OK"}
local length=$((1 + ${#body}))
function start_http_server() {
REMOTE_SERVER_LOG="server.log"
rm $REMOTE_SERVER_LOG
echo -e "HTTP/1.1 $status_code\nContent-length: $length\n\n$body" | nc -l $WEB_SERVE_PORT 2>&1 > $REMOTE_SERVER_LOG &
test_wait_for_file 30 100ms $REMOTE_SERVER_LOG
rm -f $REMOTE_SERVER_LOG server_stdin
mkfifo server_stdin
nc -k -l 127.0.0.1 $WEB_SERVE_PORT 2>&1 > $REMOTE_SERVER_LOG < server_stdin &
REMOTE_SERVER_PID=$!
echo "" > server_stdin
while ! nc -z 127.0.0.1 $WEB_SERVE_PORT; do
go-sleep 100ms
done
}

function teardown_remote_server() {
# "flush"
rm -f server_stdin
kill -9 $REMOTE_SERVER_PID > /dev/null 2>&1
sleep 5
}

function serve_content() {
local body=$1
local status_code=${2:-"200 OK"}
local length=$((1 + ${#body}))
echo -e "HTTP/1.1 $status_code\nContent-length: $length\n\n$body" > server_stdin
}

function curl_check_response_code() {
local expected_status_code=$1
local path_stub=${2:-p2p/$RECEIVER_ID/http/index.txt}
Expand Down Expand Up @@ -97,7 +104,7 @@ function curl_send_multipart_form_request() {
#
# send multipart form request
#
STATUS_CODE="$(curl -v -F file=@$FILE_PATH $SENDER_GATEWAY/p2p/$RECEIVER_ID/http/index.txt)"
STATUS_CODE="$(curl -o /dev/null -s -F file=@$FILE_PATH --write-out %{http_code} $SENDER_GATEWAY/p2p/$RECEIVER_ID/http/index.txt)"
#
# check status code
#
Expand Down Expand Up @@ -148,21 +155,23 @@ test_expect_success 'setup environment' '
RECEIVER_ID="$(iptb attr get 1 id)"
'

test_expect_success 'handle proxy http request propogates error response from remote' '
serve_http_once "SORRY GUYS, I LOST IT" "404 Not Found" &&
curl_send_proxy_request_and_check_response 404 "SORRY GUYS, I LOST IT"
'
teardown_remote_server

test_expect_success 'handle proxy http request sends bad-gateway when remote server not available ' '
curl_send_proxy_request_and_check_response 502 ""
'

test_expect_success 'start http server' '
start_http_server
'

test_expect_success 'handle proxy http request propogates error response from remote' '
serve_content "SORRY GUYS, I LOST IT" "404 Not Found" &&
curl_send_proxy_request_and_check_response 404 "SORRY GUYS, I LOST IT"
'

test_expect_success 'handle proxy http request ' '
serve_http_once "THE WOODS ARE LOVELY DARK AND DEEP" &&
serve_content "THE WOODS ARE LOVELY DARK AND DEEP" &&
curl_send_proxy_request_and_check_response 200 "THE WOODS ARE LOVELY DARK AND DEEP"
'
teardown_remote_server

test_expect_success 'handle proxy http request invalid request' '
curl_check_response_code 400 p2p/DERPDERPDERP
Expand All @@ -173,26 +182,27 @@ test_expect_success 'handle proxy http request unknown proxy peer ' '
'

test_expect_success 'handle proxy http request to custom protocol' '
serve_http_once "THE WOODS ARE LOVELY DARK AND DEEP" &&
serve_content "THE WOODS ARE LOVELY DARK AND DEEP" &&
curl_check_response_code 200 p2p/$RECEIVER_ID/x/custom/http/index.txt
'
teardown_remote_server

test_expect_success 'handle proxy http request to missing protocol' '
serve_http_once "THE WOODS ARE LOVELY DARK AND DEEP" &&
serve_content "THE WOODS ARE LOVELY DARK AND DEEP" &&
curl_check_response_code 502 p2p/$RECEIVER_ID/x/missing/http/index.txt
'
teardown_remote_server

test_expect_success 'handle proxy http request missing the /http' '
curl_check_response_code 400 p2p/$RECEIVER_ID/x/custom/index.txt
'

test_expect_success 'handle multipart/form-data http request' '
serve_http_once "OK" &&
curl_send_multipart_form_request
serve_content "OK" &&
curl_send_multipart_form_request 200
'

test_expect_success 'stop http server' '
teardown_remote_server
'
teardown_remote_server

test_expect_success 'stop nodes' '
iptb stop
Expand Down

0 comments on commit 181aa64

Please sign in to comment.