Skip to content

Commit

Permalink
added new tests and modified listener name validation
Browse files Browse the repository at this point in the history
  • Loading branch information
clopca committed Jul 3, 2024
1 parent aaa2ef4 commit 6fc6221
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 6 deletions.
18 changes: 14 additions & 4 deletions src/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,19 @@ export class Listener extends Resource implements IListener {

protected validateListenerName(name: string) {
const errors: string[] = [];
const pattern = /^(?!listener-)(?!-)(?!.*-$)(?!.*--)[a-z0-9-]+$/;
const validationSucceeded = name.length >= 3 && name.length <= 63 && pattern.test(name);
if (!validationSucceeded) {
errors.push(`Invalid Listener Name: ${name} (must be between 3-63 characters, and must be a valid name)`);
// Rules codified from https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-vpclattice-listener.html
if (name.length < 3 || name.length > 63) {
errors.push('Listener name must be at least 3 and no more than 63 characters');
}

const isPatternMatch = /^(?!listener-)(?!-)(?!.*-$)(?!.*--)[a-z0-9-]+$/.test(name);
if (!isPatternMatch) {
errors.push(
'Listener name must be composed of characters a-z, 0-9, and hyphens (-). You can\'t use a hyphen as the first or last character, or immediately after another hyphen. The name cannot start with "listener-".',
);
}
if (errors.length > 0) {
errors.unshift(`Invalid listener name (value: ${name})`);
}
return errors;
}
Expand Down Expand Up @@ -356,3 +365,4 @@ export class Listener extends Resource implements IListener {
}

// 231-232,241-242,274-298,306-308,312-316
// 241-242,274-298,306-308,312-316
2 changes: 1 addition & 1 deletion src/service-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ export class ServiceNetwork extends ServiceNetworkBase {
}

/**
* Associates a VPC with the service netwoek.
* Associates a VPC with the service network.
*/

/**
Expand Down
103 changes: 102 additions & 1 deletion test/core/unit/listener.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import { EOL } from 'node:os';
import { EOL } from 'node:os';
import * as cdk from 'aws-cdk-lib';
import { Template } from 'aws-cdk-lib/assertions';
// import * as iam from 'aws-cdk-lib/aws-iam';
Expand Down Expand Up @@ -142,4 +142,105 @@ describe('Listener', () => {
expect(() => app.synth()).toThrow(`Invalid port ${port}: Out of range (0-65535)`);
}
});

describe('Listener name validation', () => {
test('Listener name validations', () => {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'TestStack');

// WHEN
new Listener(stack, 'Listener1', {
service: new Service(stack, 'Service1', {}),
config: {
name: 'abc-xyz-34ab',
},
});

// THEN
expect(() => app.synth()).not.toThrow();

// WHEN
new Listener(stack, 'Listener2', {
service: new Service(stack, 'Service2', {}),
config: {
name: '124pp-33',
},
});

// THEN
expect(() => app.synth()).not.toThrow();
});

test('Fails with message on invalid listener names', () => {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'TestStack');
const name = `svc-${'$'.repeat(64)}`;
const expectedErrors = [
` [TestStack/Listener] Invalid listener name (value: ${name})`,
' [TestStack/Listener] Listener name must be at least 3 and no more than 63 characters',
' [TestStack/Listener] Listener name must be composed of characters a-z, 0-9, and hyphens (-). You can\'t use a hyphen as the first or last character, or immediately after another hyphen. The name cannot start with "listener-".',
].join(EOL);

// WHEN
new Listener(stack, 'Listener', {
service: new Service(stack, 'Service', {}),
config: {
name,
},
});

// THEN
expect(() => app.synth()).toThrow(`Validation failed with the following errors:${EOL}${expectedErrors}`);
});

test('Fails if listener name has less than 3 or more than 63 characters', () => {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'TestStack');

// WHEN
new Listener(stack, 'Listener1', {
service: new Service(stack, 'Service1', {}),
config: {
name: 'a',
},
});

// THEN
expect(() => app.synth()).toThrow(/at least 3/);

// WHEN
new Listener(stack, 'Listener2', {
service: new Service(stack, 'Service2', {}),
config: {
name: 'x'.repeat(64),
},
});

// THEN
expect(() => app.synth()).toThrow(/no more than 63/);
});

test('Fails if listener name does not follow the specified pattern', () => {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'TestStack');
const invalidNames = ['aAa', 'a--a', 'a./a-a', 'a//a-a', 'svc-a', '-abc123', 'abc123-'];

// WHEN
for (const name of invalidNames) {
new Listener(stack, `Listener-${name}`, {
service: new Service(stack, `Service-${name}`, {}),
config: {
name,
},
});

// THEN
expect(() => app.synth()).toThrow(/Listener name must be composed of characters a-z, 0-9, and hyphens/);
}
});
});
});

0 comments on commit 6fc6221

Please sign in to comment.