Skip to content

Commit

Permalink
fix(cli): Genertate CDK Metadata resource for region-independent stac…
Browse files Browse the repository at this point in the history
…ks (aws#3149)

Fixes a bug that caused the CDK Metadata resource to not be emitted if a
stack was not bound to a particular supported region. The fixed behavior
will generate the resource for supported regions as well as for region
independent stacks.

Fixes aws#3142
  • Loading branch information
RomainMuller authored and Kaixiang-AWS committed Jul 3, 2019
1 parent 01a09c1 commit f83288a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 28 deletions.
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);
}
}

0 comments on commit f83288a

Please sign in to comment.