-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
132 additions
and
5 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 @@ | ||
services: | ||
tests: | ||
image: buildkite/plugin-tester:v4.1.1 | ||
volumes: | ||
- ".:/plugin" |
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 |
---|---|---|
@@ -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. |
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,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" | ||
} |