Skip to content

Commit

Permalink
Add tests for option flags
Browse files Browse the repository at this point in the history
Based on the flags listed in the current README, add tests for their
functionality.
  • Loading branch information
sds committed Oct 29, 2016
1 parent 19a0a0d commit 560a8ff
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Exclude all files from the Docker build context by default.
# This speeds up the start time of any `dock` command.
#
# Whitelist files/patterns for inclusion by prepending "!" to them.
*
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ it works.
```bash
git clone git://github.com/brigade/dock
cd dock
dock
bin/dock # If you have installed Dock then you can just run `dock`
```

After running `dock` inside the Dock repository, you should be running inside
a Docker container. Your current directory will be `/repo` inside that
container, and the contents of that directory will be the Dock repository
itself.
itself (i.e. the current project).

```
$ pwd
Expand All @@ -64,7 +64,7 @@ Dockerfile LICENSE.md README.md ...
Any changes you make to these files will automatically be reflected in the
original repository, and vice versa. This allows you to continue using your
favorite tools and editors to make changes to your project, but actually
run code or tests _inside_ the container so it is isolated from the rest of
run code or tests _inside_ the container to isolate these from the rest of
your system.

## Usage
Expand Down
41 changes: 41 additions & 0 deletions run-tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
#
# Runs all tests.
#
# Specify a path to run only that test file.

start_docker() {
# Don't do anything if daemon already running
if docker info >/dev/null 2>&1; then
return
fi

sudo dockerd >/dev/null 2>&1 &
dockerd_pid=$!

local max_tries=5
for i in {1..5}; do
if docker info >/dev/null 2>&1; then
break
fi
echo "Waiting for Docker daemon to start ($i/5)..." >&2
sleep 1
done

if ! docker info >/dev/null 2>&1; then
echo "Docker daemon failed to start!" >&2
return 1
fi
}

stop_docker() {
echo "Shutting down Docker daemon..." >&2
sudo kill $dockerd_pid 2>&1 >/dev/null || true
wait $dockerd_pid
}

trap "stop_docker" EXIT INT QUIT TERM
start_docker
unset INSIDE_DOCK

bats "$@"
37 changes: 37 additions & 0 deletions test/options/attach.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/local/bin/dock bats

load ../utils

project_name=my-project

setup() {
destroy-all-containers
original_dir="$(pwd)"
cd "$(create_repo ${project_name})"
}

teardown() {
cd "${original_dir}"
}

@test "attaching when container is already running" {
docker run --detach --interactive --name "${project_name}-dock" alpine:latest \
sleep 5
run dock -a
# 137 status means we were killed by the container exiting
[ "$status" -eq 137 ]
}

@test "attaching when container exists but is not running" {
docker run --name "${project_name}-dock" alpine:latest echo
run dock -a
[ "$status" -eq 1 ]
[[ "${lines[0]}" =~ "Container ${project_name}-dock exists but is not running, so you can't attach" ]]
}

@test "attaching when no container exists" {
run dock -a
[ "$status" -eq 1 ]
[[ "${lines[0]}" =~ "No container named ${project_name}-dock is currently running." ]]
[[ "${lines[1]}" =~ "You must start the container first before you can attach!" ]]
}
24 changes: 24 additions & 0 deletions test/options/config.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/local/bin/dock bats

load ../utils

project_name=my-project

setup() {
destroy-all-containers
original_dir="$(pwd)"
cd "$(create_repo ${project_name})"
echo "image=alpine:latest" >> .dock
echo "command='exit 1'" >> .dock
}

teardown() {
cd "${original_dir}"
}

@test "specifying an explicit config file" {
echo "image=alpine:latest" >> .other-dock
echo "command=echo" >> .other-dock
run dock -d -c .other-dock sh
[ "$status" -eq 0 ]
}
22 changes: 22 additions & 0 deletions test/options/detach.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/local/bin/dock bats

load ../utils

project_name=my-project

setup() {
destroy-all-containers
original_dir="$(pwd)"
cd "$(create_repo ${project_name})"
echo "image=alpine:latest" > .dock
}

teardown() {
cd "${original_dir}"
}

@test "running Dock container as detached" {
run dock -d sh
[ "$status" -eq 0 ]
container-running "${project_name}-dock"
}
23 changes: 23 additions & 0 deletions test/options/force.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/local/bin/dock bats

load ../utils

project_name=my-project

setup() {
destroy-all-containers
original_dir="$(pwd)"
cd "$(create_repo ${project_name})"
echo "image=alpine:latest" > .dock
}

teardown() {
cd "${original_dir}"
}

@test "forcibly removing existing Dock container" {
container_id="$(dock -d sh)"
run dock -f echo
[ "$status" -eq 0 ]
! container-running "$container_id"
}
23 changes: 23 additions & 0 deletions test/utils.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Defines helper functions used across a variety of tests.

create_repo() {
local dir_name="${1:-}"
if [ -z "${dir_name}" ]; then
repo_path="$(mktemp --directory)"
else
repo_path="$(mktemp --directory)/${dir_name}"
fi

mkdir -p "${repo_path}"
git init "${repo_path}" >/dev/null 2>&1

echo ${repo_path}
}

destroy-all-containers() {
docker ps -aq | xargs --no-run-if-empty docker rm --force
}

container-running() {
[ "$(docker inspect --format={{.State.Status}} "$1")" = running ]
}

0 comments on commit 560a8ff

Please sign in to comment.