|
| 1 | +import { GetObjectTaggingCommand, ListObjectsV2Command, PutObjectTaggingCommand } from '@aws-sdk/client-s3'; |
| 2 | +import { integTest, randomString, withoutBootstrap } from '../../lib'; |
| 3 | + |
| 4 | +jest.setTimeout(2 * 60 * 60_000); // Includes the time to acquire locks, worst-case single-threaded runtime |
| 5 | + |
| 6 | +integTest( |
| 7 | + 'Garbage Collection deletes unused assets', |
| 8 | + withoutBootstrap(async (fixture) => { |
| 9 | + const toolkitStackName = fixture.bootstrapStackName; |
| 10 | + const bootstrapBucketName = `aws-cdk-garbage-collect-integ-test-bckt-${randomString()}`; |
| 11 | + fixture.rememberToDeleteBucket(bootstrapBucketName); // just in case |
| 12 | + |
| 13 | + await fixture.cdkBootstrapModern({ |
| 14 | + toolkitStackName, |
| 15 | + bootstrapBucketName, |
| 16 | + }); |
| 17 | + |
| 18 | + await fixture.cdkDeploy('lambda', { |
| 19 | + options: [ |
| 20 | + '--context', `bootstrapBucket=${bootstrapBucketName}`, |
| 21 | + '--context', `@aws-cdk/core:bootstrapQualifier=${fixture.qualifier}`, |
| 22 | + '--toolkit-stack-name', toolkitStackName, |
| 23 | + '--force', |
| 24 | + ], |
| 25 | + }); |
| 26 | + fixture.log('Setup complete!'); |
| 27 | + |
| 28 | + await fixture.cdkDestroy('lambda', { |
| 29 | + options: [ |
| 30 | + '--context', `bootstrapBucket=${bootstrapBucketName}`, |
| 31 | + '--context', `@aws-cdk/core:bootstrapQualifier=${fixture.qualifier}`, |
| 32 | + '--toolkit-stack-name', toolkitStackName, |
| 33 | + '--force', |
| 34 | + ], |
| 35 | + }); |
| 36 | + |
| 37 | + await fixture.cdkGarbageCollect({ |
| 38 | + rollbackBufferDays: 0, |
| 39 | + type: 's3', |
| 40 | + bootstrapStackName: toolkitStackName, |
| 41 | + }); |
| 42 | + fixture.log('Garbage collection complete!'); |
| 43 | + |
| 44 | + // assert that the bootstrap bucket is empty |
| 45 | + await fixture.aws.s3.send(new ListObjectsV2Command({ Bucket: bootstrapBucketName })) |
| 46 | + .then((result) => { |
| 47 | + expect(result.Contents).toBeUndefined(); |
| 48 | + }); |
| 49 | + }), |
| 50 | +); |
| 51 | + |
| 52 | +integTest( |
| 53 | + 'Garbage Collection keeps in use assets', |
| 54 | + withoutBootstrap(async (fixture) => { |
| 55 | + const toolkitStackName = fixture.bootstrapStackName; |
| 56 | + const bootstrapBucketName = `aws-cdk-garbage-collect-integ-test-bckt-${randomString()}`; |
| 57 | + fixture.rememberToDeleteBucket(bootstrapBucketName); // just in case |
| 58 | + |
| 59 | + await fixture.cdkBootstrapModern({ |
| 60 | + toolkitStackName, |
| 61 | + bootstrapBucketName, |
| 62 | + }); |
| 63 | + |
| 64 | + await fixture.cdkDeploy('lambda', { |
| 65 | + options: [ |
| 66 | + '--context', `bootstrapBucket=${bootstrapBucketName}`, |
| 67 | + '--context', `@aws-cdk/core:bootstrapQualifier=${fixture.qualifier}`, |
| 68 | + '--toolkit-stack-name', toolkitStackName, |
| 69 | + '--force', |
| 70 | + ], |
| 71 | + }); |
| 72 | + fixture.log('Setup complete!'); |
| 73 | + |
| 74 | + await fixture.cdkGarbageCollect({ |
| 75 | + rollbackBufferDays: 0, |
| 76 | + type: 's3', |
| 77 | + bootstrapStackName: toolkitStackName, |
| 78 | + }); |
| 79 | + fixture.log('Garbage collection complete!'); |
| 80 | + |
| 81 | + // assert that the bootstrap bucket has the object |
| 82 | + await fixture.aws.s3.send(new ListObjectsV2Command({ Bucket: bootstrapBucketName })) |
| 83 | + .then((result) => { |
| 84 | + expect(result.Contents).toHaveLength(1); |
| 85 | + }); |
| 86 | + |
| 87 | + await fixture.cdkDestroy('lambda', { |
| 88 | + options: [ |
| 89 | + '--context', `bootstrapBucket=${bootstrapBucketName}`, |
| 90 | + '--context', `@aws-cdk/core:bootstrapQualifier=${fixture.qualifier}`, |
| 91 | + '--toolkit-stack-name', toolkitStackName, |
| 92 | + '--force', |
| 93 | + ], |
| 94 | + }); |
| 95 | + fixture.log('Teardown complete!'); |
| 96 | + }), |
| 97 | +); |
| 98 | + |
| 99 | +integTest( |
| 100 | + 'Garbage Collection tags unused assets', |
| 101 | + withoutBootstrap(async (fixture) => { |
| 102 | + const toolkitStackName = fixture.bootstrapStackName; |
| 103 | + const bootstrapBucketName = `aws-cdk-garbage-collect-integ-test-bckt-${randomString()}`; |
| 104 | + fixture.rememberToDeleteBucket(bootstrapBucketName); // just in case |
| 105 | + |
| 106 | + await fixture.cdkBootstrapModern({ |
| 107 | + toolkitStackName, |
| 108 | + bootstrapBucketName, |
| 109 | + }); |
| 110 | + |
| 111 | + await fixture.cdkDeploy('lambda', { |
| 112 | + options: [ |
| 113 | + '--context', `bootstrapBucket=${bootstrapBucketName}`, |
| 114 | + '--context', `@aws-cdk/core:bootstrapQualifier=${fixture.qualifier}`, |
| 115 | + '--toolkit-stack-name', toolkitStackName, |
| 116 | + '--force', |
| 117 | + ], |
| 118 | + }); |
| 119 | + fixture.log('Setup complete!'); |
| 120 | + |
| 121 | + await fixture.cdkDestroy('lambda', { |
| 122 | + options: [ |
| 123 | + '--context', `bootstrapBucket=${bootstrapBucketName}`, |
| 124 | + '--context', `@aws-cdk/core:bootstrapQualifier=${fixture.qualifier}`, |
| 125 | + '--toolkit-stack-name', toolkitStackName, |
| 126 | + '--force', |
| 127 | + ], |
| 128 | + }); |
| 129 | + |
| 130 | + await fixture.cdkGarbageCollect({ |
| 131 | + rollbackBufferDays: 100, // this will ensure that we do not delete assets immediately (and just tag them) |
| 132 | + type: 's3', |
| 133 | + bootstrapStackName: toolkitStackName, |
| 134 | + }); |
| 135 | + fixture.log('Garbage collection complete!'); |
| 136 | + |
| 137 | + // assert that the bootstrap bucket has the object and is tagged |
| 138 | + await fixture.aws.s3.send(new ListObjectsV2Command({ Bucket: bootstrapBucketName })) |
| 139 | + .then(async (result) => { |
| 140 | + expect(result.Contents).toHaveLength(2); // also the CFN template |
| 141 | + const key = result.Contents![0].Key; |
| 142 | + const tags = await fixture.aws.s3.send(new GetObjectTaggingCommand({ Bucket: bootstrapBucketName, Key: key })); |
| 143 | + expect(tags.TagSet).toHaveLength(1); |
| 144 | + }); |
| 145 | + }), |
| 146 | +); |
| 147 | + |
| 148 | +integTest( |
| 149 | + 'Garbage Collection untags in-use assets', |
| 150 | + withoutBootstrap(async (fixture) => { |
| 151 | + const toolkitStackName = fixture.bootstrapStackName; |
| 152 | + const bootstrapBucketName = `aws-cdk-garbage-collect-integ-test-bckt-${randomString()}`; |
| 153 | + fixture.rememberToDeleteBucket(bootstrapBucketName); // just in case |
| 154 | + |
| 155 | + await fixture.cdkBootstrapModern({ |
| 156 | + toolkitStackName, |
| 157 | + bootstrapBucketName, |
| 158 | + }); |
| 159 | + |
| 160 | + await fixture.cdkDeploy('lambda', { |
| 161 | + options: [ |
| 162 | + '--context', `bootstrapBucket=${bootstrapBucketName}`, |
| 163 | + '--context', `@aws-cdk/core:bootstrapQualifier=${fixture.qualifier}`, |
| 164 | + '--toolkit-stack-name', toolkitStackName, |
| 165 | + '--force', |
| 166 | + ], |
| 167 | + }); |
| 168 | + fixture.log('Setup complete!'); |
| 169 | + |
| 170 | + // Artificially add tagging to the asset in the bootstrap bucket |
| 171 | + const result = await fixture.aws.s3.send(new ListObjectsV2Command({ Bucket: bootstrapBucketName })); |
| 172 | + const key = result.Contents!.filter((c) => c.Key?.split('.')[1] == 'zip')[0].Key; // fancy footwork to make sure we have the asset key |
| 173 | + await fixture.aws.s3.send(new PutObjectTaggingCommand({ |
| 174 | + Bucket: bootstrapBucketName, |
| 175 | + Key: key, |
| 176 | + Tagging: { |
| 177 | + TagSet: [{ |
| 178 | + Key: 'aws-cdk:isolated', |
| 179 | + Value: '12345', |
| 180 | + }, { |
| 181 | + Key: 'bogus', |
| 182 | + Value: 'val', |
| 183 | + }], |
| 184 | + }, |
| 185 | + })); |
| 186 | + |
| 187 | + await fixture.cdkGarbageCollect({ |
| 188 | + rollbackBufferDays: 100, // this will ensure that we do not delete assets immediately (and just tag them) |
| 189 | + type: 's3', |
| 190 | + bootstrapStackName: toolkitStackName, |
| 191 | + }); |
| 192 | + fixture.log('Garbage collection complete!'); |
| 193 | + |
| 194 | + // assert that the isolated object tag is removed while the other tag remains |
| 195 | + const newTags = await fixture.aws.s3.send(new GetObjectTaggingCommand({ Bucket: bootstrapBucketName, Key: key })); |
| 196 | + |
| 197 | + expect(newTags.TagSet).toEqual([{ |
| 198 | + Key: 'bogus', |
| 199 | + Value: 'val', |
| 200 | + }]); |
| 201 | + }), |
| 202 | +); |
0 commit comments