Use port zero for all ipfs daemon addresses in sharness testing
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Feb 11, 2016 at 19:44 UTC
0c7595b65b531f271dfbc079efefd36edaf63a5f
9 files changed
+107
-128
test/sharness/lib/test-lib.sh
+54
-52
@@ -125,29 +125,6 @@ test_config_set() {
125
126
test_init_ipfs() {
127
128
- # we have a problem where initializing daemons with the same api port
129
- # often fails-- it hangs indefinitely. The proper solution is to make
130
- # ipfs pick an unused port for the api on startup, and then use that.
131
- # Unfortunately, ipfs doesnt yet know how to do this-- the api port
132
- # must be specified. Until ipfs learns how to do this, we must use
133
- # specific port numbers, which may still fail but less frequently
134
- # if we at least use different ones.
135
-
136
- # Using RANDOM like this is clearly wrong-- it samples with replacement
137
- # and it doesnt even check the port is unused. this is a trivial stop gap
138
- # until the proper solution is implemented.
139
- RANDOM=$$
140
- PORT_API=$((RANDOM % 3000 + 5100))
141
- ADDR_API="/ip4/127.0.0.1/tcp/$PORT_API"
142
-
143
- PORT_GWAY=$((RANDOM % 3000 + 8100))
144
- ADDR_GWAY="/ip4/127.0.0.1/tcp/$PORT_GWAY"
145
-
146
- PORT_SWARM=$((RANDOM % 3000 + 12000))
147
- ADDR_SWARM="[
148
- \"/ip4/0.0.0.0/tcp/$PORT_SWARM\"
149
-]"
150
-
128
129
# we set the Addresses.API config variable.
130
# the cli client knows to use it, so only need to set.
@@ -162,40 +139,42 @@ test_init_ipfs() {
139
mkdir mountdir ipfs ipns &&
140
test_config_set Mounts.IPFS "$(pwd)/ipfs" &&
141
test_config_set Mounts.IPNS "$(pwd)/ipns" &&
165
- test_config_set Addresses.API "$ADDR_API" &&
166
- test_config_set Addresses.Gateway "$ADDR_GWAY" &&
167
- test_config_set --json Addresses.Swarm "$ADDR_SWARM" &&
142
+ test_config_set Addresses.API "/ip4/127.0.0.1/tcp/0" &&
143
+ test_config_set Addresses.Gateway "/ip4/127.0.0.1/tcp/0" &&
144
+ test_config_set --json Addresses.Swarm "[
145
+ \"/ip4/0.0.0.0/tcp/0\"
146
+]" &&
147
ipfs bootstrap rm --all ||
148
test_fsh cat "\"$IPFS_PATH/config\""
149
'
150
151
}
152
174
-test_config_ipfs_gateway_readonly() {
175
- ADDR_GWAY=$1
176
- test_expect_success "prepare config -- gateway address" '
177
- test "$ADDR_GWAY" != "" &&
178
- test_config_set "Addresses.Gateway" "$ADDR_GWAY"
179
- '
180
-
181
- # tell the user what's going on if they messed up the call.
182
- if test "$#" = 0; then
183
- echo "# Error: must call with an address, for example:"
184
- echo '# test_config_ipfs_gateway_readonly "/ip4/0.0.0.0/tcp/5002"'
185
- echo '#'
186
- fi
187
-}
188
-
153
test_config_ipfs_gateway_writable() {
190
-
191
- test_config_ipfs_gateway_readonly $1
192
-
154
test_expect_success "prepare config -- gateway writable" '
155
test_config_set --bool Gateway.Writable true ||
156
test_fsh cat "\"$IPFS_PATH/config\""
157
'
158
}
159
160
+test_wait_for_file() {
161
+ loops=$1
162
+ delay=$2
163
+ file=$3
164
+ fwaitc=0
165
+ while ! test -f "$file"
166
+ do
167
+ if test $fwaitc -ge $loops
168
+ then
169
+ echo "Error: timed out waiting for file: $file"
170
+ return 1
171
+ fi
172
+
173
+ go-sleep $delay
174
+ fwaitc=`expr $fwaitc + 1`
175
+ done
176
+}
177
+
178
test_launch_ipfs_daemon() {
179
180
args="$@"
@@ -204,19 +183,34 @@ test_launch_ipfs_daemon() {
183
ipfs daemon $args >actual_daemon 2>daemon_err &
184
'
185
186
+ # wait for api file to show up
187
+ test_expect_success "api file shows up" '
188
+ test_wait_for_file 20 100ms "$IPFS_PATH/api"
189
+ '
190
+
191
+ test_expect_success "set up address variables" '
192
+ API_MADDR=$(cat "$IPFS_PATH/api") &&
193
+ API_ADDR=$(convert_tcp_maddr $API_MADDR) &&
194
+ API_PORT=$(port_from_maddr $API_MADDR) &&
195
+
196
+ GWAY_MADDR=$(sed -n "s/^Gateway (.*) server listening on //p" actual_daemon) &&
197
+ GWAY_ADDR=$(convert_tcp_maddr $GWAY_MADDR) &&
198
+ GWAY_PORT=$(port_from_maddr $GWAY_MADDR)
199
+ '
200
+
201
+
202
+ test_expect_success "set swarm address vars" '
203
+ ipfs swarm addrs local > addrs_out &&
204
+ SWARM_MADDR=$(grep "127.0.0.1" addrs_out) &&
205
+ SWARM_PORT=$(port_from_maddr $SWARM_MADDR)
206
+ '
207
+
208
# we say the daemon is ready when the API server is ready.
209
test_expect_success "'ipfs daemon' is ready" '
210
IPFS_PID=$! &&
210
- pollEndpoint -ep=/version -host=$ADDR_API -v -tout=1s -tries=60 2>poll_apierr > poll_apiout ||
211
+ pollEndpoint -ep=/version -host=$API_MADDR -v -tout=1s -tries=60 2>poll_apierr > poll_apiout ||
212
test_fsh cat actual_daemon || test_fsh cat daemon_err || test_fsh cat poll_apierr || test_fsh cat poll_apiout
213
'
213
-
214
- if test "$ADDR_GWAY" != ""; then
215
- test_expect_success "'ipfs daemon' output includes Gateway address" '
216
- pollEndpoint -ep=/version -host=$ADDR_GWAY -v -tout=1s -tries=60 2>poll_gwerr > poll_gwout ||
217
- test_fsh cat daemon_err || test_fsh cat poll_gwerr || test_fsh cat poll_gwout
218
- '
219
- fi
214
}
215
216
do_umount() {
@@ -365,3 +359,11 @@ test_check_peerid() {
359
return 1
360
}
361
}
362
+
363
+convert_tcp_maddr() {
364
+ echo $1 | awk -F'/' '{ printf "%s:%s", $3, $5 }'
365
+}
366
+
367
+port_from_maddr() {
368
+ echo $1 | awk -F'/' '{ print $NF }'
369
+}
test/sharness/t0040-add-and-cat.sh
+1
-1
@@ -328,7 +328,7 @@ test_add_cat_5MB
328
329
test_add_cat_expensive
330
331
-test_add_named_pipe " Post http://127.0.0.1:$PORT_API/api/v0/add?encoding=json&progress=true&r=true&stream-channels=true:"
331
+test_add_named_pipe " Post http://$API_ADDR/api/v0/add?encoding=json&progress=true&r=true&stream-channels=true:"
332
333
test_kill_ipfs_daemon
334
test/sharness/t0061-daemon-opts.sh
+9
-6
@@ -13,21 +13,24 @@ test_init_ipfs
13
14
test_launch_ipfs_daemon --unrestricted-api --disable-transport-encryption
15
16
-gwyport=$PORT_GWAY
17
-apiport=$PORT_API
16
+test_expect_success "convert addresses from multiaddrs" '
17
+'
18
+
19
+gwyaddr=$GWAY_ADDR
20
+apiaddr=$API_ADDR
21
22
test_expect_success 'api gateway should be unrestricted' '
20
- echo "hello mars :$gwyport :$apiport" >expected &&
23
+ echo "hello mars :$gwyaddr :$apiaddr" >expected &&
24
HASH=$(ipfs add -q expected) &&
22
- curl -sfo actual1 "http://127.0.0.1:$gwyport/ipfs/$HASH" &&
23
- curl -sfo actual2 "http://127.0.0.1:$apiport/ipfs/$HASH" &&
25
+ curl -sfo actual1 "http://$gwyaddr/ipfs/$HASH" &&
26
+ curl -sfo actual2 "http://$apiaddr/ipfs/$HASH" &&
27
test_cmp expected actual1 &&
28
test_cmp expected actual2
29
'
30
31
# Odd. this fails here, but the inverse works on t0060-daemon.
32
test_expect_success 'transport should be unencrypted' '
30
- go-sleep 0.5s | nc localhost "$PORT_SWARM" >swarmnc &&
33
+ go-sleep 0.5s | nc localhost "$SWARM_PORT" >swarmnc &&
34
test_must_fail grep -q "AES-256,AES-128" swarmnc &&
35
grep -q "/multistream/1.0.0" swarmnc ||
36
test_fsh cat swarmnc
test/sharness/t0062-daemon-api.sh
+32
-56
@@ -9,65 +9,65 @@ test_description="Test daemon command"
9
10
test_init_ipfs
11
12
-differentport=$((PORT_API + 1))
13
-api_different="/ip4/127.0.0.1/tcp/$differentport"
12
+differentport=$((API_PORT + 1))
13
+api_other="/ip4/127.0.0.1/tcp/$differentport"
14
api_unreachable="/ip4/127.0.0.1/tcp/1"
15
16
test_expect_success "config setup" '
17
- api_fromcfg=$(ipfs config Addresses.API) &&
17
peerid=$(ipfs config Identity.PeerID) &&
18
test_check_peerid "$peerid"
19
'
20
21
test_client() {
23
- printf "$peerid" >expected &&
24
- ipfs "$@" id -f="<id>" >actual &&
25
- test_cmp expected actual
22
+ opts="$@"
23
+ echo "OPTS = " $opts
24
+ test_expect_success "client must work properly $state" '
25
+ printf "$peerid" >expected &&
26
+ ipfs id -f="<id>" $opts >actual &&
27
+ test_cmp expected actual
28
+ '
29
}
30
31
test_client_must_fail() {
29
- echo "Error: api not running" >expected_err &&
30
- test_must_fail ipfs "$@" id -f="<id>" >actual 2>actual_err &&
31
- test_cmp expected_err actual_err
32
+ opts="$@"
33
+ echo "OPTS = " $opts
34
+ test_expect_success "client should fail $state" '
35
+ echo "Error: api not running" >expected_err &&
36
+ test_must_fail ipfs id -f="<id>" $opts >actual 2>actual_err &&
37
+ test_cmp expected_err actual_err
38
+ '
39
}
40
41
test_client_suite() {
42
state="$1"
43
cfg_success="$2"
44
diff_success="$3"
45
+ api_fromcfg="$4"
46
+ api_different="$5"
47
+
48
# must always work
39
- test_expect_success "client should work $state" '
40
- test_client
41
- '
49
+ test_client
50
51
# must always err
44
- test_expect_success "client --api unreachable should err $state" '
45
- test_client_must_fail --api "$api_unreachable"
46
- '
52
+ test_client_must_fail --api "$api_unreachable"
53
54
if [ "$cfg_success" = true ]; then
49
- test_expect_success "client --api fromcfg should work $state" '
50
- test_client --api "$api_fromcfg"
51
- '
55
+ test_client --api "$api_fromcfg"
56
else
53
- test_expect_success "client --api fromcfg should err $state" '
54
- test_client_must_fail --api "$api_fromcfg"
55
- '
57
+ test_client_must_fail --api "$api_fromcfg"
58
fi
59
60
if [ "$diff_success" = true ]; then
59
- test_expect_success "client --api different should work $state" '
60
- test_client --api "$api_different"
61
- '
61
+ test_client --api "$api_different"
62
else
63
- test_expect_success "client --api different should err $state" '
64
- test_client_must_fail --api "$api_different"
65
- '
63
+ test_client_must_fail --api "$api_different"
64
fi
67
- }
65
+}
66
67
# first, test things without daemon, without /api file
70
-test_client_suite "(daemon off, no --api, no /api file)" false false
68
+# with no daemon, everything should fail
69
+# (using unreachable because API_MADDR doesnt get set until daemon start)
70
+test_client_suite "(daemon off, no --api, no /api file)" false false "$api_unreachable" "$api_other"
71
72
73
# then, test things with daemon, with /api file
@@ -78,37 +78,13 @@ test_expect_success "'ipfs daemon' creates api file" '
78
test -f ".ipfs/api"
79
'
80
81
-test_expect_success "api file looks good" '
82
- printf "$ADDR_API" >expected &&
83
- test_cmp expected .ipfs/api
84
-'
85
-
86
-test_client_suite "(daemon on, no --api, /api file from cfg)" true false
81
+test_client_suite "(daemon on, no --api, /api file from cfg)" true false "$API_MADDR" "$api_other"
82
83
# then, test things without daemon, with /api file
84
85
test_kill_ipfs_daemon
86
92
-test_client_suite "(daemon off, no --api, /api file from cfg)" false false
93
-
94
-# then, test things with daemon --api $api_different, with /api file
95
-
96
-PORT_API=$differentport
97
-ADDR_API=$api_different
98
-
99
-test_launch_ipfs_daemon --api "$ADDR_API"
100
-
101
-test_expect_success "'ipfs daemon' --api option works" '
102
- printf "$api_different" >expected &&
103
- test_cmp expected .ipfs/api
104
-'
105
-
106
-test_client_suite "(daemon on, /api file different)" false true
107
-
108
-# then, test things with daemon off, with /api file, for good measure.
109
-
110
-test_kill_ipfs_daemon
111
-
112
-test_client_suite "(daemon off, /api file different)" false false
87
+# again, both should fail
88
+test_client_suite "(daemon off, no --api, /api file from cfg)" false false "$API_MADDR" "$api_other"
89
90
test_done
test/sharness/t0110-gateway.sh
+2
-3
@@ -9,11 +9,10 @@ test_description="Test HTTP Gateway"
9
. lib/test-lib.sh
10
11
test_init_ipfs
12
-test_config_ipfs_gateway_readonly $ADDR_GWAY
12
test_launch_ipfs_daemon
13
15
-port=$PORT_GWAY
16
-apiport=$PORT_API
14
+port=$GWAY_PORT
15
+apiport=$API_PORT
16
17
# TODO check both 5001 and 5002.
18
# 5001 should have a readable gateway (part of the API)
test/sharness/t0111-gateway-writeable.sh
+4
-4
@@ -9,18 +9,18 @@ test_description="Test HTTP Gateway (Writable)"
9
. lib/test-lib.sh
10
11
test_init_ipfs
12
-test_config_ipfs_gateway_writable $ADDR_GWAY
12
+test_config_ipfs_gateway_writable
13
test_launch_ipfs_daemon
14
15
-port=$PORT_GWAY
15
+port=$GWAY_PORT
16
17
test_expect_success "ipfs daemon up" '
18
- pollEndpoint -host $ADDR_GWAY -ep=/version -v -tout=1s -tries=60 2>poll_apierr > poll_apiout ||
18
+ pollEndpoint -host $GWAY_MADDR -ep=/version -v -tout=1s -tries=60 2>poll_apierr > poll_apiout ||
19
test_fsh cat poll_apierr || test_fsh cat poll_apiout
20
'
21
22
test_expect_success "HTTP gateway gives access to sample file" '
23
- curl -s -o welcome "http://localhost:$PORT_GWAY/ipfs/$HASH_WELCOME_DOCS/readme" &&
23
+ curl -s -o welcome "http://$GWAY_ADDR/ipfs/$HASH_WELCOME_DOCS/readme" &&
24
grep "Hello and Welcome to IPFS!" welcome
25
'
26
test/sharness/t0112-gateway-cors.sh
+2
-3
@@ -19,12 +19,11 @@ test_config_ipfs_cors_headers() {
19
. lib/test-lib.sh
20
21
test_init_ipfs
22
-test_config_ipfs_gateway_readonly $ADDR_GWAY
22
test_config_ipfs_cors_headers
23
test_launch_ipfs_daemon
24
26
-gwport=$PORT_GWAY
27
-apiport=$PORT_API
25
+gwport=$GWAY_PORT
26
+apiport=$API_PORT
27
thash='QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
28
29
# Gateway
test/sharness/t0121-bootstrap-iptb.sh
+1
-1
@@ -58,7 +58,7 @@ test_expect_success "reset iptb nodes" '
58
59
test_expect_success "set bootstrap addrs" '
60
bsn_peer_id=$(ipfs id -f "<id>") &&
61
- BADDR="/ip4/127.0.0.1/tcp/$PORT_SWARM/ipfs/$bsn_peer_id" &&
61
+ BADDR="/ip4/127.0.0.1/tcp/$SWARM_PORT/ipfs/$bsn_peer_id" &&
62
ipfsi 0 bootstrap add $BADDR &&
63
ipfsi 1 bootstrap add $BADDR &&
64
ipfsi 2 bootstrap add $BADDR &&
test/sharness/t0230-channel-streaming-http-content-type.sh
+2
-2
@@ -16,7 +16,7 @@ test_ls_cmd() {
16
mkdir -p testdir &&
17
echo "hello test" >testdir/test.txt &&
18
ipfs add -r testdir &&
19
- curl -i "http://localhost:$PORT_API/api/v0/refs?arg=QmTcJAn3JP8ZMAKS6WS75q8sbTyojWKbxcUHgLYGWur4Ym&stream-channels=true&encoding=text" >actual_output
19
+ curl -i "http://$API_ADDR/api/v0/refs?arg=QmTcJAn3JP8ZMAKS6WS75q8sbTyojWKbxcUHgLYGWur4Ym&stream-channels=true&encoding=text" >actual_output
20
'
21
22
test_expect_success "Text encoded channel-streaming command output looks good" '
@@ -39,7 +39,7 @@ test_ls_cmd() {
39
mkdir -p testdir &&
40
echo "hello test" >testdir/test.txt &&
41
ipfs add -r testdir &&
42
- curl -i "http://localhost:$PORT_API/api/v0/refs?arg=QmTcJAn3JP8ZMAKS6WS75q8sbTyojWKbxcUHgLYGWur4Ym&stream-channels=true&encoding=json" >actual_output
42
+ curl -i "http://$API_ADDR/api/v0/refs?arg=QmTcJAn3JP8ZMAKS6WS75q8sbTyojWKbxcUHgLYGWur4Ym&stream-channels=true&encoding=json" >actual_output
43
'
44
45
test_expect_success "JSON encoded channel-streaming command output looks good" '