Skip to content

Commit

Permalink
Add tests for promoteImage
Browse files Browse the repository at this point in the history
Testing calls to docker to promote images between ECRs are correct.

Not testing when there are multiple destinations as each docker push is
backgrounded and this leads to difficulties when stubbing out the calls.
  • Loading branch information
Andrew Macgregor committed Nov 6, 2024
1 parent 6767ffb commit ab82ffa
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 5 deletions.
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
tests:
image: buildkite/plugin-tester:v4.1.1
volumes:
- ".:/plugin"
24 changes: 22 additions & 2 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
## Testing this plugin

Follow the guidelines in the [Buildkite plugins documentation](https://buildkite.com/docs/plugins/writing#step-5-add-a-test). The easist way to run these tests is with Docker using this command:
Follow the guidelines in the [Buildkite plugins documentation](https://buildkite.com/docs/plugins/writing#step-5-add-a-test).

The easist way to run these tests is with Docker using this command in the top
level directory, where the docker-compose.yml file is.

```sh
docker run -it --rm -v "$PWD:/plugin:ro" buildkite/plugin-tester
docker compose run --rm tests
```

Take a look at the [plugin test documentation](https://github.com/buildkite-plugins/buildkite-plugin-tester) to find out how to use assertions, mocks etc.

### Debugging Stubs

This is explained in the docs as well but is very useful. If you are stubbing an
executable like docker, then you can debug it by adding a matching ENV variable
like this:

```
@test "cleans up image if it did not exist on host" {
stub docker \
"inspect \* \* : exit 1"
export DOCKER_STUB_DEBUG=3
```

When you run the tests you will see in the output exactly what is happening in the stub.
3 changes: 0 additions & 3 deletions tests/command.bats → tests/helmDeploy.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

load "$BATS_PLUGIN_PATH/load.bash"

# Uncomment the following line to debug stub failures
# export BUILDKITE_AGENT_STUB_DEBUG=/dev/tty

setup() {
export BUILDKITE_PLUGIN_K8S_DEPLOY_CHART="test-chart"
export BUILDKITE_PLUGIN_K8S_DEPLOY_CHART_VERSION="0.1.14"
Expand Down
105 changes: 105 additions & 0 deletions tests/promoteImage.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/usr/bin/env bats

load "$BATS_PLUGIN_PATH/load.bash"

setup() {
export BUILDKITE_PLUGIN_K8S_DEPLOY_SOURCEIMAGE_REPOSITORYURL="552375026182.dkr.ecr.ap-southeast-2.amazonaws.com"
export BUILDKITE_PLUGIN_K8S_DEPLOY_SOURCEIMAGE_IMAGE="website:test_tag"
export BUILDKITE_PLUGIN_K8S_DEPLOY_DESTINATIONIMAGES_0_REPOSITORYURL="587935219773.dkr.ecr.ap-southeast-2.amazonaws.com"
export BUILDKITE_PLUGIN_K8S_DEPLOY_DESTINATIONIMAGES_0_IMAGE="website:test_tag"
export BUILDKITE_PLUGIN_K8S_DEPLOY_DESTINATIONIMAGES_0_REGION="ap-southeast-2"
export BUILDKITE_PLUGIN_K8S_DEPLOY_ACTION="promoteImage"
}

teardown() {
unstub docker
}

@test "calls docker pull with the source image" {
stub docker \
"inspect \* \* : exit 0" \
"pull \* : echo \$2 > /tmp/docker-pull-image" \
"tag \* \* : exit 0" \
"push \* : exit 0" \
"rmi \* : exit 0" \

run "$PWD/hooks/command"

assert_success
pull_image="$(cat /tmp/docker-pull-image)"
assert_equal "${pull_image}" "552375026182.dkr.ecr.ap-southeast-2.amazonaws.com/website:test_tag"
}

@test "calls docker tag, push, rmi with correct destination" {
stub docker \
"inspect \* \* : exit 0" \
"pull \* : exit 0" \
"tag \* \* : echo \$2 > /tmp/docker_tag" \
"push \* : echo \$2 > /tmp/docker_push" \
"rmi \* : echo \$2 > /tmp/docker_rmi" \

run "$PWD/hooks/command"

assert_success
docker_tag="$(cat /tmp/docker_tag)"
docker_push="$(cat /tmp/docker_push)"
docker_rmi="$(cat /tmp/docker_rmi)"
assert_equal "${docker_tag}" "552375026182.dkr.ecr.ap-southeast-2.amazonaws.com/website:test_tag"
assert_equal "${docker_push}" "587935219773.dkr.ecr.ap-southeast-2.amazonaws.com/website:test_tag"
assert_equal "${docker_rmi}" "587935219773.dkr.ecr.ap-southeast-2.amazonaws.com/website:test_tag"
assert_output --partial "Image promotion success"
}

@test "promoteImage fails if image cannot be pulled" {
stub docker \
"inspect \* \* : exit 0" \
"pull \* : exit 1" \

run "$PWD/hooks/command"

assert_failure
assert_output --partial "Failed to pull source image"
}

@test "promoteImage fails if image cannot be pushed" {
stub docker \
"inspect \* \* : exit 0" \
"pull \* : exit 0" \
"tag \* \* : exit 0" \
"push \* : exit 1" \
"rmi \* : exit 0" \

run "$PWD/hooks/command"

assert_failure
assert_output --partial "Promotion failed"
}

@test "cleans up image if it did not exist on host" {
stub docker \
"inspect \* \* : exit 1" \
"pull \* : exit 0" \
"tag \* \* : exit 0" \
"push \* : exit 0" \
"rmi \* : exit 0" \
"image rm \* : echo 'image rm called'"

run "$PWD/hooks/command"

assert_success
assert_output --partial "image rm called"
}

@test "does not clean up image if it did exist on host" {
stub docker \
"inspect \* \* : exit 0" \
"pull \* : exit 0" \
"tag \* \* : exit 0" \
"push \* : exit 0" \
"rmi \* : exit 0"

run "$PWD/hooks/command"

assert_success
refute_output --partial "image rm called"
}

0 comments on commit ab82ffa

Please sign in to comment.