From bcaf84ed35dd0a7d59e9af596aed50e15837ae0d Mon Sep 17 00:00:00 2001 From: Marko Klemetti Date: Wed, 3 May 2017 11:52:29 +0300 Subject: [PATCH] Add testing using bats --- .gitignore | 5 +++++ Dockerfile | 11 +++++++++++ README.md | 9 ++++++++- package.json | 5 +++++ wait-for | 3 ++- wait-for.bats | 14 ++++++++++++++ 6 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 package.json create mode 100644 wait-for.bats diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f3b8994 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# OSX +.DS_Store + +# Node +node_modules diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..412d8e1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM node:alpine + +RUN apk add --no-cache bash + +RUN mkdir -p /app +WORKDIR /app + +COPY . /app +RUN npm install + +CMD ./node_modules/.bin/bats wait-for.bats diff --git a/README.md b/README.md index 6d5ee0a..02d8d1d 100644 --- a/README.md +++ b/README.md @@ -39,4 +39,11 @@ services: command: sh -c './wait-for db:5432 -- npm start' depends_on: - db -``` \ No newline at end of file +``` + +## 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)). + + docker build -t wait-for . + docker run -t wait-for \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..d607845 --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "bats": "^0.4.2" + } +} diff --git a/wait-for b/wait-for index 315e307..077a8d3 100755 --- a/wait-for +++ b/wait-for @@ -22,7 +22,8 @@ USAGE wait_for() { command="$*" for i in `seq $TIMEOUT` ; do - nc -z "$HOST" "$PORT" + nc -z "$HOST" "$PORT" > /dev/null 2>&1 + result=$? if [ $result -eq 0 ] ; then if [ -n "$command" ] ; then diff --git a/wait-for.bats b/wait-for.bats new file mode 100644 index 0000000..cbea6a4 --- /dev/null +++ b/wait-for.bats @@ -0,0 +1,14 @@ +#!/usr/bin/env bats + +@test "google should be immediately found" { + run ./wait-for google.com:80 -- echo 'success' + + [ "$output" = "success" ] +} + +@test "nonexistent server should not start command" { + run ./wait-for -t 1 noserver:9999 -- echo 'success' + + [ "$status" -ne 0 ] + [ "$output" != "success" ] +}