Skip to content

Commit

Permalink
add mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
corymhall committed Oct 8, 2024
1 parent 1edc771 commit e674fa4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
55 changes: 53 additions & 2 deletions src/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Check warning on line 20 in src/stack.ts

View workflow job for this annotation

GitHub Actions / Run lint

'MockMonitor' is defined but never used
import { setMockOptions } from '@pulumi/pulumi/runtime';

Check warning on line 21 in src/stack.ts

View workflow job for this annotation

GitHub Actions / Run lint

'setMockOptions' is defined but never used
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) => {

Check warning on line 28 in src/stack.ts

View workflow job for this annotation

GitHub Actions / Run lint

'args' is defined but never used
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<AppConverter> implements ICloudAssemblyDirectoryProducer {
/** @internal */
Expand Down Expand Up @@ -75,7 +111,22 @@ export class App extends AppComponent<AppConverter> implements ICloudAssemblyDir

protected async initialize(): Promise<AppConverter> {
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();
Expand All @@ -85,7 +136,6 @@ export class App extends AppComponent<AppConverter> implements ICloudAssemblyDir

async produce(context: Record<string, any>): Promise<string> {
const app = new cdk.App({
// outdir: 'cdk.out',
...(this.appProps ?? {}),
autoSynth: false,
analyticsReporting: false,
Expand All @@ -94,6 +144,7 @@ export class App extends AppComponent<AppConverter> 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;
Expand Down
10 changes: 2 additions & 8 deletions tests/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}

0 comments on commit e674fa4

Please sign in to comment.