Add testing using bats

This commit is contained in:
Marko Klemetti 2017-05-03 11:52:29 +03:00
parent 1168f11eab
commit bcaf84ed35
6 changed files with 45 additions and 2 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
# OSX
.DS_Store
# Node
node_modules

11
Dockerfile Normal file
View File

@ -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

View File

@ -39,4 +39,11 @@ services:
command: sh -c './wait-for db:5432 -- npm start'
depends_on:
- db
```
```
## 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

5
package.json Normal file
View File

@ -0,0 +1,5 @@
{
"dependencies": {
"bats": "^0.4.2"
}
}

View File

@ -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

14
wait-for.bats Normal file
View File

@ -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" ]
}