Skip to content

Commit

Permalink
chore: unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
nmussy committed Jan 14, 2025
1 parent cfb8d7b commit 291a461
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
69 changes: 69 additions & 0 deletions packages/aws-cdk-lib/aws-ecs-patterns/test/ec2/l3s.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,75 @@ describe('ApplicationLoadBalancedEc2Service', () => {
});

describe('NetworkLoadBalancedEc2Service', () => {
test('ECS loadbalanced construct', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
cluster.addAsgCapacityProvider(new AsgCapacityProvider(stack, 'DefaultAutoScalingGroupProvider', {
autoScalingGroup: new AutoScalingGroup(stack, 'DefaultAutoScalingGroup', {
vpc,
instanceType: new ec2.InstanceType('t2.micro'),
machineImage: MachineImage.latestAmazonLinux(),
}),
}));

// WHEN
new ecsPatterns.NetworkLoadBalancedEc2Service(stack, 'Service', {
cluster,
memoryLimitMiB: 1024,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('test'),
environment: {
TEST_ENVIRONMENT_VARIABLE1: 'test environment variable 1 value',
TEST_ENVIRONMENT_VARIABLE2: 'test environment variable 2 value',
},
dockerLabels: { label1: 'labelValue1', label2: 'labelValue2' },
entryPoint: ['echo', 'ecs-is-awesome'],
command: ['/bin/bash'],
},
desiredCount: 2,
ipAddressType: IpAddressType.DUAL_STACK,
});

// THEN - stack contains a load balancer and a service
Template.fromStack(stack).resourceCountIs('AWS::ElasticLoadBalancingV2::LoadBalancer', 1);
Template.fromStack(stack).hasResourceProperties('AWS::ElasticLoadBalancingV2::LoadBalancer', {
Scheme: 'internet-facing',
Type: 'network',
IpAddressType: 'dualstack',
});

Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
DesiredCount: 2,
LaunchType: 'EC2',
});

Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
Match.objectLike({
Environment: [
{
Name: 'TEST_ENVIRONMENT_VARIABLE1',
Value: 'test environment variable 1 value',
},
{
Name: 'TEST_ENVIRONMENT_VARIABLE2',
Value: 'test environment variable 2 value',
},
],
Memory: 1024,
DockerLabels: {
label1: 'labelValue1',
label2: 'labelValue2',
},
EntryPoint: ['echo', 'ecs-is-awesome'],
Command: ['/bin/bash'],
}),
],
});
});

test('setting vpc and cluster throws error', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,57 @@ describe('ApplicationLoadBalancedFargateService', () => {
});

describe('NetworkLoadBalancedFargateService', () => {
test('setting healthCheckGracePeriod works', () => {
// GIVEN
const stack = new cdk.Stack();

// WHEN
new ecsPatterns.NetworkLoadBalancedFargateService(stack, 'Service', {
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('/aws/aws-example-app'),
},
healthCheckGracePeriod: cdk.Duration.seconds(600),
});
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
HealthCheckGracePeriodSeconds: 600,
});
});

test('setting healthCheck works', () => {
// GIVEN
const stack = new cdk.Stack();

// WHEN
new ecsPatterns.NetworkLoadBalancedFargateService(stack, 'Service', {
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('/aws/aws-example-app'),
},
healthCheck: {
command: ['CMD-SHELL', 'curl -f http://localhost/ || exit 1'],
interval: cdk.Duration.minutes(1),
retries: 5,
startPeriod: cdk.Duration.minutes(1),
timeout: cdk.Duration.minutes(1),
},
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', {
ContainerDefinitions: Match.arrayWith([
Match.objectLike({
HealthCheck: {
Command: ['CMD-SHELL', 'curl -f http://localhost/ || exit 1'],
Interval: 60,
Retries: 5,
StartPeriod: 60,
Timeout: 60,
},
}),
]),
});
});

test('setting loadBalancerType to Network creates an NLB Public', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down Expand Up @@ -1714,6 +1765,50 @@ describe('NetworkLoadBalancedFargateService', () => {
});
});

test('setting a command for taskImageOption', () => {
// GIVEN
const stack = new cdk.Stack();

// WHEN
new ecsPatterns.NetworkLoadBalancedFargateService(stack, 'Service', {
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('/aws/aws-example-app'),
command: ['./app/bin/start.sh', '--foo'],
},
});
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
Match.objectLike({
Image: '/aws/aws-example-app',
Command: ['./app/bin/start.sh', '--foo'],
}),
],
});
});

test('setting an entryPoint for taskImageOptions', () => {
// GIVEN
const stack = new cdk.Stack();

// WHEN
new ecsPatterns.NetworkLoadBalancedFargateService(stack, 'Service', {
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('/aws/aws-example-app'),
entryPoint: ['echo', 'foo'],
},
});
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
Match.objectLike({
Image: '/aws/aws-example-app',
EntryPoint: ['echo', 'foo'],
}),
],
});
});

test('setting NLB circuitBreaker works', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit 291a461

Please sign in to comment.