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(cli): Genertate CDK Metadata resource for region-independent stacks #3149

Merged
merged 5 commits into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/api/cxapp/stacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export class AppStacks {
if (!stack.template.Resources) {
stack.template.Resources = {};
}
const resourcePresent = stack.environment.region === 'default-region'
const resourcePresent = stack.environment.region === cxapi.UNKNOWN_REGION
|| regionInfo.Fact.find(stack.environment.region, regionInfo.FactName.CDK_METADATA_RESOURCE_AVAILABLE) === 'YES';
if (resourcePresent) {
if (!stack.template.Resources.CDKMetadata) {
Expand Down
94 changes: 69 additions & 25 deletions packages/aws-cdk/test/api/test.stacks.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
import cxapi = require('@aws-cdk/cx-api');
import { Test } from 'nodeunit';
import { Test, testCase } from 'nodeunit';
import { SDK } from '../../lib';
import { AppStacks, DefaultSelection } from '../../lib/api/cxapp/stacks';
import { Configuration } from '../../lib/settings';
import { testAssembly } from '../util';

const FIXED_RESULT = testAssembly({
stackName: 'withouterrors',
template: { resource: 'noerrorresource' },
},
{
stackName: 'witherrors',
template: { resource: 'errorresource' },
metadata: {
'/resource': [
{
type: cxapi.ERROR_METADATA_KEY,
data: 'this is an error'
}
]
},
});

export = {
export = testCase({
async 'do not throw when selecting stack without errors'(test: Test) {
// GIVEN
const stacks = testStacks();
Expand Down Expand Up @@ -95,14 +78,75 @@ export = {
// THEN
test.ok(thrown && thrown.includes('Since this app includes more than a single stack, specify which stacks to use (wildcards are supported)'));
test.done();
}
},

'AWS::CDK::Metadata': {
async 'is generated for relocatable stacks'(test: Test) {
const stacks = testStacks({ env: `aws://${cxapi.UNKNOWN_ACCOUNT}/${cxapi.UNKNOWN_REGION}`, versionReporting: true });

const result = await stacks.synthesizeStack('withouterrors');
const metadata = result.template.Resources && result.template.Resources.CDKMetadata;
test.deepEqual(metadata, {
Type: 'AWS::CDK::Metadata',
Properties: {
Modules: `${require('../../package.json').name}=${require('../../package.json').version}`
}
});

test.done();
},

async 'is generated for stacks in supported regions'(test: Test) {
const stacks = testStacks({ env: 'aws://012345678912/us-east-1', versionReporting: true });

const result = await stacks.synthesizeStack('withouterrors');
const metadata = result.template.Resources && result.template.Resources.CDKMetadata;
test.deepEqual(metadata, {
Type: 'AWS::CDK::Metadata',
Properties: {
Modules: `${require('../../package.json').name}=${require('../../package.json').version}`
}
});

test.done();
},

async 'is not generated for stacks in unsupported regions'(test: Test) {
const stacks = testStacks({ env: 'aws://012345678912/bermuda-triangle-1337', versionReporting: true });

const result = await stacks.synthesizeStack('withouterrors');
const metadata = result.template.Resources && result.template.Resources.CDKMetadata;
test.equal(metadata, undefined);

test.done();
}
},
});

};
function testStacks({ env, versionReporting = true }: { env?: string, versionReporting?: boolean } = {}) {
const configuration = new Configuration();
configuration.settings.set(['versionReporting'], versionReporting);

function testStacks() {
return new AppStacks({
configuration: new Configuration(),
configuration,
aws: new SDK(),
synthesizer: async () => FIXED_RESULT,
synthesizer: async () => testAssembly({
stackName: 'withouterrors',
env,
template: { resource: 'noerrorresource' },
},
{
stackName: 'witherrors',
env,
template: { resource: 'errorresource' },
metadata: {
'/resource': [
{
type: cxapi.ERROR_METADATA_KEY,
data: 'this is an error'
}
]
},
}),
});
}
}
5 changes: 3 additions & 2 deletions packages/aws-cdk/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path = require('path');
export interface TestStackArtifact {
stackName: string;
template: any;
env?: string,
depends?: string[];
metadata?: cxapi.StackMetadata;
assets?: cxapi.AssetMetadataEntry[];
Expand All @@ -27,7 +28,7 @@ export function testAssembly(...stacks: TestStackArtifact[]): cxapi.CloudAssembl

builder.addArtifact(stack.stackName, {
type: cxapi.ArtifactType.AWS_CLOUDFORMATION_STACK,
environment: 'aws://12345/here',
environment: stack.env || 'aws://12345/here',

dependencies: stack.depends,
metadata,
Expand All @@ -43,4 +44,4 @@ export function testAssembly(...stacks: TestStackArtifact[]): cxapi.CloudAssembl
export function testStack(stack: TestStackArtifact) {
const assembly = testAssembly(stack);
return assembly.getStack(stack.stackName);
}
}