Skip to content

Commit fa99f74

Browse files
authored
fix: avoid mktemp (#85)
1 parent d29b741 commit fa99f74

File tree

3 files changed

+13
-77
lines changed

3 files changed

+13
-77
lines changed

lib/backends/s3.bash

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,11 @@ function restore() {
120120
fi
121121

122122
if [[ ! "${BK_AWS_FOUND}" =~ (false) ]]; then
123-
TMP_FILE="$(mktemp)"
124-
aws s3 cp ${BK_CUSTOM_AWS_ARGS} "s3://${BUCKET}/${TAR_FILE}" "${TMP_FILE}" || s3_download_failed=true
123+
aws s3 cp ${BK_CUSTOM_AWS_ARGS} "s3://${BUCKET}/${TAR_FILE}" "${TAR_FILE}" || s3_download_failed=true
125124
if ${s3_download_failed:-false}; then
126125
echo -e "S3 download failed, soft failing and skipping cache restore..."
126+
rm -f "${TAR_FILE}"
127127
return 0
128-
else
129-
mv -f "${TMP_FILE}" "${TAR_FILE}"
130128
fi
131129

132130
if [ "${BK_CACHE_SAVE_CACHE}" == "true" ]; then
@@ -169,9 +167,7 @@ function cache() {
169167
fi
170168

171169
if [ ! -f "$TAR_FILE" ]; then
172-
TMP_FILE="$(mktemp)"
173-
tar "${BK_TAR_ARGS[@]}" "${TMP_FILE}" ${TAR_TARGETS}
174-
mv -f "${TMP_FILE}" "${TAR_FILE}"
170+
tar "${BK_TAR_ARGS[@]}" "${TAR_FILE}" ${TAR_TARGETS}
175171
aws s3 cp ${BK_CUSTOM_AWS_ARGS} "${TAR_FILE}" "s3://${BUCKET}/$(basename "${TAR_FILE}")"
176172
fi
177173
}

lib/backends/tarball.bash

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ function cache() {
126126
fi
127127

128128
if [ ! -f "$TAR_FILE" ]; then
129-
TMP_FILE="$(mktemp)"
130-
tar "${BK_TAR_ARGS[@]}" "${TMP_FILE}" ${TAR_TARGETS}
131-
mv -f "${TMP_FILE}" "${TAR_FILE}"
129+
tar "${BK_TAR_ARGS[@]}" "${TAR_FILE}" ${TAR_TARGETS}
132130
fi
133131
}

tests/command.bats

Lines changed: 9 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,9 @@ setup() {
1010
}
1111

1212
@test "Pre-command restores cache with basic key" {
13-
stub mktemp \
14-
" : echo '/tmp/tempfile'"
15-
16-
stub mv \
17-
"-f /tmp/tempfile v1-cache-key.tar : true"
18-
1913
stub aws \
2014
"s3api head-object --bucket my-bucket --key 'my-org/my-pipeline/v1-cache-key.tar' --profile my-profile : true" \
21-
"s3 cp --profile my-profile s3://my-bucket/my-org/my-pipeline/v1-cache-key.tar /tmp/tempfile : echo Copied from S3"
15+
"s3 cp --profile my-profile s3://my-bucket/my-org/my-pipeline/v1-cache-key.tar v1-cache-key.tar : echo Copied from S3"
2216

2317
stub tar \
2418
"-xf v1-cache-key.tar -C . : echo Extracted tar archive"
@@ -45,8 +39,6 @@ setup() {
4539

4640
unstub aws
4741
unstub tar
48-
unstub mv
49-
unstub mktemp
5042
}
5143

5244
@test "Pre-command restores S3 backed cache using local file" {
@@ -84,12 +76,8 @@ setup() {
8476

8577
@test "Post-command syncs artifacts with a single path" {
8678

87-
stub mktemp \
88-
" : echo '/tmp/tempfile'"
8979
stub tar \
90-
"--ignore-failed-read -cf /tmp/tempfile Pods : echo Created tar archive"
91-
stub mv \
92-
"-f /tmp/tempfile v1-cache-key.tar : true"
80+
"--ignore-failed-read -cf v1-cache-key.tar Pods : echo Created tar archive"
9381
stub aws \
9482
"s3 cp --profile my-profile v1-cache-key.tar s3://my-bucket/my-org/my-pipeline/v1-cache-key.tar : echo Copied to S3"
9583

@@ -116,24 +104,16 @@ setup() {
116104
unset BUILDKITE_PIPELINE_SLUG
117105
unset BUILDKITE_ORGANIZATION_SLUG
118106

119-
unstub mktemp
120107
unstub tar
121-
unstub mv
122108
unstub aws
123109
}
124110

125111
@test "Cache key template evaluation on file" {
126112
CHECKSUM=355831032f586e782b45744f2ed79316cc830244
127113

128-
stub mktemp \
129-
" : echo '/tmp/tempfile'"
130-
131-
stub mv \
132-
"-f /tmp/tempfile v1-cache-key-${CHECKSUM}.tar : true"
133-
134114
stub aws \
135115
"s3api head-object --bucket my-bucket --key 'my-org/my-pipeline/v1-cache-key-${CHECKSUM}.tar' --profile my-profile : true" \
136-
"s3 cp --profile my-profile s3://my-bucket/my-org/my-pipeline/v1-cache-key-${CHECKSUM}.tar /tmp/tempfile : echo Copied from S3"
116+
"s3 cp --profile my-profile s3://my-bucket/my-org/my-pipeline/v1-cache-key-${CHECKSUM}.tar v1-cache-key-${CHECKSUM}.tar : echo Copied from S3"
137117

138118
stub tar \
139119
"-xf v1-cache-key-${CHECKSUM}.tar -C . : echo Extracted tar archive"
@@ -157,24 +137,16 @@ setup() {
157137
unset BUILDKITE_PIPELINE_SLUG
158138
unset BUILDKITE_ORGANIZATION_SLUG
159139

160-
unstub mktemp
161-
unstub mv
162140
unstub aws
163141
unstub tar
164142
}
165143

166144
@test "Cache key template evaluation on dir" {
167145
CHECKSUM=4cfa4e590847976f26d761074e355e4d95fa8107
168146

169-
stub mktemp \
170-
" : echo '/tmp/tempfile'"
171-
172-
stub mv \
173-
"-f /tmp/tempfile v1-cache-key-${CHECKSUM}.tar : true"
174-
175147
stub aws \
176148
"s3api head-object --bucket my-bucket --key 'my-org/my-pipeline/v1-cache-key-${CHECKSUM}.tar' --profile my-profile : true" \
177-
"s3 cp --profile my-profile s3://my-bucket/my-org/my-pipeline/v1-cache-key-${CHECKSUM}.tar /tmp/tempfile : echo Copied from S3"
149+
"s3 cp --profile my-profile s3://my-bucket/my-org/my-pipeline/v1-cache-key-${CHECKSUM}.tar v1-cache-key-${CHECKSUM}.tar : echo Copied from S3"
178150

179151
stub tar \
180152
"-xf v1-cache-key-${CHECKSUM}.tar -C . : echo Extracted tar archive"
@@ -198,24 +170,16 @@ setup() {
198170
unset BUILDKITE_PIPELINE_SLUG
199171
unset BUILDKITE_ORGANIZATION_SLUG
200172

201-
unstub mktemp
202-
unstub mv
203173
unstub aws
204174
unstub tar
205175
}
206176

207177
@test "Cache key multi-template evaluation" {
208178
CHECKSUMS=355831032f586e782b45744f2ed79316cc830244-241bc31c8ddc004c48e6d88d7fa51ee981b8ce51
209179

210-
stub mktemp \
211-
" : echo '/tmp/tempfile'"
212-
213-
stub mv \
214-
"-f /tmp/tempfile v1-cache-key-${CHECKSUMS}.tar : true"
215-
216180
stub aws \
217181
"s3api head-object --bucket my-bucket --key 'my-org/my-pipeline/v1-cache-key-${CHECKSUMS}.tar' --profile my-profile : true" \
218-
"s3 cp --profile my-profile s3://my-bucket/my-org/my-pipeline/v1-cache-key-${CHECKSUMS}.tar /tmp/tempfile : echo Copied from S3"
182+
"s3 cp --profile my-profile s3://my-bucket/my-org/my-pipeline/v1-cache-key-${CHECKSUMS}.tar v1-cache-key-${CHECKSUMS}.tar : echo Copied from S3"
219183

220184
stub tar \
221185
"-xf v1-cache-key-${CHECKSUMS}.tar -C . : echo Extracted tar archive"
@@ -239,24 +203,16 @@ setup() {
239203
unset BUILDKITE_PIPELINE_SLUG
240204
unset BUILDKITE_ORGANIZATION_SLUG
241205

242-
unstub mktemp
243-
unstub mv
244206
unstub aws
245207
unstub tar
246208
}
247209

248210
@test "Cache key template evaluation in middle of key" {
249211
CHECKSUM=355831032f586e782b45744f2ed79316cc830244
250212

251-
stub mktemp \
252-
" : echo '/tmp/tempfile'"
253-
254-
stub mv \
255-
"-f /tmp/tempfile v1-cache-${CHECKSUM}-key.tar : true"
256-
257213
stub aws \
258214
"s3api head-object --bucket my-bucket --key 'my-org/my-pipeline/v1-cache-$CHECKSUM-key.tar' --profile my-profile : true" \
259-
"s3 cp --profile my-profile s3://my-bucket/my-org/my-pipeline/v1-cache-$CHECKSUM-key.tar /tmp/tempfile : echo Copied from S3"
215+
"s3 cp --profile my-profile s3://my-bucket/my-org/my-pipeline/v1-cache-$CHECKSUM-key.tar v1-cache-$CHECKSUM-key.tar : echo Copied from S3"
260216

261217
stub tar \
262218
"-xf v1-cache-$CHECKSUM-key.tar -C . : echo Extracted tar archive"
@@ -280,8 +236,6 @@ setup() {
280236
unset BUILDKITE_PIPELINE_SLUG
281237
unset BUILDKITE_ORGANIZATION_SLUG
282238

283-
unstub mktemp
284-
unstub mv
285239
unstub aws
286240
unstub tar
287241
}
@@ -310,15 +264,9 @@ setup() {
310264

311265
@test "S3 arguments are passed through to copy command" {
312266

313-
stub mktemp \
314-
" : echo '/tmp/tempfile'"
315-
316-
stub mv \
317-
"-f /tmp/tempfile v1-cache-key.tar : true"
318-
319267
stub aws \
320268
"s3api head-object --bucket my-bucket --key 'my-org/my-pipeline/v1-cache-key.tar' --profile my-profile : true" \
321-
"s3 cp --profile my-profile --acl bucket-owner-full-control s3://my-bucket/my-org/my-pipeline/v1-cache-key.tar /tmp/tempfile : echo Copied from S3"
269+
"s3 cp --profile my-profile --acl bucket-owner-full-control s3://my-bucket/my-org/my-pipeline/v1-cache-key.tar v1-cache-key.tar : echo Copied from S3"
322270

323271
stub tar \
324272
"-xf v1-cache-key.tar -C . : echo Extracted tar archive"
@@ -344,20 +292,15 @@ setup() {
344292
unset BUILDKITE_PIPELINE_SLUG
345293
unset BUILDKITE_ORGANIZATION_SLUG
346294

347-
unstub mktemp
348-
unstub mv
349295
unstub aws
350296
unstub tar
351297
}
352298

353299
@test "S3 download errors cause soft failure" {
354300

355-
stub mktemp \
356-
" : echo '/tmp/tempfile'"
357-
358301
stub aws \
359302
"s3api head-object --bucket my-bucket --key 'my-org/my-pipeline/v1-cache-key.tar' --profile my-profile : true" \
360-
"s3 cp --profile my-profile --acl bucket-owner-full-control s3://my-bucket/my-org/my-pipeline/v1-cache-key.tar /tmp/tempfile : false"
303+
"s3 cp --profile my-profile --acl bucket-owner-full-control s3://my-bucket/my-org/my-pipeline/v1-cache-key.tar v1-cache-key.tar : false"
361304

362305
export BUILDKITE_ORGANIZATION_SLUG="my-org"
363306
export BUILDKITE_PIPELINE_SLUG="my-pipeline"
@@ -379,6 +322,5 @@ setup() {
379322
unset BUILDKITE_PIPELINE_SLUG
380323
unset BUILDKITE_ORGANIZATION_SLUG
381324

382-
unstub mktemp
383325
unstub aws
384-
}
326+
}

0 commit comments

Comments
 (0)