-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from buildkite-plugins/add-tests-and-fix-git
Fix version check and complete tests
- Loading branch information
Showing
6 changed files
with
210 additions
and
106 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
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,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 |
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,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'" | ||
} |
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,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" | ||
} |
This file was deleted.
Oops, something went wrong.