Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ecs): add additional missing unit tests #3679

Merged
merged 4 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
244 changes: 202 additions & 42 deletions packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, haveResource } from '@aws-cdk/assert';
import ec2 = require('@aws-cdk/aws-ec2');
import elb = require('@aws-cdk/aws-elasticloadbalancing');
import elbv2 = require("@aws-cdk/aws-elasticloadbalancingv2");
import cloudmap = require('@aws-cdk/aws-servicediscovery');
import cdk = require('@aws-cdk/core');
import { Test } from 'nodeunit';
Expand All @@ -10,7 +11,7 @@ import { LaunchType } from '../../lib/base/base-service';
import { PlacementConstraint, PlacementStrategy } from '../../lib/placement';

export = {
"When creating an ECS Service": {
"When creating an EC2 Service": {
"with only required properties set, it correctly sets default properties"(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down Expand Up @@ -164,23 +165,28 @@ export = {
test.done();
},

"allows a cluster without any capacity added to it"(test: Test) {
// GIVEN
"throws when task definition is not EC2 compatible"(test: Test) {
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef');
const taskDefinition = new ecs.TaskDefinition(stack, 'FargateTaskDef', {
compatibility: ecs.Compatibility.FARGATE,
cpu: "256",
memoryMiB: "512"
});
taskDefinition.addContainer('BaseContainer', {
image: ecs.ContainerImage.fromRegistry('test'),
memoryReservationMiB: 10,
});

new ecs.Ec2Service(stack, "Ec2Service", {
cluster,
taskDefinition,
});

// THEN
test.throws(() => {
new ecs.Ec2Service(stack, "Ec2Service", {
cluster,
taskDefinition,
});
}, /Supplied TaskDefinition is not configured for compatibility with EC2/);

test.done();
},

Expand Down Expand Up @@ -898,8 +904,8 @@ export = {
}
},

'classic ELB': {
'can attach to classic ELB'(test: Test) {
"attachToClassicLB": {
"allows network mode of task definition to be host"(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');
Expand All @@ -911,28 +917,14 @@ export = {
memoryLimitMiB: 1024,
});
container.addPortMappings({ containerPort: 808 });
const service = new ecs.Ec2Service(stack, 'Service', { cluster, taskDefinition});

// WHEN
const lb = new elb.LoadBalancer(stack, 'LB', { vpc });
lb.addTarget(service);
const service = new ecs.Ec2Service(stack, 'Service', {
cluster,
taskDefinition
});

// THEN
expect(stack).to(haveResource('AWS::ECS::Service', {
LoadBalancers: [
{
ContainerName: "web",
ContainerPort: 808,
LoadBalancerName: { Ref: "LB8A12904C" }
}
]
}));

expect(stack).to(haveResource('AWS::ECS::Service', {
// if any load balancer is configured and healthCheckGracePeriodSeconds is not
// set, then it should default to 60 seconds.
HealthCheckGracePeriodSeconds: 60
}));
const lb = new elb.LoadBalancer(stack, 'LB', { vpc });
service.attachToClassicLB(lb);

test.done();
},
Expand All @@ -949,11 +941,14 @@ export = {
memoryLimitMiB: 1024,
});
container.addPortMappings({ containerPort: 808 });
const service = new ecs.Ec2Service(stack, 'Service', { cluster, taskDefinition});
const service = new ecs.Ec2Service(stack, 'Service', {
cluster,
taskDefinition
});

// THEN
const lb = new elb.LoadBalancer(stack, 'LB', { vpc });
iamhopaul123 marked this conversation as resolved.
Show resolved Hide resolved
lb.addTarget(service);
service.attachToClassicLB(lb);

test.done();
},
Expand All @@ -970,14 +965,15 @@ export = {
memoryLimitMiB: 1024,
});
container.addPortMappings({ containerPort: 808 });
const service = new ecs.Ec2Service(stack, 'Service', { cluster, taskDefinition});

// WHEN
const lb = new elb.LoadBalancer(stack, 'LB', { vpc });
const service = new ecs.Ec2Service(stack, 'Service', {
cluster,
taskDefinition
});

// THEN
const lb = new elb.LoadBalancer(stack, 'LB', { vpc });
test.throws(() => {
lb.addTarget(service);
service.attachToClassicLB(lb);
}, /Cannot use a Classic Load Balancer if NetworkMode is Bridge. Use Host or AwsVpc instead./);

test.done();
Expand All @@ -995,15 +991,179 @@ export = {
memoryLimitMiB: 1024,
});
container.addPortMappings({ containerPort: 808 });
const service = new ecs.Ec2Service(stack, 'Service', { cluster, taskDefinition});
const service = new ecs.Ec2Service(stack, 'Service', {
cluster,
taskDefinition
});

// THEN
const lb = new elb.LoadBalancer(stack, 'LB', { vpc });
test.throws(() => {
service.attachToClassicLB(lb);
}, /Cannot use a Classic Load Balancer if NetworkMode is None. Use Host or AwsVpc instead./);

test.done();
}
},

"attachToApplicationTargetGroup": {
"allows network mode of task definition to be other than none"(test: Test) {
// GIVEN
const stack = new cdk.Stack();
iamhopaul123 marked this conversation as resolved.
Show resolved Hide resolved
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef', { networkMode: ecs.NetworkMode.AWS_VPC });
const container = taskDefinition.addContainer('MainContainer', {
image: ContainerImage.fromRegistry('hello'),
});
container.addPortMappings({ containerPort: 8000 });

const service = new ecs.Ec2Service(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,
});

// THEN
service.attachToApplicationTargetGroup(targetGroup);

test.done();
},

"throws when network mode of task definition is none"(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.Ec2TaskDefinition(stack, 'Ec2TaskDef', { networkMode: ecs.NetworkMode.NONE });
const container = taskDefinition.addContainer('MainContainer', {
image: ContainerImage.fromRegistry('hello'),
});
container.addPortMappings({ containerPort: 8000 });

const service = new ecs.Ec2Service(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,
});

// THEN
test.throws(() => {
service.attachToApplicationTargetGroup(targetGroup);
}, /Cannot use a load balancer if NetworkMode is None. Use Bridge, Host or AwsVpc instead./);

test.done();
}
},

"attachToNetworkTargetGroup": {
"allows network mode of task definition to be other than none"(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.Ec2TaskDefinition(stack, 'Ec2TaskDef', { networkMode: ecs.NetworkMode.AWS_VPC });
const container = taskDefinition.addContainer('MainContainer', {
image: ContainerImage.fromRegistry('hello'),
});
container.addPortMappings({ containerPort: 8000 });

const service = new ecs.Ec2Service(stack, 'Service', {
cluster,
taskDefinition
});

const lb = new elbv2.NetworkLoadBalancer(stack, "lb", { vpc });
const listener = lb.addListener("listener", { port: 80 });
const targetGroup = listener.addTargets("target", {
port: 80,
});

// THEN
service.attachToNetworkTargetGroup(targetGroup);

test.done();
},

"throws when network mode of task definition is none"(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.Ec2TaskDefinition(stack, 'Ec2TaskDef', { networkMode: ecs.NetworkMode.NONE });
const container = taskDefinition.addContainer('MainContainer', {
image: ContainerImage.fromRegistry('hello'),
});
container.addPortMappings({ containerPort: 8000 });

const service = new ecs.Ec2Service(stack, 'Service', {
cluster,
taskDefinition
});

const lb = new elbv2.NetworkLoadBalancer(stack, "lb", { vpc });
const listener = lb.addListener("listener", { port: 80 });
const targetGroup = listener.addTargets("target", {
port: 80,
});

// THEN
test.throws(() => {
service.attachToNetworkTargetGroup(targetGroup);
}, /Cannot use a load balancer if NetworkMode is None. Use Bridge, Host or AwsVpc instead./);

test.done();
}
},

'classic ELB': {
'can attach to classic ELB'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') });
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TD', { networkMode: ecs.NetworkMode.HOST });
const container = taskDefinition.addContainer('web', {
image: ecs.ContainerImage.fromRegistry('test'),
memoryLimitMiB: 1024,
});
container.addPortMappings({ containerPort: 808 });
const service = new ecs.Ec2Service(stack, 'Service', {
cluster,
taskDefinition
});

// WHEN
const lb = new elb.LoadBalancer(stack, 'LB', { vpc });
lb.addTarget(service);

// THEN
test.throws(() => {
lb.addTarget(service);
}, /Cannot use a Classic Load Balancer if NetworkMode is None. Use Host or AwsVpc instead./);
expect(stack).to(haveResource('AWS::ECS::Service', {
LoadBalancers: [
{
ContainerName: "web",
ContainerPort: 808,
LoadBalancerName: { Ref: "LB8A12904C" }
}
]
}));

expect(stack).to(haveResource('AWS::ECS::Service', {
// if any load balancer is configured and healthCheckGracePeriodSeconds is not
// set, then it should default to 60 seconds.
HealthCheckGracePeriodSeconds: 60
}));

test.done();
}
Expand Down
Loading