Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for waiting on multiple services #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ When using this tool, you only need to pick the `wait-for` file as part of your
## Usage

```
./wait-for host:port [-t timeout] [-- command args]
./wait-for host:port [host:port...] [-t timeout] [-- command args]
-q | --quiet Do not output any status messages
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
Expand Down Expand Up @@ -43,6 +43,26 @@ services:
- db
```

To wait for several containers to become available:

```
version: '2'

services:
db:
image: postgres:9.4

elk:
image: sebp/elk

backend:
build: backend
command: sh -c './wait-for db:5432 elk:9563 -- npm start'
depends_on:
- db
- elk
```

## Testing

Ironically testing is done using [bats](https://github.com/sstephenson/bats), which on the other hand is depending on [bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell)).
Expand Down
37 changes: 25 additions & 12 deletions wait-for
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ usage() {
exitcode="$1"
cat << USAGE >&2
Usage:
$cmdname host:port [-t timeout] [-- command args]
$cmdname host:port [host:port...] [-t timeout] [-- command args]
-q | --quiet Do not output any status messages
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
Expand All @@ -25,23 +25,22 @@ wait_for() {

result=$?
if [ $result -eq 0 ] ; then
if [ $# -gt 0 ] ; then
exec "$@"
fi
exit 0
return
else
sleep 1
fi
sleep 1
done
echo "Operation timed out" >&2
exit 1
}

SERVICES=""

while [ $# -gt 0 ]
do
case "$1" in
*:* )
HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
SERVICES="${SERVICES} $1"
shift 1
;;
-q | --quiet)
Expand Down Expand Up @@ -71,9 +70,23 @@ do
esac
done

if [ "$HOST" = "" -o "$PORT" = "" ]; then
echoerr "Error: you need to provide a host and port to test."
usage 2
if [ "$SERVICES" = "" ] ; then
echoerr "Error: you need to provide at least one service to test."
usage 2
fi

wait_for "$@"
for SERVICE in ${SERVICES} ; do
HOST=$(printf "%s\n" "$SERVICE"| cut -d : -f 1)
PORT=$(printf "%s\n" "$SERVICE"| cut -d : -f 2)

if [ "$HOST" = "" -o "$PORT" = "" ]; then
echoerr "Error: you need to provide a host and port to test."
usage 2
fi

wait_for
done

if [ $# -gt 0 ] ; then
exec "$@"
fi
6 changes: 6 additions & 0 deletions wait-for.bats
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@
[ "$status" -ne 0 ]
[ "$output" != "success" ]
}

@test "google and bing should be immediately found" {
run ./wait-for google.com:80 bing.com:80 -- echo 'success'

[ "$output" = "success" ]
}