Skip to content

Commit

Permalink
Merge pull request #1 from buildkite-plugins/add-tests-and-fix-git
Browse files Browse the repository at this point in the history
Fix version check and complete tests
  • Loading branch information
toolmantim authored Jun 8, 2022
2 parents 0b510f0 + bdeba0a commit 7bc5875
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 106 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Test Collector Buildkite Plugin (Alpha)
# Test Collector Buildkite Plugin (WIP)

A Buildkite plugin for uploading [JSON](https://buildkite.com/docs/test-analytics/importing-json) or [JUnit](https://buildkite.com/docs/test-analytics/importing-junit-xml) files to [Buildkite Test Analytics](https://buildkite.com/test-analytics)

Expand All @@ -13,7 +13,7 @@ steps:
- label: "🔨 Test"
command: "make test"
plugins:
- test-collector#main:
- test-collector#v0.0.1:
files: "test/junit-*.xml"
format: "junit"
```
Expand All @@ -27,7 +27,7 @@ steps:
- label: "🔨 Test"
command: "make test"
plugins:
- test-collector#main:
- test-collector#v0.0.1:
files: "test-data-*.json"
format: "json"
```
Expand Down
8 changes: 4 additions & 4 deletions hooks/pre-exit
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ FORMAT="${BUILDKITE_PLUGIN_TEST_COLLECTOR_FORMAT:-}"

TIMEOUT="${BUILDKITE_PLUGIN_TEST_COLLECTOR_TIMEOUT:-30}"

DEBUG="false"

if [[ "${BUILDKITE_PLUGIN_TEST_COLLECTOR_DEBUG:-}" =~ ^(true|on|1|always)$ ]]; then
DEBUG="true"
elif [[ "${BUILDKITE_ANALYTICS_DEBUG_ENABLED:-}" =~ ^(true|on|1|always)$ ]]; then
DEBUG="true"
else
DEBUG="false"
fi

TOKEN_VALUE=$(eval "echo \${$TOKEN_ENV_NAME:-}")
PLUGIN_VERSION=$( (which git && git rev-parse --short HEAD) || echo "" )
PLUGIN_VERSION=$(git rev-parse --short HEAD 2>/dev/null || echo "")

# Uploads files to the Test Analytics API
#
Expand Down Expand Up @@ -83,7 +83,7 @@ if [[ -z "${FORMAT}" ]]; then
exit 1
fi

readarray -d '' matching_files < <(find . -path "./${FILES_PATTERN}" -print0)
readarray -d '' matching_files < <(find . -path "./${FILES_PATTERN}" -print0 | sort -z)

if [[ "${#matching_files[@]}" -eq "0" ]]; then
echo "No files found matching '${FILES_PATTERN}'"
Expand Down
23 changes: 23 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test Collector Buildkite Plugin
description: ...
author: https://github.com/buildkite
requirements:
- docker
configuration:
properties:
files:
type: string
artifacts:
type: string
format:
enum:
- json
- junit
api-token-env-name:
type: string
oneOf:
- required:
- files
- required:
- artifacts
additionalProperties: false
48 changes: 48 additions & 0 deletions tests/pre-exit-errors.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bats

setup() {
load "$BATS_PLUGIN_PATH/load.bash"
export BUILDKITE_PLUGIN_TEST_COLLECTOR_API_TOKEN_ENV_NAME=""
}

@test "Errors with no token set" {
run "$PWD/hooks/pre-exit"

assert_failure
assert_output --partial "Missing BUILDKITE_ANALYTICS_TOKEN environment variable"
}

@test "Errors with no token set w/ custom env name" {
export BUILDKITE_PLUGIN_TEST_COLLECTOR_API_TOKEN_ENV_NAME="CUSTOM_TOKEN_ENV_NAME"
run "$PWD/hooks/pre-exit"

assert_failure
assert_output --partial "Missing CUSTOM_TOKEN_ENV_NAME environment variable"
}

@test "Errors with no 'files' set" {
export BUILDKITE_ANALYTICS_TOKEN='abc'
run "$PWD/hooks/pre-exit"

assert_failure
assert_output --partial "Missing file upload pattern 'files', e.g. 'junit-*.xml'"
}

@test "Errors with no 'format' set" {
export BUILDKITE_ANALYTICS_TOKEN='abc'
export BUILDKITE_PLUGIN_TEST_COLLECTOR_FILES='file.xml'
run "$PWD/hooks/pre-exit"

assert_failure
assert_output --partial "Missing file format 'format'. Possible values: 'junit', 'xml'"
}

@test "Errors if no file is found" {
export BUILDKITE_ANALYTICS_TOKEN='abc'
export BUILDKITE_PLUGIN_TEST_COLLECTOR_FILES='file.xml'
export BUILDKITE_PLUGIN_TEST_COLLECTOR_FORMAT='junit'
run "$PWD/hooks/pre-exit"

assert_failure
assert_output --partial "No files found matching 'file.xml'"
}
132 changes: 132 additions & 0 deletions tests/pre-exit-success.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/usr/bin/env bats

# To debug stubs, uncomment these lines:
# export CURL_STUB_DEBUG=/dev/tty
# export GIT_STUB_DEBUG=/dev/tty

setup() {
load "$BATS_PLUGIN_PATH/load.bash"
export BUILDKITE_PLUGIN_TEST_COLLECTOR_API_TOKEN_ENV_NAME=""

# Config
export BUILDKITE_ANALYTICS_TOKEN='a-secret-analytics-token'
export BUILDKITE_PLUGIN_TEST_COLLECTOR_FILES='**/*/junit-1.xml'
export BUILDKITE_PLUGIN_TEST_COLLECTOR_FORMAT='junit'
# Build env
export BUILDKITE_BUILD_ID="an-id"
export BUILDKITE_BUILD_URL="https://url.com/"
export BUILDKITE_BRANCH="a-branch"
export BUILDKITE_COMMIT="a-commit"
export BUILDKITE_BUILD_NUMBER="123"
export BUILDKITE_JOB_ID="321"
export BUILDKITE_MESSAGE="A message"
}

@test "Uploads a file" {
stub git "rev-parse --short HEAD : echo 'some-commit-id'"
stub curl "-X POST --silent --show-error --max-time 30 --form format=junit --form data=\"@./tests/fixtures/junit-1.xml\" --form run_env[CI]=buildkite --form run_env[key]=an-id --form run_env[url]=https://url.com/ --form run_env[branch]=a-branch --form run_env[commit_sha]=a-commit --form run_env[number]=123 --form run_env[job_id]=321 --form run_env[message]=\"A message\" --form run_env[collector]=test-collector-buildkite-plugin --form run_env[version]=some-commit-id https://analytics-api.buildkite.com/v1/uploads -H 'Authorization: Token token=\"a-secret-analytics-token\"' : echo 'curl success'"

run "$PWD/hooks/pre-exit"

unstub curl
unstub git

assert_success
assert_output --partial "Uploading './tests/fixtures/junit-1.xml'..."
assert_output --partial "curl success"
}

@test "Uploads multiple file" {
export BUILDKITE_PLUGIN_TEST_COLLECTOR_FILES='**/*/junit-*.xml'

stub git "rev-parse --short HEAD : echo 'some-commit-id'"
stub curl \
"-X POST --silent --show-error --max-time 30 --form format=junit --form data=\"@./tests/fixtures/junit-1.xml\" --form run_env[CI]=buildkite --form run_env[key]=an-id --form run_env[url]=https://url.com/ --form run_env[branch]=a-branch --form run_env[commit_sha]=a-commit --form run_env[number]=123 --form run_env[job_id]=321 --form run_env[message]=\"A message\" --form run_env[collector]=test-collector-buildkite-plugin --form run_env[version]=some-commit-id https://analytics-api.buildkite.com/v1/uploads -H 'Authorization: Token token=\"a-secret-analytics-token\"' : echo 'curl success 1'" \
"-X POST --silent --show-error --max-time 30 --form format=junit --form data=\"@./tests/fixtures/junit-2.xml\" --form run_env[CI]=buildkite --form run_env[key]=an-id --form run_env[url]=https://url.com/ --form run_env[branch]=a-branch --form run_env[commit_sha]=a-commit --form run_env[number]=123 --form run_env[job_id]=321 --form run_env[message]=\"A message\" --form run_env[collector]=test-collector-buildkite-plugin --form run_env[version]=some-commit-id https://analytics-api.buildkite.com/v1/uploads -H 'Authorization: Token token=\"a-secret-analytics-token\"' : echo 'curl success 2'"

run "$PWD/hooks/pre-exit"

unstub curl
unstub git

assert_success
assert_output --partial "Uploading './tests/fixtures/junit-1.xml'..."
assert_output --partial "Uploading './tests/fixtures/junit-2.xml'..."
assert_output --partial "curl success 1"
assert_output --partial "curl success 2"
}

@test "Debug true prints the curl info w/o token" {
export BUILDKITE_PLUGIN_TEST_COLLECTOR_DEBUG="true"

stub git "rev-parse --short HEAD : echo 'some-commit-id'"
stub curl "-X POST --silent --show-error --max-time 30 --form format=junit --form data=\"@./tests/fixtures/junit-1.xml\" --form run_env[CI]=buildkite --form run_env[key]=an-id --form run_env[url]=https://url.com/ --form run_env[branch]=a-branch --form run_env[commit_sha]=a-commit --form run_env[number]=123 --form run_env[job_id]=321 --form run_env[message]=\"A message\" --form run_env[collector]=test-collector-buildkite-plugin --form run_env[debug]=true --form run_env[version]=some-commit-id https://analytics-api.buildkite.com/v1/uploads -H 'Authorization: Token token=\"a-secret-analytics-token\"' : echo 'curl success'"

run "$PWD/hooks/pre-exit"

unstub curl
unstub git

assert_success
assert_output --partial "curl -X POST"
refute_output --partial "a-secret-analytics-token"
}

@test "Debug env var true prints the curl info w/o token" {
export BUILDKITE_ANALYTICS_DEBUG_ENABLED="true"

stub git "rev-parse --short HEAD : echo 'some-commit-id'"
stub curl "-X POST --silent --show-error --max-time 30 --form format=junit --form data=\"@./tests/fixtures/junit-1.xml\" --form run_env[CI]=buildkite --form run_env[key]=an-id --form run_env[url]=https://url.com/ --form run_env[branch]=a-branch --form run_env[commit_sha]=a-commit --form run_env[number]=123 --form run_env[job_id]=321 --form run_env[message]=\"A message\" --form run_env[collector]=test-collector-buildkite-plugin --form run_env[debug]=true --form run_env[version]=some-commit-id https://analytics-api.buildkite.com/v1/uploads -H 'Authorization: Token token=\"a-secret-analytics-token\"' : echo 'curl success'"

run "$PWD/hooks/pre-exit"

unstub curl
unstub git

assert_success
assert_output --partial "curl -X POST"
refute_output --partial "a-secret-analytics-token"
}

@test "Debug false does not print the curl info" {
export BUILDKITE_PLUGIN_TEST_COLLECTOR_DEBUG="false"

stub git "rev-parse --short HEAD : echo 'some-commit-id'"
stub curl "-X POST --silent --show-error --max-time 30 --form format=junit --form data=\"@./tests/fixtures/junit-1.xml\" --form run_env[CI]=buildkite --form run_env[key]=an-id --form run_env[url]=https://url.com/ --form run_env[branch]=a-branch --form run_env[commit_sha]=a-commit --form run_env[number]=123 --form run_env[job_id]=321 --form run_env[message]=\"A message\" --form run_env[collector]=test-collector-buildkite-plugin --form run_env[version]=some-commit-id https://analytics-api.buildkite.com/v1/uploads -H 'Authorization: Token token=\"a-secret-analytics-token\"' : echo 'curl success'"

run "$PWD/hooks/pre-exit"

unstub curl
unstub git

assert_success
refute_output --partial "curl -X POST"
}

@test "Timeout is configurable" {
export BUILDKITE_PLUGIN_TEST_COLLECTOR_TIMEOUT='999'

stub git "rev-parse --short HEAD : echo 'some-commit-id'"
stub curl "-X POST --silent --show-error --max-time 999 --form format=junit --form data=\"@./tests/fixtures/junit-1.xml\" --form run_env[CI]=buildkite --form run_env[key]=an-id --form run_env[url]=https://url.com/ --form run_env[branch]=a-branch --form run_env[commit_sha]=a-commit --form run_env[number]=123 --form run_env[job_id]=321 --form run_env[message]=\"A message\" --form run_env[collector]=test-collector-buildkite-plugin --form run_env[version]=some-commit-id https://analytics-api.buildkite.com/v1/uploads -H 'Authorization: Token token=\"a-secret-analytics-token\"' : echo 'curl success'"

run "$PWD/hooks/pre-exit"

unstub curl
unstub git

assert_success
assert_output --partial "curl success"
}

@test "Git unavailable sends no plugin version" {
stub git "rev-parse --short HEAD : echo 'git error' >&2; exit 1"
stub curl "-X POST --silent --show-error --max-time 30 --form format=junit --form data=\"@./tests/fixtures/junit-1.xml\" --form run_env[CI]=buildkite --form run_env[key]=an-id --form run_env[url]=https://url.com/ --form run_env[branch]=a-branch --form run_env[commit_sha]=a-commit --form run_env[number]=123 --form run_env[job_id]=321 --form run_env[message]=\"A message\" --form run_env[collector]=test-collector-buildkite-plugin https://analytics-api.buildkite.com/v1/uploads -H 'Authorization: Token token=\"a-secret-analytics-token\"' : echo 'curl success'"

run "$PWD/hooks/pre-exit"

unstub curl
unstub git

assert_success
assert_output --partial "curl success"
}
99 changes: 0 additions & 99 deletions tests/pre-exit.bats

This file was deleted.

0 comments on commit 7bc5875

Please sign in to comment.