From 560a8ff3a9d6830be39d978a1519cf1a34391a40 Mon Sep 17 00:00:00 2001 From: Shane da Silva Date: Sat, 29 Oct 2016 01:26:25 -0400 Subject: [PATCH] Add tests for option flags Based on the flags listed in the current README, add tests for their functionality. --- .dockerignore | 5 +++++ README.md | 6 +++--- run-tests | 41 ++++++++++++++++++++++++++++++++++++++++ test/options/attach.bats | 37 ++++++++++++++++++++++++++++++++++++ test/options/config.bats | 24 +++++++++++++++++++++++ test/options/detach.bats | 22 +++++++++++++++++++++ test/options/force.bats | 23 ++++++++++++++++++++++ test/utils.bash | 23 ++++++++++++++++++++++ 8 files changed, 178 insertions(+), 3 deletions(-) create mode 100644 .dockerignore create mode 100755 run-tests create mode 100644 test/options/attach.bats create mode 100644 test/options/config.bats create mode 100644 test/options/detach.bats create mode 100644 test/options/force.bats create mode 100644 test/utils.bash diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..8d7f738 --- /dev/null +++ b/.dockerignore @@ -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. +* diff --git a/README.md b/README.md index e0aca7d..6c50f00 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/run-tests b/run-tests new file mode 100755 index 0000000..a17297b --- /dev/null +++ b/run-tests @@ -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 "$@" diff --git a/test/options/attach.bats b/test/options/attach.bats new file mode 100644 index 0000000..493a598 --- /dev/null +++ b/test/options/attach.bats @@ -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!" ]] +} diff --git a/test/options/config.bats b/test/options/config.bats new file mode 100644 index 0000000..176e3e5 --- /dev/null +++ b/test/options/config.bats @@ -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 ] +} diff --git a/test/options/detach.bats b/test/options/detach.bats new file mode 100644 index 0000000..357a33b --- /dev/null +++ b/test/options/detach.bats @@ -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" +} diff --git a/test/options/force.bats b/test/options/force.bats new file mode 100644 index 0000000..cbfcd0e --- /dev/null +++ b/test/options/force.bats @@ -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" +} diff --git a/test/utils.bash b/test/utils.bash new file mode 100644 index 0000000..d1f70f2 --- /dev/null +++ b/test/utils.bash @@ -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 ] +}