@@ -8,6 +8,7 @@ import * as logs from '../../aws-logs';
8
8
import * as s3 from '../../aws-s3' ;
9
9
import * as cloudmap from '../../aws-servicediscovery' ;
10
10
import * as cdk from '../../core' ;
11
+ import { getWarnings } from '../../core/test/util' ;
11
12
import * as cxapi from '../../cx-api' ;
12
13
import * as ecs from '../lib' ;
13
14
@@ -2194,36 +2195,76 @@ describe('cluster', () => {
2194
2195
2195
2196
} ) ;
2196
2197
2197
- test ( 'creates ASG capacity providers with expected defaults' , ( ) => {
2198
- // GIVEN
2199
- const app = new cdk . App ( ) ;
2200
- const stack = new cdk . Stack ( app , 'test' ) ;
2201
- const vpc = new ec2 . Vpc ( stack , 'Vpc' ) ;
2202
- const autoScalingGroup = new autoscaling . AutoScalingGroup ( stack , 'asg' , {
2203
- vpc,
2204
- instanceType : new ec2 . InstanceType ( 'bogus' ) ,
2205
- machineImage : ecs . EcsOptimizedImage . amazonLinux2 ( ) ,
2198
+ describe ( 'creates ASG capacity providers ' , ( ) => {
2199
+ test ( 'with expected defaults' , ( ) => {
2200
+ // GIVEN
2201
+ const app = new cdk . App ( ) ;
2202
+ const stack = new cdk . Stack ( app , 'test' ) ;
2203
+ const vpc = new ec2 . Vpc ( stack , 'Vpc' ) ;
2204
+ const autoScalingGroup = new autoscaling . AutoScalingGroup ( stack , 'asg' , {
2205
+ vpc,
2206
+ instanceType : new ec2 . InstanceType ( 'bogus' ) ,
2207
+ machineImage : ecs . EcsOptimizedImage . amazonLinux2 ( ) ,
2208
+ } ) ;
2209
+
2210
+ // WHEN
2211
+ new ecs . AsgCapacityProvider ( stack , 'provider' , {
2212
+ autoScalingGroup,
2213
+ } ) ;
2214
+
2215
+ // THEN
2216
+ Template . fromStack ( stack ) . hasResourceProperties ( 'AWS::ECS::CapacityProvider' , {
2217
+ AutoScalingGroupProvider : {
2218
+ AutoScalingGroupArn : {
2219
+ Ref : 'asgASG4D014670' ,
2220
+ } ,
2221
+ ManagedScaling : {
2222
+ Status : 'ENABLED' ,
2223
+ TargetCapacity : 100 ,
2224
+ } ,
2225
+ ManagedTerminationProtection : 'ENABLED' ,
2226
+ } ,
2227
+ } ) ;
2206
2228
} ) ;
2207
2229
2208
- // WHEN
2209
- new ecs . AsgCapacityProvider ( stack , 'provider' , {
2210
- autoScalingGroup,
2230
+ test ( 'with IAutoScalingGroup should throw an error if Managed Termination Protection is enabled.' , ( ) => {
2231
+ // GIVEN
2232
+ const app = new cdk . App ( ) ;
2233
+ const stack = new cdk . Stack ( app , 'test' ) ;
2234
+ const autoScalingGroup = autoscaling . AutoScalingGroup . fromAutoScalingGroupName ( stack , 'ASG' , 'my-asg' ) ;
2235
+
2236
+ // THEN
2237
+ expect ( ( ) => {
2238
+ new ecs . AsgCapacityProvider ( stack , 'provider' , {
2239
+ autoScalingGroup,
2240
+ } ) ;
2241
+ } ) . toThrow ( 'Cannot enable Managed Termination Protection on a Capacity Provider when providing an imported AutoScalingGroup.' ) ;
2211
2242
} ) ;
2212
2243
2213
- // THEN
2214
- Template . fromStack ( stack ) . hasResourceProperties ( 'AWS::ECS::CapacityProvider' , {
2215
- AutoScalingGroupProvider : {
2216
- AutoScalingGroupArn : {
2217
- Ref : 'asgASG4D014670' ,
2218
- } ,
2219
- ManagedScaling : {
2220
- Status : 'ENABLED' ,
2221
- TargetCapacity : 100 ,
2244
+ test ( 'with IAutoScalingGroup should not throw an error if Managed Termination Protection is disabled.' , ( ) => {
2245
+ // GIVEN
2246
+ const app = new cdk . App ( ) ;
2247
+ const stack = new cdk . Stack ( app , 'test' ) ;
2248
+ const autoScalingGroup = autoscaling . AutoScalingGroup . fromAutoScalingGroupName ( stack , 'ASG' , 'my-asg' ) ;
2249
+
2250
+ // WHEN
2251
+ new ecs . AsgCapacityProvider ( stack , 'provider' , {
2252
+ autoScalingGroup,
2253
+ enableManagedTerminationProtection : false ,
2254
+ } ) ;
2255
+
2256
+ // THEN
2257
+ Template . fromStack ( stack ) . hasResourceProperties ( 'AWS::ECS::CapacityProvider' , {
2258
+ AutoScalingGroupProvider : {
2259
+ AutoScalingGroupArn : 'my-asg' ,
2260
+ ManagedScaling : {
2261
+ Status : 'ENABLED' ,
2262
+ TargetCapacity : 100 ,
2263
+ } ,
2264
+ ManagedTerminationProtection : 'DISABLED' ,
2222
2265
} ,
2223
- ManagedTerminationProtection : 'ENABLED' ,
2224
- } ,
2266
+ } ) ;
2225
2267
} ) ;
2226
-
2227
2268
} ) ;
2228
2269
2229
2270
test ( 'can disable Managed Scaling and Managed Termination Protection for ASG capacity provider' , ( ) => {
@@ -2483,6 +2524,23 @@ describe('cluster', () => {
2483
2524
2484
2525
} ) ;
2485
2526
2527
+ test ( 'throws when calling Cluster.addAsgCapacityProvider with an AsgCapacityProvider created with an imported ASG' , ( ) => {
2528
+ // GIVEN
2529
+ const app = new cdk . App ( ) ;
2530
+ const stack = new cdk . Stack ( app , 'test' ) ;
2531
+ const importedAsg = autoscaling . AutoScalingGroup . fromAutoScalingGroupName ( stack , 'ASG' , 'my-asg' ) ;
2532
+ const cluster = new ecs . Cluster ( stack , 'EcsCluster' ) ;
2533
+
2534
+ const capacityProvider = new ecs . AsgCapacityProvider ( stack , 'provider' , {
2535
+ autoScalingGroup : importedAsg ,
2536
+ enableManagedTerminationProtection : false ,
2537
+ } ) ;
2538
+ // THEN
2539
+ expect ( ( ) => {
2540
+ cluster . addAsgCapacityProvider ( capacityProvider ) ;
2541
+ } ) . toThrow ( 'Cannot configure the AutoScalingGroup because it is an imported resource.' ) ;
2542
+ } ) ;
2543
+
2486
2544
test ( 'should throw an error if capacity provider with default strategy is not present in capacity providers' , ( ) => {
2487
2545
// GIVEN
2488
2546
const app = new cdk . App ( ) ;
@@ -3042,11 +3100,17 @@ test('throws when InstanceWarmupPeriod is greater than 10000', () => {
3042
3100
describe ( 'Accessing container instance role' , function ( ) {
3043
3101
3044
3102
const addUserDataMock = jest . fn ( ) ;
3045
- const autoScalingGroup : autoscaling . AutoScalingGroup = {
3046
- addUserData : addUserDataMock ,
3047
- addToRolePolicy : jest . fn ( ) ,
3048
- protectNewInstancesFromScaleIn : jest . fn ( ) ,
3049
- } as unknown as autoscaling . AutoScalingGroup ;
3103
+
3104
+ function getAutoScalingGroup ( stack : cdk . Stack ) : autoscaling . AutoScalingGroup {
3105
+ const vpc = new ec2 . Vpc ( stack , 'Vpc' ) ;
3106
+ const asg = new autoscaling . AutoScalingGroup ( stack , 'asg' , {
3107
+ vpc,
3108
+ instanceType : new ec2 . InstanceType ( 'bogus' ) ,
3109
+ machineImage : ecs . EcsOptimizedImage . amazonLinux2 ( ) ,
3110
+ } ) ;
3111
+ asg . addUserData = addUserDataMock ;
3112
+ return asg ;
3113
+ }
3050
3114
3051
3115
afterEach ( ( ) => {
3052
3116
addUserDataMock . mockClear ( ) ;
@@ -3057,11 +3121,12 @@ describe('Accessing container instance role', function () {
3057
3121
const app = new cdk . App ( ) ;
3058
3122
const stack = new cdk . Stack ( app , 'test' ) ;
3059
3123
const cluster = new ecs . Cluster ( stack , 'EcsCluster' ) ;
3124
+ const autoScalingGroup = getAutoScalingGroup ( stack ) ;
3060
3125
3061
3126
// WHEN
3062
3127
3063
3128
const capacityProvider = new ecs . AsgCapacityProvider ( stack , 'Provider' , {
3064
- autoScalingGroup : autoScalingGroup ,
3129
+ autoScalingGroup,
3065
3130
} ) ;
3066
3131
3067
3132
cluster . addAsgCapacityProvider ( capacityProvider ) ;
@@ -3077,10 +3142,11 @@ describe('Accessing container instance role', function () {
3077
3142
const app = new cdk . App ( ) ;
3078
3143
const stack = new cdk . Stack ( app , 'test' ) ;
3079
3144
const cluster = new ecs . Cluster ( stack , 'EcsCluster' ) ;
3145
+ const autoScalingGroup = getAutoScalingGroup ( stack ) ;
3080
3146
3081
3147
// WHEN
3082
3148
const capacityProvider = new ecs . AsgCapacityProvider ( stack , 'Provider' , {
3083
- autoScalingGroup : autoScalingGroup ,
3149
+ autoScalingGroup,
3084
3150
} ) ;
3085
3151
3086
3152
cluster . addAsgCapacityProvider ( capacityProvider , {
@@ -3098,6 +3164,7 @@ describe('Accessing container instance role', function () {
3098
3164
const app = new cdk . App ( ) ;
3099
3165
const stack = new cdk . Stack ( app , 'test' ) ;
3100
3166
const cluster = new ecs . Cluster ( stack , 'EcsCluster' ) ;
3167
+ const autoScalingGroup = getAutoScalingGroup ( stack ) ;
3101
3168
3102
3169
// WHEN
3103
3170
const capacityProvider = new ecs . AsgCapacityProvider ( stack , 'Provider' , {
@@ -3118,6 +3185,7 @@ describe('Accessing container instance role', function () {
3118
3185
const app = new cdk . App ( ) ;
3119
3186
const stack = new cdk . Stack ( app , 'test' ) ;
3120
3187
const cluster = new ecs . Cluster ( stack , 'EcsCluster' ) ;
3188
+ const autoScalingGroup = getAutoScalingGroup ( stack ) ;
3121
3189
3122
3190
// WHEN
3123
3191
const capacityProvider = new ecs . AsgCapacityProvider ( stack , 'Provider' , {
@@ -3140,6 +3208,7 @@ describe('Accessing container instance role', function () {
3140
3208
const app = new cdk . App ( ) ;
3141
3209
const stack = new cdk . Stack ( app , 'test' ) ;
3142
3210
const cluster = new ecs . Cluster ( stack , 'EcsCluster' ) ;
3211
+ const autoScalingGroup = getAutoScalingGroup ( stack ) ;
3143
3212
3144
3213
// WHEN
3145
3214
const capacityProvider = new ecs . AsgCapacityProvider ( stack , 'Provider' , {
@@ -3162,6 +3231,7 @@ describe('Accessing container instance role', function () {
3162
3231
const app = new cdk . App ( ) ;
3163
3232
const stack = new cdk . Stack ( app , 'test' ) ;
3164
3233
const cluster = new ecs . Cluster ( stack , 'EcsCluster' ) ;
3234
+ const autoScalingGroup = getAutoScalingGroup ( stack ) ;
3165
3235
3166
3236
// WHEN
3167
3237
const capacityProvider = new ecs . AsgCapacityProvider ( stack , 'Provider' , {
0 commit comments