Skip to content

Commit

Permalink
Merge branch 'master' into cross-account-event-bus
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Feb 26, 2021
2 parents ca4b28f + fc87574 commit aff9a71
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 15 deletions.
3 changes: 3 additions & 0 deletions packages/@aws-cdk/aws-ec2/lib/cfn-init-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ export abstract class InitFile extends InitElement {
* Use a literal string as the file content
*/
public static fromString(fileName: string, content: string, options: InitFileOptions = {}): InitFile {
if (!content) {
throw new Error(`InitFile ${fileName}: cannot create empty file. Please supply at least one character of content.`);
}
return new class extends InitFile {
protected _doBind(bindOptions: InitBindOptions) {
return {
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-ec2/lib/vpc-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ export class InterfaceVpcEndpointAwsService implements IInterfaceVpcEndpointServ
public static readonly KMS = new InterfaceVpcEndpointAwsService('kms');
public static readonly CLOUDWATCH_LOGS = new InterfaceVpcEndpointAwsService('logs');
public static readonly CLOUDWATCH = new InterfaceVpcEndpointAwsService('monitoring');
public static readonly RDS = new InterfaceVpcEndpointAwsService('rds');
public static readonly SAGEMAKER_API = new InterfaceVpcEndpointAwsService('sagemaker.api');
public static readonly SAGEMAKER_RUNTIME = new InterfaceVpcEndpointAwsService('sagemaker.runtime');
public static readonly SAGEMAKER_RUNTIME_FIPS = new InterfaceVpcEndpointAwsService('sagemaker.runtime-fips');
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-ec2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
"docs-public-apis:@aws-cdk/aws-ec2.InterfaceVpcEndpointAwsService.KINESIS_STREAMS",
"docs-public-apis:@aws-cdk/aws-ec2.InterfaceVpcEndpointAwsService.KINESIS_FIREHOSE",
"docs-public-apis:@aws-cdk/aws-ec2.InterfaceVpcEndpointAwsService.KMS",
"docs-public-apis:@aws-cdk/aws-ec2.InterfaceVpcEndpointAwsService.RDS",
"docs-public-apis:@aws-cdk/aws-ec2.InterfaceVpcEndpointAwsService.SAGEMAKER_API",
"docs-public-apis:@aws-cdk/aws-ec2.InterfaceVpcEndpointAwsService.SAGEMAKER_NOTEBOOK",
"docs-public-apis:@aws-cdk/aws-ec2.InterfaceVpcEndpointAwsService.SAGEMAKER_RUNTIME",
Expand Down
6 changes: 6 additions & 0 deletions packages/@aws-cdk/aws-ec2/test/cfn-init-element.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ describe('InitFile', () => {
});
});

test('empty content string throws error', () => {
expect(() => {
ec2.InitFile.fromString('/tmp/foo', '');
}).toThrow('InitFile /tmp/foo: cannot create empty file. Please supply at least one character of content.');
});

test('symlink throws an error if mode is set incorrectly', () => {
expect(() => {
ec2.InitFile.symlink('/tmp/foo', '/tmp/bar', {
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-ecs/lib/ec2/ec2-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ export class Ec2Service extends BaseService implements IEc2Service {
this.addPlacementConstraints(...props.placementConstraints || []);
this.addPlacementStrategies(...props.placementStrategies || []);

if (!this.taskDefinition.defaultContainer) {
throw new Error('A TaskDefinition must have at least one essential container');
}
this.node.addValidation({
validate: () => !this.taskDefinition.defaultContainer ? ['A TaskDefinition must have at least one essential container'] : [],
});
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-ecs/lib/fargate/fargate-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ export class FargateService extends BaseService implements IFargateService {

this.configureAwsVpcNetworkingWithSecurityGroups(props.cluster.vpc, props.assignPublicIp, props.vpcSubnets, securityGroups);

if (!props.taskDefinition.defaultContainer) {
throw new Error('A TaskDefinition must have at least one essential container');
}
this.node.addValidation({
validate: () => !this.taskDefinition.defaultContainer ? ['A TaskDefinition must have at least one essential container'] : [],
});
}
}

Expand Down
44 changes: 39 additions & 5 deletions packages/@aws-cdk/aws-ecs/test/ec2/ec2-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, haveResource } from '@aws-cdk/assert';
import { expect, haveResource, haveResourceLike } from '@aws-cdk/assert';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as elb from '@aws-cdk/aws-elasticloadbalancing';
import * as elbv2 from '@aws-cdk/aws-elasticloadbalancingv2';
Expand Down Expand Up @@ -541,14 +541,48 @@ nodeunitShim({
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef');

// Errors on validation, not on construction.
new ecs.Ec2Service(stack, 'Ec2Service', {
cluster,
taskDefinition,
});

// THEN
test.throws(() => {
new ecs.Ec2Service(stack, 'Ec2Service', {
cluster,
taskDefinition,
});
expect(stack);
}, /one essential container/);

test.done();
},

'allows adding the default container after creating the service'(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');

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

// Add the container *after* creating the service
taskDefinition.addContainer('main', {
image: ecs.ContainerImage.fromRegistry('somecontainer'),
memoryReservationMiB: 10,
});

// THEN
expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Name: 'main',
},
],
}));

test.done();
},

Expand Down
40 changes: 36 additions & 4 deletions packages/@aws-cdk/aws-ecs/test/fargate/fargate-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,14 +482,46 @@ nodeunitShim({
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef');

// Errors on validation, not on construction.
new ecs.FargateService(stack, 'FargateService', {
cluster,
taskDefinition,
});

// THEN
test.throws(() => {
new ecs.FargateService(stack, 'FargateService', {
cluster,
taskDefinition,
});
expect(stack);
}, /one essential container/);

test.done();
},

'allows adding the default container after creating the service'(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.FargateTaskDefinition(stack, 'FargateTaskDef');

new ecs.FargateService(stack, 'FargateService', {
cluster,
taskDefinition,
});

// Add the container *after* creating the service
taskDefinition.addContainer('main', {
image: ecs.ContainerImage.fromRegistry('somecontainer'),
});

// THEN
expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Name: 'main',
},
],
}));

test.done();
},

Expand Down

0 comments on commit aff9a71

Please sign in to comment.