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

fix(core): Prevent volatile physical name generation #2984

Merged
merged 7 commits into from
Jul 2, 2019
Merged
Prev Previous commit
Next Next commit
Add test
RomainMuller committed Jun 24, 2019
commit 9ff4304af7c1f2d5cf505ac2334fa96da835934a
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import nodeunit = require('nodeunit');
import { App, Aws, Resource, Stack } from '../../lib';
import { generatePhysicalName } from '../../lib/private/physical-name-generator';

export = nodeunit.testCase({
'generates correct physical names'(test: nodeunit.Test) {
const app = new App();
const stack = new Stack(app, 'TestStack', { env: { account: '012345678912', region: 'bermuda-triangle-1' } });

const testResourceA = new TestResource(stack, 'A');
const testResourceB = new TestResource(testResourceA, 'B');

test.equal(generatePhysicalName(testResourceA), 'teststackteststackaa164c141d59b37c1b663');
test.equal(generatePhysicalName(testResourceB), 'teststackteststackab27595cd34d8188283a1f');

test.done();
},

'generates different names in different accounts'(test: nodeunit.Test) {
const appA = new App();
const stackA = new Stack(appA, 'TestStack', { env: { account: '012345678912', region: 'bermuda-triangle-1' } });
const resourceA = new TestResource(stackA, 'Resource');

const appB = new App();
const stackB = new Stack(appB, 'TestStack', { env: { account: '012345678913', region: 'bermuda-triangle-1' } });
const resourceB = new TestResource(stackB, 'Resource');

test.notEqual(generatePhysicalName(resourceA), generatePhysicalName(resourceB));

test.done();
},

'generates different names in different regions'(test: nodeunit.Test) {
const appA = new App();
const stackA = new Stack(appA, 'TestStack', { env: { account: '012345678912', region: 'bermuda-triangle-1' } });
const resourceA = new TestResource(stackA, 'Resource');

const appB = new App();
const stackB = new Stack(appB, 'TestStack', { env: { account: '012345678912', region: 'bermuda-triangle-2' } });
const resourceB = new TestResource(stackB, 'Resource');

test.notEqual(generatePhysicalName(resourceA), generatePhysicalName(resourceB));

test.done();
},

'fails when the region is an unresolved token'(test: nodeunit.Test) {
const app = new App();
const stack = new Stack(app, 'TestStack', { env: { account: '012345678912', region: Aws.REGION } });
const testResource = new TestResource(stack, 'A');

test.throws(() => generatePhysicalName(testResource),
/Cannot generate a physical name for TestStack\/A, because the region is un-resolved or missing/);

test.done();
},

'fails when the region is not provided'(test: nodeunit.Test) {
const app = new App();
const stack = new Stack(app, 'TestStack', { env: { account: '012345678912' } });
const testResource = new TestResource(stack, 'A');

test.throws(() => generatePhysicalName(testResource),
/Cannot generate a physical name for TestStack\/A, because the region is un-resolved or missing/);

test.done();
},

'fails when the account is an unresolved token'(test: nodeunit.Test) {
const app = new App();
const stack = new Stack(app, 'TestStack', { env: { account: Aws.ACCOUNT_ID, region: 'bermuda-triangle-1' } });
const testResource = new TestResource(stack, 'A');

test.throws(() => generatePhysicalName(testResource),
/Cannot generate a physical name for TestStack\/A, because the account is un-resolved or missing/);

test.done();
},

'fails when the account is not provided'(test: nodeunit.Test) {
const app = new App();
const stack = new Stack(app, 'TestStack', { env: { region: 'bermuda-triangle-1' } });
const testResource = new TestResource(stack, 'A');

test.throws(() => generatePhysicalName(testResource),
/Cannot generate a physical name for TestStack\/A, because the account is un-resolved or missing/);

test.done();
},
});

class TestResource extends Resource {}