1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-29 17:36:38 +08:00

Use port zero for all ipfs daemon addresses in sharness testing

License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
This commit is contained in:
Jeromy
2016-02-11 19:44:00 -08:00
parent 7217637933
commit 0c7595b65b
9 changed files with 107 additions and 128 deletions

View File

@ -125,29 +125,6 @@ test_config_set() {
test_init_ipfs() { 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. # we set the Addresses.API config variable.
# the cli client knows to use it, so only need to set. # the cli client knows to use it, so only need to set.
@ -162,40 +139,42 @@ test_init_ipfs() {
mkdir mountdir ipfs ipns && mkdir mountdir ipfs ipns &&
test_config_set Mounts.IPFS "$(pwd)/ipfs" && test_config_set Mounts.IPFS "$(pwd)/ipfs" &&
test_config_set Mounts.IPNS "$(pwd)/ipns" && test_config_set Mounts.IPNS "$(pwd)/ipns" &&
test_config_set Addresses.API "$ADDR_API" && test_config_set Addresses.API "/ip4/127.0.0.1/tcp/0" &&
test_config_set Addresses.Gateway "$ADDR_GWAY" && test_config_set Addresses.Gateway "/ip4/127.0.0.1/tcp/0" &&
test_config_set --json Addresses.Swarm "$ADDR_SWARM" && test_config_set --json Addresses.Swarm "[
\"/ip4/0.0.0.0/tcp/0\"
]" &&
ipfs bootstrap rm --all || ipfs bootstrap rm --all ||
test_fsh cat "\"$IPFS_PATH/config\"" 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_writable() {
test_config_ipfs_gateway_readonly $1
test_expect_success "prepare config -- gateway writable" ' test_expect_success "prepare config -- gateway writable" '
test_config_set --bool Gateway.Writable true || test_config_set --bool Gateway.Writable true ||
test_fsh cat "\"$IPFS_PATH/config\"" 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() { test_launch_ipfs_daemon() {
args="$@" args="$@"
@ -204,19 +183,34 @@ test_launch_ipfs_daemon() {
ipfs daemon $args >actual_daemon 2>daemon_err & 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. # we say the daemon is ready when the API server is ready.
test_expect_success "'ipfs daemon' is ready" ' test_expect_success "'ipfs daemon' is ready" '
IPFS_PID=$! && 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 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() { do_umount() {
@ -365,3 +359,11 @@ test_check_peerid() {
return 1 return 1
} }
} }
convert_tcp_maddr() {
echo $1 | awk -F'/' '{ printf "%s:%s", $3, $5 }'
}
port_from_maddr() {
echo $1 | awk -F'/' '{ print $NF }'
}

View File

@ -328,7 +328,7 @@ test_add_cat_5MB
test_add_cat_expensive 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 test_kill_ipfs_daemon

View File

@ -13,21 +13,24 @@ test_init_ipfs
test_launch_ipfs_daemon --unrestricted-api --disable-transport-encryption test_launch_ipfs_daemon --unrestricted-api --disable-transport-encryption
gwyport=$PORT_GWAY test_expect_success "convert addresses from multiaddrs" '
apiport=$PORT_API '
gwyaddr=$GWAY_ADDR
apiaddr=$API_ADDR
test_expect_success 'api gateway should be unrestricted' ' 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) && HASH=$(ipfs add -q expected) &&
curl -sfo actual1 "http://127.0.0.1:$gwyport/ipfs/$HASH" && curl -sfo actual1 "http://$gwyaddr/ipfs/$HASH" &&
curl -sfo actual2 "http://127.0.0.1:$apiport/ipfs/$HASH" && curl -sfo actual2 "http://$apiaddr/ipfs/$HASH" &&
test_cmp expected actual1 && test_cmp expected actual1 &&
test_cmp expected actual2 test_cmp expected actual2
' '
# Odd. this fails here, but the inverse works on t0060-daemon. # Odd. this fails here, but the inverse works on t0060-daemon.
test_expect_success 'transport should be unencrypted' ' 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 && test_must_fail grep -q "AES-256,AES-128" swarmnc &&
grep -q "/multistream/1.0.0" swarmnc || grep -q "/multistream/1.0.0" swarmnc ||
test_fsh cat swarmnc test_fsh cat swarmnc

View File

@ -9,65 +9,65 @@ test_description="Test daemon command"
test_init_ipfs test_init_ipfs
differentport=$((PORT_API + 1)) differentport=$((API_PORT + 1))
api_different="/ip4/127.0.0.1/tcp/$differentport" api_other="/ip4/127.0.0.1/tcp/$differentport"
api_unreachable="/ip4/127.0.0.1/tcp/1" api_unreachable="/ip4/127.0.0.1/tcp/1"
test_expect_success "config setup" ' test_expect_success "config setup" '
api_fromcfg=$(ipfs config Addresses.API) &&
peerid=$(ipfs config Identity.PeerID) && peerid=$(ipfs config Identity.PeerID) &&
test_check_peerid "$peerid" test_check_peerid "$peerid"
' '
test_client() { test_client() {
printf "$peerid" >expected && opts="$@"
ipfs "$@" id -f="<id>" >actual && echo "OPTS = " $opts
test_cmp expected actual test_expect_success "client must work properly $state" '
printf "$peerid" >expected &&
ipfs id -f="<id>" $opts >actual &&
test_cmp expected actual
'
} }
test_client_must_fail() { test_client_must_fail() {
echo "Error: api not running" >expected_err && opts="$@"
test_must_fail ipfs "$@" id -f="<id>" >actual 2>actual_err && echo "OPTS = " $opts
test_cmp expected_err actual_err test_expect_success "client should fail $state" '
echo "Error: api not running" >expected_err &&
test_must_fail ipfs id -f="<id>" $opts >actual 2>actual_err &&
test_cmp expected_err actual_err
'
} }
test_client_suite() { test_client_suite() {
state="$1" state="$1"
cfg_success="$2" cfg_success="$2"
diff_success="$3" diff_success="$3"
api_fromcfg="$4"
api_different="$5"
# must always work # must always work
test_expect_success "client should work $state" ' test_client
test_client
'
# must always err # 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 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 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 fi
if [ "$diff_success" = true ]; then 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 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 fi
} }
# first, test things without daemon, without /api file # 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 # 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 -f ".ipfs/api"
' '
test_expect_success "api file looks good" ' test_client_suite "(daemon on, no --api, /api file from cfg)" true false "$API_MADDR" "$api_other"
printf "$ADDR_API" >expected &&
test_cmp expected .ipfs/api
'
test_client_suite "(daemon on, no --api, /api file from cfg)" true false
# then, test things without daemon, with /api file # then, test things without daemon, with /api file
test_kill_ipfs_daemon test_kill_ipfs_daemon
test_client_suite "(daemon off, no --api, /api file from cfg)" false false # again, both should fail
test_client_suite "(daemon off, no --api, /api file from cfg)" false false "$API_MADDR" "$api_other"
# 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
test_done test_done

View File

@ -9,11 +9,10 @@ test_description="Test HTTP Gateway"
. lib/test-lib.sh . lib/test-lib.sh
test_init_ipfs test_init_ipfs
test_config_ipfs_gateway_readonly $ADDR_GWAY
test_launch_ipfs_daemon test_launch_ipfs_daemon
port=$PORT_GWAY port=$GWAY_PORT
apiport=$PORT_API apiport=$API_PORT
# TODO check both 5001 and 5002. # TODO check both 5001 and 5002.
# 5001 should have a readable gateway (part of the API) # 5001 should have a readable gateway (part of the API)

View File

@ -9,18 +9,18 @@ test_description="Test HTTP Gateway (Writable)"
. lib/test-lib.sh . lib/test-lib.sh
test_init_ipfs test_init_ipfs
test_config_ipfs_gateway_writable $ADDR_GWAY test_config_ipfs_gateway_writable
test_launch_ipfs_daemon test_launch_ipfs_daemon
port=$PORT_GWAY port=$GWAY_PORT
test_expect_success "ipfs daemon up" ' 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_fsh cat poll_apierr || test_fsh cat poll_apiout
' '
test_expect_success "HTTP gateway gives access to sample file" ' 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 grep "Hello and Welcome to IPFS!" welcome
' '

View File

@ -19,12 +19,11 @@ test_config_ipfs_cors_headers() {
. lib/test-lib.sh . lib/test-lib.sh
test_init_ipfs test_init_ipfs
test_config_ipfs_gateway_readonly $ADDR_GWAY
test_config_ipfs_cors_headers test_config_ipfs_cors_headers
test_launch_ipfs_daemon test_launch_ipfs_daemon
gwport=$PORT_GWAY gwport=$GWAY_PORT
apiport=$PORT_API apiport=$API_PORT
thash='QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' thash='QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
# Gateway # Gateway

View File

@ -58,7 +58,7 @@ test_expect_success "reset iptb nodes" '
test_expect_success "set bootstrap addrs" ' test_expect_success "set bootstrap addrs" '
bsn_peer_id=$(ipfs id -f "<id>") && bsn_peer_id=$(ipfs id -f "<id>") &&
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 0 bootstrap add $BADDR &&
ipfsi 1 bootstrap add $BADDR && ipfsi 1 bootstrap add $BADDR &&
ipfsi 2 bootstrap add $BADDR && ipfsi 2 bootstrap add $BADDR &&

View File

@ -16,7 +16,7 @@ test_ls_cmd() {
mkdir -p testdir && mkdir -p testdir &&
echo "hello test" >testdir/test.txt && echo "hello test" >testdir/test.txt &&
ipfs add -r testdir && 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" ' test_expect_success "Text encoded channel-streaming command output looks good" '
@ -39,7 +39,7 @@ test_ls_cmd() {
mkdir -p testdir && mkdir -p testdir &&
echo "hello test" >testdir/test.txt && echo "hello test" >testdir/test.txt &&
ipfs add -r testdir && 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" ' test_expect_success "JSON encoded channel-streaming command output looks good" '