From be4b3ab6d5195cdac0096ea08aa0f181ff36d278 Mon Sep 17 00:00:00 2001 From: Kaizen Conroy Date: Wed, 30 Oct 2024 16:28:34 -0400 Subject: [PATCH] fix(cli): ecr garbage collection hangs when repository has no images --- .../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: [