-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Based on the flags listed in the current README, add tests for their functionality.
- Loading branch information
Showing
8 changed files
with
178 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" ]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ] | ||
} |