Skip to content

Commit

Permalink
Add missing unit tests for ecs (aws#3551)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamhopaul123 authored and mergify[bot] committed Aug 6, 2019
1 parent dd52cec commit 92cb0f9
Show file tree
Hide file tree
Showing 6 changed files with 740 additions and 18 deletions.
73 changes: 73 additions & 0 deletions packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
};
197 changes: 180 additions & 17 deletions packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-task-definition.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -51,7 +52,6 @@ export = {
memoryLimitMiB: 512 // add validation?
});

// TODO test other containerDefinition methods
container.addPortMappings({
containerPort: 3000
});
Expand All @@ -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",
Expand All @@ -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"
}],
}));

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit 92cb0f9

Please sign in to comment.