From 0132a1f46254b65c280f4e679fb33513660842fd Mon Sep 17 00:00:00 2001 From: Niranjan Jayakar Date: Sat, 28 Aug 2021 15:05:56 +0100 Subject: [PATCH] s3 --- packages/@aws-cdk/aws-s3/package.json | 2 +- packages/@aws-cdk/aws-s3/test/aspect.test.ts | 21 +++--- .../aws-s3/test/bucket-policy.test.ts | 47 +++++++------ packages/@aws-cdk/aws-s3/test/cors.test.ts | 25 ++++--- packages/@aws-cdk/aws-s3/test/metrics.test.ts | 45 ++++++------ .../@aws-cdk/aws-s3/test/notification.test.ts | 58 ++++++++-------- packages/@aws-cdk/aws-s3/test/rules.test.ts | 65 +++++++++-------- packages/@aws-cdk/aws-s3/test/util.test.ts | 69 ++++++++++--------- 8 files changed, 164 insertions(+), 168 deletions(-) diff --git a/packages/@aws-cdk/aws-s3/package.json b/packages/@aws-cdk/aws-s3/package.json index da9bdd3d05df4..865be5d5232b8 100644 --- a/packages/@aws-cdk/aws-s3/package.json +++ b/packages/@aws-cdk/aws-s3/package.json @@ -78,8 +78,8 @@ "cdk-build-tools": "0.0.0", "cdk-integ-tools": "0.0.0", "cfn2ts": "0.0.0", + "jest": "^26.6.3", "pkglint": "0.0.0", - "nodeunit-shim": "0.0.0", "@aws-cdk/assert-internal": "0.0.0" }, "dependencies": { diff --git a/packages/@aws-cdk/aws-s3/test/aspect.test.ts b/packages/@aws-cdk/aws-s3/test/aspect.test.ts index ef0394a4cf9cb..023ef826c18f8 100644 --- a/packages/@aws-cdk/aws-s3/test/aspect.test.ts +++ b/packages/@aws-cdk/aws-s3/test/aspect.test.ts @@ -1,12 +1,11 @@ -// import { expect, haveResource, haveResourceLike, SynthUtils } from '@aws-cdk/assert-internal'; +import '@aws-cdk/assert-internal/jest'; import { SynthUtils } from '@aws-cdk/assert-internal'; import * as cdk from '@aws-cdk/core'; import { IConstruct } from 'constructs'; -import { nodeunitShim, Test } from 'nodeunit-shim'; import * as s3 from '../lib'; -nodeunitShim({ - 'bucket must have versioning: failure'(test: Test) { +describe('aspect', () => { + test('bucket must have versioning: failure', () => { // GIVEN const stack = new cdk.Stack(); new s3.Bucket(stack, 'MyBucket'); @@ -17,12 +16,12 @@ nodeunitShim({ // THEN const assembly = SynthUtils.synthesize(stack); const errorMessage = assembly.messages.find(m => m.entry.data === 'Bucket versioning is not enabled'); - test.ok(errorMessage, 'Error message not reported'); + expect(errorMessage).toBeDefined(); - test.done(); - }, - 'bucket must have versioning: success'(test: Test) { + }); + + test('bucket must have versioning: success', () => { // GIVEN const stack = new cdk.Stack(); new s3.Bucket(stack, 'MyBucket', { @@ -34,10 +33,10 @@ nodeunitShim({ // THEN const assembly = SynthUtils.synthesize(stack); - test.deepEqual(assembly.messages, []); + expect(assembly.messages.length).toEqual(0); + - test.done(); - }, + }); }); class BucketVersioningChecker implements cdk.IAspect { diff --git a/packages/@aws-cdk/aws-s3/test/bucket-policy.test.ts b/packages/@aws-cdk/aws-s3/test/bucket-policy.test.ts index b64ab3d99df43..13b4b798bbd09 100644 --- a/packages/@aws-cdk/aws-s3/test/bucket-policy.test.ts +++ b/packages/@aws-cdk/aws-s3/test/bucket-policy.test.ts @@ -1,14 +1,13 @@ -import { expect, haveResource } from '@aws-cdk/assert-internal'; +import '@aws-cdk/assert-internal/jest'; import { AnyPrincipal, PolicyStatement } from '@aws-cdk/aws-iam'; import { RemovalPolicy, Stack, App } from '@aws-cdk/core'; -import { nodeunitShim, Test } from 'nodeunit-shim'; import * as s3 from '../lib'; // to make it easy to copy & paste from output: /* eslint-disable quote-props */ -nodeunitShim({ - 'default properties'(test: Test) { +describe('bucket policy', () => { + test('default properties', () => { const stack = new Stack(); const myBucket = new s3.Bucket(stack, 'MyBucket'); @@ -21,7 +20,7 @@ nodeunitShim({ principals: [new AnyPrincipal()], })); - expect(stack).to(haveResource('AWS::S3::BucketPolicy', { + expect(stack).toHaveResource('AWS::S3::BucketPolicy', { Bucket: { 'Ref': 'MyBucketF68F3FF0', }, @@ -36,12 +35,12 @@ nodeunitShim({ }, ], }, - })); + }); - test.done(); - }, - 'when specifying a removalPolicy at creation'(test: Test) { + }); + + test('when specifying a removalPolicy at creation', () => { const stack = new Stack(); const myBucket = new s3.Bucket(stack, 'MyBucket'); @@ -55,7 +54,7 @@ nodeunitShim({ principals: [new AnyPrincipal()], })); - expect(stack).toMatch({ + expect(stack).toMatchTemplate({ 'Resources': { 'MyBucketF68F3FF0': { 'Type': 'AWS::S3::Bucket', @@ -86,10 +85,10 @@ nodeunitShim({ }, }); - test.done(); - }, - 'when specifying a removalPolicy after creation'(test: Test) { + }); + + test('when specifying a removalPolicy after creation', () => { const stack = new Stack(); const myBucket = new s3.Bucket(stack, 'MyBucket'); @@ -100,7 +99,7 @@ nodeunitShim({ })); myBucket.policy?.applyRemovalPolicy(RemovalPolicy.RETAIN); - expect(stack).toMatch({ + expect(stack).toMatchTemplate({ 'Resources': { 'MyBucketF68F3FF0': { 'Type': 'AWS::S3::Bucket', @@ -131,10 +130,10 @@ nodeunitShim({ }, }); - test.done(); - }, - 'fails if bucket policy has no actions'(test: Test) { + }); + + test('fails if bucket policy has no actions', () => { const app = new App(); const stack = new Stack(app, 'my-stack'); const myBucket = new s3.Bucket(stack, 'MyBucket'); @@ -143,12 +142,12 @@ nodeunitShim({ principals: [new AnyPrincipal()], })); - test.throws(() => app.synth(), /A PolicyStatement must specify at least one \'action\' or \'notAction\'/); + expect(() => app.synth()).toThrow(/A PolicyStatement must specify at least one \'action\' or \'notAction\'/); + - test.done(); - }, + }); - 'fails if bucket policy has no IAM principals'(test: Test) { + test('fails if bucket policy has no IAM principals', () => { const app = new App(); const stack = new Stack(app, 'my-stack'); const myBucket = new s3.Bucket(stack, 'MyBucket'); @@ -157,8 +156,8 @@ nodeunitShim({ actions: ['s3:GetObject*'], })); - test.throws(() => app.synth(), /A PolicyStatement used in a resource-based policy must specify at least one IAM principal/); + expect(() => app.synth()).toThrow(/A PolicyStatement used in a resource-based policy must specify at least one IAM principal/); + - test.done(); - }, + }); }); \ No newline at end of file diff --git a/packages/@aws-cdk/aws-s3/test/cors.test.ts b/packages/@aws-cdk/aws-s3/test/cors.test.ts index 5769c1059640d..feddf0b159669 100644 --- a/packages/@aws-cdk/aws-s3/test/cors.test.ts +++ b/packages/@aws-cdk/aws-s3/test/cors.test.ts @@ -1,10 +1,9 @@ -import { expect, haveResource } from '@aws-cdk/assert-internal'; +import '@aws-cdk/assert-internal/jest'; import { Stack } from '@aws-cdk/core'; -import { nodeunitShim, Test } from 'nodeunit-shim'; import { Bucket, HttpMethods } from '../lib'; -nodeunitShim({ - 'Can use addCors() to add a CORS configuration'(test: Test) { +describe('cors', () => { + test('Can use addCors() to add a CORS configuration', () => { // GIVEN const stack = new Stack(); @@ -16,19 +15,19 @@ nodeunitShim({ }); // THEN - expect(stack).to(haveResource('AWS::S3::Bucket', { + expect(stack).toHaveResource('AWS::S3::Bucket', { CorsConfiguration: { CorsRules: [{ AllowedMethods: ['GET', 'HEAD'], AllowedOrigins: ['https://example.com'], }], }, - })); + }); + - test.done(); - }, + }); - 'Bucket with multiple cors configurations'(test: Test) { + test('Bucket with multiple cors configurations', () => { // GIVEN const stack = new Stack(); @@ -74,7 +73,7 @@ nodeunitShim({ }); // THEN - expect(stack).to(haveResource('AWS::S3::Bucket', { + expect(stack).toHaveResource('AWS::S3::Bucket', { CorsConfiguration: { CorsRules: [ { @@ -114,8 +113,8 @@ nodeunitShim({ }, ], }, - })); + }); + - test.done(); - }, + }); }); diff --git a/packages/@aws-cdk/aws-s3/test/metrics.test.ts b/packages/@aws-cdk/aws-s3/test/metrics.test.ts index bf7e57e04b557..9072c33a5fcb6 100644 --- a/packages/@aws-cdk/aws-s3/test/metrics.test.ts +++ b/packages/@aws-cdk/aws-s3/test/metrics.test.ts @@ -1,10 +1,9 @@ -import { expect, haveResource } from '@aws-cdk/assert-internal'; +import '@aws-cdk/assert-internal/jest'; import { Stack } from '@aws-cdk/core'; -import { nodeunitShim, Test } from 'nodeunit-shim'; import { Bucket } from '../lib'; -nodeunitShim({ - 'Can use addMetrics() to add a metric configuration'(test: Test) { +describe('metrics', () => { + test('Can use addMetrics() to add a metric configuration', () => { // GIVEN const stack = new Stack(); @@ -15,16 +14,16 @@ nodeunitShim({ }); // THEN - expect(stack).to(haveResource('AWS::S3::Bucket', { + expect(stack).toHaveResource('AWS::S3::Bucket', { MetricsConfigurations: [{ Id: 'test', }], - })); + }); + - test.done(); - }, + }); - 'Bucket with metrics on prefix'(test: Test) { + test('Bucket with metrics on prefix', () => { // GIVEN const stack = new Stack(); @@ -37,17 +36,17 @@ nodeunitShim({ }); // THEN - expect(stack).to(haveResource('AWS::S3::Bucket', { + expect(stack).toHaveResource('AWS::S3::Bucket', { MetricsConfigurations: [{ Id: 'test', Prefix: 'prefix', }], - })); + }); + - test.done(); - }, + }); - 'Bucket with metrics on tag filter'(test: Test) { + test('Bucket with metrics on tag filter', () => { // GIVEN const stack = new Stack(); @@ -60,7 +59,7 @@ nodeunitShim({ }); // THEN - expect(stack).to(haveResource('AWS::S3::Bucket', { + expect(stack).toHaveResource('AWS::S3::Bucket', { MetricsConfigurations: [{ Id: 'test', TagFilters: [ @@ -68,12 +67,12 @@ nodeunitShim({ { Key: 'tagname2', Value: 'tagvalue2' }, ], }], - })); + }); + - test.done(); - }, + }); - 'Bucket with multiple metric configurations'(test: Test) { + test('Bucket with multiple metric configurations', () => { // GIVEN const stack = new Stack(); @@ -93,7 +92,7 @@ nodeunitShim({ }); // THEN - expect(stack).to(haveResource('AWS::S3::Bucket', { + expect(stack).toHaveResource('AWS::S3::Bucket', { MetricsConfigurations: [{ Id: 'test', TagFilters: [ @@ -105,8 +104,8 @@ nodeunitShim({ Id: 'test2', Prefix: 'prefix', }], - })); + }); + - test.done(); - }, + }); }); diff --git a/packages/@aws-cdk/aws-s3/test/notification.test.ts b/packages/@aws-cdk/aws-s3/test/notification.test.ts index 75906826ddeff..2f3178fb42af0 100644 --- a/packages/@aws-cdk/aws-s3/test/notification.test.ts +++ b/packages/@aws-cdk/aws-s3/test/notification.test.ts @@ -1,10 +1,10 @@ -import { expect, haveResource, haveResourceLike, ResourcePart } from '@aws-cdk/assert-internal'; +import '@aws-cdk/assert-internal/jest'; +import { ResourcePart } from '@aws-cdk/assert-internal'; import * as cdk from '@aws-cdk/core'; -import { nodeunitShim, Test } from 'nodeunit-shim'; import * as s3 from '../lib'; -nodeunitShim({ - 'when notification is added a custom s3 bucket notification resource is provisioned'(test: Test) { +describe('notification', () => { + test('when notification is added a custom s3 bucket notification resource is provisioned', () => { const stack = new cdk.Stack(); const bucket = new s3.Bucket(stack, 'MyBucket'); @@ -16,8 +16,8 @@ nodeunitShim({ }), }); - expect(stack).to(haveResource('AWS::S3::Bucket')); - expect(stack).to(haveResource('Custom::S3BucketNotifications', { + expect(stack).toHaveResource('AWS::S3::Bucket'); + expect(stack).toHaveResource('Custom::S3BucketNotifications', { NotificationConfiguration: { TopicConfigurations: [ { @@ -28,12 +28,12 @@ nodeunitShim({ }, ], }, - })); + }); + - test.done(); - }, + }); - 'can specify prefix and suffix filter rules'(test: Test) { + test('can specify prefix and suffix filter rules', () => { const stack = new cdk.Stack(); const bucket = new s3.Bucket(stack, 'MyBucket'); @@ -45,7 +45,7 @@ nodeunitShim({ }), }, { prefix: 'images/', suffix: '.png' }); - expect(stack).to(haveResource('Custom::S3BucketNotifications', { + expect(stack).toHaveResource('Custom::S3BucketNotifications', { NotificationConfiguration: { TopicConfigurations: [ { @@ -70,12 +70,12 @@ nodeunitShim({ }, ], }, - })); + }); + - test.done(); - }, + }); - 'the notification lambda handler must depend on the role to prevent executing too early'(test: Test) { + test('the notification lambda handler must depend on the role to prevent executing too early', () => { const stack = new cdk.Stack(); const bucket = new s3.Bucket(stack, 'MyBucket'); @@ -87,7 +87,7 @@ nodeunitShim({ }), }); - expect(stack).to(haveResourceLike('AWS::Lambda::Function', { + expect(stack).toHaveResourceLike('AWS::Lambda::Function', { Type: 'AWS::Lambda::Function', Properties: { Role: { @@ -99,38 +99,38 @@ nodeunitShim({ }, DependsOn: ['BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleDefaultPolicy2CF63D36', 'BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleB6FB88EC'], - }, ResourcePart.CompleteDefinition ) ); + }, ResourcePart.CompleteDefinition ); - test.done(); - }, - 'throws with multiple prefix rules in a filter'(test: Test) { + }); + + test('throws with multiple prefix rules in a filter', () => { const stack = new cdk.Stack(); const bucket = new s3.Bucket(stack, 'MyBucket'); - test.throws(() => bucket.addEventNotification(s3.EventType.OBJECT_CREATED, { + expect(() => bucket.addEventNotification(s3.EventType.OBJECT_CREATED, { bind: () => ({ arn: 'ARN', type: s3.BucketNotificationDestinationType.TOPIC, }), - }, { prefix: 'images/' }, { prefix: 'archive/' }), /prefix rule/); + }, { prefix: 'images/' }, { prefix: 'archive/' })).toThrow(/prefix rule/); + - test.done(); - }, + }); - 'throws with multiple suffix rules in a filter'(test: Test) { + test('throws with multiple suffix rules in a filter', () => { const stack = new cdk.Stack(); const bucket = new s3.Bucket(stack, 'MyBucket'); - test.throws(() => bucket.addEventNotification(s3.EventType.OBJECT_CREATED, { + expect(() => bucket.addEventNotification(s3.EventType.OBJECT_CREATED, { bind: () => ({ arn: 'ARN', type: s3.BucketNotificationDestinationType.TOPIC, }), - }, { suffix: '.png' }, { suffix: '.zip' }), /suffix rule/); + }, { suffix: '.png' }, { suffix: '.zip' })).toThrow(/suffix rule/); + - test.done(); - }, + }); }); diff --git a/packages/@aws-cdk/aws-s3/test/rules.test.ts b/packages/@aws-cdk/aws-s3/test/rules.test.ts index fb613bcf109e8..818ae7d830439 100644 --- a/packages/@aws-cdk/aws-s3/test/rules.test.ts +++ b/packages/@aws-cdk/aws-s3/test/rules.test.ts @@ -1,10 +1,9 @@ -import { expect, haveResource } from '@aws-cdk/assert-internal'; +import '@aws-cdk/assert-internal/jest'; import { Duration, Stack } from '@aws-cdk/core'; -import { nodeunitShim, Test } from 'nodeunit-shim'; import { Bucket, StorageClass } from '../lib'; -nodeunitShim({ - 'Bucket with expiration days'(test: Test) { +describe('rules', () => { + test('Bucket with expiration days', () => { // GIVEN const stack = new Stack(); @@ -16,19 +15,19 @@ nodeunitShim({ }); // THEN - expect(stack).to(haveResource('AWS::S3::Bucket', { + expect(stack).toHaveResource('AWS::S3::Bucket', { LifecycleConfiguration: { Rules: [{ ExpirationInDays: 30, Status: 'Enabled', }], }, - })); + }); + - test.done(); - }, + }); - 'Can use addLifecycleRule() to add a lifecycle rule'(test: Test) { + test('Can use addLifecycleRule() to add a lifecycle rule', () => { // GIVEN const stack = new Stack(); @@ -39,19 +38,19 @@ nodeunitShim({ }); // THEN - expect(stack).to(haveResource('AWS::S3::Bucket', { + expect(stack).toHaveResource('AWS::S3::Bucket', { LifecycleConfiguration: { Rules: [{ ExpirationInDays: 30, Status: 'Enabled', }], }, - })); + }); - test.done(); - }, - 'Bucket with expiration date'(test: Test) { + }); + + test('Bucket with expiration date', () => { // GIVEN const stack = new Stack(); @@ -63,19 +62,19 @@ nodeunitShim({ }); // THEN - expect(stack).to(haveResource('AWS::S3::Bucket', { + expect(stack).toHaveResource('AWS::S3::Bucket', { LifecycleConfiguration: { Rules: [{ ExpirationDate: '2018-01-01T00:00:00', Status: 'Enabled', }], }, - })); + }); + - test.done(); - }, + }); - 'Bucket with transition rule'(test: Test) { + test('Bucket with transition rule', () => { // GIVEN const stack = new Stack(); @@ -90,7 +89,7 @@ nodeunitShim({ }); // THEN - expect(stack).to(haveResource('AWS::S3::Bucket', { + expect(stack).toHaveResource('AWS::S3::Bucket', { LifecycleConfiguration: { Rules: [{ Transitions: [{ @@ -100,23 +99,23 @@ nodeunitShim({ Status: 'Enabled', }], }, - })); + }); + - test.done(); - }, + }); - 'Noncurrent rule on nonversioned bucket fails'(test: Test) { + test('Noncurrent rule on nonversioned bucket fails', () => { // GIVEN const stack = new Stack(); // WHEN: Fail because of lack of versioning - test.throws(() => { + expect(() => { new Bucket(stack, 'Bucket1', { lifecycleRules: [{ noncurrentVersionExpiration: Duration.days(10), }], }); - }); + }).toThrow(); // WHEN: Succeeds because versioning is enabled new Bucket(stack, 'Bucket2', { @@ -126,10 +125,10 @@ nodeunitShim({ }], }); - test.done(); - }, - 'Bucket with expiredObjectDeleteMarker'(test: Test) { + }); + + test('Bucket with expiredObjectDeleteMarker', () => { // GIVEN const stack = new Stack(); @@ -141,15 +140,15 @@ nodeunitShim({ }); // THEN - expect(stack).to(haveResource('AWS::S3::Bucket', { + expect(stack).toHaveResource('AWS::S3::Bucket', { LifecycleConfiguration: { Rules: [{ ExpiredObjectDeleteMarker: true, Status: 'Enabled', }], }, - })); + }); + - test.done(); - }, + }); }); diff --git a/packages/@aws-cdk/aws-s3/test/util.test.ts b/packages/@aws-cdk/aws-s3/test/util.test.ts index 528b83b10404b..e688932a0f7eb 100644 --- a/packages/@aws-cdk/aws-s3/test/util.test.ts +++ b/packages/@aws-cdk/aws-s3/test/util.test.ts @@ -1,68 +1,69 @@ +import '@aws-cdk/assert-internal/jest'; import * as cdk from '@aws-cdk/core'; -import { nodeunitShim, Test } from 'nodeunit-shim'; import { parseBucketArn, parseBucketName } from '../lib/util'; -nodeunitShim({ - parseBucketArn: { - 'explicit arn'(test: Test) { +describe('utils', () => { + describe('parseBucketArn', () => { + test('explicit arn', () => { const stack = new cdk.Stack(); const bucketArn = 'my:bucket:arn'; - test.deepEqual(parseBucketArn(stack, { bucketArn }), bucketArn); - test.done(); - }, + expect(parseBucketArn(stack, { bucketArn })).toEqual(bucketArn); - 'produce arn from bucket name'(test: Test) { + }); + + test('produce arn from bucket name', () => { const stack = new cdk.Stack(); const bucketName = 'hello'; - test.deepEqual(stack.resolve(parseBucketArn(stack, { bucketName })), { + expect(stack.resolve(parseBucketArn(stack, { bucketName }))).toEqual({ 'Fn::Join': ['', ['arn:', { Ref: 'AWS::Partition' }, ':s3:::hello']], }); - test.done(); - }, - 'fails if neither arn nor name are provided'(test: Test) { + }); + + test('fails if neither arn nor name are provided', () => { const stack = new cdk.Stack(); - test.throws(() => parseBucketArn(stack, {}), /Cannot determine bucket ARN. At least `bucketArn` or `bucketName` is needed/); - test.done(); - }, - }, + expect(() => parseBucketArn(stack, {})).toThrow(/Cannot determine bucket ARN. At least `bucketArn` or `bucketName` is needed/); - parseBucketName: { + }); + }); - 'explicit name'(test: Test) { + describe('parseBucketName', () => { + + test('explicit name', () => { const stack = new cdk.Stack(); const bucketName = 'foo'; - test.deepEqual(stack.resolve(parseBucketName(stack, { bucketName })), 'foo'); - test.done(); - }, + expect(stack.resolve(parseBucketName(stack, { bucketName }))).toEqual('foo'); + + }); - 'extract bucket name from string arn'(test: Test) { + test('extract bucket name from string arn', () => { const stack = new cdk.Stack(); const bucketArn = 'arn:aws:s3:::my-bucket'; - test.deepEqual(stack.resolve(parseBucketName(stack, { bucketArn })), 'my-bucket'); - test.done(); - }, + expect(stack.resolve(parseBucketName(stack, { bucketArn }))).toEqual('my-bucket'); - 'can parse bucket name even if it contains a token'(test: Test) { + }); + + test('can parse bucket name even if it contains a token', () => { const stack = new cdk.Stack(); const bucketArn = `arn:aws:s3:::${cdk.Token.asString({ Ref: 'my-bucket' })}`; - test.deepEqual( + expect( stack.resolve(parseBucketName(stack, { bucketArn })), + ).toEqual( { Ref: 'my-bucket' }, ); - test.done(); - }, - 'fails if ARN has invalid format'(test: Test) { + }); + + test('fails if ARN has invalid format', () => { const stack = new cdk.Stack(); const bucketArn = 'invalid-arn'; - test.throws(() => parseBucketName(stack, { bucketArn }), /ARNs must/); - test.done(); - }, - }, + expect(() => parseBucketName(stack, { bucketArn })).toThrow(/ARNs must/); + + }); + }); });