diff --git a/packages/aws-cdk/lib/api/cxapp/stacks.ts b/packages/aws-cdk/lib/api/cxapp/stacks.ts index 3fd78ed659354..cbc12dfe1273b 100644 --- a/packages/aws-cdk/lib/api/cxapp/stacks.ts +++ b/packages/aws-cdk/lib/api/cxapp/stacks.ts @@ -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) { diff --git a/packages/aws-cdk/test/api/test.stacks.ts b/packages/aws-cdk/test/api/test.stacks.ts index ceb172ea18c9a..c5d4b69763ec5 100644 --- a/packages/aws-cdk/test/api/test.stacks.ts +++ b/packages/aws-cdk/test/api/test.stacks.ts @@ -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(); @@ -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' + } + ] + }, + }), }); -} \ No newline at end of file +} diff --git a/packages/aws-cdk/test/util.ts b/packages/aws-cdk/test/util.ts index 2e1c11d97693b..710341356c526 100644 --- a/packages/aws-cdk/test/util.ts +++ b/packages/aws-cdk/test/util.ts @@ -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[]; @@ -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, @@ -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); -} \ No newline at end of file +}