From a235a9fad29bf0da141b27ca391861fa613f42b8 Mon Sep 17 00:00:00 2001 From: Kaizen Conroy <36202692+kaizencc@users.noreply.github.com> Date: Wed, 30 Oct 2024 18:57:41 -0400 Subject: [PATCH] fix(cli): ecr garbage collection hangs when repository has no images (#31951) because somehow I wrote `continue` instead of `break` in that instance... added a test to make sure. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../garbage-collection/garbage-collector.ts | 2 +- .../test/api/garbage-collection.test.ts | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/aws-cdk/lib/api/garbage-collection/garbage-collector.ts b/packages/aws-cdk/lib/api/garbage-collection/garbage-collector.ts index 67f7f98cd4ec0..cc2065db0a001 100644 --- a/packages/aws-cdk/lib/api/garbage-collection/garbage-collector.ts +++ b/packages/aws-cdk/lib/api/garbage-collection/garbage-collector.ts @@ -635,7 +635,7 @@ export class GarbageCollector { // No images in the repository if (!response.imageIds || response.imageIds.length === 0) { - continue; + break; } // map unique image digest to (possibly multiple) tags diff --git a/packages/aws-cdk/test/api/garbage-collection.test.ts b/packages/aws-cdk/test/api/garbage-collection.test.ts index 220cf8f565072..73c739f6cab71 100644 --- a/packages/aws-cdk/test/api/garbage-collection.test.ts +++ b/packages/aws-cdk/test/api/garbage-collection.test.ts @@ -633,6 +633,40 @@ describe('ECR Garbage Collection', () => { }); }); + test('succeeds when no images are present', async () => { + mockTheToolkitInfo({ + Outputs: [ + { + OutputKey: 'BootstrapVersion', + OutputValue: '999', + }, + ], + }); + + const mockListImagesNone = jest.fn().mockImplementation(() => { + return Promise.resolve({ + images: [], + }); + }); + + sdk.stubEcr({ + batchGetImage: mockBatchGetImage, + describeImages: mockDescribeImages, + batchDeleteImage: mockBatchDeleteImage, + putImage: mockPutImage, + listImages: mockListImagesNone, + }); + + garbageCollector = garbageCollector = gc({ + type: 'ecr', + rollbackBufferDays: 0, + action: 'full', + }); + + // succeeds without hanging + await garbageCollector.garbageCollect(); + }); + test('tags are unique', async () => { mockTheToolkitInfo({ Outputs: [