Skip to content

Commit 17ab179

Browse files
Start BDD Test Containers intelligently
Rather than sleeping and 'hoping' the membersrvc is ready once the thread wakes up, add a script which polls the membersrvc rpc port to check if it is open, and if it is, run the peer. For docker-compose configurations that don't use the membersrvc, the peers will start immediately. Change-Id: I65aff6a6432fbca7168309719893ec8a9f4e290f Signed-off-by: Julian Carrivick <[email protected]>
1 parent 457635a commit 17ab179

File tree

4 files changed

+189
-5
lines changed

4 files changed

+189
-5
lines changed

bddtests/compose-defaults.yml

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ vp:
55
- CORE_VM_ENDPOINT=http://172.17.0.1:2375
66
# TODO: This is currently required due to BUG in variant logic based upon log level.
77
- CORE_LOGGING_LEVEL=DEBUG
8-
# Startup of peer must be delayed to allow membersrvc to come up first
9-
command: sh -c "sleep 5; peer node start"
10-
#command: peer node start
11-
8+
# Script will wait until membersrvc is up (if it exists) before starting
9+
# $$GOPATH (double dollar) required to prevent docker-compose doing its own
10+
# substitution before the value gets to the container
11+
command: sh -c '$$GOPATH/src/github.com/hyperledger/fabric/bddtests/scripts/start-peer.sh'
12+
1213
# Use these options if coverage desired for peers
1314
#image: hyperledger/fabric-peer-coverage
1415
#command: ./peer.test --test.coverprofile=coverage.cov node start

bddtests/docker-compose-1-devmode.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ ccenv:
1111
environment:
1212
- CORE_CHAINCODE_ID_NAME=testCC
1313
- CORE_PEER_ADDRESS=vp0:7051
14-
command: bash -c "GOBIN=/opt/gopath/bin go install github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 && /opt/gopath/bin/chaincode_example02"
14+
# $$GOPATH (double dollar) required to prevent docker-compose doing its own
15+
# substitution before the value gets to the container
16+
command: bash -c 'go install github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 && $$GOPATH/bin/chaincode_example02'
1517
links:
1618
- vp0

bddtests/scripts/start-peer.sh

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/sh
2+
3+
SCRIPT_DIR=$(dirname $0)
4+
MEMBERSHIP_IP=$(cat /etc/hosts | grep membersrvc | head -n 1 | cut -f1)
5+
TIMEOUT=10
6+
7+
if [ -n "$MEMBERSHIP_IP" ]; then
8+
echo "membersrvc detected, waiting for it before starting with a $TIMEOUT second timout"
9+
"$SCRIPT_DIR"/wait-for-it.sh -t "$TIMEOUT" "$MEMBERSHIP_IP":7054
10+
11+
if [ $? -ne 0 ]; then
12+
echo "Failed to contact membersrvc within $TIMEOUT seconds"
13+
exit 1
14+
fi
15+
else
16+
echo "No membersrvc to wait for, starting immediately"
17+
fi
18+
19+
peer node start
20+

bddtests/scripts/wait-for-it.sh

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
#!/bin/sh
2+
# Modified from https://github.com/vishnubob/wait-for-it to be POSIX compatible
3+
# Use this script to test if a given TCP host/port are available
4+
5+
cmdname=$(basename $0)
6+
7+
TIMEOUT=${TIMEOUT:-15}
8+
STRICT=${STRICT:-0}
9+
CHILD=${CHILD:-0}
10+
QUIET=${QUIET:-0}
11+
12+
echoerr() { if [ $QUIET -ne 1 ]; then echo "$@" 1>&2; fi }
13+
14+
usage()
15+
{
16+
cat << USAGE >&2
17+
Usage:
18+
$cmdname host:port [-s] [-t timeout] [-- command args]
19+
-h HOST | --host=HOST Host or IP under test
20+
-p PORT | --port=PORT TCP port under test
21+
Alternatively, you specify the host and port as host:port
22+
-s | --strict Only execute subcommand if the test succeeds
23+
-q | --quiet Don't output any status messages
24+
-t TIMEOUT | --timeout=TIMEOUT
25+
Timeout in seconds, zero for no timeout
26+
-- COMMAND ARGS Execute command with args after the test finishes
27+
USAGE
28+
exit 1
29+
}
30+
31+
wait_for()
32+
{
33+
if [ $TIMEOUT -gt 0 ]; then
34+
echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT"
35+
else
36+
echoerr "$cmdname: waiting for $HOST:$PORT without a timeout"
37+
fi
38+
start_ts=$(date +%s)
39+
while :
40+
do
41+
nc -z $HOST $PORT >/dev/null 2>&1
42+
result=$?
43+
if [ $result -eq 0 ]; then
44+
end_ts=$(date +%s)
45+
echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds"
46+
break
47+
fi
48+
sleep 1
49+
done
50+
return $result
51+
}
52+
53+
wait_for_wrapper()
54+
{
55+
# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
56+
if [ $QUIET -eq 1 ]; then
57+
timeout $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
58+
else
59+
timeout $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
60+
fi
61+
PID=$!
62+
trap "kill -INT -$PID" INT
63+
wait $PID
64+
RESULT=$?
65+
if [ $RESULT -ne 0 ]; then
66+
echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT"
67+
fi
68+
return $RESULT
69+
}
70+
71+
# process arguments
72+
while [ $# -gt 0 ]
73+
do
74+
case "$1" in
75+
*:* )
76+
HOST=$(echo $1 | cut --fields=1 --delimiter=:)
77+
PORT=$(echo $1 | cut --fields=2- --delimiter=:)
78+
shift 1
79+
;;
80+
--child)
81+
CHILD=1
82+
shift 1
83+
;;
84+
-q | --quiet)
85+
QUIET=1
86+
shift 1
87+
;;
88+
-s | --strict)
89+
STRICT=1
90+
shift 1
91+
;;
92+
-h)
93+
HOST="$2"
94+
if [ "$HOST" = "" ]; then break; fi
95+
shift 2
96+
;;
97+
--host=*)
98+
HOST="${1#*=}"
99+
shift 1
100+
;;
101+
-p)
102+
PORT="$2"
103+
if [ "$PORT" = "" ]; then break; fi
104+
shift 2
105+
;;
106+
--port=*)
107+
PORT="${1#*=}"
108+
shift 1
109+
;;
110+
-t)
111+
TIMEOUT="$2"
112+
if [ "$TIMEOUT" = "" ]; then break; fi
113+
shift 2
114+
;;
115+
--timeout=*)
116+
TIMEOUT="${1#*=}"
117+
shift 1
118+
;;
119+
--)
120+
shift
121+
CLI="$@"
122+
break
123+
;;
124+
--help)
125+
usage
126+
;;
127+
*)
128+
echoerr "Unknown argument: $1"
129+
usage
130+
;;
131+
esac
132+
done
133+
134+
if [ "$HOST" = "" ] || [ "$PORT" = "" ]; then
135+
echoerr "Error: you need to provide a host and port to test."
136+
usage
137+
fi
138+
139+
if [ $CHILD -gt 0 ]; then
140+
wait_for
141+
RESULT=$?
142+
exit $RESULT
143+
else
144+
if [ $TIMEOUT -gt 0 ]; then
145+
wait_for_wrapper
146+
RESULT=$?
147+
else
148+
wait_for
149+
RESULT=$?
150+
fi
151+
fi
152+
153+
if [ "$CLI" != "" ]; then
154+
if [ $RESULT -ne 0 ] && [ $STRICT -eq 1 ]; then
155+
echoerr "$cmdname: strict mode, refusing to execute subprocess"
156+
exit $RESULT
157+
fi
158+
exec $CLI
159+
else
160+
exit $RESULT
161+
fi

0 commit comments

Comments
 (0)