Skip to content

Commit 367e897

Browse files
author
Ola Buildkite
committed
Add bats test for retry logic
1 parent 1f0939d commit 367e897

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

tests/command.bats

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,58 @@ teardown() {
9494
# Clean up stub (it should not have been called)
9595
unstub curl || true
9696
}
97+
98+
# Tests for download_with_retry() function (SUP-5615)
99+
100+
@test "download retries on transient failure and succeeds" {
101+
export BUILDKITE_PLUGIN_MONOREPO_DIFF_DOWNLOAD=true
102+
export BUILDKITE_PLUGIN_MONOREPO_DIFF_BUILDKITE_PLUGIN_TEST_MODE=false
103+
# Use pinned version to skip get_latest_version API call
104+
export BUILDKITE_PLUGINS='[{"github.com/buildkite-plugins/monorepo-diff-buildkite-plugin#v1.0.0": {}}]'
105+
106+
# Remove mock binary so it actually needs to download
107+
rm -f "$PWD/monorepo-diff-buildkite-plugin"
108+
rm -f "$PWD/monorepo-diff-buildkite-plugin.version"
109+
110+
# Stub curl: fail twice on download, then succeed and create executable
111+
stub curl \
112+
"-sSfL * -o * : exit 1" \
113+
"-sSfL * -o * : exit 1" \
114+
"-sSfL * -o * : echo '#!/bin/bash' > \"\${4}\"; echo 'echo test' >> \"\${4}\"; exit 0"
115+
116+
run "$PWD/hooks/command"
117+
118+
assert_output --partial "Downloading binary (attempt 1/3)"
119+
assert_output --partial "Download failed, retrying"
120+
assert_output --partial "Downloading binary (attempt 2/3)"
121+
assert_output --partial "Downloading binary (attempt 3/3)"
122+
assert_output --partial "Download successful"
123+
124+
unstub curl
125+
}
126+
127+
@test "download fails after max retries exhausted" {
128+
export BUILDKITE_PLUGIN_MONOREPO_DIFF_DOWNLOAD=true
129+
export BUILDKITE_PLUGIN_MONOREPO_DIFF_BUILDKITE_PLUGIN_TEST_MODE=false
130+
# Use pinned version to skip get_latest_version API call
131+
export BUILDKITE_PLUGINS='[{"github.com/buildkite-plugins/monorepo-diff-buildkite-plugin#v1.0.0": {}}]'
132+
133+
# Remove mock binary so it actually needs to download
134+
rm -f "$PWD/monorepo-diff-buildkite-plugin"
135+
136+
# Stub curl to always fail on download
137+
stub curl \
138+
"-sSfL * -o * : exit 1" \
139+
"-sSfL * -o * : exit 1" \
140+
"-sSfL * -o * : exit 1"
141+
142+
run "$PWD/hooks/command"
143+
144+
assert_failure
145+
assert_output --partial "Downloading binary (attempt 1/3)"
146+
assert_output --partial "Downloading binary (attempt 2/3)"
147+
assert_output --partial "Downloading binary (attempt 3/3)"
148+
assert_output --partial "Failed to download"
149+
150+
unstub curl
151+
}

0 commit comments

Comments
 (0)