|
1 | | -import { expect, haveResource } from '@aws-cdk/assert'; |
| 1 | +import { expect, haveResource, haveResourceLike } from '@aws-cdk/assert'; |
2 | 2 | import * as ec2 from '@aws-cdk/aws-ec2'; |
3 | 3 | import * as elb from '@aws-cdk/aws-elasticloadbalancing'; |
4 | 4 | import * as elbv2 from '@aws-cdk/aws-elasticloadbalancingv2'; |
@@ -541,14 +541,48 @@ nodeunitShim({ |
541 | 541 | const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); |
542 | 542 | const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef'); |
543 | 543 |
|
| 544 | + // Errors on validation, not on construction. |
| 545 | + new ecs.Ec2Service(stack, 'Ec2Service', { |
| 546 | + cluster, |
| 547 | + taskDefinition, |
| 548 | + }); |
| 549 | + |
544 | 550 | // THEN |
545 | 551 | test.throws(() => { |
546 | | - new ecs.Ec2Service(stack, 'Ec2Service', { |
547 | | - cluster, |
548 | | - taskDefinition, |
549 | | - }); |
| 552 | + expect(stack); |
| 553 | + }, /one essential container/); |
| 554 | + |
| 555 | + test.done(); |
| 556 | + }, |
| 557 | + |
| 558 | + 'allows adding the default container after creating the service'(test: Test) { |
| 559 | + // GIVEN |
| 560 | + const stack = new cdk.Stack(); |
| 561 | + const vpc = new ec2.Vpc(stack, 'MyVpc', {}); |
| 562 | + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); |
| 563 | + cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') }); |
| 564 | + const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef'); |
| 565 | + |
| 566 | + new ecs.Ec2Service(stack, 'FargateService', { |
| 567 | + cluster, |
| 568 | + taskDefinition, |
550 | 569 | }); |
551 | 570 |
|
| 571 | + // Add the container *after* creating the service |
| 572 | + taskDefinition.addContainer('main', { |
| 573 | + image: ecs.ContainerImage.fromRegistry('somecontainer'), |
| 574 | + memoryReservationMiB: 10, |
| 575 | + }); |
| 576 | + |
| 577 | + // THEN |
| 578 | + expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', { |
| 579 | + ContainerDefinitions: [ |
| 580 | + { |
| 581 | + Name: 'main', |
| 582 | + }, |
| 583 | + ], |
| 584 | + })); |
| 585 | + |
552 | 586 | test.done(); |
553 | 587 | }, |
554 | 588 |
|
|
0 commit comments