|
| 1 | +import { ListObjectsV2Command, PutObjectTaggingCommand } from '@aws-sdk/client-s3'; |
| 2 | +import { integTest, withoutBootstrap, randomString } from '../../lib'; |
| 3 | + |
| 4 | +jest.setTimeout(2 * 60 * 60_000); // Includes the time to acquire locks, worst-case single-threaded runtime |
| 5 | + |
| 6 | +const DAY = 24 * 60 * 60 * 1000; |
| 7 | +const S3_ISOLATED_TAG = 'aws-cdk:isolated'; |
| 8 | + |
| 9 | +integTest( |
| 10 | + 'Garbage Collection deletes unused s3 objects with rollback-buffer-days', |
| 11 | + withoutBootstrap(async (fixture) => { |
| 12 | + const toolkitStackName = fixture.bootstrapStackName; |
| 13 | + const bootstrapBucketName = `aws-cdk-garbage-collect-integ-test-bckt-${randomString()}`; |
| 14 | + fixture.rememberToDeleteBucket(bootstrapBucketName); // just in case |
| 15 | + |
| 16 | + await fixture.cdkBootstrapModern({ |
| 17 | + toolkitStackName, |
| 18 | + bootstrapBucketName, |
| 19 | + }); |
| 20 | + |
| 21 | + await fixture.cdkDeploy('lambda', { |
| 22 | + options: [ |
| 23 | + '--context', `bootstrapBucket=${bootstrapBucketName}`, |
| 24 | + '--context', `@aws-cdk/core:bootstrapQualifier=${fixture.qualifier}`, |
| 25 | + '--toolkit-stack-name', toolkitStackName, |
| 26 | + '--force', |
| 27 | + ], |
| 28 | + }); |
| 29 | + fixture.log('Setup complete!'); |
| 30 | + |
| 31 | + await fixture.cdkDestroy('lambda', { |
| 32 | + options: [ |
| 33 | + '--context', `bootstrapBucket=${bootstrapBucketName}`, |
| 34 | + '--context', `@aws-cdk/core:bootstrapQualifier=${fixture.qualifier}`, |
| 35 | + '--toolkit-stack-name', toolkitStackName, |
| 36 | + '--force', |
| 37 | + ], |
| 38 | + }); |
| 39 | + |
| 40 | + // Pretend the assets were tagged with an old date > 1 day ago so that garbage collection |
| 41 | + // should pick up and delete asset even with rollbackBufferDays=1 |
| 42 | + const res = await fixture.aws.s3.send(new ListObjectsV2Command({ Bucket: bootstrapBucketName })); |
| 43 | + for (const contents of res.Contents ?? []) { |
| 44 | + await fixture.aws.s3.send(new PutObjectTaggingCommand({ |
| 45 | + Bucket: bootstrapBucketName, |
| 46 | + Key: contents.Key, |
| 47 | + Tagging: { |
| 48 | + TagSet: [{ |
| 49 | + Key: S3_ISOLATED_TAG, |
| 50 | + Value: String(Date.now() - (30 * DAY)), |
| 51 | + }], |
| 52 | + }, |
| 53 | + })); |
| 54 | + } |
| 55 | + |
| 56 | + await fixture.cdkGarbageCollect({ |
| 57 | + rollbackBufferDays: 1, |
| 58 | + type: 's3', |
| 59 | + bootstrapStackName: toolkitStackName, |
| 60 | + }); |
| 61 | + fixture.log('Garbage collection complete!'); |
| 62 | + |
| 63 | + // assert that the bootstrap bucket is empty |
| 64 | + await fixture.aws.s3.send(new ListObjectsV2Command({ Bucket: bootstrapBucketName })) |
| 65 | + .then((result) => { |
| 66 | + expect(result.Contents).toBeUndefined(); |
| 67 | + }); |
| 68 | + }), |
| 69 | +); |
0 commit comments