Skip to content

Commit

Permalink
most tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
comcalvi committed Jun 13, 2024
1 parent b3a5716 commit 4b88b03
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 48 deletions.
278 changes: 230 additions & 48 deletions packages/aws-cdk/test/cdk-toolkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ describe('deploy', () => {
});
});

test('with sns notification arns', async () => {
test('with sns notification arns as options', async () => {
// GIVEN
const notificationArns = [
'arn:aws:sns:us-east-2:444455556666:MyTopic',
Expand All @@ -506,7 +506,7 @@ describe('deploy', () => {
});
});

test('fail with incorrect sns notification arns', async () => {
test('fail with incorrect sns notification arns as options', async () => {
// GIVEN
const notificationArns = ['arn:::cfn-my-cool-topic'];
const toolkit = new CdkToolkit({
Expand All @@ -526,64 +526,206 @@ describe('deploy', () => {
hotswap: HotswapMode.FULL_DEPLOYMENT,
}),
).rejects.toThrow('Notification arn arn:::cfn-my-cool-topic is not a valid arn for an SNS topic');

});

test('globless bootstrap uses environment without question', async () => {
// GIVEN
const toolkit = defaultToolkitSetup();
describe('sns notification arns', () => {
beforeEach(() => {
cloudExecutable = new MockCloudExecutable({
stacks: [
MockStack.MOCK_STACK_WITH_NOTIFICATION_ARNS,
MockStack.MOCK_STACK_WITH_BAD_NOTIFICATION_ARNS,
],
});
});

// WHEN
await toolkit.bootstrap(['aws://56789/south-pole'], bootstrapper, {});
test('with sns notification arns in the executable', async () => {
// GIVEN
const expectedNotificationArns = [
'arn:aws:sns:bermuda-triangle-1337:123456789012:MyTopic',
];
const toolkit = new CdkToolkit({
cloudExecutable,
configuration: cloudExecutable.configuration,
sdkProvider: cloudExecutable.sdkProvider,
deployments: new FakeCloudFormation({
'Test-Stack-Notification-Arns': { Foo: 'Bar' },
}, expectedNotificationArns),
});

// WHEN
await toolkit.deploy({
selector: { patterns: ['Test-Stack-Notification-Arns'] },
hotswap: HotswapMode.FULL_DEPLOYMENT,
});
});

// THEN
expect(bootstrapper.bootstrapEnvironment).toHaveBeenCalledWith({
account: '56789',
region: 'south-pole',
name: 'aws://56789/south-pole',
}, expect.anything(), expect.anything());
expect(bootstrapper.bootstrapEnvironment).toHaveBeenCalledTimes(1);
});
test('fail with incorrect sns notification arns in the executable', async () => {
// GIVEN
const toolkit = new CdkToolkit({
cloudExecutable,
configuration: cloudExecutable.configuration,
sdkProvider: cloudExecutable.sdkProvider,
deployments: new FakeCloudFormation({
'Test-Stack-Bad-Notification-Arns': { Foo: 'Bar' },
}),
});

// WHEN
await expect(() =>
toolkit.deploy({
selector: { patterns: ['Test-Stack-Bad-Notification-Arns'] },
hotswap: HotswapMode.FULL_DEPLOYMENT,
}),
).rejects.toThrow('Notification arn arn:1337:123456789012:sns:bad is not a valid arn for an SNS topic');
});

test('globby bootstrap uses whats in the stacks', async () => {
// GIVEN
const toolkit = defaultToolkitSetup();
cloudExecutable.configuration.settings.set(['app'], 'something');
test('with sns notification arns in the executable and as options', async () => {
// GIVEN
const notificationArns = [
'arn:aws:sns:us-east-2:444455556666:MyTopic',
'arn:aws:sns:eu-west-1:111155556666:my-great-topic',
];

const expectedNotificationArns = notificationArns.concat(['arn:aws:sns:bermuda-triangle-1337:123456789012:MyTopic']);
const toolkit = new CdkToolkit({
cloudExecutable,
configuration: cloudExecutable.configuration,
sdkProvider: cloudExecutable.sdkProvider,
deployments: new FakeCloudFormation({
'Test-Stack-Notification-Arns': { Foo: 'Bar' },
}, expectedNotificationArns),
});

// WHEN
await toolkit.deploy({
selector: { patterns: ['Test-Stack-Notification-Arns'] },
notificationArns,
hotswap: HotswapMode.FULL_DEPLOYMENT,
});
});

// WHEN
await toolkit.bootstrap(['aws://*/bermuda-triangle-1'], bootstrapper, {});
test('fail with incorrect sns notification arns in the executable and incorrect sns notification arns as options', async () => {
// GIVEN
const notificationArns = ['arn:::cfn-my-cool-topic'];
const toolkit = new CdkToolkit({
cloudExecutable,
configuration: cloudExecutable.configuration,
sdkProvider: cloudExecutable.sdkProvider,
deployments: new FakeCloudFormation({
'Test-Stack-Bad-Notification-Arns': { Foo: 'Bar' },
}, notificationArns),
});

// WHEN
await expect(() =>
toolkit.deploy({
selector: { patterns: ['Test-Stack-Bad-Notification-Arns'] },
notificationArns,
hotswap: HotswapMode.FULL_DEPLOYMENT,
}),
).rejects.toThrow('Notification arn arn:::cfn-my-cool-topic is not a valid arn for an SNS topic');
});

// THEN
expect(bootstrapper.bootstrapEnvironment).toHaveBeenCalledWith({
account: '123456789012',
region: 'bermuda-triangle-1',
name: 'aws://123456789012/bermuda-triangle-1',
}, expect.anything(), expect.anything());
expect(bootstrapper.bootstrapEnvironment).toHaveBeenCalledTimes(1);
test('fail with incorrect sns notification arns in the executable and correct sns notification arns as options', async () => {
// GIVEN
const notificationArns = ['arn:aws:sns:bermuda-triangle-1337:123456789012:MyTopic'];
const toolkit = new CdkToolkit({
cloudExecutable,
configuration: cloudExecutable.configuration,
sdkProvider: cloudExecutable.sdkProvider,
deployments: new FakeCloudFormation({
'Test-Stack-Bad-Notification-Arns': { Foo: 'Bar' },
}, notificationArns),
});

// WHEN
await expect(() =>
toolkit.deploy({
selector: { patterns: ['Test-Stack-Bad-Notification-Arns'] },
notificationArns,
hotswap: HotswapMode.FULL_DEPLOYMENT,
}),
).rejects.toThrow('Notification arn arn:1337:123456789012:sns:bad is not a valid arn for an SNS topic');
});

test('fail with correct sns notification arns in the executable and incorrect sns notification arns as options', async () => {
// GIVEN
const notificationArns = ['arn:::cfn-my-cool-topic'];
const toolkit = new CdkToolkit({
cloudExecutable,
configuration: cloudExecutable.configuration,
sdkProvider: cloudExecutable.sdkProvider,
deployments: new FakeCloudFormation({
'Test-Stack-Notification-Arns': { Foo: 'Bar' },
}, notificationArns),
});

// WHEN
await expect(() =>
toolkit.deploy({
selector: { patterns: ['Test-Stack-Notification-Arns'] },
notificationArns,
hotswap: HotswapMode.FULL_DEPLOYMENT,
}),
).rejects.toThrow('Notification arn arn:::cfn-my-cool-topic is not a valid arn for an SNS topic');
});
});
});

test('bootstrap can be invoked without the --app argument', async () => {
// GIVEN
cloudExecutable.configuration.settings.clear();
const mockSynthesize = jest.fn();
cloudExecutable.synthesize = mockSynthesize;
test('globless bootstrap uses environment without question', async () => {
// GIVEN
const toolkit = defaultToolkitSetup();

const toolkit = defaultToolkitSetup();
// WHEN
await toolkit.bootstrap(['aws://56789/south-pole'], bootstrapper, {});

// WHEN
await toolkit.bootstrap(['aws://123456789012/west-pole'], bootstrapper, {});
// THEN
expect(bootstrapper.bootstrapEnvironment).toHaveBeenCalledWith({
account: '56789',
region: 'south-pole',
name: 'aws://56789/south-pole',
}, expect.anything(), expect.anything());
expect(bootstrapper.bootstrapEnvironment).toHaveBeenCalledTimes(1);
});

// THEN
expect(bootstrapper.bootstrapEnvironment).toHaveBeenCalledWith({
account: '123456789012',
region: 'west-pole',
name: 'aws://123456789012/west-pole',
}, expect.anything(), expect.anything());
expect(bootstrapper.bootstrapEnvironment).toHaveBeenCalledTimes(1);
test('globby bootstrap uses whats in the stacks', async () => {
// GIVEN
const toolkit = defaultToolkitSetup();
cloudExecutable.configuration.settings.set(['app'], 'something');

expect(cloudExecutable.hasApp).toEqual(false);
expect(mockSynthesize).not.toHaveBeenCalled();
});
// WHEN
await toolkit.bootstrap(['aws://*/bermuda-triangle-1'], bootstrapper, {});

// THEN
expect(bootstrapper.bootstrapEnvironment).toHaveBeenCalledWith({
account: '123456789012',
region: 'bermuda-triangle-1',
name: 'aws://123456789012/bermuda-triangle-1',
}, expect.anything(), expect.anything());
expect(bootstrapper.bootstrapEnvironment).toHaveBeenCalledTimes(1);
});

test('bootstrap can be invoked without the --app argument', async () => {
// GIVEN
cloudExecutable.configuration.settings.clear();
const mockSynthesize = jest.fn();
cloudExecutable.synthesize = mockSynthesize;

const toolkit = defaultToolkitSetup();

// WHEN
await toolkit.bootstrap(['aws://123456789012/west-pole'], bootstrapper, {});

// THEN
expect(bootstrapper.bootstrapEnvironment).toHaveBeenCalledWith({
account: '123456789012',
region: 'west-pole',
name: 'aws://123456789012/west-pole',
}, expect.anything(), expect.anything());
expect(bootstrapper.bootstrapEnvironment).toHaveBeenCalledTimes(1);

expect(cloudExecutable.hasApp).toEqual(false);
expect(mockSynthesize).not.toHaveBeenCalled();
});
});

Expand Down Expand Up @@ -1155,6 +1297,39 @@ class MockStack {
},
},
}
public static readonly MOCK_STACK_WITH_NOTIFICATION_ARNS: TestStackArtifact = {
stackName: 'Test-Stack-Notification-Arns',
notificationArns: ['arn:aws:sns:bermuda-triangle-1337:123456789012:MyTopic'],
template: { Resources: { TemplateName: 'Test-Stack-Notification-Arns' } },
env: 'aws://123456789012/bermuda-triangle-1337',
metadata: {
'/Test-Stack-Notification-Arns': [
{
type: cxschema.ArtifactMetadataEntryType.STACK_TAGS,
data: [
{ key: 'Foo', value: 'Bar' },
],
},
],
},
}

public static readonly MOCK_STACK_WITH_BAD_NOTIFICATION_ARNS: TestStackArtifact = {
stackName: 'Test-Stack-Bad-Notification-Arns',
notificationArns: ['arn:1337:123456789012:sns:bad'],
template: { Resources: { TemplateName: 'Test-Stack-Bad-Notification-Arns' } },
env: 'aws://123456789012/bermuda-triangle-1337',
metadata: {
'/Test-Stack-Bad-Notification-Arns': [
{
type: cxschema.ArtifactMetadataEntryType.STACK_TAGS,
data: [
{ key: 'Foo', value: 'Bar' },
],
},
],
},
}
}

class FakeCloudFormation extends Deployments {
Expand Down Expand Up @@ -1183,6 +1358,9 @@ class FakeCloudFormation extends Deployments {
MockStack.MOCK_STACK_B.stackName,
MockStack.MOCK_STACK_C.stackName,
MockStack.MOCK_STACK_WITH_ASSET.stackName,
MockStack.MOCK_STACK_WITH_ERROR.stackName,
MockStack.MOCK_STACK_WITH_NOTIFICATION_ARNS.stackName,
MockStack.MOCK_STACK_WITH_BAD_NOTIFICATION_ARNS.stackName,
]).toContain(options.stack.stackName);

if (this.expectedTags[options.stack.stackName]) {
Expand Down Expand Up @@ -1213,8 +1391,12 @@ class FakeCloudFormation extends Deployments {
return Promise.resolve({});
case MockStack.MOCK_STACK_WITH_ASSET.stackName:
return Promise.resolve({});
case MockStack.MOCK_STACK_WITH_NOTIFICATION_ARNS.stackName:
return Promise.resolve({});
case MockStack.MOCK_STACK_WITH_BAD_NOTIFICATION_ARNS.stackName:
return Promise.resolve({});
default:
return Promise.reject(`Not an expected mock stack: ${stack.stackName}`);
throw new Error(`not an expected mock stack: ${stack.stackName}`);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/aws-cdk/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface TestStackArtifact {
env?: string;
depends?: string[];
metadata?: cxapi.StackMetadata;
notificationArns?: string[];

/** Old-style assets */
assets?: cxschema.AssetMetadataEntry[];
Expand Down Expand Up @@ -101,6 +102,7 @@ function addAttributes(assembly: TestAssembly, builder: cxapi.CloudAssemblyBuild
...stack.properties,
templateFile,
terminationProtection: stack.terminationProtection,
notificationArns: stack.notificationArns,
},
displayName: stack.displayName,
});
Expand Down

0 comments on commit 4b88b03

Please sign in to comment.