Skip to content

Commit

Permalink
update entrypoint.sh error messages and entrypoint.bats test names to…
Browse files Browse the repository at this point in the history
… reference to input params
  • Loading branch information
lizrabuya committed Sep 19, 2023
1 parent a75749c commit 6e7a819
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 65 deletions.
12 changes: 5 additions & 7 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ jobs:
steps:

- name: Check out code
uses: actions/checkout@v3
uses: actions/checkout@v3

- name: Setup BATS
uses: mig4/setup-bats@v1
with:
bats-version: 1.2.1

- name: Verify that the Docker image for the action builds
run: docker build . --file Dockerfile
run: docker build . --file Dockerfile

- name: Run unit test for the action
run: docker run --rm -ti -v "$PWD":/plugin buildkite/plugin-tester:v4.0.0

10 changes: 5 additions & 5 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ function get_INPUT_BUILD_ENV_VARS_json() {
}

if [[ -z "${INPUT_BUILDKITE_API_ACCESS_TOKEN:-}" ]]; then
echo "You must set the INPUT_BUILDKITE_API_ACCESS_TOKEN environment variable (e.g. INPUT_BUILDKITE_API_ACCESS_TOKEN = \"xyz\")"
echo "You must set the buildkite_api_access_token input parameter (e.g. buildkite_api_access_token: \"1234567890\")"
exit 1
fi

if [[ -z "${INPUT_PIPELINE:-}" ]]; then
echo "You must set the INPUT_PIPELINE environment variable (e.g. PIPELINE = \"my-org/my-pipeline\")"
echo "You must set the pipeline input parameter (e.g. pipeline: \"my-org/my-pipeline\")"
exit 1
fi

Expand All @@ -84,7 +84,7 @@ fi
if [[ "$INPUT_BUILD_ENV_VARS" ]]; then
if ! echo "$INPUT_BUILD_ENV_VARS" | jq empty; then
echo ""
echo "Error: INPUT_BUILD_ENV_VARS provided invalid JSON: $INPUT_BUILD_ENV_VARS"
echo "Error: build_env_vars provided invalid JSON: $INPUT_BUILD_ENV_VARS"
exit 1
fi
fi
Expand Down Expand Up @@ -120,7 +120,7 @@ fi
if [[ "${INPUT_BUILD_META_DATA:-}" ]]; then
if ! JSON=$(echo "$JSON" | jq -c --argjson INPUT_BUILD_META_DATA "$INPUT_BUILD_META_DATA" '. + {meta_data: $INPUT_BUILD_META_DATA}'); then
echo ""
echo "Error: INPUT_BUILD_META_DATA provided invalid JSON: $INPUT_BUILD_META_DATA"
echo "Error: build_meta_data provided invalid JSON: $INPUT_BUILD_META_DATA"
exit 1
fi
fi
Expand All @@ -129,7 +129,7 @@ fi
if [[ "${INPUT_IGNORE_PIPELINE_BRANCH_FILTER:-}" ]]; then
if ! JSON=$(echo "$JSON" | jq -c --argjson INPUT_IGNORE_PIPELINE_BRANCH_FILTER "$INPUT_IGNORE_PIPELINE_BRANCH_FILTER" '. + {INPUT_IGNORE_PIPELINE_BRANCH_FILTERs: $INPUT_IGNORE_PIPELINE_BRANCH_FILTER}'); then
echo ""
echo "Error: Could not set INPUT_IGNORE_PIPELINE_BRANCH_FILTERs"
echo "Error: Could not set ignore_pipeline_branch_filter"
exit 1
fi
fi
Expand Down
12 changes: 0 additions & 12 deletions install-bats-libs.sh

This file was deleted.

82 changes: 41 additions & 41 deletions tests/entrypoint.bats
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env
#!/usr/bin/env bats

# load "${BATS_PLUGIN_PATH}/load.bash"
load "${BATS_PLUGIN_PATH}/load.bash"

# Uncomment to enable stub debugging
# export CURL_STUB_DEBUG=/dev/tty
Expand All @@ -15,27 +15,27 @@ setup() {
}

teardown() {
unset BUILDKITE_API_ACCESS_TOKEN
unset INPUT_BUILDKITE_API_ACCESS_TOKEN
unset INPUT_PIPELINE
if [[ -f "$HOME/push.json" ]]; then rm "$HOME/push.json"; fi
}

@test "Prints error and fails if \$BUILDKITE_API_ACCESS_TOKEN isn't set" {
@test "Prints error and fails if \${{ inputs.buildkite_api_access_token }} isn't set" {
run "${PWD}"/entrypoint.sh
assert_output --partial "You must set the BUILDKITE_API_ACCESS_TOKEN environment variable"
assert_output --partial "You must set the buildkite_api_access_token input parameter"
assert_failure
}

@test "Prints error and fails if \${{ inputs.pipeline }} isn't set" {
export BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"

run "${PWD}"/entrypoint.sh
assert_output --partial "You must set the INPUT_PIPELINE environment variable"
assert_output --partial "You must set the pipeline input parameter"
assert_failure
}

@test "Creates a build with defaults" {
export BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_PIPELINE="my-org/my-pipeline"
export GITHUB_EVENT_NAME="create"

Expand All @@ -56,8 +56,8 @@ teardown() {
unstub curl
}

@test "Creates a build with commit from \$COMMIT" {
export BUILDKITE_API_ACCESS_TOKEN="123"
@test "Creates a build with commit from \${{ inputs.commit }}" {
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_PIPELINE="my-org/my-pipeline"
export INPUT_COMMIT="custom-commit"
export GITHUB_EVENT_NAME="create"
Expand All @@ -79,10 +79,10 @@ teardown() {
unstub curl
}

@test "Creates a build with branch from \$BRANCH" {
export BUILDKITE_API_ACCESS_TOKEN="123"
@test "Creates a build with branch from \${{ inputs.branch }}" {
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_PIPELINE="my-org/my-pipeline"
export INPUT_BRANCH="custom-branch"
export INPUT_BRANCH="custom-branch"
export GITHUB_EVENT_NAME="create"

EXPECTED_JSON='{"commit":"a-sha","branch":"custom-branch","message":"","author":{"name":"The Pusher","email":"[email protected]"},"env":{"GITHUB_REPOSITORY":"buildkite/test-repo","SOURCE_REPO_SHA":"a-sha","SOURCE_REPO_REF":"a-branch"}}'
Expand All @@ -103,7 +103,7 @@ teardown() {
}

@test "Creates a build from pull request" {
export BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_PIPELINE="my-org/my-pipeline"
export GITHUB_EVENT_PATH="tests/pullrequest.json"
export GITHUB_EVENT_NAME="create"
Expand All @@ -125,8 +125,8 @@ teardown() {
unstub curl
}

@test "Creates a build with branch from \$INPUT_MESSAGE" {
export BUILDKITE_API_ACCESS_TOKEN="123"
@test "Creates a build with branch from \${{ inputs.message }}" {
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_PIPELINE="my-org/my-pipeline"
export INPUT_MESSAGE="A custom message"
export GITHUB_EVENT_NAME="create"
Expand All @@ -148,10 +148,10 @@ teardown() {
unstub curl
}

@test "Creates a build with build env vars from \$BUILD_ENV_VARS" {
export BUILDKITE_API_ACCESS_TOKEN="123"
@test "Creates a build with build env vars from \${{ inputs.build_env_vars }}" {
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_PIPELINE="my-org/my-pipeline"
export BUILD_ENV_VARS="{\"FOO\": \"bar\"}"
export INPUT_BUILD_ENV_VARS="{\"FOO\": \"bar\"}"
export GITHUB_EVENT_NAME="create"

EXPECTED_JSON='{"commit":"a-sha","branch":"a-branch","message":"","author":{"name":"The Pusher","email":"[email protected]"},"env":{"FOO":"bar","GITHUB_REPOSITORY":"buildkite/test-repo","SOURCE_REPO_SHA":"a-sha","SOURCE_REPO_REF":"a-branch"}}'
Expand All @@ -171,8 +171,8 @@ teardown() {
unstub curl
}

@test "Creates a build with build meta-data vars from \$BUILD_META_DATA" {
export BUILDKITE_API_ACCESS_TOKEN="123"
@test "Creates a build with build meta-data vars from \${{ inputs.build_meta_data }}" {
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_PIPELINE="my-org/my-pipeline"
export BUILD_META_DATA="{\"FOO\": \"bar\"}"
export GITHUB_EVENT_NAME="create"
Expand All @@ -194,10 +194,10 @@ teardown() {
unstub curl
}

@test "Creates a build with ignore_pipeline_branch_filters set to true from \$IGNORE_PIPELINE_BRANCH_FILTER" {
export BUILDKITE_API_ACCESS_TOKEN="123"
@test "Creates a build with ignore_pipeline_branch_filters set to true from \${{ inputs.ignore_pipeline_branch_filter }}" {
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_PIPELINE="my-org/my-pipeline"
export BUILD_ENV_VARS="{\"FOO\": \"bar\"}"
export INPUT_BUILD_ENV_VARS="{\"FOO\": \"bar\"}"
export IGNORE_PIPELINE_BRANCH_FILTER="true"
export GITHUB_EVENT_NAME="create"

Expand All @@ -221,9 +221,9 @@ teardown() {
@test "Writes outputs to \$GITHUB_OUTPUT file if defined" {
TEST_TEMP_DIR="$(temp_make)"

export BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_PIPELINE="my-org/my-pipeline"
export BUILD_ENV_VARS="{\"FOO\": \"bar\"}"
export INPUT_BUILD_ENV_VARS="{\"FOO\": \"bar\"}"
export GITHUB_OUTPUT=$TEST_TEMP_DIR/github_output_file
export GITHUB_EVENT_NAME="create"

Expand Down Expand Up @@ -252,7 +252,7 @@ teardown() {
}

@test "Prints curl error on HTTP error" {
export BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_PIPELINE="my-org/my-pipeline"
export GITHUB_EVENT_NAME="create"

Expand All @@ -271,7 +271,7 @@ teardown() {
}

@test "Prints curl error and ignores non-JSON response on HTTP error" {
export BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_PIPELINE="my-org/my-pipeline"
export GITHUB_EVENT_NAME="create"

Expand All @@ -292,7 +292,7 @@ teardown() {
}

@test "Prints curl error but not null JSON response message on HTTP error" {
export BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_PIPELINE="my-org/my-pipeline"
export GITHUB_EVENT_NAME="create"

Expand All @@ -312,7 +312,7 @@ teardown() {
}

@test "Prints curl error and JSON response message on HTTP error" {
export BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_PIPELINE="my-org/my-pipeline"
export GITHUB_EVENT_NAME="create"

Expand All @@ -331,34 +331,34 @@ teardown() {
unstub curl
}

@test "Prints error and fails if \$BUILD_ENV_VARS is not valid JSON" {
export BUILDKITE_API_ACCESS_TOKEN="123"
@test "Prints error and fails if \${{ inputs.build_env_vars }} is not valid JSON" {
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_PIPELINE="my-org/my-pipeline"
export BUILD_ENV_VARS="broken"
export INPUT_BUILD_ENV_VARS="broken"
export GITHUB_EVENT_NAME="create"

run "${PWD}"/entrypoint.sh

assert_output --partial "Error: BUILD_ENV_VARS provided invalid JSON: broken"
assert_output --partial "Error: build_env_vars provided invalid JSON: broken"

assert_failure
}

@test "Prints error and fails if \$BUILD_META_DATA is not valid JSON" {
export BUILDKITE_API_ACCESS_TOKEN="123"
@test "Prints error and fails if \${{ inputs.build_meta_data }} is not valid JSON" {
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_PIPELINE="my-org/my-pipeline"
export BUILD_META_DATA="broken"
export GITHUB_EVENT_NAME="create"

run $PWD/entrypoint.sh

assert_output --partial "Error: BUILD_META_DATA provided invalid JSON: broken"
assert_output --partial "Error: build_meta_data provided invalid JSON: broken"

assert_failure
}

@test "Sets DELETED_EVENT_REF on delete event" {
export BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_PIPELINE="my-org/my-pipeline"
export GITHUB_ACTION="delete"
export GITHUB_EVENT_NAME="delete"
Expand All @@ -378,10 +378,10 @@ teardown() {
unstub curl
}

@test "Combines DELETED_EVENT_REF and BUILD_ENV_VARS correctly" {
export BUILDKITE_API_ACCESS_TOKEN="123"
@test "Combines DELETED_EVENT_REF and build_env_vars correctly" {
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
export INPUT_PIPELINE="my-org/my-pipeline"
export BUILD_ENV_VARS="{\"FOO\": \"bar\"}"
export INPUT_BUILD_ENV_VARS="{\"FOO\": \"bar\"}"
export GITHUB_ACTION="delete"
export GITHUB_EVENT_NAME="delete"

Expand Down

0 comments on commit 6e7a819

Please sign in to comment.