Skip to content

Commit 19a7f59

Browse files
committed
added assertions for logging buckets
1 parent dd8c5ea commit 19a7f59

File tree

2 files changed

+59
-33
lines changed

2 files changed

+59
-33
lines changed

source/patterns/@aws-solutions-constructs/aws-fargate-s3/README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ Here is a minimal deployable pattern definition in Typescript:
2929
``` typescript
3030
import { FargateToS3, FargateToS3Props } from '@aws-solutions-constructs/aws-fargate-s3';
3131

32-
// Obtain a pre-existing certificate from your account
33-
const certificate = acm.Certificate.fromCertificateArn(
34-
scope,
35-
'existing-cert',
36-
"arn:aws:acm:us-east-1:123456789012:certificate/11112222-3333-1234-1234-123456789012"
37-
);
38-
3932
const props: FargateToS3Props = {
4033
publicApi: true,
4134
ecrRepositoryArn: "arn of a repo in ECR in your account",

source/patterns/@aws-solutions-constructs/aws-fargate-s3/test/fargate-s3.test.ts

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,33 @@ import * as ecs from '@aws-cdk/aws-ecs';
2020

2121
test('New service/new bucket, public API, new VPC', () => {
2222
// An environment with region is required to enable logging on an ALB
23-
const stack = new cdk.Stack(undefined, undefined, {
24-
env: { account: "123456789012", region: 'us-east-1' },
25-
});
23+
const stack = new cdk.Stack();
2624
const publicApi = true;
2725
const clusterName = "custom-cluster-name";
2826
const containerName = "custom-container-name";
2927
const serviceName = "custom-service-name";
3028
const bucketName = "custom-bucket-name";
29+
const familyName = "family-name";
3130

32-
new FargateToS3(stack, 'test-construct', {
31+
const construct = new FargateToS3(stack, 'test-construct', {
3332
publicApi,
3433
ecrRepositoryArn: defaults.fakeEcrRepoArn,
3534
vpcProps: { cidr: '172.0.0.0/16' },
3635
clusterProps: { clusterName },
3736
containerDefinitionProps: { containerName },
38-
fargateTaskDefinitionProps: { family: 'family-name' },
37+
fargateTaskDefinitionProps: { family: familyName},
3938
fargateServiceProps: { serviceName },
4039
bucketProps: { bucketName },
4140
logS3AccessLogs: false,
4241
bucketPermissions: ['Delete', 'Put', 'Read', 'ReadWrite', 'Write']
4342
});
4443

44+
expect(construct.vpc !== null);
45+
expect(construct.service !== null);
46+
expect(construct.container !== null);
47+
expect(construct.s3Bucket !== null);
48+
expect(construct.s3BucketInterface !== null);
49+
4550
expect(stack).toHaveResourceLike("AWS::ECS::Service", {
4651
LaunchType: 'FARGATE',
4752
DesiredCount: 2,
@@ -56,7 +61,7 @@ test('New service/new bucket, public API, new VPC', () => {
5661
ServiceName: serviceName
5762
});
5863
expect(stack).toHaveResourceLike("AWS::ECS::TaskDefinition", {
59-
Family: 'family-name'
64+
Family: familyName
6065
});
6166

6267
expect(stack).toHaveResourceLike("AWS::ECS::Cluster", {
@@ -108,15 +113,21 @@ test('New service/new bucket, public API, new VPC', () => {
108113
test('New service/new bucket, private API, new VPC', () => {
109114

110115
// An environment with region is required to enable logging on an ALB
111-
const stack = new cdk.Stack(undefined, undefined, {
112-
env: { account: "123456789012", region: 'us-east-1' },
113-
});
116+
const stack = new cdk.Stack();
114117
const publicApi = false;
118+
const bucketName = 'bucket-name';
119+
const loggingBucketName = 'logging-bucket-name';
115120

116121
new FargateToS3(stack, 'test-construct', {
117122
publicApi,
118123
ecrRepositoryArn: defaults.fakeEcrRepoArn,
119-
vpcProps: { cidr: '172.0.0.0/16' }
124+
vpcProps: { cidr: '172.0.0.0/16' },
125+
bucketProps: {
126+
bucketName
127+
},
128+
loggingBucketProps: {
129+
bucketName: loggingBucketName
130+
}
120131
});
121132

122133
expect(stack).toHaveResourceLike("AWS::ECS::Service", {
@@ -130,6 +141,7 @@ test('New service/new bucket, private API, new VPC', () => {
130141
});
131142

132143
expect(stack).toHaveResourceLike("AWS::S3::Bucket", {
144+
BucketName: bucketName,
133145
BucketEncryption: {
134146
ServerSideEncryptionConfiguration: [{
135147
ServerSideEncryptionByDefault: {
@@ -139,6 +151,10 @@ test('New service/new bucket, private API, new VPC', () => {
139151
}
140152
});
141153

154+
expect(stack).toHaveResourceLike("AWS::S3::Bucket", {
155+
BucketName: loggingBucketName
156+
});
157+
142158
expect(stack).toHaveResourceLike("AWS::EC2::VPC", {
143159
CidrBlock: '172.0.0.0/16'
144160
});
@@ -151,9 +167,7 @@ test('New service/new bucket, private API, new VPC', () => {
151167

152168
test('New service/existing bucket, private API, existing VPC', () => {
153169
// An environment with region is required to enable logging on an ALB
154-
const stack = new cdk.Stack(undefined, undefined, {
155-
env: { account: "123456789012", region: 'us-east-1' },
156-
});
170+
const stack = new cdk.Stack();
157171
const publicApi = false;
158172
const bucketName = 'custom-bucket-name';
159173

@@ -186,18 +200,22 @@ test('New service/existing bucket, private API, existing VPC', () => {
186200
expect(stack).toHaveResourceLike("AWS::EC2::VPC", {
187201
CidrBlock: '172.168.0.0/16'
188202
});
203+
// Confirm we created an Isolated VPC
204+
expect(stack).not.toHaveResourceLike('AWS::EC2::InternetGateway', {});
189205
expect(stack).toCountResources('AWS::EC2::VPC', 1);
190-
expect(stack).toCountResources('AWS::S3::Bucket', 1);
191206
expect(stack).toCountResources('AWS::ECS::Service', 1);
207+
expect(stack).toCountResources('AWS::S3::Bucket', 1);
192208
});
193209

194210
test('Existing service/new bucket, public API, existing VPC', () => {
195211
// An environment with region is required to enable logging on an ALB
196-
const stack = new cdk.Stack(undefined, undefined, {
197-
env: { account: "123456789012", region: 'us-east-1' },
198-
});
212+
const stack = new cdk.Stack();
199213
const publicApi = true;
200214
const serviceName = 'custom-name';
215+
const customName = 'CUSTOM_NAME';
216+
const customArn = 'CUSTOM_ARN';
217+
const bucketName = 'bucket-name';
218+
const loggingBucketName = 'logging-bucket-name';
201219

202220
const existingVpc = defaults.getTestVpc(stack);
203221

@@ -216,8 +234,14 @@ test('Existing service/new bucket, public API, existing VPC', () => {
216234
existingFargateServiceObject: testService,
217235
existingContainerDefinitionObject: testContainer,
218236
existingVpc,
219-
bucketArnEnvironmentVariableName: 'CUSTOM_ARN',
220-
bucketEnvironmentVariableName: 'CUSTOM_NAME',
237+
bucketArnEnvironmentVariableName: customArn,
238+
bucketEnvironmentVariableName: customName,
239+
bucketProps: {
240+
bucketName
241+
},
242+
loggingBucketProps: {
243+
bucketName: loggingBucketName
244+
}
221245
});
222246

223247
expect(stack).toHaveResourceLike("AWS::ECS::Service", {
@@ -229,7 +253,7 @@ test('Existing service/new bucket, public API, existing VPC', () => {
229253
{
230254
Environment: [
231255
{
232-
Name: 'CUSTOM_ARN',
256+
Name: customArn,
233257
Value: {
234258
"Fn::GetAtt": [
235259
"testconstructS3Bucket81E8552A",
@@ -238,7 +262,7 @@ test('Existing service/new bucket, public API, existing VPC', () => {
238262
}
239263
},
240264
{
241-
Name: 'CUSTOM_NAME',
265+
Name: customName,
242266
Value: {
243267
Ref: "testconstructS3Bucket81E8552A"
244268
}
@@ -268,22 +292,29 @@ test('Existing service/new bucket, public API, existing VPC', () => {
268292
}
269293
]
270294
});
295+
296+
expect(stack).toHaveResourceLike("AWS::S3::Bucket", {
297+
BucketName: bucketName
298+
});
299+
271300
expect(stack).toHaveResourceLike("AWS::S3::Bucket", {
301+
BucketName: loggingBucketName
272302
});
303+
273304
expect(stack).toHaveResourceLike("AWS::EC2::VPC", {
274305
CidrBlock: '172.168.0.0/16'
275306
});
307+
// Confirm we created a Public/Private VPC
308+
expect(stack).toHaveResourceLike('AWS::EC2::InternetGateway', {});
276309
expect(stack).toCountResources('AWS::EC2::VPC', 1);
277-
expect(stack).toCountResources('AWS::S3::Bucket', 2);
278310
expect(stack).toCountResources('AWS::ECS::Service', 1);
311+
expect(stack).toCountResources('AWS::S3::Bucket', 2);
279312
});
280313

281314
// Test existing service/existing bucket, private API, new VPC
282315
test('Existing service/existing bucket, private API, existing VPC', () => {
283316
// An environment with region is required to enable logging on an ALB
284-
const stack = new cdk.Stack(undefined, undefined, {
285-
env: { account: "123456789012", region: 'us-east-1' },
286-
});
317+
const stack = new cdk.Stack();
287318
const publicApi = false;
288319
const serviceName = 'custom-name';
289320
const bucketName = 'custom-bucket-name';
@@ -367,7 +398,9 @@ test('Existing service/existing bucket, private API, existing VPC', () => {
367398
expect(stack).toHaveResourceLike("AWS::EC2::VPC", {
368399
CidrBlock: '172.168.0.0/16'
369400
});
401+
// Confirm we created an Isolated VPC
402+
expect(stack).not.toHaveResourceLike('AWS::EC2::InternetGateway', {});
370403
expect(stack).toCountResources('AWS::EC2::VPC', 1);
371-
expect(stack).toCountResources('AWS::S3::Bucket', 1);
372404
expect(stack).toCountResources('AWS::ECS::Service', 1);
405+
expect(stack).toCountResources('AWS::S3::Bucket', 1);
373406
});

0 commit comments

Comments
 (0)