Skip to content

Commit

Permalink
chore(aws-applicationautoscaling): add unit tests to increase branch …
Browse files Browse the repository at this point in the history
…coverage (#17173)

increasing branch coverage from 74/75% to 80%


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
madeline-k committed Oct 28, 2021
1 parent 7585680 commit 1f570e8
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import '@aws-cdk/assert-internal/jest';
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as iam from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import * as appscaling from '../lib';
import { createScalableTarget } from './util';
Expand All @@ -26,8 +27,6 @@ describe('scalable target', () => {
MinCapacity: 1,
MaxCapacity: 20,
});


});

test('validation does not fail when using Tokens', () => {
Expand All @@ -51,8 +50,6 @@ describe('scalable target', () => {
MinCapacity: 10,
MaxCapacity: 1,
});


});

test('add scheduled scaling', () => {
Expand Down Expand Up @@ -80,8 +77,6 @@ describe('scalable target', () => {
},
],
});


});

test('step scaling on MathExpression', () => {
Expand Down Expand Up @@ -136,21 +131,99 @@ describe('scalable target', () => {
],
Threshold: 49,
});
});

test('test service namespace enum', () => {
expect(appscaling.ServiceNamespace.APPSTREAM).toEqual('appstream');
expect(appscaling.ServiceNamespace.COMPREHEND).toEqual('comprehend');
expect(appscaling.ServiceNamespace.CUSTOM_RESOURCE).toEqual('custom-resource');
expect(appscaling.ServiceNamespace.DYNAMODB).toEqual('dynamodb');
expect(appscaling.ServiceNamespace.EC2).toEqual('ec2');
expect(appscaling.ServiceNamespace.ECS).toEqual('ecs');
expect(appscaling.ServiceNamespace.ELASTIC_MAP_REDUCE).toEqual('elasticmapreduce');
expect(appscaling.ServiceNamespace.LAMBDA).toEqual('lambda');
expect(appscaling.ServiceNamespace.RDS).toEqual('rds');
expect(appscaling.ServiceNamespace.SAGEMAKER).toEqual('sagemaker');
});

test('create scalable target with negative minCapacity throws error', () => {
const stack = new cdk.Stack();
expect(() => {
new appscaling.ScalableTarget(stack, 'Target', {
serviceNamespace: appscaling.ServiceNamespace.DYNAMODB,
scalableDimension: 'test:TestCount',
resourceId: 'test:this/test',
minCapacity: -1,
maxCapacity: 20,
});
}).toThrow('minCapacity cannot be negative, got: -1');
});

test('test service namespace enum', () => {
expect(appscaling.ServiceNamespace.APPSTREAM).toEqual( 'appstream');
expect(appscaling.ServiceNamespace.COMPREHEND).toEqual( 'comprehend');
expect(appscaling.ServiceNamespace.CUSTOM_RESOURCE).toEqual( 'custom-resource');
expect(appscaling.ServiceNamespace.DYNAMODB).toEqual( 'dynamodb');
expect(appscaling.ServiceNamespace.EC2).toEqual( 'ec2');
expect(appscaling.ServiceNamespace.ECS).toEqual( 'ecs');
expect(appscaling.ServiceNamespace.ELASTIC_MAP_REDUCE).toEqual( 'elasticmapreduce');
expect(appscaling.ServiceNamespace.LAMBDA).toEqual( 'lambda');
expect(appscaling.ServiceNamespace.RDS).toEqual( 'rds');
expect(appscaling.ServiceNamespace.SAGEMAKER).toEqual( 'sagemaker');
test('create scalable target with negative maxCapacity throws error', () => {
const stack = new cdk.Stack();
expect(() => {
new appscaling.ScalableTarget(stack, 'Target', {
serviceNamespace: appscaling.ServiceNamespace.DYNAMODB,
scalableDimension: 'test:TestCount',
resourceId: 'test:this/test',
minCapacity: 1,
maxCapacity: -1,
});
}).toThrow('maxCapacity cannot be negative, got: -1');
});

test('create scalable target with maxCapacity less than minCapacity throws error', () => {
const stack = new cdk.Stack();
expect(() => {
new appscaling.ScalableTarget(stack, 'Target', {
serviceNamespace: appscaling.ServiceNamespace.DYNAMODB,
scalableDimension: 'test:TestCount',
resourceId: 'test:this/test',
minCapacity: 2,
maxCapacity: 1,
});
}).toThrow('minCapacity (2) should be lower than maxCapacity (1)');
});

test('create scalable target with custom role', () => {
// GIVEN
const stack = new cdk.Stack();

// WHEN
new appscaling.ScalableTarget(stack, 'Target', {
serviceNamespace: appscaling.ServiceNamespace.DYNAMODB,
scalableDimension: 'test:TestCount',
resourceId: 'test:this/test',
minCapacity: 1,
maxCapacity: 20,
role: new iam.Role(stack, 'Role', {
assumedBy: new iam.ServicePrincipal('test.amazonaws.com'),
}),
});

// THEN
expect(stack).toHaveResource('AWS::ApplicationAutoScaling::ScalableTarget', {
ServiceNamespace: 'dynamodb',
ScalableDimension: 'test:TestCount',
ResourceId: 'test:this/test',
MinCapacity: 1,
MaxCapacity: 20,
RoleARN: {
'Fn::GetAtt': [
'Role1ABCC5F0',
'Arn',
],
},
});
});

test('add scheduled scaling with neither of min/maxCapacity defined throws error', () => {
const stack = new cdk.Stack();
const target = createScalableTarget(stack);
expect(() => {
target.scaleOnSchedule('ScaleUp', {
schedule: appscaling.Schedule.rate(cdk.Duration.minutes(1)),
});
}).toThrow(/You must supply at least one of minCapacity or maxCapacity, got/);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ describe('cron', () => {
expect(appscaling.Schedule.cron({ hour: '18', minute: '24' }).expressionString).toEqual('cron(24 18 * * ? *)');

});
});

describe('rate', () => {
test('rate must be whole number of minutes', () => {
expect(() => {
appscaling.Schedule.rate(Duration.minutes(0.13456));
Expand Down Expand Up @@ -53,3 +55,16 @@ describe('cron', () => {

});
});

describe('expression', () => {
test('test using a literal schedule expression', () => {
expect(appscaling.Schedule.expression('cron(0 18 * * ? *)').expressionString).toEqual('cron(0 18 * * ? *)');

});
});

describe('at', () => {
test('test using at with a specific Date', () => {
expect(appscaling.Schedule.at(new Date(2021, 10, 26)).expressionString).toEqual('at(2021-11-26T00:00:00)');
});
});

0 comments on commit 1f570e8

Please sign in to comment.