Skip to content

Commit c4477cd

Browse files
committed
Formatting of entrypoint, adding tests
1 parent f5151b1 commit c4477cd

File tree

2 files changed

+183
-25
lines changed

2 files changed

+183
-25
lines changed

entrypoint.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,11 @@ function curl_with_retry() {
184184
TOTAL_DELAY=$(calculate_backoff_delay "$BASE_DELAY" "$ATTEMPT")
185185

186186
echo "Network error or curl failure. Retrying in ${TOTAL_DELAY}s (attempt $ATTEMPT/$MAX_ATTEMPTS)..." >&2
187+
187188
sleep "$TOTAL_DELAY"
189+
188190
ATTEMPT=$((ATTEMPT + 1))
191+
189192
continue
190193
else
191194
echo "Network error or curl failure after $MAX_ATTEMPTS attempts" >&2

tests/entrypoint.bats

Lines changed: 180 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ teardown() {
2121
}
2222

2323
@test "Prints error and fails if \${{ inputs.buildkite_api_access_token }} isn't set" {
24-
24+
2525
run "${PWD}"/entrypoint.sh
2626
assert_output --partial "You must set the buildkite_api_access_token input parameter"
2727
assert_failure
2828
}
2929

3030
@test "Prints error and fails if \${{ inputs.pipeline }} isn't set" {
31-
31+
3232
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
3333

34-
run "${PWD}"/entrypoint.sh
35-
assert_output --partial "You must set the pipeline input parameter"
34+
run "${PWD}"/entrypoint.sh
35+
assert_output --partial "You must set the pipeline input parameter"
3636
assert_failure
3737
}
3838

3939
@test "Creates a build with defaults" {
40-
40+
4141
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
4242
export INPUT_PIPELINE="my-org/my-pipeline"
4343
export GITHUB_EVENT_NAME="create"
@@ -60,7 +60,7 @@ teardown() {
6060
}
6161

6262
@test "Creates a build with commit from \${{ inputs.commit }}" {
63-
63+
6464
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
6565
export INPUT_PIPELINE="my-org/my-pipeline"
6666
export INPUT_COMMIT="custom-commit"
@@ -84,7 +84,7 @@ teardown() {
8484
}
8585

8686
@test "Creates a build with branch from \${{ inputs.branch }}" {
87-
87+
8888
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
8989
export INPUT_PIPELINE="my-org/my-pipeline"
9090
export INPUT_BRANCH="custom-branch"
@@ -108,7 +108,7 @@ teardown() {
108108
}
109109

110110
@test "Creates a build from pull request" {
111-
111+
112112
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
113113
export INPUT_PIPELINE="my-org/my-pipeline"
114114
export INPUT_PULL_REQUEST_BASE_BRANCH="b-branch"
@@ -133,7 +133,7 @@ teardown() {
133133
}
134134

135135
@test "Creates a build with branch from \${{ inputs.message }}" {
136-
136+
137137
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
138138
export INPUT_PIPELINE="my-org/my-pipeline"
139139
export INPUT_MESSAGE="A custom message"
@@ -157,7 +157,7 @@ teardown() {
157157
}
158158

159159
@test "Creates a build with build env vars from \${{ inputs.build_env_vars }}" {
160-
160+
161161
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
162162
export INPUT_PIPELINE="my-org/my-pipeline"
163163
export INPUT_BUILD_ENV_VARS="{\"FOO\": \"bar\"}"
@@ -180,7 +180,7 @@ teardown() {
180180
unstub curl
181181
}
182182

183-
@test "Creates a build with build meta-data vars from \${{ inputs.build_meta_data }}" {
183+
@test "Creates a build with build meta-data vars from \${{ inputs.build_meta_data }}" {
184184
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
185185
export INPUT_PIPELINE="my-org/my-pipeline"
186186
export INPUT_BUILD_META_DATA="{\"FOO\": \"bar\"}"
@@ -203,7 +203,7 @@ teardown() {
203203
unstub curl
204204
}
205205

206-
@test "Creates a build with ignore_pipeline_branch_filters set to true from \${{ inputs.ignore_pipeline_branch_filter }}" {
206+
@test "Creates a build with ignore_pipeline_branch_filters set to true from \${{ inputs.ignore_pipeline_branch_filter }}" {
207207
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
208208
export INPUT_PIPELINE="my-org/my-pipeline"
209209
export INPUT_BUILD_ENV_VARS="{\"FOO\": \"bar\"}"
@@ -227,7 +227,7 @@ teardown() {
227227
unstub curl
228228
}
229229

230-
@test "Creates a build with send_pull_request set to false from \${{ inputs.send_pull_request }}" {
230+
@test "Creates a build with send_pull_request set to false from \${{ inputs.send_pull_request }}" {
231231
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
232232
export INPUT_PIPELINE="my-org/my-pipeline"
233233
export INPUT_BUILD_ENV_VARS="{\"FOO\": \"bar\"}"
@@ -252,7 +252,7 @@ teardown() {
252252
unstub curl
253253
}
254254

255-
@test "Writes outputs to \$GITHUB_OUTPUT file if defined" {
255+
@test "Writes outputs to \$GITHUB_OUTPUT file if defined" {
256256
TEST_TEMP_DIR="$(temp_make)"
257257

258258
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
@@ -286,7 +286,7 @@ teardown() {
286286
}
287287

288288
@test "Prints curl error on HTTP error" {
289-
289+
290290
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
291291
export INPUT_PIPELINE="my-org/my-pipeline"
292292
export GITHUB_EVENT_NAME="create"
@@ -306,7 +306,7 @@ teardown() {
306306
}
307307

308308
@test "Prints curl error and ignores non-JSON response on HTTP error" {
309-
309+
310310
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
311311
export INPUT_PIPELINE="my-org/my-pipeline"
312312
export GITHUB_EVENT_NAME="create"
@@ -328,7 +328,7 @@ teardown() {
328328
}
329329

330330
@test "Prints curl error but not null JSON response message on HTTP error" {
331-
331+
332332
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
333333
export INPUT_PIPELINE="my-org/my-pipeline"
334334
export GITHUB_EVENT_NAME="create"
@@ -349,7 +349,7 @@ teardown() {
349349
}
350350

351351
@test "Prints curl error and JSON response message on HTTP error" {
352-
352+
353353
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
354354
export INPUT_PIPELINE="my-org/my-pipeline"
355355
export GITHUB_EVENT_NAME="create"
@@ -369,7 +369,7 @@ teardown() {
369369
unstub curl
370370
}
371371

372-
@test "Prints error and fails if \${{ inputs.build_env_vars }} is not valid JSON" {
372+
@test "Prints error and fails if \${{ inputs.build_env_vars }} is not valid JSON" {
373373
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
374374
export INPUT_PIPELINE="my-org/my-pipeline"
375375
export INPUT_BUILD_ENV_VARS="broken"
@@ -396,7 +396,7 @@ teardown() {
396396
}
397397

398398
@test "Sets DELETED_EVENT_REF on delete event" {
399-
399+
400400
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
401401
export INPUT_PIPELINE="my-org/my-pipeline"
402402
export GITHUB_ACTION="delete"
@@ -418,7 +418,7 @@ teardown() {
418418
}
419419

420420
@test "Combines DELETED_EVENT_REF and build_env_vars correctly" {
421-
421+
422422
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
423423
export INPUT_PIPELINE="my-org/my-pipeline"
424424
export INPUT_BUILD_ENV_VARS="{\"FOO\": \"bar\"}"
@@ -456,8 +456,8 @@ teardown() {
456456
# Stub curl to handle both the create build call and the status check calls
457457
stub curl \
458458
"--fail-with-body --silent --show-error -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$CREATE_RESPONSE'" \
459-
"--fail-with-body --silent --show-error -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds/123 : echo '$STATUS_RESPONSE_RUNNING'" \
460-
"--fail-with-body --silent --show-error -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds/123 : echo '$STATUS_RESPONSE_PASSED'"
459+
"--silent --show-error --write-out %{http_code} --output * -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds/123 : output_file=\"\"; for i in \"\$@\"; do if [ \"\$output_file\" = \"next\" ]; then echo '$STATUS_RESPONSE_RUNNING' > \"\$i\"; break; fi; [ \"\$i\" = \"--output\" ] && output_file=\"next\"; done; echo 200" \
460+
"--silent --show-error --write-out %{http_code} --output * -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds/123 : output_file=\"\"; for i in \"\$@\"; do if [ \"\$output_file\" = \"next\" ]; then echo '$STATUS_RESPONSE_PASSED' > \"\$i\"; break; fi; [ \"\$i\" = \"--output\" ] && output_file=\"next\"; done; echo 200"
461461

462462
run "${PWD}"/entrypoint.sh
463463

@@ -487,8 +487,8 @@ teardown() {
487487
# Stub curl to handle both the create build call and the status check calls
488488
stub curl \
489489
"--fail-with-body --silent --show-error -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$CREATE_RESPONSE'" \
490-
"--fail-with-body --silent --show-error -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds/123 : echo '$STATUS_RESPONSE_RUNNING'" \
491-
"--fail-with-body --silent --show-error -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds/123 : echo '$STATUS_RESPONSE_FAILED'"
490+
"--silent --show-error --write-out %{http_code} --output * -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds/123 : output_file=\"\"; for i in \"\$@\"; do if [ \"\$output_file\" = \"next\" ]; then echo '$STATUS_RESPONSE_RUNNING' > \"\$i\"; break; fi; [ \"\$i\" = \"--output\" ] && output_file=\"next\"; done; echo 200" \
491+
"--silent --show-error --write-out %{http_code} --output * -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds/123 : output_file=\"\"; for i in \"\$@\"; do if [ \"\$output_file\" = \"next\" ]; then echo '$STATUS_RESPONSE_FAILED' > \"\$i\"; break; fi; [ \"\$i\" = \"--output\" ] && output_file=\"next\"; done; echo 200"
492492

493493
run "${PWD}"/entrypoint.sh
494494

@@ -578,3 +578,158 @@ teardown() {
578578

579579
unstub curl
580580
}
581+
582+
@test "Retries on 500 error and succeeds" {
583+
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
584+
export INPUT_PIPELINE="my-org/my-pipeline"
585+
export INPUT_WAIT="true"
586+
export INPUT_WAIT_INTERVAL="1"
587+
export INPUT_RETRY_MAX_ATTEMPTS="3"
588+
export INPUT_RETRY_BASE_DELAY="1"
589+
export GITHUB_EVENT_NAME="create"
590+
export GITHUB_OUTPUT=$TEST_TEMP_DIR/github_output_file
591+
592+
EXPECTED_JSON='{"commit":"a-sha","branch":"a-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"}}'
593+
CREATE_RESPONSE='{"web_url": "https://buildkite.com/build-url", "number": "123"}'
594+
STATUS_RESPONSE_PASSED='{"state": "passed"}'
595+
596+
stub curl \
597+
"--fail-with-body --silent --show-error -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$CREATE_RESPONSE'" \
598+
"--silent --show-error --write-out %{http_code} --output * -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds/123 : echo 500" \
599+
"--silent --show-error --write-out %{http_code} --output * -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds/123 : output_file=\"\"; for i in \"\$@\"; do if [ \"\$output_file\" = \"next\" ]; then echo '$STATUS_RESPONSE_PASSED' > \"\$i\"; break; fi; [ \"\$i\" = \"--output\" ] && output_file=\"next\"; done; echo 200"
600+
601+
run "${PWD}"/entrypoint.sh
602+
603+
assert_output --partial "Build created:"
604+
assert_output --partial "Waiting for build 123 to complete..."
605+
assert_output --partial "API request failed with HTTP 500:"
606+
assert_output --partial "Retrying in"
607+
assert_output --partial "Build passed!"
608+
assert_success
609+
610+
unstub curl
611+
}
612+
613+
@test "Retries on 503 error and succeeds" {
614+
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
615+
export INPUT_PIPELINE="my-org/my-pipeline"
616+
export INPUT_WAIT="true"
617+
export INPUT_WAIT_INTERVAL="1"
618+
export INPUT_RETRY_MAX_ATTEMPTS="3"
619+
export INPUT_RETRY_BASE_DELAY="1"
620+
export GITHUB_EVENT_NAME="create"
621+
export GITHUB_OUTPUT=$TEST_TEMP_DIR/github_output_file
622+
623+
EXPECTED_JSON='{"commit":"a-sha","branch":"a-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"}}'
624+
CREATE_RESPONSE='{"web_url": "https://buildkite.com/build-url", "number": "123"}'
625+
STATUS_RESPONSE_PASSED='{"state": "passed"}'
626+
627+
stub curl \
628+
"--fail-with-body --silent --show-error -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$CREATE_RESPONSE'" \
629+
"--silent --show-error --write-out %{http_code} --output * -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds/123 : echo 503" \
630+
"--silent --show-error --write-out %{http_code} --output * -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds/123 : output_file=\"\"; for i in \"\$@\"; do if [ \"\$output_file\" = \"next\" ]; then echo '$STATUS_RESPONSE_PASSED' > \"\$i\"; break; fi; [ \"\$i\" = \"--output\" ] && output_file=\"next\"; done; echo 200"
631+
632+
run "${PWD}"/entrypoint.sh
633+
634+
assert_output --partial "Build created:"
635+
assert_output --partial "Waiting for build 123 to complete..."
636+
assert_output --partial "API request failed with HTTP 503:"
637+
assert_output --partial "Retrying in"
638+
assert_output --partial "Build passed!"
639+
assert_success
640+
641+
unstub curl
642+
}
643+
644+
@test "Retries on 429 rate limit and succeeds" {
645+
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
646+
export INPUT_PIPELINE="my-org/my-pipeline"
647+
export INPUT_WAIT="true"
648+
export INPUT_WAIT_INTERVAL="1"
649+
export INPUT_RETRY_MAX_ATTEMPTS="3"
650+
export INPUT_RETRY_BASE_DELAY="1"
651+
export GITHUB_EVENT_NAME="create"
652+
export GITHUB_OUTPUT=$TEST_TEMP_DIR/github_output_file
653+
654+
EXPECTED_JSON='{"commit":"a-sha","branch":"a-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"}}'
655+
CREATE_RESPONSE='{"web_url": "https://buildkite.com/build-url", "number": "123"}'
656+
STATUS_RESPONSE_PASSED='{"state": "passed"}'
657+
658+
stub curl \
659+
"--fail-with-body --silent --show-error -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$CREATE_RESPONSE'" \
660+
"--silent --show-error --write-out %{http_code} --output * -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds/123 : echo 429" \
661+
"--silent --show-error --write-out %{http_code} --output * -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds/123 : output_file=\"\"; for i in \"\$@\"; do if [ \"\$output_file\" = \"next\" ]; then echo '$STATUS_RESPONSE_PASSED' > \"\$i\"; break; fi; [ \"\$i\" = \"--output\" ] && output_file=\"next\"; done; echo 200"
662+
663+
run "${PWD}"/entrypoint.sh
664+
665+
assert_output --partial "Build created:"
666+
assert_output --partial "Waiting for build 123 to complete..."
667+
assert_output --partial "API request failed with HTTP 429:"
668+
assert_output --partial "Retrying in"
669+
assert_output --partial "Build passed!"
670+
assert_success
671+
672+
unstub curl
673+
}
674+
675+
@test "Fails fast on 404 error without retry" {
676+
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
677+
export INPUT_PIPELINE="my-org/my-pipeline"
678+
export INPUT_WAIT="true"
679+
export INPUT_WAIT_INTERVAL="1"
680+
export INPUT_RETRY_MAX_ATTEMPTS="3"
681+
export INPUT_RETRY_BASE_DELAY="1"
682+
export GITHUB_EVENT_NAME="create"
683+
export GITHUB_OUTPUT=$TEST_TEMP_DIR/github_output_file
684+
685+
EXPECTED_JSON='{"commit":"a-sha","branch":"a-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"}}'
686+
CREATE_RESPONSE='{"web_url": "https://buildkite.com/build-url", "number": "123"}'
687+
688+
stub curl \
689+
"--fail-with-body --silent --show-error -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$CREATE_RESPONSE'" \
690+
"--silent --show-error --write-out %{http_code} --output * -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds/123 : echo 404"
691+
692+
run "${PWD}"/entrypoint.sh
693+
694+
assert_output --partial "Build created:"
695+
assert_output --partial "Waiting for build 123 to complete..."
696+
assert_output --partial "API request failed with HTTP 404 (non-retryable client error)"
697+
assert_output --partial "Failed to fetch build status after retries"
698+
assert_output --partial "Build did not complete successfully"
699+
refute_output --partial "Retrying"
700+
assert_failure
701+
702+
unstub curl
703+
}
704+
705+
@test "Fails after max retries" {
706+
export INPUT_BUILDKITE_API_ACCESS_TOKEN="123"
707+
export INPUT_PIPELINE="my-org/my-pipeline"
708+
export INPUT_WAIT="true"
709+
export INPUT_WAIT_INTERVAL="1"
710+
export INPUT_RETRY_MAX_ATTEMPTS="2"
711+
export INPUT_RETRY_BASE_DELAY="1"
712+
export GITHUB_EVENT_NAME="create"
713+
export GITHUB_OUTPUT=$TEST_TEMP_DIR/github_output_file
714+
715+
EXPECTED_JSON='{"commit":"a-sha","branch":"a-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"}}'
716+
CREATE_RESPONSE='{"web_url": "https://buildkite.com/build-url", "number": "123"}'
717+
718+
stub curl \
719+
"--fail-with-body --silent --show-error -X POST -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds -d '$EXPECTED_JSON' : echo '$CREATE_RESPONSE'" \
720+
"--silent --show-error --write-out %{http_code} --output * -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds/123 : echo 500" \
721+
"--silent --show-error --write-out %{http_code} --output * -H \"Authorization: Bearer 123\" https://api.buildkite.com/v2/organizations/my-org/pipelines/my-pipeline/builds/123 : echo 500"
722+
723+
run "${PWD}"/entrypoint.sh
724+
725+
assert_output --partial "Build created:"
726+
assert_output --partial "Waiting for build 123 to complete..."
727+
assert_output --partial "API request failed with HTTP 500:"
728+
assert_output --partial "Retrying in"
729+
assert_output --partial "API request failed with HTTP 500 after 2 attempts"
730+
assert_output --partial "Failed to fetch build status after retries"
731+
assert_output --partial "Build did not complete successfully"
732+
assert_failure
733+
734+
unstub curl
735+
}

0 commit comments

Comments
 (0)