import { ABSENT } from '@aws-cdk/assert-internal' ; import '@aws-cdk/assert-internal/jest' ; import * as ecs from '@aws-cdk/aws-ecs' ; import * as cdk from '@aws-cdk/core' ; import { Container , Environment , Service , ServiceDescription } from '../lib' ; describe ( 'service' , ( ) => { test ( 'should error if a service is prepared with no addons' , ( ) => { const stack = new cdk . Stack ( ) ; const environment = new Environment ( stack , 'production' ) ; const serviceDescription = new ServiceDescription ( ) ; expect ( ( ) => { new Service ( stack , 'my-service' , { environment , serviceDescription , } ) ; } ) . toThrow ( /Service 'my-service' must have a Container extension/ ) ; } ) ; test ( 'allows scaling on a target CPU utilization' , ( ) => { const stack = new cdk . Stack ( ) ; const environment = new Environment ( stack , 'production' ) ; const serviceDescription = new ServiceDescription ( ) ; serviceDescription . add ( new Container ( { cpu : 256 , memoryMiB : 512 , trafficPort : 80 , image : ecs . ContainerImage . fromRegistry ( 'nathanpeck/name' ) , } ) ) ; new Service ( stack , 'my-service' , { environment , serviceDescription , desiredCount : 3 , autoScaleTaskCount : { maxTaskCount : 5 , targetCpuUtilization : 70 , } , } ) ; expect ( stack ) . toHaveResourceLike ( 'AWS::ECS::Service' , { DesiredCount : ABSENT , } ) ; expect ( stack ) . toHaveResourceLike ( 'AWS::ApplicationAutoScaling::ScalableTarget' , { MaxCapacity : 5 , MinCapacity : 1 , } ) ; expect ( stack ) . toHaveResource ( 'AWS::ApplicationAutoScaling::ScalingPolicy' , { PolicyType : 'TargetTrackingScaling' , TargetTrackingScalingPolicyConfiguration : { PredefinedMetricSpecification : { PredefinedMetricType : 'ECSServiceAverageCPUUtilization' } , TargetValue : 70 , } , } ) ; } ) ; test ( 'allows scaling on a target memory utilization' , ( ) => { const stack = new cdk . Stack ( ) ; const environment = new Environment ( stack , 'production' ) ; const serviceDescription = new ServiceDescription ( ) ; serviceDescription . add ( new Container ( { cpu : 256 , memoryMiB : 512 , trafficPort : 80 , image : ecs . ContainerImage . fromRegistry ( 'nathanpeck/name' ) , } ) ) ; new Service ( stack , 'my-service' , { environment , serviceDescription , desiredCount : 3 , autoScaleTaskCount : { maxTaskCount : 5 , targetMemoryUtilization : 70 , } , } ) ; expect ( stack ) . toHaveResourceLike ( 'AWS::ECS::Service' , { DesiredCount : ABSENT , } ) ; expect ( stack ) . toHaveResourceLike ( 'AWS::ApplicationAutoScaling::ScalableTarget' , { MaxCapacity : 5 , MinCapacity : 1 , } ) ; expect ( stack ) . toHaveResource ( 'AWS::ApplicationAutoScaling::ScalingPolicy' , { PolicyType : 'TargetTrackingScaling' , TargetTrackingScalingPolicyConfiguration : { PredefinedMetricSpecification : { PredefinedMetricType : 'ECSServiceAverageMemoryUtilization' } , TargetValue : 70 , } , } ) ; } ) ; test ( 'should error when no auto scaling policies have been configured after creating the auto scaling target' , ( ) => { const stack = new cdk . Stack ( ) ; const environment = new Environment ( stack , 'production' ) ; const serviceDescription = new ServiceDescription ( ) ; serviceDescription . add ( new Container ( { cpu : 256 , memoryMiB : 512 , trafficPort : 80 , image : ecs . ContainerImage . fromRegistry ( 'nathanpeck/name' ) , } ) ) ; expect ( ( ) => { new Service ( stack , 'my-service' , { environment , serviceDescription , autoScaleTaskCount : { maxTaskCount : 5 , } , } ) ; } ) . toThrow ( /The auto scaling target for the service 'my-service' has been created but no auto scaling policies have been configured./ ) ; } ) ; } ) ;