@cryptotaxi247 / kubo / commits / acb2cacef

use iptb in http proxy tests

Also: * Disable bootstrapping * Listen on random ports * Write temporary files to the current (trash) directory instead of temp (easier to find, collected by Jenkins). * Fix indentation. License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>

Steven Allen committed Oct 25, 2018 at 08:11 UTC acb2cacefef3b0584b40dcd822f61cbd082240e6
1 file changed +36 -55
test/sharness/t0184-http-proxy-over-p2p.sh
+36 -55
@@ -10,11 +10,11 @@ function show_logs() {
10 echo "*****************"
11 echo " RECEIVER LOG "
12 echo "*****************"
13 - cat $RECEIVER_LOG
13 + iptb logs 1
14 echo "*****************"
15 echo " SENDER LOG "
16 echo "*****************"
17 - cat $SENDER_LOG
17 + iptb logs 0
18 echo "*****************"
19 echo "REMOTE_SERVER LOG"
20 echo $REMOTE_SERVER_LOG
@@ -29,56 +29,37 @@ function serve_http_once() {
29 local body=$1
30 local status_code=${2:-"200 OK"}
31 local length=$((1 + ${#body}))
32 - REMOTE_SERVER_LOG=$(mktemp)
32 + REMOTE_SERVER_LOG="server.log"
33 echo -e "HTTP/1.1 $status_code\nContent-length: $length\n\n$body" | nc -l $WEB_SERVE_PORT 2>&1 > $REMOTE_SERVER_LOG &
34 REMOTE_SERVER_PID=$!
35 }
36
37 -function setup_receiver_ipfs() {
38 - #
39 - # setup RECEIVER IPFS daemon
40 - #
41 - local IPFS_PATH=$(mktemp -d)
42 - RECEIVER_LOG=$IPFS_PATH/ipfs.log
43 - ipfs init >> $RECEIVER_LOG 2>&1
44 - ipfs config --json Experimental.Libp2pStreamMounting true >> $RECEIVER_LOG 2>&1
45 - ipfs config --json Addresses.API "\"/ip4/127.0.0.1/tcp/6001\"" >> $RECEIVER_LOG 2>&1
46 - ipfs config --json Addresses.Gateway "\"/ip4/127.0.0.1/tcp/8081\"" >> $RECEIVER_LOG 2>&1
47 - ipfs config --json Addresses.Swarm "[\"/ip4/0.0.0.0/tcp/7001\", \"/ip6/::/tcp/7001\"]" >> $RECEIVER_LOG 2>&1
48 - ipfs daemon >> $RECEIVER_LOG 2>&1 &
49 - RECEIVER_PID=$!
50 - # wait for daemon to start.. maybe?
51 - # ipfs id returns empty string if we don't wait here..
52 - sleep 10
53 - RECEIVER_ID=$(ipfs id -f "<id>")
54 - #
55 - # start a p2p listener on RECIVER to the HTTP server with our content
56 - #
57 - ipfs p2p listen --allow-custom-protocol test /ip4/127.0.0.1/tcp/$WEB_SERVE_PORT >> $RECEIVER_LOG 2>&1
58 -}
37 +function setup_sender_and_receiver_ipfs() {
38 + iptb init -n 2 -p 0 -f --bootstrap=none &&
39
40 + # Configure features
41
61 -function setup_sender_ipfs() {
62 - #
63 - # setup SENDER IPFS daemon
64 - #
65 - local IPFS_PATH=$(mktemp -d)
66 - SENDER_LOG=$IPFS_PATH/ipfs.log
67 - ipfs init >> $SENDER_LOG 2>&1
68 - ipfs config --json Experimental.Libp2pStreamMounting true >> $SENDER_LOG 2>&1
69 - ipfs config --json Experimental.P2pHttpProxy true >> $RECEIVER_LOG 2>&1
70 - ipfs daemon >> $SENDER_LOG 2>&1 &
71 - SENDER_PID=$!
72 - sleep 10
73 -}
42 + ipfsi 0 config --json Experimental.Libp2pStreamMounting true &&
43 + ipfsi 1 config --json Experimental.Libp2pStreamMounting true &&
44 + ipfsi 0 config --json Experimental.P2pHttpProxy true &&
45
75 -function setup_sender_and_receiver_ipfs() {
76 - setup_receiver_ipfs && setup_sender_ipfs
46 + # Start
47 +
48 + iptb start &&
49 + iptb connect 0 1 &&
50 +
51 + # Setup the p2p listener
52 +
53 + ipfsi 1 p2p listen --allow-custom-protocol test /ip4/127.0.0.1/tcp/$WEB_SERVE_PORT &&
54 +
55 + # Setup Environment
56 +
57 + RECEIVER_ID="$(iptb get id 1)" &&
58 + SENDER_URL="$(sed 's|^/ip[46]/\([^/]*\)/tcp/\([0-9]*\)$|http://\1:\2/proxy/http|' < "$(iptb get path 0)/api")"
59 }
60
61 function teardown_sender_and_receiver() {
80 - kill -9 $SENDER_PID $RECEIVER_PID > /dev/null 2>&1
81 - sleep 5
62 + iptb stop
63 }
64
65 function teardown_remote_server() {
@@ -89,7 +70,7 @@ function teardown_remote_server() {
70 function curl_check_response_code() {
71 local expected_status_code=$1
72 local path_stub=${2:-http/$RECEIVER_ID/test/index.txt}
92 - local status_code=$(curl -s --write-out %{http_code} --output /dev/null http://localhost:5001/proxy/http/$path_stub)
73 + local status_code=$(curl -s --write-out %{http_code} --output /dev/null $SENDER_URL/$path_stub)
74
75 if [[ "$status_code" -ne "$expected_status_code" ]];
76 then
@@ -107,8 +88,8 @@ function curl_send_proxy_request_and_check_response() {
88 #
89 # make a request to SENDER_IPFS via the proxy endpoint
90 #
110 - CONTENT_PATH=$(mktemp)
111 - STATUS_CODE=$(curl -s -o $CONTENT_PATH --write-out %{http_code} http://localhost:5001/proxy/http/$RECEIVER_ID/test/index.txt)
91 + CONTENT_PATH="retrieved-file"
92 + STATUS_CODE=$(curl -s -o $CONTENT_PATH --write-out %{http_code} $SENDER_URL/$RECEIVER_ID/test/index.txt)
93
94 #
95 # check status code
@@ -134,13 +115,13 @@ function curl_send_proxy_request_and_check_response() {
115
116 function curl_send_multipart_form_request() {
117 local expected_status_code=$1
137 - local FILE_PATH=$(mktemp)
118 + local FILE_PATH="uploaded-file"
119 FILE_CONTENT="curl will send a multipart-form POST request when sending a file which is handy"
120 echo $FILE_CONTENT > $FILE_PATH
121 #
122 # send multipart form request
123 #
143 - STATUS_CODE=$(curl -v -F file=@$FILE_PATH http://localhost:5001/proxy/http/$RECEIVER_ID/test/index.txt)
124 + STATUS_CODE=$(curl -v -F file=@$FILE_PATH $SENDER_URL/$RECEIVER_ID/test/index.txt)
125 #
126 # check status code
127 #
@@ -171,7 +152,7 @@ function curl_send_multipart_form_request() {
152 }
153
154 test_expect_success 'handle proxy http request propogates error response from remote' '
174 -serve_http_once "SORRY GUYS, I LOST IT" "404 Not Found" &&
155 + serve_http_once "SORRY GUYS, I LOST IT" "404 Not Found" &&
156 setup_sender_and_receiver_ipfs &&
157 curl_send_proxy_request_and_check_response 404 "SORRY GUYS, I LOST IT"
158 '
@@ -179,13 +160,13 @@ teardown_sender_and_receiver
160 teardown_remote_server
161
162 test_expect_success 'handle proxy http request sends bad-gateway when remote server not available ' '
182 -setup_sender_and_receiver_ipfs &&
163 + setup_sender_and_receiver_ipfs &&
164 curl_send_proxy_request_and_check_response 502 ""
165 '
166 teardown_sender_and_receiver
167
168 test_expect_success 'handle proxy http request ' '
188 -serve_http_once "THE WOODS ARE LOVELY DARK AND DEEP" &&
169 + serve_http_once "THE WOODS ARE LOVELY DARK AND DEEP" &&
170 setup_sender_and_receiver_ipfs &&
171 curl_send_proxy_request_and_check_response 200 "THE WOODS ARE LOVELY DARK AND DEEP"
172 '
@@ -193,21 +174,21 @@ teardown_sender_and_receiver
174 teardown_remote_server
175
176 test_expect_success 'handle proxy http request invalid request' '
196 -setup_sender_and_receiver_ipfs &&
177 + setup_sender_and_receiver_ipfs &&
178 curl_check_response_code 400 DERPDERPDERP
179 '
180 teardown_sender_and_receiver
181
182 test_expect_success 'handle proxy http request unknown proxy peer ' '
202 -setup_sender_and_receiver_ipfs &&
183 + setup_sender_and_receiver_ipfs &&
184 curl_check_response_code 502 unknown_peer/test/index.txt
185 '
186 teardown_sender_and_receiver
187
188 test_expect_success 'handle multipart/form-data http request' '
208 -serve_http_once "OK" &&
209 -setup_sender_and_receiver_ipfs &&
210 -curl_send_multipart_form_request
189 + serve_http_once "OK" &&
190 + setup_sender_and_receiver_ipfs &&
191 + curl_send_multipart_form_request
192 '
193 teardown_sender_and_receiver
194 teardown_remote_server