From 92cb0f9d9a10f771a66da216a59df4906eb46294 Mon Sep 17 00:00:00 2001 From: Penghao He Date: Tue, 6 Aug 2019 13:26:50 -0700 Subject: [PATCH] Add missing unit tests for ecs (#3551) --- .../aws-ecs/test/ec2/test.ec2-service.ts | 73 +++++ .../test/ec2/test.ec2-task-definition.ts | 197 ++++++++++++-- .../test/fargate/test.fargate-service.ts | 254 ++++++++++++++++++ .../aws-ecs/test/test.aws-log-driver.ts | 37 +++ .../aws-ecs/test/test.container-definition.ts | 88 ++++++ .../@aws-cdk/aws-ecs/test/test.ecs-cluster.ts | 109 +++++++- 6 files changed, 740 insertions(+), 18 deletions(-) diff --git a/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts b/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts index 9c5b7da030393..ed504a74f0cef 100644 --- a/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts +++ b/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts @@ -513,6 +513,68 @@ export = { test.done(); }, + "with packedbyCpu placement strategy"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'MyVpc', {}); + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') }); + const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef'); + + taskDefinition.addContainer("web", { + image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"), + memoryLimitMiB: 512 + }); + + const service = new ecs.Ec2Service(stack, "Ec2Service", { + cluster, + taskDefinition, + }); + + service.addPlacementStrategies(PlacementStrategy.packedByCpu()); + + // THEN + expect(stack).to(haveResource("AWS::ECS::Service", { + PlacementStrategies: [{ + Field: "cpu", + Type: "binpack" + }] + })); + + test.done(); + }, + + "with packedbyMemory placement strategy"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'MyVpc', {}); + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') }); + const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef'); + + taskDefinition.addContainer("web", { + image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"), + memoryLimitMiB: 512 + }); + + const service = new ecs.Ec2Service(stack, "Ec2Service", { + cluster, + taskDefinition, + }); + + service.addPlacementStrategies(PlacementStrategy.packedByMemory()); + + // THEN + expect(stack).to(haveResource("AWS::ECS::Service", { + PlacementStrategies: [{ + Field: "memory", + Type: "binpack" + }] + })); + + test.done(); + }, + "with packedBy placement strategy"(test: Test) { // GIVEN const stack = new cdk.Stack(); @@ -1043,6 +1105,17 @@ export = { statistic: 'Average' }); + test.deepEqual(stack.resolve(service.metricCpuUtilization()), { + dimensions: { + ClusterName: { Ref: 'EcsCluster97242B84' }, + ServiceName: { 'Fn::GetAtt': ['ServiceD69D759B', 'Name'] } + }, + namespace: 'AWS/ECS', + metricName: 'CPUUtilization', + period: cdk.Duration.minutes(5), + statistic: 'Average' + }); + test.done(); } }; diff --git a/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-task-definition.ts b/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-task-definition.ts index f2d827fe6c72d..baff4b526cc5d 100644 --- a/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-task-definition.ts +++ b/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-task-definition.ts @@ -1,5 +1,6 @@ import { expect, haveResource, haveResourceLike } from '@aws-cdk/assert'; import { Protocol } from '@aws-cdk/aws-ec2'; +import { Repository } from '@aws-cdk/aws-ecr'; import iam = require('@aws-cdk/aws-iam'); import cdk = require('@aws-cdk/core'); import { Test } from 'nodeunit'; @@ -51,7 +52,6 @@ export = { memoryLimitMiB: 512 // add validation? }); - // TODO test other containerDefinition methods container.addPortMappings({ containerPort: 3000 }); @@ -62,6 +62,16 @@ export = { softLimit: 128 }); + container.addVolumesFrom({ + sourceContainer: "foo", + readOnly: true + }); + + container.addToExecutionPolicy(new iam.PolicyStatement({ + resources: ['*'], + actions: ['ecs:*'], + })); + // THEN expect(stack).to(haveResource("AWS::ECS::TaskDefinition", { Family: "Ec2TaskDef", @@ -75,11 +85,105 @@ export = { HostPort: 0, Protocol: Protocol.TCP }], - Ulimits: [{ - HardLimit: 128, - Name: "rss", - SoftLimit: 128 - }], + Ulimits: [ + { + HardLimit: 128, + Name: "rss", + SoftLimit: 128 + } + ], + VolumesFrom: [ + { + ReadOnly: true, + SourceContainer: "foo" + } + ] + }], + })); + + expect(stack).to(haveResource('AWS::IAM::Policy', { + PolicyDocument: { + Version: '2012-10-17', + Statement: [ + { + Action: "ecs:*", + Effect: "Allow", + Resource: "*" + } + ], + }, + })); + + test.done(); + }, + + "correctly sets containers from ECR repository"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + + const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef'); + + taskDefinition.addContainer("web", { + image: ecs.ContainerImage.fromEcrRepository(new Repository(stack, "myECRImage")), + memoryLimitMiB: 512 + }); + + // THEN + expect(stack).to(haveResource("AWS::ECS::TaskDefinition", { + Family: "Ec2TaskDef", + ContainerDefinitions: [{ + Essential: true, + Memory: 512, + Image: { + "Fn::Join": [ + "", + [ + { + "Fn::Select": [ + 4, + { + "Fn::Split": [ + ":", + { + "Fn::GetAtt": [ + "myECRImage7DEAE474", + "Arn" + ] + } + ] + } + ] + }, + ".dkr.ecr.", + { + "Fn::Select": [ + 3, + { + "Fn::Split": [ + ":", + { + "Fn::GetAtt": [ + "myECRImage7DEAE474", + "Arn" + ] + } + ] + } + ] + }, + ".", + { + Ref: "AWS::URLSuffix" + }, + "/", + { + Ref: "myECRImage7DEAE474" + }, + ":latest" + ] + ] + }, + Name: "web" }], })); @@ -205,19 +309,49 @@ export = { // THEN expect(stack).to(haveResourceLike("AWS::ECS::TaskDefinition", { - ContainerDefinitions: [{ - Links: [ - 'linked1:linked', - 'linked2' + ContainerDefinitions: [ + { + Links: [ + 'linked1:linked', + 'linked2' + ], + Name: "web" + }, + { + Name: 'linked1' + }, + { + Name: 'linked2' + } + ] + })); + + test.done(); + }, + + "correctly set policy statement to the task IAM role"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef'); + + // WHEN + taskDefinition.addToTaskRolePolicy(new iam.PolicyStatement({ + actions: ['test:SpecialName'], + resources: ['*'] + })); + + // THEN + expect(stack).to(haveResource('AWS::IAM::Policy', { + PolicyDocument: { + Version: '2012-10-17', + Statement: [ + { + Action: "test:SpecialName", + Effect: "Allow", + Resource: "*" + } ], - Name: "web" - }, - { - Name: 'linked1' }, - { - Name: 'linked2' - }] })); test.done(); @@ -251,6 +385,35 @@ export = { test.done(); }, + + "correctly set policy statement to the task execution IAM role"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef'); + + // WHEN + taskDefinition.addToExecutionRolePolicy(new iam.PolicyStatement({ + actions: ['test:SpecialName'], + resources: ['*'] + })); + + // THEN + expect(stack).to(haveResource('AWS::IAM::Policy', { + PolicyDocument: { + Version: '2012-10-17', + Statement: [ + { + Action: "test:SpecialName", + Effect: "Allow", + Resource: "*" + } + ], + }, + })); + + test.done(); + }, + "correctly sets volumes"(test: Test) { // GIVEN const stack = new cdk.Stack(); diff --git a/packages/@aws-cdk/aws-ecs/test/fargate/test.fargate-service.ts b/packages/@aws-cdk/aws-ecs/test/fargate/test.fargate-service.ts index cade240f727c2..93f3582f02ce6 100644 --- a/packages/@aws-cdk/aws-ecs/test/fargate/test.fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs/test/fargate/test.fargate-service.ts @@ -1,4 +1,6 @@ import { expect, haveResource, haveResourceLike } from '@aws-cdk/assert'; +import appscaling = require('@aws-cdk/aws-applicationautoscaling'); +import cloudwatch = require('@aws-cdk/aws-cloudwatch'); import ec2 = require('@aws-cdk/aws-ec2'); import elbv2 = require("@aws-cdk/aws-elasticloadbalancingv2"); import cloudmap = require('@aws-cdk/aws-servicediscovery'); @@ -319,6 +321,258 @@ export = { } }, + 'allows scaling on a specified scheduled time'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'MyVpc', {}); + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef'); + const container = taskDefinition.addContainer('MainContainer', { + image: ContainerImage.fromRegistry('hello'), + }); + container.addPortMappings({ containerPort: 8000 }); + + const service = new ecs.FargateService(stack, 'Service', { + cluster, + taskDefinition + }); + + // WHEN + const capacity = service.autoScaleTaskCount({ maxCapacity: 10, minCapacity: 1 }); + capacity.scaleOnSchedule("ScaleOnSchedule", { + schedule: appscaling.Schedule.cron({ hour: '8', minute: '0' }), + minCapacity: 10, + }); + + // THEN + expect(stack).to(haveResource('AWS::ApplicationAutoScaling::ScalableTarget', { + ScheduledActions: [ + { + ScalableTargetAction: { + MinCapacity: 10 + }, + Schedule: "cron(0 8 * * ? *)", + ScheduledActionName: "ScaleOnSchedule" + } + ] + })); + + test.done(); + }, + + 'allows scaling on a specified metric value'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'MyVpc', {}); + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef'); + const container = taskDefinition.addContainer('MainContainer', { + image: ContainerImage.fromRegistry('hello'), + }); + container.addPortMappings({ containerPort: 8000 }); + + const service = new ecs.FargateService(stack, 'Service', { + cluster, + taskDefinition + }); + + // WHEN + const capacity = service.autoScaleTaskCount({ maxCapacity: 10, minCapacity: 1 }); + capacity.scaleOnMetric("ScaleOnMetric", { + metric: new cloudwatch.Metric({ namespace: 'Test', metricName: 'Metric' }), + scalingSteps: [ + { upper: 0, change: -1 }, + { lower: 100, change: +1 }, + { lower: 500, change: +5 } + ] + }); + + // THEN + expect(stack).to(haveResource('AWS::ApplicationAutoScaling::ScalingPolicy', { + PolicyType: "StepScaling", + ScalingTargetId: { + Ref: "ServiceTaskCountTarget23E25614" + }, + StepScalingPolicyConfiguration: { + AdjustmentType: "ChangeInCapacity", + MetricAggregationType: "Average", + StepAdjustments: [ + { + MetricIntervalUpperBound: 0, + ScalingAdjustment: -1 + } + ] + } + })); + + test.done(); + }, + + 'allows scaling on a target CPU utilization'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'MyVpc', {}); + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef'); + const container = taskDefinition.addContainer('MainContainer', { + image: ContainerImage.fromRegistry('hello'), + }); + container.addPortMappings({ containerPort: 8000 }); + + const service = new ecs.FargateService(stack, 'Service', { + cluster, + taskDefinition + }); + + // WHEN + const capacity = service.autoScaleTaskCount({ maxCapacity: 10, minCapacity: 1 }); + capacity.scaleOnCpuUtilization("ScaleOnCpu", { + targetUtilizationPercent: 30 + }); + + // THEN + expect(stack).to(haveResource('AWS::ApplicationAutoScaling::ScalingPolicy', { + PolicyType: "TargetTrackingScaling", + TargetTrackingScalingPolicyConfiguration: { + PredefinedMetricSpecification: { PredefinedMetricType: "ECSServiceAverageCPUUtilization" }, + TargetValue: 30 + } + })); + + test.done(); + }, + + 'allows scaling on memory utilization'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'MyVpc', {}); + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef'); + const container = taskDefinition.addContainer('MainContainer', { + image: ContainerImage.fromRegistry('hello'), + }); + container.addPortMappings({ containerPort: 8000 }); + + const service = new ecs.FargateService(stack, 'Service', { + cluster, + taskDefinition + }); + + // WHEN + const capacity = service.autoScaleTaskCount({ maxCapacity: 10, minCapacity: 1 }); + capacity.scaleOnMemoryUtilization("ScaleOnMemory", { + targetUtilizationPercent: 30 + }); + + // THEN + expect(stack).to(haveResource('AWS::ApplicationAutoScaling::ScalingPolicy', { + PolicyType: "TargetTrackingScaling", + TargetTrackingScalingPolicyConfiguration: { + PredefinedMetricSpecification: { PredefinedMetricType: "ECSServiceAverageMemoryUtilization" }, + TargetValue: 30 + } + })); + + test.done(); + }, + + 'allows scaling on custom CloudWatch metric'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'MyVpc', {}); + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef'); + const container = taskDefinition.addContainer('MainContainer', { + image: ContainerImage.fromRegistry('hello'), + }); + container.addPortMappings({ containerPort: 8000 }); + + const service = new ecs.FargateService(stack, 'Service', { + cluster, + taskDefinition + }); + + // WHEN + const capacity = service.autoScaleTaskCount({ maxCapacity: 10, minCapacity: 1 }); + capacity.scaleToTrackCustomMetric("ScaleOnCustomMetric", { + metric: new cloudwatch.Metric({ namespace: 'Test', metricName: 'Metric' }), + targetValue: 5 + }); + + // THEN + expect(stack).to(haveResource('AWS::ApplicationAutoScaling::ScalingPolicy', { + PolicyType: "TargetTrackingScaling", + TargetTrackingScalingPolicyConfiguration: { + CustomizedMetricSpecification: { + MetricName: "Metric", + Namespace: "Test", + Statistic: "Average" + }, + TargetValue: 5 + } + })); + + test.done(); + }, + + "allow adding a load balancing target to an application target group"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'MyVpc', {}); + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef'); + const container = taskDefinition.addContainer('MainContainer', { + image: ContainerImage.fromRegistry('hello'), + }); + container.addPortMappings({ containerPort: 8000 }); + + const service = new ecs.FargateService(stack, 'Service', { + cluster, + taskDefinition + }); + + const lb = new elbv2.ApplicationLoadBalancer(stack, "lb", { vpc }); + const listener = lb.addListener("listener", { port: 80 }); + const targetGroup = listener.addTargets("target", { + port: 80, + }); + + // WHEN + targetGroup.addTarget(service); + + const capacity = service.autoScaleTaskCount({ maxCapacity: 10, minCapacity: 1 }); + capacity.scaleOnRequestCount("ScaleOnRequests", { + requestsPerTarget: 1000, + targetGroup + }); + + // THEN + expect(stack).to(haveResource('AWS::ApplicationAutoScaling::ScalableTarget', { + MaxCapacity: 10, + MinCapacity: 1, + ResourceId: { + "Fn::Join": [ + "", + [ + "service/", + { + Ref: "EcsCluster97242B84" + }, + "/", + { + "Fn::GetAtt": [ + "ServiceD69D759B", + "Name" + ] + } + ] + ] + }, + })); + + test.done(); + }, + 'When enabling service discovery': { 'throws if namespace has not been added to cluster'(test: Test) { // GIVEN diff --git a/packages/@aws-cdk/aws-ecs/test/test.aws-log-driver.ts b/packages/@aws-cdk/aws-ecs/test/test.aws-log-driver.ts index 4b82ea9a03260..f33b3de7731d4 100644 --- a/packages/@aws-cdk/aws-ecs/test/test.aws-log-driver.ts +++ b/packages/@aws-cdk/aws-ecs/test/test.aws-log-driver.ts @@ -53,6 +53,43 @@ export = { test.done(); }, + "create an aws log driver using awsLogs"(test: Test) { + // WHEN + td.addContainer('Container', { + image, + logging: ecs.AwsLogDriver.awsLogs({ + datetimeFormat: 'format', + logRetention: logs.RetentionDays.ONE_MONTH, + multilinePattern: 'pattern', + streamPrefix: 'hello' + }) + }); + + // THEN + expect(stack).to(haveResource('AWS::Logs::LogGroup', { + RetentionInDays: logs.RetentionDays.ONE_MONTH + })); + + expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', { + ContainerDefinitions: [ + { + LogConfiguration: { + LogDriver: 'awslogs', + Options: { + 'awslogs-group': { Ref: 'TaskDefinitionContainerLogGroup4D0A87C1' }, + 'awslogs-stream-prefix': 'hello', + 'awslogs-region': { Ref: 'AWS::Region' }, + 'awslogs-datetime-format': 'format', + 'awslogs-multiline-pattern': 'pattern' + } + } + } + ] + })); + + test.done(); + }, + 'with a defined log group'(test: Test) { // GIVEN const logGroup = new logs.LogGroup(stack, 'LogGroup'); diff --git a/packages/@aws-cdk/aws-ecs/test/test.container-definition.ts b/packages/@aws-cdk/aws-ecs/test/test.container-definition.ts index 897bd33a8e0e2..dd531d624a813 100644 --- a/packages/@aws-cdk/aws-ecs/test/test.container-definition.ts +++ b/packages/@aws-cdk/aws-ecs/test/test.container-definition.ts @@ -706,6 +706,94 @@ export = { test.done(); }, + + "with one or more host devices"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TaskDef'); + + const linuxParameters = new ecs.LinuxParameters(stack, 'LinuxParameters', { + initProcessEnabled: true, + sharedMemorySize: 1024, + }); + + // WHEN + linuxParameters.addDevices({ + hostPath: "a/b/c", + }); + + taskDefinition.addContainer('cont', { + image: ecs.ContainerImage.fromRegistry('test'), + memoryLimitMiB: 1024, + linuxParameters, + }); + + // THEN + expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', { + ContainerDefinitions: [ + { + Image: 'test', + LinuxParameters: { + Devices: [ + { + HostPath: "a/b/c" + } + ], + Tmpfs: [], + InitProcessEnabled: true, + SharedMemorySize: 1024, + }, + } + ] + })); + + test.done(); + }, + + "with the tmpfs mount for a container"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TaskDef'); + + const linuxParameters = new ecs.LinuxParameters(stack, 'LinuxParameters', { + initProcessEnabled: true, + sharedMemorySize: 1024, + }); + + // WHEN + linuxParameters.addTmpfs({ + containerPath: "a/b/c", + size: 1024 + }); + + taskDefinition.addContainer('cont', { + image: ecs.ContainerImage.fromRegistry('test'), + memoryLimitMiB: 1024, + linuxParameters, + }); + + // THEN + expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', { + ContainerDefinitions: [ + { + Image: 'test', + LinuxParameters: { + Devices: [], + Tmpfs: [ + { + ContainerPath: "a/b/c", + Size: 1024 + } + ], + InitProcessEnabled: true, + SharedMemorySize: 1024, + }, + } + ] + })); + + test.done(); + } }, // render extra hosts test diff --git a/packages/@aws-cdk/aws-ecs/test/test.ecs-cluster.ts b/packages/@aws-cdk/aws-ecs/test/test.ecs-cluster.ts index 072774c51e033..da8f65b0a45f5 100644 --- a/packages/@aws-cdk/aws-ecs/test/test.ecs-cluster.ts +++ b/packages/@aws-cdk/aws-ecs/test/test.ecs-cluster.ts @@ -505,6 +505,72 @@ export = { test.done(); }, + "allows returning the correct image for windows for EcsOptimizedAmi"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const ami = new ecs.EcsOptimizedAmi({ + windowsVersion: ecs.WindowsOptimizedVersion.SERVER_2019, + }); + + test.equal(ami.getImage(stack).osType, ec2.OperatingSystemType.WINDOWS); + + test.done(); + }, + + "allows returning the correct image for linux for EcsOptimizedAmi"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const ami = new ecs.EcsOptimizedAmi({ + generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX + }); + + test.equal(ami.getImage(stack).osType, ec2.OperatingSystemType.LINUX); + + test.done(); + }, + + "allows returning the correct image for linux 2 for EcsOptimizedAmi"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const ami = new ecs.EcsOptimizedAmi({ + generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2 + }); + + test.equal(ami.getImage(stack).osType, ec2.OperatingSystemType.LINUX); + + test.done(); + }, + + "allows returning the correct image for linux for EcsOptimizedImage"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + + test.equal(ecs.EcsOptimizedImage.amazonLinux().getImage(stack).osType, + ec2.OperatingSystemType.LINUX); + + test.done(); + }, + + "allows returning the correct image for linux 2 for EcsOptimizedImage"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + + test.equal(ecs.EcsOptimizedImage.amazonLinux2().getImage(stack).osType, + ec2.OperatingSystemType.LINUX); + + test.done(); + }, + + "allows returning the correct image for windows for EcsOptimizedImage"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + + test.equal(ecs.EcsOptimizedImage.windows(ecs.WindowsOptimizedVersion.SERVER_2019).getImage(stack).osType, + ec2.OperatingSystemType.WINDOWS); + + test.done(); + }, + /* * TODO:v2.0.0 END OF OBSOLETE BLOCK */ @@ -723,5 +789,46 @@ export = { cluster2.vpc.selectSubnets(); test.done(); - } + }, + + "Metric"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'MyVpc', {}); + + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + + // THEN + test.deepEqual(stack.resolve(cluster.metricCpuReservation()), { + dimensions: { + ClusterName: { Ref: 'EcsCluster97242B84' }, + }, + namespace: 'AWS/ECS', + metricName: 'CPUReservation', + period: cdk.Duration.minutes(5), + statistic: 'Average' + }); + + test.deepEqual(stack.resolve(cluster.metricMemoryReservation()), { + dimensions: { + ClusterName: { Ref: 'EcsCluster97242B84' }, + }, + namespace: 'AWS/ECS', + metricName: 'MemoryReservation', + period: cdk.Duration.minutes(5), + statistic: 'Average' + }); + + test.deepEqual(stack.resolve(cluster.metric("myMetric")), { + dimensions: { + ClusterName: { Ref: 'EcsCluster97242B84' }, + }, + namespace: 'AWS/ECS', + metricName: 'myMetric', + period: cdk.Duration.minutes(5), + statistic: 'Average' + }); + + test.done(); + }, };