From 0c7595b65b531f271dfbc079efefd36edaf63a5f Mon Sep 17 00:00:00 2001 From: Jeromy Date: Thu, 11 Feb 2016 19:44:00 -0800 Subject: [PATCH] Use port zero for all ipfs daemon addresses in sharness testing License: MIT Signed-off-by: Jeromy --- test/sharness/lib/test-lib.sh | 106 +++++++++--------- test/sharness/t0040-add-and-cat.sh | 2 +- test/sharness/t0061-daemon-opts.sh | 15 ++- test/sharness/t0062-daemon-api.sh | 88 ++++++--------- test/sharness/t0110-gateway.sh | 5 +- test/sharness/t0111-gateway-writeable.sh | 8 +- test/sharness/t0112-gateway-cors.sh | 5 +- test/sharness/t0121-bootstrap-iptb.sh | 2 +- ...230-channel-streaming-http-content-type.sh | 4 +- 9 files changed, 107 insertions(+), 128 deletions(-) diff --git a/test/sharness/lib/test-lib.sh b/test/sharness/lib/test-lib.sh index 771471861..1ceaf859b 100644 --- a/test/sharness/lib/test-lib.sh +++ b/test/sharness/lib/test-lib.sh @@ -125,29 +125,6 @@ test_config_set() { test_init_ipfs() { - # we have a problem where initializing daemons with the same api port - # often fails-- it hangs indefinitely. The proper solution is to make - # ipfs pick an unused port for the api on startup, and then use that. - # Unfortunately, ipfs doesnt yet know how to do this-- the api port - # must be specified. Until ipfs learns how to do this, we must use - # specific port numbers, which may still fail but less frequently - # if we at least use different ones. - - # Using RANDOM like this is clearly wrong-- it samples with replacement - # and it doesnt even check the port is unused. this is a trivial stop gap - # until the proper solution is implemented. - RANDOM=$$ - PORT_API=$((RANDOM % 3000 + 5100)) - ADDR_API="/ip4/127.0.0.1/tcp/$PORT_API" - - PORT_GWAY=$((RANDOM % 3000 + 8100)) - ADDR_GWAY="/ip4/127.0.0.1/tcp/$PORT_GWAY" - - PORT_SWARM=$((RANDOM % 3000 + 12000)) - ADDR_SWARM="[ - \"/ip4/0.0.0.0/tcp/$PORT_SWARM\" -]" - # we set the Addresses.API config variable. # the cli client knows to use it, so only need to set. @@ -162,40 +139,42 @@ test_init_ipfs() { mkdir mountdir ipfs ipns && test_config_set Mounts.IPFS "$(pwd)/ipfs" && test_config_set Mounts.IPNS "$(pwd)/ipns" && - test_config_set Addresses.API "$ADDR_API" && - test_config_set Addresses.Gateway "$ADDR_GWAY" && - test_config_set --json Addresses.Swarm "$ADDR_SWARM" && + test_config_set Addresses.API "/ip4/127.0.0.1/tcp/0" && + test_config_set Addresses.Gateway "/ip4/127.0.0.1/tcp/0" && + test_config_set --json Addresses.Swarm "[ + \"/ip4/0.0.0.0/tcp/0\" +]" && ipfs bootstrap rm --all || test_fsh cat "\"$IPFS_PATH/config\"" ' } -test_config_ipfs_gateway_readonly() { - ADDR_GWAY=$1 - test_expect_success "prepare config -- gateway address" ' - test "$ADDR_GWAY" != "" && - test_config_set "Addresses.Gateway" "$ADDR_GWAY" - ' - - # tell the user what's going on if they messed up the call. - if test "$#" = 0; then - echo "# Error: must call with an address, for example:" - echo '# test_config_ipfs_gateway_readonly "/ip4/0.0.0.0/tcp/5002"' - echo '#' - fi -} - test_config_ipfs_gateway_writable() { - - test_config_ipfs_gateway_readonly $1 - test_expect_success "prepare config -- gateway writable" ' test_config_set --bool Gateway.Writable true || test_fsh cat "\"$IPFS_PATH/config\"" ' } +test_wait_for_file() { + loops=$1 + delay=$2 + file=$3 + fwaitc=0 + while ! test -f "$file" + do + if test $fwaitc -ge $loops + then + echo "Error: timed out waiting for file: $file" + return 1 + fi + + go-sleep $delay + fwaitc=`expr $fwaitc + 1` + done +} + test_launch_ipfs_daemon() { args="$@" @@ -204,19 +183,34 @@ test_launch_ipfs_daemon() { ipfs daemon $args >actual_daemon 2>daemon_err & ' + # wait for api file to show up + test_expect_success "api file shows up" ' + test_wait_for_file 20 100ms "$IPFS_PATH/api" + ' + + test_expect_success "set up address variables" ' + API_MADDR=$(cat "$IPFS_PATH/api") && + API_ADDR=$(convert_tcp_maddr $API_MADDR) && + API_PORT=$(port_from_maddr $API_MADDR) && + + GWAY_MADDR=$(sed -n "s/^Gateway (.*) server listening on //p" actual_daemon) && + GWAY_ADDR=$(convert_tcp_maddr $GWAY_MADDR) && + GWAY_PORT=$(port_from_maddr $GWAY_MADDR) + ' + + + test_expect_success "set swarm address vars" ' + ipfs swarm addrs local > addrs_out && + SWARM_MADDR=$(grep "127.0.0.1" addrs_out) && + SWARM_PORT=$(port_from_maddr $SWARM_MADDR) + ' + # we say the daemon is ready when the API server is ready. test_expect_success "'ipfs daemon' is ready" ' IPFS_PID=$! && - pollEndpoint -ep=/version -host=$ADDR_API -v -tout=1s -tries=60 2>poll_apierr > poll_apiout || + pollEndpoint -ep=/version -host=$API_MADDR -v -tout=1s -tries=60 2>poll_apierr > poll_apiout || test_fsh cat actual_daemon || test_fsh cat daemon_err || test_fsh cat poll_apierr || test_fsh cat poll_apiout ' - - if test "$ADDR_GWAY" != ""; then - test_expect_success "'ipfs daemon' output includes Gateway address" ' - pollEndpoint -ep=/version -host=$ADDR_GWAY -v -tout=1s -tries=60 2>poll_gwerr > poll_gwout || - test_fsh cat daemon_err || test_fsh cat poll_gwerr || test_fsh cat poll_gwout - ' - fi } do_umount() { @@ -365,3 +359,11 @@ test_check_peerid() { return 1 } } + +convert_tcp_maddr() { + echo $1 | awk -F'/' '{ printf "%s:%s", $3, $5 }' +} + +port_from_maddr() { + echo $1 | awk -F'/' '{ print $NF }' +} diff --git a/test/sharness/t0040-add-and-cat.sh b/test/sharness/t0040-add-and-cat.sh index e7640c4ee..1dfeea04d 100755 --- a/test/sharness/t0040-add-and-cat.sh +++ b/test/sharness/t0040-add-and-cat.sh @@ -328,7 +328,7 @@ test_add_cat_5MB test_add_cat_expensive -test_add_named_pipe " Post http://127.0.0.1:$PORT_API/api/v0/add?encoding=json&progress=true&r=true&stream-channels=true:" +test_add_named_pipe " Post http://$API_ADDR/api/v0/add?encoding=json&progress=true&r=true&stream-channels=true:" test_kill_ipfs_daemon diff --git a/test/sharness/t0061-daemon-opts.sh b/test/sharness/t0061-daemon-opts.sh index bc5df7024..9f39d8197 100755 --- a/test/sharness/t0061-daemon-opts.sh +++ b/test/sharness/t0061-daemon-opts.sh @@ -13,21 +13,24 @@ test_init_ipfs test_launch_ipfs_daemon --unrestricted-api --disable-transport-encryption -gwyport=$PORT_GWAY -apiport=$PORT_API +test_expect_success "convert addresses from multiaddrs" ' +' + +gwyaddr=$GWAY_ADDR +apiaddr=$API_ADDR test_expect_success 'api gateway should be unrestricted' ' - echo "hello mars :$gwyport :$apiport" >expected && + echo "hello mars :$gwyaddr :$apiaddr" >expected && HASH=$(ipfs add -q expected) && - curl -sfo actual1 "http://127.0.0.1:$gwyport/ipfs/$HASH" && - curl -sfo actual2 "http://127.0.0.1:$apiport/ipfs/$HASH" && + curl -sfo actual1 "http://$gwyaddr/ipfs/$HASH" && + curl -sfo actual2 "http://$apiaddr/ipfs/$HASH" && test_cmp expected actual1 && test_cmp expected actual2 ' # Odd. this fails here, but the inverse works on t0060-daemon. test_expect_success 'transport should be unencrypted' ' - go-sleep 0.5s | nc localhost "$PORT_SWARM" >swarmnc && + go-sleep 0.5s | nc localhost "$SWARM_PORT" >swarmnc && test_must_fail grep -q "AES-256,AES-128" swarmnc && grep -q "/multistream/1.0.0" swarmnc || test_fsh cat swarmnc diff --git a/test/sharness/t0062-daemon-api.sh b/test/sharness/t0062-daemon-api.sh index db6ca1c27..4307a7b9f 100755 --- a/test/sharness/t0062-daemon-api.sh +++ b/test/sharness/t0062-daemon-api.sh @@ -9,65 +9,65 @@ test_description="Test daemon command" test_init_ipfs -differentport=$((PORT_API + 1)) -api_different="/ip4/127.0.0.1/tcp/$differentport" +differentport=$((API_PORT + 1)) +api_other="/ip4/127.0.0.1/tcp/$differentport" api_unreachable="/ip4/127.0.0.1/tcp/1" test_expect_success "config setup" ' - api_fromcfg=$(ipfs config Addresses.API) && peerid=$(ipfs config Identity.PeerID) && test_check_peerid "$peerid" ' test_client() { - printf "$peerid" >expected && - ipfs "$@" id -f="" >actual && - test_cmp expected actual + opts="$@" + echo "OPTS = " $opts + test_expect_success "client must work properly $state" ' + printf "$peerid" >expected && + ipfs id -f="" $opts >actual && + test_cmp expected actual + ' } test_client_must_fail() { - echo "Error: api not running" >expected_err && - test_must_fail ipfs "$@" id -f="" >actual 2>actual_err && - test_cmp expected_err actual_err + opts="$@" + echo "OPTS = " $opts + test_expect_success "client should fail $state" ' + echo "Error: api not running" >expected_err && + test_must_fail ipfs id -f="" $opts >actual 2>actual_err && + test_cmp expected_err actual_err + ' } test_client_suite() { state="$1" cfg_success="$2" diff_success="$3" + api_fromcfg="$4" + api_different="$5" + # must always work - test_expect_success "client should work $state" ' - test_client - ' + test_client # must always err - test_expect_success "client --api unreachable should err $state" ' - test_client_must_fail --api "$api_unreachable" - ' + test_client_must_fail --api "$api_unreachable" if [ "$cfg_success" = true ]; then - test_expect_success "client --api fromcfg should work $state" ' - test_client --api "$api_fromcfg" - ' + test_client --api "$api_fromcfg" else - test_expect_success "client --api fromcfg should err $state" ' - test_client_must_fail --api "$api_fromcfg" - ' + test_client_must_fail --api "$api_fromcfg" fi if [ "$diff_success" = true ]; then - test_expect_success "client --api different should work $state" ' - test_client --api "$api_different" - ' + test_client --api "$api_different" else - test_expect_success "client --api different should err $state" ' - test_client_must_fail --api "$api_different" - ' + test_client_must_fail --api "$api_different" fi - } +} # first, test things without daemon, without /api file -test_client_suite "(daemon off, no --api, no /api file)" false false +# with no daemon, everything should fail +# (using unreachable because API_MADDR doesnt get set until daemon start) +test_client_suite "(daemon off, no --api, no /api file)" false false "$api_unreachable" "$api_other" # then, test things with daemon, with /api file @@ -78,37 +78,13 @@ test_expect_success "'ipfs daemon' creates api file" ' test -f ".ipfs/api" ' -test_expect_success "api file looks good" ' - printf "$ADDR_API" >expected && - test_cmp expected .ipfs/api -' - -test_client_suite "(daemon on, no --api, /api file from cfg)" true false +test_client_suite "(daemon on, no --api, /api file from cfg)" true false "$API_MADDR" "$api_other" # then, test things without daemon, with /api file test_kill_ipfs_daemon -test_client_suite "(daemon off, no --api, /api file from cfg)" false false - -# then, test things with daemon --api $api_different, with /api file - -PORT_API=$differentport -ADDR_API=$api_different - -test_launch_ipfs_daemon --api "$ADDR_API" - -test_expect_success "'ipfs daemon' --api option works" ' - printf "$api_different" >expected && - test_cmp expected .ipfs/api -' - -test_client_suite "(daemon on, /api file different)" false true - -# then, test things with daemon off, with /api file, for good measure. - -test_kill_ipfs_daemon - -test_client_suite "(daemon off, /api file different)" false false +# again, both should fail +test_client_suite "(daemon off, no --api, /api file from cfg)" false false "$API_MADDR" "$api_other" test_done diff --git a/test/sharness/t0110-gateway.sh b/test/sharness/t0110-gateway.sh index 7bfa22f72..dacf4c4b8 100755 --- a/test/sharness/t0110-gateway.sh +++ b/test/sharness/t0110-gateway.sh @@ -9,11 +9,10 @@ test_description="Test HTTP Gateway" . lib/test-lib.sh test_init_ipfs -test_config_ipfs_gateway_readonly $ADDR_GWAY test_launch_ipfs_daemon -port=$PORT_GWAY -apiport=$PORT_API +port=$GWAY_PORT +apiport=$API_PORT # TODO check both 5001 and 5002. # 5001 should have a readable gateway (part of the API) diff --git a/test/sharness/t0111-gateway-writeable.sh b/test/sharness/t0111-gateway-writeable.sh index a171fbcb5..7b0f83d84 100755 --- a/test/sharness/t0111-gateway-writeable.sh +++ b/test/sharness/t0111-gateway-writeable.sh @@ -9,18 +9,18 @@ test_description="Test HTTP Gateway (Writable)" . lib/test-lib.sh test_init_ipfs -test_config_ipfs_gateway_writable $ADDR_GWAY +test_config_ipfs_gateway_writable test_launch_ipfs_daemon -port=$PORT_GWAY +port=$GWAY_PORT test_expect_success "ipfs daemon up" ' - pollEndpoint -host $ADDR_GWAY -ep=/version -v -tout=1s -tries=60 2>poll_apierr > poll_apiout || + pollEndpoint -host $GWAY_MADDR -ep=/version -v -tout=1s -tries=60 2>poll_apierr > poll_apiout || test_fsh cat poll_apierr || test_fsh cat poll_apiout ' test_expect_success "HTTP gateway gives access to sample file" ' - curl -s -o welcome "http://localhost:$PORT_GWAY/ipfs/$HASH_WELCOME_DOCS/readme" && + curl -s -o welcome "http://$GWAY_ADDR/ipfs/$HASH_WELCOME_DOCS/readme" && grep "Hello and Welcome to IPFS!" welcome ' diff --git a/test/sharness/t0112-gateway-cors.sh b/test/sharness/t0112-gateway-cors.sh index 7bdd82915..1cc9371c4 100755 --- a/test/sharness/t0112-gateway-cors.sh +++ b/test/sharness/t0112-gateway-cors.sh @@ -19,12 +19,11 @@ test_config_ipfs_cors_headers() { . lib/test-lib.sh test_init_ipfs -test_config_ipfs_gateway_readonly $ADDR_GWAY test_config_ipfs_cors_headers test_launch_ipfs_daemon -gwport=$PORT_GWAY -apiport=$PORT_API +gwport=$GWAY_PORT +apiport=$API_PORT thash='QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' # Gateway diff --git a/test/sharness/t0121-bootstrap-iptb.sh b/test/sharness/t0121-bootstrap-iptb.sh index 290ebe879..e6a955ff3 100755 --- a/test/sharness/t0121-bootstrap-iptb.sh +++ b/test/sharness/t0121-bootstrap-iptb.sh @@ -58,7 +58,7 @@ test_expect_success "reset iptb nodes" ' test_expect_success "set bootstrap addrs" ' bsn_peer_id=$(ipfs id -f "") && - BADDR="/ip4/127.0.0.1/tcp/$PORT_SWARM/ipfs/$bsn_peer_id" && + BADDR="/ip4/127.0.0.1/tcp/$SWARM_PORT/ipfs/$bsn_peer_id" && ipfsi 0 bootstrap add $BADDR && ipfsi 1 bootstrap add $BADDR && ipfsi 2 bootstrap add $BADDR && diff --git a/test/sharness/t0230-channel-streaming-http-content-type.sh b/test/sharness/t0230-channel-streaming-http-content-type.sh index 3825a2cf8..f6e5a6a7f 100755 --- a/test/sharness/t0230-channel-streaming-http-content-type.sh +++ b/test/sharness/t0230-channel-streaming-http-content-type.sh @@ -16,7 +16,7 @@ test_ls_cmd() { mkdir -p testdir && echo "hello test" >testdir/test.txt && ipfs add -r testdir && - curl -i "http://localhost:$PORT_API/api/v0/refs?arg=QmTcJAn3JP8ZMAKS6WS75q8sbTyojWKbxcUHgLYGWur4Ym&stream-channels=true&encoding=text" >actual_output + curl -i "http://$API_ADDR/api/v0/refs?arg=QmTcJAn3JP8ZMAKS6WS75q8sbTyojWKbxcUHgLYGWur4Ym&stream-channels=true&encoding=text" >actual_output ' test_expect_success "Text encoded channel-streaming command output looks good" ' @@ -39,7 +39,7 @@ test_ls_cmd() { mkdir -p testdir && echo "hello test" >testdir/test.txt && ipfs add -r testdir && - curl -i "http://localhost:$PORT_API/api/v0/refs?arg=QmTcJAn3JP8ZMAKS6WS75q8sbTyojWKbxcUHgLYGWur4Ym&stream-channels=true&encoding=json" >actual_output + curl -i "http://$API_ADDR/api/v0/refs?arg=QmTcJAn3JP8ZMAKS6WS75q8sbTyojWKbxcUHgLYGWur4Ym&stream-channels=true&encoding=json" >actual_output ' test_expect_success "JSON encoded channel-streaming command output looks good" '