Skip to content

Commit

Permalink
Merge pull request #222 from pecigonzalo/fix/rm-and-logs
Browse files Browse the repository at this point in the history
Handle single container and --rm configuration
  • Loading branch information
lox committed May 7, 2019
2 parents 8eeb47d + 95348a5 commit 40424b7
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 23 deletions.
12 changes: 8 additions & 4 deletions commands/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,14 @@ if [[ -n "${BUILDKITE_AGENT_ACCESS_TOKEN:-}" ]] ; then
if [[ "$(plugin_read_config CHECK_LINKED_CONTAINERS "true")" != "false" ]] ; then

# Get list of failed containers
failed_containers=($(
docker inspect -f '{{if ne 0 .State.ExitCode}}{{.Name}}.{{.State.ExitCode}}{{ end }}' \
$(docker_ps_by_project -q)
))
containers=($(docker_ps_by_project -q))
failed_containers=()
if [[ 0 != "${#containers[@]}" ]] ; then
failed_containers=($(
docker inspect -f '{{if ne 0 .State.ExitCode}}{{.Name}}.{{.State.ExitCode}}{{ end }}' \
${containers[@]}
))
fi

if [[ 0 != "${#failed_containers[@]}" ]] ; then
echo "+++ :warning: Some containers had non-zero exit codes"
Expand Down
6 changes: 4 additions & 2 deletions lib/run.bash
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ check_linked_containers_and_save_logs() {
[[ -d "$logdir" ]] && rm -rf "$logdir"
mkdir -p "$logdir"

while read -r line ; do
containers=$(docker_ps_by_project --format '{{.ID}}\t{{.Label "com.docker.compose.service"}}')
IFS=$'\n'
for line in ${containers} ; do
if [[ -z "${line}" ]]; then
# Skip empty lines
continue
Expand All @@ -48,7 +50,7 @@ check_linked_containers_and_save_logs() {
plugin_prompt_and_run docker logs --timestamps --tail 5 "$service_container_id"
docker logs -t "$service_container_id" &> "${logdir}/${service_name}.log"
fi
done <<< "$(docker_ps_by_project --format '{{.ID}}\t{{.Label "com.docker.compose.service"}}')"
done
}

# docker-compose's -v arguments don't do local path expansion like the .yml
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/id-multiple-services.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
456456
789789
2 changes: 2 additions & 0 deletions tests/fixtures/id-service-multiple-services.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
456456 failed-other
789789 working-other
Empty file.
2 changes: 2 additions & 0 deletions tests/fixtures/service-id-exit-multiple-services-failed.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
failed-other 456456 1
working-other 789789 0
2 changes: 2 additions & 0 deletions tests/fixtures/service-id-exit-multiple-services-succeded.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
failed-other 456456 0
working-other 789789 0
75 changes: 58 additions & 17 deletions tests/output.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ load '../lib/run'
# export BATS_MOCK_TMPDIR=$PWD


@test "Detect some failed containers" {
@test "Logs: Detect some containers KO" {
export BUILDKITE_AGENT_ACCESS_TOKEN="123123"
export BUILDKITE_JOB_ID=1111
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice
Expand All @@ -24,22 +24,23 @@ load '../lib/run'


stub buildkite-agent \
"meta-data get docker-compose-plugin-built-image-tag-myservice : exit 1" \
"meta-data exists docker-compose-plugin-built-image-tag-myservice : exit 1" \
"artifact upload : exit 0"

stub docker-compose \
"-f docker-compose.yml -p buildkite1111 build --pull myservice : echo built myservice" \
"-f docker-compose.yml -p buildkite1111 up -d --scale myservice=0 : echo ran myservice dependencies" \
"-f docker-compose.yml -p buildkite1111 run --name buildkite1111_myservice_build_1 myservice echo 'hello world' : echo ran myservice"
"-f docker-compose.yml -p buildkite1111 run --name buildkite1111_myservice_build_1 --rm myservice echo 'hello world' : echo ran myservice"

stub docker \
"ps -a --filter label=com.docker.compose.project=buildkite1111 -q : echo 123123" \
"inspect -f {{if\ ne\ 0\ .State.ExitCode}}{{.Name}}.{{.State.ExitCode}}{{\ end\ }} 123123 : echo 123123.1" \
"ps -a --filter label=com.docker.compose.project=buildkite1111 --format : echo 123123 1" \
"ps -a --filter label=com.docker.compose.project=buildkite1111 --format : echo 123123 myservice" \
"inspect --format={{.State.ExitCode}} 123123\ myservice : echo 1" \
"logs : exit 0" \
"logs : exit 0"
"ps -a --filter label=com.docker.compose.project=buildkite1111 -q : cat tests/fixtures/id-multiple-services.txt" \
"inspect -f {{if\ ne\ 0\ .State.ExitCode}}{{.Name}}.{{.State.ExitCode}}{{\ end\ }} 456456 : echo 456456.1" \
"ps -a --filter label=com.docker.compose.project=buildkite1111 --format : cat tests/fixtures/service-id-exit-multiple-services-failed.txt" \
"ps -a --filter label=com.docker.compose.project=buildkite1111 --format : cat tests/fixtures/id-service-multiple-services.txt" \
"inspect --format={{.State.ExitCode}} 456456 : echo 1" \
"logs --timestamps --tail 5 456456 : exit 0" \
"logs -t 456456 : exit 0" \
"inspect --format={{.State.ExitCode}} 789789 : echo 0"

run $PWD/hooks/command

Expand All @@ -52,7 +53,7 @@ load '../lib/run'
unstub docker
}

@test "Detect no failed containers" {
@test "Logs: Detect all containers OK" {
export BUILDKITE_AGENT_ACCESS_TOKEN="123123"
export BUILDKITE_JOB_ID=1111
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice
Expand All @@ -66,18 +67,19 @@ load '../lib/run'


stub buildkite-agent \
"meta-data get docker-compose-plugin-built-image-tag-myservice : exit 1" \
"meta-data exists docker-compose-plugin-built-image-tag-myservice : exit 1" \

stub docker-compose \
"-f docker-compose.yml -p buildkite1111 build --pull myservice : echo built myservice" \
"-f docker-compose.yml -p buildkite1111 up -d --scale myservice=0 : echo ran myservice dependencies" \
"-f docker-compose.yml -p buildkite1111 run --name buildkite1111_myservice_build_1 myservice echo 'hello world' : echo ran myservice"
"-f docker-compose.yml -p buildkite1111 run --name buildkite1111_myservice_build_1 --rm myservice echo 'hello world' : echo ran myservice"

stub docker \
"ps -a --filter label=com.docker.compose.project=buildkite1111 -q : echo 123123" \
"inspect -f {{if\ ne\ 0\ .State.ExitCode}}{{.Name}}.{{.State.ExitCode}}{{\ end\ }} 123123 : " \
"ps -a --filter : echo myservice 123123 0" \
"inspect --format={{.State.ExitCode}} myservice\ 123123\ 0 : echo 0"
"ps -a --filter label=com.docker.compose.project=buildkite1111 -q : cat tests/fixtures/id-multiple-services.txt" \
"inspect -f {{if\ ne\ 0\ .State.ExitCode}}{{.Name}}.{{.State.ExitCode}}{{\ end\ }} 456456 789789 : echo" \
"ps -a --filter : cat tests/fixtures/id-service-multiple-services.txt" \
"inspect --format={{.State.ExitCode}} 456456 : echo 0" \
"inspect --format={{.State.ExitCode}} 789789 : echo 0"

run $PWD/hooks/command

Expand All @@ -89,3 +91,42 @@ load '../lib/run'
unstub docker-compose
unstub buildkite-agent
}

@test "Logs: Skip output if there are no containers for a project" {
# This covers the case when you have a single container being ran with `--rm` which
# already outputs its logs to the console and given there are no other containers
# we sohuld not try to get the logs or inspect them
export BUILDKITE_AGENT_ACCESS_TOKEN="123123"
export BUILDKITE_JOB_ID=1111
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice
export BUILDKITE_PIPELINE_SLUG=test
export BUILDKITE_BUILD_NUMBER=1
export BUILDKITE_COMMAND=""
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_COMMAND_0=echo
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_COMMAND_1="hello world"
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CHECK_LINKED_CONTAINERS=true
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLEANUP=false


stub buildkite-agent \
"meta-data exists docker-compose-plugin-built-image-tag-myservice : exit 1" \

stub docker-compose \
"-f docker-compose.yml -p buildkite1111 build --pull myservice : echo built myservice" \
"-f docker-compose.yml -p buildkite1111 up -d --scale myservice=0 : echo ran myservice dependencies" \
"-f docker-compose.yml -p buildkite1111 run --name buildkite1111_myservice_build_1 --rm myservice echo 'hello world' : echo ran myservice"

stub docker \
"ps -a --filter label=com.docker.compose.project=buildkite1111 -q : echo" \
"ps -a --filter : cat tests/fixtures/id-service-no-services.txt"

run $PWD/hooks/command

assert_success
assert_output --partial "built myservice"
assert_output --partial "ran myservice"
refute_output --partial "Uploading linked container logs"
unstub docker
unstub docker-compose
unstub buildkite-agent
}

0 comments on commit 40424b7

Please sign in to comment.