From 7a0de8aa98d738a4ef7f5117c4ba982837b4a2ac Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Wed, 24 Jan 2018 09:35:09 +1100 Subject: [PATCH 1/5] Add failing test for #75 --- tests/run.bats | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/run.bats b/tests/run.bats index ad540081..b64a6139 100644 --- a/tests/run.bats +++ b/tests/run.bats @@ -33,6 +33,31 @@ load '../lib/run' unstub buildkite-agent } +@test "Run without a prebuilt image with a complicated command" { + 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="sh -c 'echo hello world; pwd'" + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CHECK_LINKED_CONTAINERS=false + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLEANUP=false + + stub docker-compose \ + "-f docker-compose.yml -p buildkite1111 build --pull myservice : echo built myservice" \ + "-f docker-compose.yml -p buildkite1111 run --name buildkite1111_myservice_build_1 myservice $BUILDKITE_COMMAND : echo ran myservice" + + stub buildkite-agent \ + "meta-data get docker-compose-plugin-built-image-tag-myservice : exit 1" + + run $PWD/hooks/command + + assert_success + assert_output --partial "built myservice" + assert_output --partial "ran myservice" + unstub docker-compose + unstub buildkite-agent +} + @test "Run without a prebuilt image with custom env" { export BUILDKITE_JOB_ID=1111 export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice From b9bb095d1253891b6215496d7f018181e4aa2413 Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Wed, 24 Jan 2018 09:50:40 +1100 Subject: [PATCH 2/5] Fix for incorrect command escaping --- hooks/commands/run.sh | 22 ++++++++++------------ lib/shared.bash | 4 ++++ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/hooks/commands/run.sh b/hooks/commands/run.sh index 7a070d1f..5101ca86 100755 --- a/hooks/commands/run.sh +++ b/hooks/commands/run.sh @@ -52,28 +52,26 @@ done <<< "$(printf '%s\n%s' \ run_params+=("$service_name") -# append command tokens if there are any. We do this to avoid word splitting -# issues as discussed in https://github.com/koalaman/shellcheck/wiki/SC2207 -if [[ -n "${BUILDKITE_COMMAND:-}" ]] ; then - while IFS=$' \t\n' read -r -a line ; do - for token in "${line[@]}" ; do - run_params+=("$token") - done - done <<< "$BUILDKITE_COMMAND" -fi - ( + IFS= set +e + set -f + + # The eval statements below are used to allow $BUILDKITE_COMMAND to be interpolated correctly + # When paired with -f we ensure that it word splits correctly, e.g bash -c "pwd" should split + # into [bash, -c, "pwd"]. Eval ends up the simplest way to do this, and when paired with the + # set -f above we ensure globs aren't expanded (imagine a command like `cat *`, which bash would + # helpfully expand prior to passing it to docker-compose) if [[ -f "$override_file" ]]; then echo "+++ :docker: Running command in Docker Compose service: $service_name" >&2 - run_docker_compose "${run_params[@]}" + eval "run_docker_compose \${run_params[@]} $BUILDKITE_COMMAND" else echo "~~~ :docker: Building Docker Compose Service: $service_name" >&2 run_docker_compose build --pull "$service_name" echo "+++ :docker: Running command in Docker Compose service: $service_name" >&2 - run_docker_compose "${run_params[@]}" + eval "run_docker_compose \${run_params[@]} $BUILDKITE_COMMAND" fi ) diff --git a/lib/shared.bash b/lib/shared.bash index 94fed8f7..46e33504 100644 --- a/lib/shared.bash +++ b/lib/shared.bash @@ -143,6 +143,10 @@ function run_docker_compose() { command+=(-p "$(docker_compose_project_name)") + for token in "${@}" ; do + echo "token[$token]" + done + plugin_prompt_and_run "${command[@]}" "$@" } From 6b2df597b1702b47e46737ddb069f07654d9cead Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Wed, 24 Jan 2018 10:04:12 +1100 Subject: [PATCH 3/5] Swap to buildkite plugin-tester image for patched bats-mock --- Dockerfile | 14 -------------- docker-compose.yml | 4 ++-- 2 files changed, 2 insertions(+), 16 deletions(-) delete mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 9b8e6cc4..00000000 --- a/Dockerfile +++ /dev/null @@ -1,14 +0,0 @@ -FROM lucor/bats - -ENV LIBS_BATS_MOCK_VERSION "1.0.1" -RUN mkdir -p /usr/local/lib/bats/bats-mock \ - && curl -sSL https://github.com/jasonkarns/bats-mock/archive/v$LIBS_BATS_MOCK_VERSION.tar.gz -o /tmp/bats-mock.tgz \ - && tar -zxf /tmp/bats-mock.tgz -C /usr/local/lib/bats/bats-mock --strip 1 \ - && printf 'source "%s"\n' "/usr/local/lib/bats/bats-mock/stub.bash" >> /usr/local/lib/bats/load.bash \ - && rm -rf /tmp/bats-mock.tgz - -RUN apk --no-cache add ncurses bc - -WORKDIR /app -ENTRYPOINT ["/usr/local/bin/bats"] -CMD ["tests/"] diff --git a/docker-compose.yml b/docker-compose.yml index e11e403e..8023abd9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,6 @@ version: '2' services: tests: - build: . + image: buildkite/plugin-tester volumes: - - .:/app + - ".:/plugin" From 9e680c9e047f14c686d8de9fcc2256d02defe2bb Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Wed, 24 Jan 2018 10:04:44 +1100 Subject: [PATCH 4/5] Remove debugging code --- lib/shared.bash | 4 ---- tests/run.bats | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/shared.bash b/lib/shared.bash index 46e33504..94fed8f7 100644 --- a/lib/shared.bash +++ b/lib/shared.bash @@ -143,10 +143,6 @@ function run_docker_compose() { command+=(-p "$(docker_compose_project_name)") - for token in "${@}" ; do - echo "token[$token]" - done - plugin_prompt_and_run "${command[@]}" "$@" } diff --git a/tests/run.bats b/tests/run.bats index b64a6139..6934ffdc 100644 --- a/tests/run.bats +++ b/tests/run.bats @@ -38,7 +38,7 @@ load '../lib/run' export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice export BUILDKITE_PIPELINE_SLUG=test export BUILDKITE_BUILD_NUMBER=1 - export BUILDKITE_COMMAND="sh -c 'echo hello world; pwd'" + export BUILDKITE_COMMAND="sh -c 'echo hello world'" export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CHECK_LINKED_CONTAINERS=false export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLEANUP=false From c36c50db16507e2bfabac7fc4484d77a717c9be1 Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Wed, 24 Jan 2018 10:17:08 +1100 Subject: [PATCH 5/5] Use default command for plugin-tester --- .buildkite/pipeline.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.buildkite/pipeline.sh b/.buildkite/pipeline.sh index f287dc07..28e9ae86 100755 --- a/.buildkite/pipeline.sh +++ b/.buildkite/pipeline.sh @@ -19,7 +19,6 @@ steps: command: .buildkite/steps/shellcheck - label: run bats tests - command: tests/ plugins: ${BUILDKITE_REPO}#${commit}: run: tests @@ -114,4 +113,4 @@ steps: push: helloworld config: tests/composefiles/docker-compose.v2.1.yml -YAML \ No newline at end of file +YAML