Skip to content

Commit 5662a64

Browse files
authored
Merge pull request #61 from buildkite-plugins/toote_list_object
Correct usage of `list-object-v2`
2 parents 8310d2f + ee42ffc commit 5662a64

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

backends/cache_s3

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ s3_sync() {
3333
s3_listobjects() {
3434
local prefix="$1"
3535

36-
aws_cmd=(aws s3api list-objects-v2 --bucket "${BUILDKITE_PLUGIN_S3_CACHE_BUCKET}" --prefix "$(build_key "${prefix}")" --max-items 1)
36+
aws_cmd=(aws s3api list-objects-v2 --bucket "${BUILDKITE_PLUGIN_S3_CACHE_BUCKET}" --prefix "$(build_key "${prefix}")" --max-items 1 --query Contents)
3737

3838
if [ -n "${BUILDKITE_PLUGIN_S3_CACHE_ENDPOINT}" ]; then
3939
aws_cmd+=(--endpoint-url "${BUILDKITE_PLUGIN_S3_CACHE_ENDPOINT}")
4040
fi
4141

42-
"${aws_cmd[@]}"
42+
result=$("${aws_cmd[@]}")
43+
[ "${result}" != 'null' ]
4344
}
4445

4546
restore_cache() {
@@ -56,7 +57,7 @@ save_cache() {
5657

5758
exists_cache() {
5859
if [ -z "$1" ]; then exit 1; fi
59-
[ -n "$(s3_listobjects "$1")" ]
60+
s3_listobjects "$1"
6061
}
6162

6263
OPCODE="$1"

tests/cache_s3.bats

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ setup() {
3737
}
3838

3939
@test 'Exists on non-existing file fails' {
40-
stub aws 'exit 1'
40+
stub aws 'echo null'
4141

4242
run "${PWD}/backends/cache_s3" exists PATH/THAT/DOES/NOT/EXIST
4343

@@ -97,10 +97,10 @@ setup() {
9797
stub aws \
9898
's3 sync --endpoint-url https://s3.somewhere.com \* \* : echo ' \
9999
's3 sync --endpoint-url https://s3.somewhere.com \* \* : echo ' \
100-
's3api list-objects-v2 --bucket \* --prefix \* --max-items 1 --endpoint-url https://s3.somewhere.com : echo exists' \
100+
's3api list-objects-v2 --bucket \* --prefix \* --max-items 1 --query Contents --endpoint-url https://s3.somewhere.com : echo exists' \
101101
's3 sync \* \* : echo ' \
102102
's3 sync \* \* : echo ' \
103-
's3api list-objects-v2 --bucket \* --prefix \* --max-items 1 : echo exists'
103+
's3api list-objects-v2 --bucket \* --prefix \* --max-items 1 --query Contents : echo exists'
104104

105105
run "${PWD}/backends/cache_s3" save from to
106106

@@ -141,7 +141,7 @@ setup() {
141141
touch "${BATS_TEST_TMPDIR}/new-file"
142142
mkdir "${BATS_TEST_TMPDIR}/s3-cache"
143143
stub aws \
144-
"echo" \
144+
"echo null" \
145145
"ln -s \$3 $BATS_TEST_TMPDIR/s3-cache/\$(echo \$4 | md5sum | cut -c-32)" \
146146
"echo 'exists'" \
147147
"cp -r $BATS_TEST_TMPDIR/s3-cache/\$(echo \$3 | md5sum | cut -c-32) \$4"
@@ -179,7 +179,7 @@ setup() {
179179
echo 'random content' > "${BATS_TEST_TMPDIR}/new-folder/new-file"
180180

181181
stub aws \
182-
"echo" \
182+
"echo null" \
183183
"ln -s \$3 $BATS_TEST_TMPDIR/s3-cache/\$(echo \$4 | md5sum | cut -c-32)" \
184184
"echo 'exists'" \
185185
"cp -r $BATS_TEST_TMPDIR/s3-cache/\$(echo \$3 | md5sum | cut -c-32) \$4"

0 commit comments

Comments
 (0)