Skip to content

Commit

Permalink
Merge pull request #101 from simpsonjulian/huge-junits-2
Browse files Browse the repository at this point in the history
Don't send annotations if they're too large for the API
  • Loading branch information
toolmantim authored Apr 8, 2019
2 parents edecfa5 + 5f82e5b commit a9941d4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ the build to fail.

## Developing

To test the junit parser (in Ruby) and plugin hooks (in Bash):
To test the plugin hooks (in Bash) and the junit parser (in Ruby):

```bash
docker-compose run --rm plugin &&
Expand Down
14 changes: 14 additions & 0 deletions hooks/command
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
set -euo pipefail

PLUGIN_DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)/.."
MAX_SIZE=100 # in KB

echo "--- :junit: Download the junits"

Expand All @@ -15,6 +16,12 @@ function cleanup {
rm -rf "${annotation_dir}"
}

function check_size {
local size_in_kb
size_in_kb=$(du -k "${annotation_path}" | cut -f 1)
[ "${size_in_kb}" -lt "${MAX_SIZE}" ]
}

trap cleanup EXIT

buildkite-agent artifact download \
Expand All @@ -37,6 +44,13 @@ docker \
cat "$annotation_path"

if grep -q "<details>" "$annotation_path"; then

if ! check_size; then
echo "--- :warning: Failures too large to annotate"
echo "The failures are too large to create a build annotation. Please inspect the failed JUnit artifacts manually."
exit 0
fi

echo "--- :buildkite: Creating annotation"
# shellcheck disable=SC2002
cat "$annotation_path" | buildkite-agent annotate --context junit --style error
Expand Down
30 changes: 30 additions & 0 deletions tests/command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ load "$BATS_PATH/load.bash"
# export MKTEMP_STUB_DEBUG=/dev/tty
# export BUILDKITE_AGENT_STUB_DEBUG=/dev/tty
# export DOCKER_STUB_DEBUG=/dev/tty
# export DU_STUB_DEBUG=/dev/tty

@test "runs the annotator and creates the annotation" {
export BUILDKITE_PLUGIN_JUNIT_ANNOTATE_ARTIFACTS="junits/*.xml"
Expand Down Expand Up @@ -118,3 +119,32 @@ load "$BATS_PATH/load.bash"

assert_output --partial "BUILDKITE_PLUGIN_JUNIT_ANNOTATE_ARTIFACTS: unbound variable"
}

@test "fails if the annotation is larger than " {
export BUILDKITE_PLUGIN_JUNIT_ANNOTATE_ARTIFACTS="junits/*.xml"

artifacts_tmp="tests/tmp/$PWD/junit-artifacts"
annotation_tmp="tests/tmp/$PWD/junit-annotation"

stub mktemp \
"-d junit-annotate-plugin-artifacts-tmp.XXXXXXXXXX : mkdir -p $artifacts_tmp; echo $artifacts_tmp" \
"-d junit-annotate-plugin-annotation-tmp.XXXXXXXXXX : mkdir -p $annotation_tmp; echo $annotation_tmp"

# 1kb over the 100k size limit of annotations
stub du "-k /plugin/tests/tmp//plugin/junit-annotation/annotation.md : echo 101 /plugin/tests/tmp//plugin/junit-annotation/annotation.md"

stub buildkite-agent "artifact download junits/*.xml /plugin/tests/tmp//plugin/junit-artifacts : echo Downloaded artifacts"

stub docker "--log-level error run --rm --volume /plugin/tests/tmp//plugin/junit-artifacts:/junits --volume /plugin/hooks/../ruby:/src --env BUILDKITE_PLUGIN_JUNIT_ANNOTATE_JOB_UUID_FILE_PATTERN= --env BUILDKITE_PLUGIN_JUNIT_ANNOTATE_FAILURE_FORMAT= ruby:2.5-alpine /src/bin/annotate /junits : echo '<details>Failure</details>'"

run "$PWD/hooks/command"

assert_success

assert_output --partial "Failures too large to annotate"

unstub docker
unstub buildkite-agent
unstub du
unstub mktemp
}

0 comments on commit a9941d4

Please sign in to comment.