Skip to content

Commit a7bd925

Browse files
authored
chore(ecs-patterns): migrate to Assertions (#18520)
I removed 3 tests that seem pointless, but I opened an issue to investigate further: #18519 It really seems like the scenario they were testing (omiting the cluster and only providing vpc) is not a working scenario anyway. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent c7f39ca commit a7bd925

9 files changed

+290
-354
lines changed

packages/@aws-cdk/aws-ecs-patterns/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
},
7373
"license": "Apache-2.0",
7474
"devDependencies": {
75-
"@aws-cdk/assert-internal": "0.0.0",
75+
"@aws-cdk/assertions": "0.0.0",
7676
"@aws-cdk/cdk-build-tools": "0.0.0",
7777
"@aws-cdk/cdk-integ-tools": "0.0.0",
7878
"@aws-cdk/cfn2ts": "0.0.0",

packages/@aws-cdk/aws-ecs-patterns/test/ec2/l3s-v2.test.ts

+22-63
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { SynthUtils } from '@aws-cdk/assert-internal';
2-
import '@aws-cdk/assert-internal/jest';
1+
import { Match, Template } from '@aws-cdk/assertions';
32
import { AutoScalingGroup } from '@aws-cdk/aws-autoscaling';
43
import { Certificate } from '@aws-cdk/aws-certificatemanager';
54
import { MachineImage, Vpc } from '@aws-cdk/aws-ec2';
@@ -44,16 +43,16 @@ describe('When Application Load Balancer', () => {
4443
});
4544

4645
// THEN - stack contains a load balancer, a service, and a target group.
47-
expect(stack).toHaveResource('AWS::ElasticLoadBalancingV2::LoadBalancer');
46+
Template.fromStack(stack).resourceCountIs('AWS::ElasticLoadBalancingV2::LoadBalancer', 1);
4847

49-
expect(stack).toHaveResource('AWS::ECS::Service', {
48+
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
5049
DesiredCount: 1,
5150
LaunchType: 'EC2',
5251
});
5352

54-
expect(stack).toHaveResourceLike('AWS::ECS::TaskDefinition', {
53+
Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', {
5554
ContainerDefinitions: [
56-
{
55+
Match.objectLike({
5756
Image: 'test',
5857
LogConfiguration: {
5958
LogDriver: 'awslogs',
@@ -76,7 +75,7 @@ describe('When Application Load Balancer', () => {
7675
Protocol: 'tcp',
7776
},
7877
],
79-
},
78+
}),
8079
],
8180
NetworkMode: 'bridge',
8281
RequiresCompatibilities: [
@@ -167,7 +166,7 @@ describe('When Application Load Balancer', () => {
167166
});
168167

169168
// THEN
170-
expect(stack).toHaveResource('AWS::ECS::Service', {
169+
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
171170
DesiredCount: 3,
172171
LaunchType: 'EC2',
173172
EnableECSManagedTags: true,
@@ -192,7 +191,7 @@ describe('When Application Load Balancer', () => {
192191
ServiceName: 'myService',
193192
});
194193

195-
expect(stack).toHaveResourceLike('AWS::ECS::TaskDefinition', {
194+
Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', {
196195
ContainerDefinitions: [
197196
{
198197
Cpu: 256,
@@ -260,7 +259,7 @@ describe('When Application Load Balancer', () => {
260259
},
261260
});
262261

263-
expect(stack).toHaveResourceLike('AWS::ElasticLoadBalancingV2::Listener', {
262+
Template.fromStack(stack).hasResourceProperties('AWS::ElasticLoadBalancingV2::Listener', {
264263
Port: 443,
265264
Protocol: 'HTTPS',
266265
Certificates: [{
@@ -270,26 +269,6 @@ describe('When Application Load Balancer', () => {
270269
});
271270
});
272271

273-
test('set vpc instead of cluster', () => {
274-
// GIVEN
275-
const stack = new Stack();
276-
const vpc = new Vpc(stack, 'VPC');
277-
278-
// WHEN
279-
new ApplicationMultipleTargetGroupsEc2Service(stack, 'Service', {
280-
vpc,
281-
memoryLimitMiB: 1024,
282-
taskImageOptions: {
283-
image: ContainerImage.fromRegistry('test'),
284-
},
285-
});
286-
287-
// THEN - stack does not contain a LaunchConfiguration
288-
const template = SynthUtils.synthesize(stack, { skipValidation: true });
289-
expect(template).not.toHaveResource('AWS::AutoScaling::LaunchConfiguration');
290-
expect(() => SynthUtils.synthesize(stack)).toThrow();
291-
});
292-
293272
test('able to pass pre-defined task definition', () => {
294273
// GIVEN
295274
const stack = new Stack();
@@ -319,7 +298,7 @@ describe('When Application Load Balancer', () => {
319298
});
320299

321300
// THEN
322-
expect(stack).toHaveResourceLike('AWS::ECS::TaskDefinition', {
301+
Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', {
323302
ContainerDefinitions: [
324303
{
325304
Essential: true,
@@ -408,8 +387,8 @@ describe('When Application Load Balancer', () => {
408387
});
409388

410389
// THEN
411-
const template = SynthUtils.synthesize(stack).template.Outputs;
412-
expect(template).toEqual({
390+
const outputs = Template.fromStack(stack).findOutputs('*');
391+
expect(outputs).toEqual({
413392
ServiceLoadBalancerDNSlb175E78BFE: {
414393
Value: {
415394
'Fn::GetAtt': [
@@ -596,7 +575,7 @@ describe('When Application Load Balancer', () => {
596575
});
597576

598577
// THEN
599-
expect(stack).toHaveResource('AWS::ECS::Service', {
578+
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
600579
ServiceRegistries: [
601580
{
602581
ContainerName: 'web',
@@ -611,7 +590,7 @@ describe('When Application Load Balancer', () => {
611590
],
612591
});
613592

614-
expect(stack).toHaveResource('AWS::ServiceDiscovery::Service', {
593+
Template.fromStack(stack).hasResourceProperties('AWS::ServiceDiscovery::Service', {
615594
DnsConfig: {
616595
DnsRecords: [
617596
{
@@ -926,14 +905,14 @@ describe('When Network Load Balancer', () => {
926905
});
927906

928907
// THEN - stack contains a load balancer and a service
929-
expect(stack).toHaveResource('AWS::ElasticLoadBalancingV2::LoadBalancer');
908+
Template.fromStack(stack).resourceCountIs('AWS::ElasticLoadBalancingV2::LoadBalancer', 1);
930909

931-
expect(stack).toHaveResource('AWS::ECS::Service', {
910+
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
932911
DesiredCount: 1,
933912
LaunchType: 'EC2',
934913
});
935914

936-
expect(stack).toHaveResourceLike('AWS::ECS::TaskDefinition', {
915+
Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', {
937916
ContainerDefinitions: [
938917
{
939918
Essential: true,
@@ -1066,7 +1045,7 @@ describe('When Network Load Balancer', () => {
10661045
});
10671046

10681047
// THEN
1069-
expect(stack).toHaveResource('AWS::ECS::Service', {
1048+
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
10701049
DesiredCount: 3,
10711050
EnableECSManagedTags: true,
10721051
HealthCheckGracePeriodSeconds: 2,
@@ -1092,7 +1071,7 @@ describe('When Network Load Balancer', () => {
10921071
ServiceName: 'myService',
10931072
});
10941073

1095-
expect(stack).toHaveResourceLike('AWS::ECS::TaskDefinition', {
1074+
Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', {
10961075
ContainerDefinitions: [
10971076
{
10981077
Cpu: 256,
@@ -1161,26 +1140,6 @@ describe('When Network Load Balancer', () => {
11611140
});
11621141
});
11631142

1164-
test('set vpc instead of cluster', () => {
1165-
// GIVEN
1166-
const stack = new Stack();
1167-
const vpc = new Vpc(stack, 'VPC');
1168-
1169-
// WHEN
1170-
new NetworkMultipleTargetGroupsEc2Service(stack, 'Service', {
1171-
vpc,
1172-
memoryLimitMiB: 256,
1173-
taskImageOptions: {
1174-
image: ContainerImage.fromRegistry('test'),
1175-
},
1176-
});
1177-
1178-
// THEN - stack does not contain a LaunchConfiguration
1179-
const template = SynthUtils.synthesize(stack, { skipValidation: true });
1180-
expect(template).not.toHaveResource('AWS::AutoScaling::LaunchConfiguration');
1181-
expect(() => SynthUtils.synthesize(stack)).toThrow();
1182-
});
1183-
11841143
test('able to pass pre-defined task definition', () => {
11851144
// GIVEN
11861145
const stack = new Stack();
@@ -1210,7 +1169,7 @@ describe('When Network Load Balancer', () => {
12101169
});
12111170

12121171
// THEN
1213-
expect(stack).toHaveResourceLike('AWS::ECS::TaskDefinition', {
1172+
Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', {
12141173
ContainerDefinitions: [
12151174
{
12161175
Essential: true,
@@ -1357,7 +1316,7 @@ describe('When Network Load Balancer', () => {
13571316
});
13581317

13591318
// THEN
1360-
expect(stack).toHaveResource('AWS::ECS::Service', {
1319+
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
13611320
ServiceRegistries: [
13621321
{
13631322
ContainerName: 'web',
@@ -1372,7 +1331,7 @@ describe('When Network Load Balancer', () => {
13721331
],
13731332
});
13741333

1375-
expect(stack).toHaveResource('AWS::ServiceDiscovery::Service', {
1334+
Template.fromStack(stack).hasResourceProperties('AWS::ServiceDiscovery::Service', {
13761335
DnsConfig: {
13771336
DnsRecords: [
13781337
{

0 commit comments

Comments
 (0)