Skip to content
This repository was archived by the owner on Oct 7, 2021. It is now read-only.

Commit 7c02f49

Browse files
authored
Merge pull request #1 from ianfixes/2017-10-25_multiple_fixes
apply fixes from ianfixes
2 parents 8d9b444 + 96511d6 commit 7c02f49

File tree

7 files changed

+101
-19
lines changed

7 files changed

+101
-19
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ services:
77

88
before_install:
99
- docker build -t eficode/wait-for .
10+
11+
script:
12+
- npm install
13+
- ./run_tests.sh
1014
- docker run eficode/wait-for

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ WORKDIR /app
88
COPY . /app
99
RUN npm install
1010

11-
CMD ./node_modules/.bin/bats wait-for.bats
11+
# On launch, run the test suite via npm
12+
CMD npm test

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ When using this tool, you only need to pick the `wait-for` file as part of your
99
## Usage
1010

1111
```
12-
./wait-for host:port [-t timeout] [-- command args]
12+
wait-for host:port [-t timeout] [-- command args]
1313
-q | --quiet Do not output any status messages
14+
-l | --loose Execute subcommand even if the test times out
1415
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
1516
-- COMMAND ARGS Execute command with args after the test finishes
1617
```
@@ -21,11 +22,19 @@ To check if [eficode.com](https://eficode.com) is available:
2122

2223
```
2324
$ ./wait-for www.eficode.com:80 -- echo "Eficode site is up"
24-
25-
Connection to www.eficode.com port 80 [tcp/http] succeeded!
2625
Eficode site is up
2726
```
2827

28+
The subcommand will be executed regardless if the service is up or not. If you wish to execute the subcommand only if the service is up, add the --strict argument. In this example, we will test port 81 on www.google.com which will fail:
29+
30+
```
31+
$ ./wait-for www.google.com:81 --timeout=1 -- echo "google is up"
32+
Operation timed out
33+
$ ./wait-for www.google.com:81 --timeout=1 --loose -- echo "waited for google"
34+
Operation timed out
35+
waited for google
36+
```
37+
2938
To wait for database container to become available:
3039

3140

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "wait-for",
33
"version": "0.1.0",
44
"scripts": {
5-
"test": "./node_modules/.bin/bats wait-for.bats"
5+
"test": "./run_tests.sh"
66
},
77
"dependencies": {
88
"bats": "^0.4.2"

run_tests.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
3+
# Although it would be possible to just call this directly from the Dockerfile,
4+
# centralizing tests in this file allows both the docker container and the
5+
# CI machine to run the same set of tests for an additional datapoint --
6+
# which gives a better chance of turning up POSIX noncompliance
7+
8+
./node_modules/.bin/bats wait-for.bats

wait-for

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,71 @@
11
#!/bin/sh
22

3+
OLD_TIMEOUT=$TIMEOUT
4+
OLD_QUIET=$QUIET
5+
OLD_PORT=$PORT
6+
OLD_HOST=$HOST
7+
OLD_LOOSE=$LOOSE
8+
39
TIMEOUT=15
410
QUIET=0
11+
LOOSE=0
12+
13+
if ! which nc >/dev/null; then
14+
echo "Netcat is not installed. This script requires netcat to work correctly."
15+
exit 1
16+
fi
517

618
echoerr() {
719
if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
820
}
921

22+
conditionally_output() {
23+
if [ "$QUIET" -ne 1 ]; then
24+
"$@"
25+
else
26+
"$@" > /dev/null 2>&1
27+
fi
28+
}
29+
1030
usage() {
1131
exitcode="$1"
1232
cat << USAGE >&2
1333
Usage:
14-
$cmdname host:port [-t timeout] [-- command args]
34+
$(basename $0) host:port [-t timeout] [-- command args]
1535
-q | --quiet Do not output any status messages
36+
-l | --loose Execute subcommand even if the test times out
1637
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
1738
-- COMMAND ARGS Execute command with args after the test finishes
1839
USAGE
1940
exit "$exitcode"
2041
}
2142

43+
test_connection() {
44+
conditionally_output echo "Testing connection to $1:$2..."
45+
46+
# force a 1-second timeout on darwin (https://stackoverflow.com/a/20460402/2063546)
47+
# POSIX-compliant string inclusion test https://stackoverflow.com/a/8811800/2063546
48+
if [ "${OSTYPE#*darwin*}" != "$OSTYPE" ] ; then
49+
conditionally_output nc -z -w 1 -G 1 "$1" "$2"
50+
else
51+
conditionally_output nc -z -w 1 "$1" "$2"
52+
fi
53+
}
54+
2255
wait_for() {
56+
local result
2357
for i in `seq $TIMEOUT` ; do
24-
nc -z "$HOST" "$PORT" > /dev/null 2>&1
25-
58+
# use a 1-second timeout, but still sleep 0.1 seconds after just to be safe
59+
test_connection "$HOST" "$PORT"
2660
result=$?
27-
if [ $result -eq 0 ] ; then
28-
if [ $# -gt 0 ] ; then
29-
exec "$@"
30-
fi
31-
exit 0
32-
fi
61+
if [ $result -eq 0 ] ; then break ; fi
3362
sleep 1
3463
done
35-
echo "Operation timed out" >&2
36-
exit 1
64+
[ $result -ne 0 ] && echoerr "Operation timed out"
65+
if [ $result -eq 0 -o $LOOSE -eq 1 -a $# -gt 0 ] ; then
66+
TIMEOUT=$OLD_TIMEOUT QUIET=$OLD_QUIET PORT=$OLD_PORT HOST=$OLD_HOST LOOSE=$OLD_LOOSE exec "$@"
67+
fi
68+
exit $result
3769
}
3870

3971
while [ $# -gt 0 ]
@@ -48,6 +80,10 @@ do
4880
QUIET=1
4981
shift 1
5082
;;
83+
-l | --loose)
84+
LOOSE=1
85+
shift 1
86+
;;
5187
-t)
5288
TIMEOUT="$2"
5389
if [ "$TIMEOUT" = "" ]; then break; fi

wait-for.bats

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env bats
22

3-
@test "google should be immediately found" {
4-
run ./wait-for google.com:80 -- echo 'success'
5-
3+
@test "google should be immediately found, no output other than our own" {
4+
run ./wait-for -q google.com:80 -- echo 'success'
5+
66
[ "$output" = "success" ]
77
}
88

@@ -12,3 +12,27 @@
1212
[ "$status" -ne 0 ]
1313
[ "$output" != "success" ]
1414
}
15+
16+
@test "nonexistent server should start command if loose option is specified" {
17+
run ./wait-for -q -t 1 -l noserver:9999 -- echo 'passable' 2>&1
18+
19+
[ "$status" -eq 0 ]
20+
21+
[ "$output" = "passable" ]
22+
}
23+
24+
@test "preserve existing environment variables" {
25+
TIMEOUT=mytimeout
26+
QUIET=myquiet
27+
HOST=myhost
28+
PORT=myport
29+
LOOSE=myloose
30+
31+
run ./wait-for google.com:80 -- echo 'success'
32+
33+
[ "$(echo $TIMEOUT)" = 'mytimeout' ]
34+
[ "$(echo $QUIET)" = 'myquiet' ]
35+
[ "$(echo $HOST)" = 'myhost' ]
36+
[ "$(echo $PORT)" = 'myport' ]
37+
[ "$(echo $LOOSE)" = 'myloose' ]
38+
}

0 commit comments

Comments
 (0)