Skip to content

Add support for HTTP status check with wget #14

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

Closed
wants to merge 1 commit into from
Closed
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
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

When using this tool, you only need to pick the `wait-for` file as part of your project.

It uses `netcat` for testing ports, and `wget` for HTTP status.

[![Build Status](https://travis-ci.org/eficode/wait-for.svg?branch=master)](https://travis-ci.org/eficode/wait-for)

## Usage

```
./wait-for host:port [-t timeout] [-- command args]
./wait-for host:port|url [-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 @@ -24,12 +26,14 @@ $ ./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

$ ./wait-for http://www.eficode.com:80/ping -- echo "Eficode site is up"
```

To wait for database container to become available:


```
```yml
version: '2'

services:
Expand All @@ -43,6 +47,27 @@ services:
- db
```

To wait for your API service to become available:


```yml
version: '2'

services:
db:
image: postgres:9.4

api:
image: my_awesome_api

tests:
build: tests
command: sh -c './wait-for http://api/ping -- jest test'
depends_on:
- db
- api
```

## 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
40 changes: 33 additions & 7 deletions wait-for
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/bin/sh

TIMEOUT=15
TIMEOUT=150
QUIET=0
WAITFOR_RETRY_DELAY=0.1

echoerr() {
if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
Expand All @@ -11,7 +12,7 @@ usage() {
exitcode="$1"
cat << USAGE >&2
Usage:
$cmdname host:port [-t timeout] [-- command args]
$cmdname host:port|url [-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 @@ -22,15 +23,32 @@ USAGE
wait_for() {
for i in `seq $TIMEOUT` ; do
nc -z "$HOST" "$PORT" > /dev/null 2>&1


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

wait_for_http() {
for i in `seq $TIMEOUT` ; do
wget --timeout=1 -q "$WAITFOR_URL" -O /dev/null > /dev/null 2>&1

result=$?
if [ $result -eq 0 ] ; then
if [ $# -gt 0 ] ; then
exec "$@"
fi
exit 0
fi
sleep 1
sleep $WAITFOR_RETRY_DELAY
done
echo "Operation timed out" >&2
exit 1
Expand All @@ -39,6 +57,10 @@ wait_for() {
while [ $# -gt 0 ]
do
case "$1" in
http*)
WAITFOR_URL=$1
shift 1
;;
*:* )
HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
Expand Down Expand Up @@ -71,9 +93,13 @@ do
esac
done

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

wait_for "$@"
if [ "$WAITFOR_URL" != "" ]; then
wait_for_http "$@"
else
wait_for "$@"
fi
22 changes: 21 additions & 1 deletion wait-for.bats
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

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


[ "$output" = "success" ]
}

@test "https://duckduckgo.com should be immediately found" {
run ./wait-for https://duckduckgo.com -- echo 'success'

[ "$output" = "success" ]
}

Expand All @@ -12,3 +18,17 @@
[ "$status" -ne 0 ]
[ "$output" != "success" ]
}

@test "connection error in HTTP test should not start command" {
run ./wait-for -t 1 http://google.com:8080 -- echo 'success'

[ "$status" -ne 0 ]
[ "$output" != "success" ]
}

@test "not found HTTP status should not start command" {
run ./wait-for -t 1 http://google.com/ping -- echo 'success'

[ "$status" -ne 0 ]
[ "$output" != "success" ]
}