Skip to content

Commit

Permalink
Change strict option to loose option to avoid breaking existing behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatz-drizly committed Oct 25, 2018
1 parent cd8c81b commit 8d2abc1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ 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 [-t timeout] [-- command args]
-q | --quiet Do not output any status messages
-s | --strict Only execute subcommand if the test succeeds
-l | --loose Execute subcommand even if the test times out
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
```
Expand All @@ -22,18 +22,17 @@ To check if [eficode.com](https://eficode.com) is available:

```
$ ./wait-for www.eficode.com:80 -- echo "Eficode site is up"
Connection to www.eficode.com port 80 [tcp/http] succeeded!
Eficode site is up
```

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:

```
$ ./wait-for www.eficode.com:80 -- echo "Eficode site is up"
$ ./wait-for www.google.com:81 --timeout=1 --strict -- echo "google is up"
$ ./wait-for www.google.com:81 --timeout=1 -- echo "google is up"
Operation timed out
$ ./wait-for www.google.com:81 --timeout=1 --loose -- echo "waited for google"
Operation timed out
google is up
waited for google
```

To wait for database container to become available:
Expand Down
30 changes: 11 additions & 19 deletions wait-for
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ OLD_TIMEOUT=$TIMEOUT
OLD_QUIET=$QUIET
OLD_PORT=$PORT
OLD_HOST=$HOST
OLD_LOOSE=$LOOSE

TIMEOUT=15
QUIET=0
LOOSE=0

if ! which nc >/dev/null; then
echo "Netcat is not installed. This script requires netcat to work correctly."
Expand All @@ -23,7 +25,7 @@ usage() {
Usage:
$(basename $0) host:port [-t timeout] [-- command args]
-q | --quiet Do not output any status messages
-s | --strict Only execute subcommand if the test succeeds
-l | --loose Execute subcommand even if the test times out
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
USAGE
Expand All @@ -39,27 +41,19 @@ test_connection() {
}

wait_for() {
local result
for i in `seq $TIMEOUT` ; do
# use a 1-second timeout, but still sleep 0.1 seconds after just to be safe
test_connection "$HOST" "$PORT"

result=$?
if [ $result -eq 0 ] ; then
if [ $# -gt 0 ] ; then
TIMEOUT=$OLD_TIMEOUT QUIET=$OLD_QUIET PORT=$OLD_PORT HOST=$OLD_HOST exec "$@"
fi
exit 0
fi
if [ $result -eq 0 ] ; then break ; fi
sleep 0.1
done
echo "Operation timed out" >&2
if [ $result -ne 0 ] && [ $STRICT -ne 1 ] ; then
if [ $# -gt 0 ] ; then
exec "$@"
fi
exit 0
[ $result -ne 0 ] && echo "Operation timed out" >&2
if [ $result -eq 0 -o $LOOSE -eq 1 -a $# -gt 0 ] ; then
TIMEOUT=$OLD_TIMEOUT QUIET=$OLD_QUIET PORT=$OLD_PORT HOST=$OLD_HOST LOOSE=$OLD_LOOSE exec "$@"
fi
exit 1
exit $result
}

while [ $# -gt 0 ]
Expand All @@ -74,8 +68,8 @@ do
QUIET=1
shift 1
;;
-s | --strict)
STRICT=1
-l | --loose)
LOOSE=1
shift 1
;;
-t)
Expand Down Expand Up @@ -106,6 +100,4 @@ if [ "$HOST" = "" -o "$PORT" = "" ]; then
usage 2
fi

STRICT=${STRICT:-0}

wait_for "$@"
26 changes: 21 additions & 5 deletions wait-for.bats
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,27 @@
[ "$output" != "success" ]
}

@test "preserve existing environment variable" {
HOST=myweb.com
PORT=8080
@test "nonexistent server should start command if loose option is specified" {
run ./wait-for -t 1 -l noserver:9999 -- echo 'passable' 2>&1

[ "$status" -eq 0 ]

[ "${lines[0]}" = "Operation timed out" ]
[ "${lines[1]}" = "passable" ]
}

@test "preserve existing environment variables" {
TIMEOUT=mytimeout
QUIET=myquiet
HOST=myhost
PORT=myport
LOOSE=myloose

run ./wait-for google.com:80 -- echo 'success'

[ "$(echo $HOST)" = 'myweb.com' ]
[ "$(echo $PORT)" = '8080' ]
[ "$(echo $TIMEOUT)" = 'mytimeout' ]
[ "$(echo $QUIET)" = 'myquiet' ]
[ "$(echo $HOST)" = 'myhost' ]
[ "$(echo $PORT)" = 'myport' ]
[ "$(echo $LOOSE)" = 'myloose' ]
}

0 comments on commit 8d2abc1

Please sign in to comment.