diff --git a/src/stack.ts b/src/stack.ts index fbf1ee44..45d6d22d 100644 --- a/src/stack.ts +++ b/src/stack.ts @@ -17,9 +17,45 @@ import * as pulumi from '@pulumi/pulumi'; import { AppComponent, AppOptions, PulumiStack } from './types'; import { AppConverter, StackConverter } from './converters/app-converter'; import { AwsCdkCli, ICloudAssemblyDirectoryProducer } from '@aws-cdk/cli-lib-alpha'; +import { MockCallArgs, MockMonitor, MockResourceArgs, setMocks } from '@pulumi/pulumi/runtime/mocks'; +import { setMockOptions } from '@pulumi/pulumi/runtime'; +import { error } from '@pulumi/pulumi/log'; const STACK_SYMBOL = Symbol.for('@pulumi/cdk.Stack'); export type create = (scope: App) => void; +setMocks( + { + call: async (args: MockCallArgs) => { + return {}; + }, + newResource: async (args: MockResourceArgs) => { + console.error(args.type); + switch (args.type) { + case 'cdk:index:App': + return { id: 'app', state: { outputs: {} } }; + case 'aws-native:ssm:Parameter': + return { + id: `${args.name}${Math.random()}`, + state: { + ...args.inputs, + name: `${args.name}${Math.random()}`, + id: `${args.name}${Math.random()}`, + }, + }; + default: + return { + id: `${args.name}${Math.random()}`, + state: { + ...args.inputs, + }, + }; + } + }, + }, + pulumi.runtime.getProject(), + pulumi.runtime.getStack(), + pulumi.runtime.isDryRun(), +); export class App extends AppComponent implements ICloudAssemblyDirectoryProducer { /** @internal */ @@ -75,7 +111,22 @@ export class App extends AppComponent implements ICloudAssemblyDir protected async initialize(): Promise { const cli = AwsCdkCli.fromCloudAssemblyDirectoryProducer(this); - await cli.synth({ quiet: true }); + try { + await cli.synth({ quiet: true /*, lookups: false */ }); + } catch (e: any) { + if (typeof e.message === 'string' && e.message.includes('Context lookups have been disabled')) { + const message = e.message as string; + const messageParts = message.split('Context lookups have been disabled. '); + const missingParts = messageParts[1].split('Missing context keys: '); + error( + 'Context lookups have been disabled. Make sure all necessary context is already in "cdk.context.json". \n' + + 'Missing context keys: ' + + missingParts[1], + ); + } else { + error(e.message, this); + } + } const converter = new AppConverter(this); converter.convert(); @@ -85,7 +136,6 @@ export class App extends AppComponent implements ICloudAssemblyDir async produce(context: Record): Promise { const app = new cdk.App({ - // outdir: 'cdk.out', ...(this.appProps ?? {}), autoSynth: false, analyticsReporting: false, @@ -94,6 +144,7 @@ export class App extends AppComponent implements ICloudAssemblyDir this._app = app; this.assemblyDir = app.outdir; this.createFunc(this); + app.node.children.forEach((child) => { if (Stack.isPulumiStack(child)) { this.stacks[child.artifactId] = child; diff --git a/tests/mocks.ts b/tests/mocks.ts index 0aadf3e4..7b9f7efe 100644 --- a/tests/mocks.ts +++ b/tests/mocks.ts @@ -41,14 +41,6 @@ export function setMocks(assertFn: (args: MockResourceArgs) => void) { return { id: '', state: {} }; case 'cdk:index:Component': return { id: '', state: {} }; - // case 'aws-native:s3:Bucket': - // return { - // id: args.name + '_id', - // state: { - // ...args.inputs, - // arn: args. - // }, - // }; default: assertFn(args); return { @@ -62,6 +54,8 @@ export function setMocks(assertFn: (args: MockResourceArgs) => void) { }, }; + // I don't know why, but using pulumi.runtime.setMocks caused the mocks + // to interfere with each other between tests const mockMonitor = new MockMonitor(mocks); setMockOptions(mockMonitor, 'project', 'stack', false); }