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): cdk synth too eager with validation in Pipelines #15147

Merged
merged 2 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions packages/aws-cdk/bin/cdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ async function parseCommandLineArguments() {
)
.command(['synthesize [STACKS..]', 'synth [STACKS..]'], 'Synthesizes and prints the CloudFormation template for this stack', yargs => yargs
.option('exclusively', { type: 'boolean', alias: 'e', desc: 'Only synthesize requested stacks, don\'t include dependencies' })
.option('validation', { type: 'boolean', desc: 'After synthesis, validate stacks with the "validateOnSynth" attribute set (can also be controlled with CDK_VALIDATION)', default: true })
.option('quiet', { type: 'boolean', alias: 'q', desc: 'Do not output CloudFormation Template to stdout', default: false }))
.command('bootstrap [ENVIRONMENTS..]', 'Deploys the CDK toolkit stack into an AWS environment', yargs => yargs
.option('bootstrap-bucket-name', { type: 'string', alias: ['b', 'toolkit-bucket-name'], desc: 'The name of the CDK toolkit bucket; bucket will be created and must not exist', default: undefined })
Expand Down Expand Up @@ -328,9 +329,9 @@ async function initCommandLine() {
case 'synthesize':
case 'synth':
if (args.exclusively) {
return cli.synth(args.STACKS, args.exclusively, args.quiet);
return cli.synth(args.STACKS, args.exclusively, args.quiet, args.validation);
} else {
return cli.synth(args.STACKS, true, args.quiet);
return cli.synth(args.STACKS, true, args.quiet, args.validation);
}


Expand Down
12 changes: 7 additions & 5 deletions packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ export class CdkToolkit {
* OUTPUT: If more than one stack ends up being selected, an output directory
* should be supplied, where the templates will be written.
*/
public async synth(stackNames: string[], exclusively: boolean, quiet: boolean): Promise<any> {
const stacks = await this.selectStacksForDiff(stackNames, exclusively);
public async synth(stackNames: string[], exclusively: boolean, quiet: boolean, autoValidate?: boolean): Promise<any> {
const stacks = await this.selectStacksForDiff(stackNames, exclusively, autoValidate);

// if we have a single stack, print it to STDOUT
if (stacks.stackCount === 1) {
Expand Down Expand Up @@ -392,7 +392,7 @@ export class CdkToolkit {
return stacks;
}

private async selectStacksForDiff(stackNames: string[], exclusively?: boolean) {
private async selectStacksForDiff(stackNames: string[], exclusively?: boolean, autoValidate?: boolean) {
const assembly = await this.assembly();

const selectedForDiff = await assembly.selectStacks({ patterns: stackNames }, {
Expand All @@ -401,9 +401,11 @@ export class CdkToolkit {
});

const allStacks = await this.selectStacksForList([]);
const flaggedStacks = allStacks.filter(art => art.validateOnSynth ?? false);
const autoValidateStacks = autoValidate
? allStacks.filter(art => art.validateOnSynth ?? false)
: new StackCollection(assembly, []);

await this.validateStacks(selectedForDiff.concat(flaggedStacks));
await this.validateStacks(selectedForDiff.concat(autoValidateStacks));

return selectedForDiff;
}
Expand Down
32 changes: 21 additions & 11 deletions packages/aws-cdk/test/cdk-toolkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,22 +178,32 @@ describe('synth', () => {
process.env.STACKS_TO_VALIDATE = undefined;
});

test('stack has error and is flagged for validation', async() => {
cloudExecutable = new MockCloudExecutable({
stacks: [
MockStack.MOCK_STACK_A,
MockStack.MOCK_STACK_B,
],
nestedAssemblies: [{
describe('stack with error and flagged for validation', () => {
beforeEach(() => {
cloudExecutable = new MockCloudExecutable({
stacks: [
{ properties: { validateOnSynth: true }, ...MockStack.MOCK_STACK_WITH_ERROR },
MockStack.MOCK_STACK_A,
MockStack.MOCK_STACK_B,
],
}],
nestedAssemblies: [{
stacks: [
{ properties: { validateOnSynth: true }, ...MockStack.MOCK_STACK_WITH_ERROR },
],
}],
});
});

const toolkit = defaultToolkitSetup();
test('causes synth to fail if autoValidate=true', async() => {
const toolkit = defaultToolkitSetup();
const autoValidate = true;
await expect(toolkit.synth([], false, true, autoValidate)).rejects.toBeDefined();
});

await expect(toolkit.synth([], false, true)).rejects.toBeDefined();
test('causes synth to succeed if autoValidate=false', async() => {
const toolkit = defaultToolkitSetup();
const autoValidate = false;
await expect(toolkit.synth([], false, true, autoValidate)).resolves.toBeUndefined();
});
});

test('stack has error and was explicitly selected', async() => {
Expand Down
21 changes: 21 additions & 0 deletions packages/aws-cdk/test/integ/cli/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const lambda = require('@aws-cdk/aws-lambda');
const docker = require('@aws-cdk/aws-ecr-assets');
const core = require('@aws-cdk/core')
const { StackWithNestedStack, StackWithNestedStackUsingParameters } = require('./nested-stack');
const { Annotations } = require('@aws-cdk/core');

const stackPrefix = process.env.STACK_NAME_PREFIX;
if (!stackPrefix) {
Expand Down Expand Up @@ -136,7 +137,22 @@ class ProvidingStack extends cdk.Stack {
}
}

class StackWithError extends cdk.Stack {
constructor(parent, id, props) {
super(parent, id, props);

this.topic = new sns.Topic(this, 'BogusTopic'); // Some filler
Annotations.of(this).addError('This is an error');
}
}

class StageWithError extends cdk.Stage {
constructor(parent, id, props) {
super(parent, id, props);

new StackWithError(this, 'Stack');
}
}

class ConsumingStack extends cdk.Stack {
constructor(parent, id, props) {
Expand Down Expand Up @@ -335,6 +351,11 @@ switch (stackSet) {
});
break;

case 'stage-with-errors':
const stage = new StageWithError(app, `${stackPrefix}-stage-with-errors`);
stage.synth({ validateOnSynthesis: true });
break;

default:
throw new Error(`Unrecognized INTEG_STACK_SET: '${stackSet}'`);
}
Expand Down
19 changes: 19 additions & 0 deletions packages/aws-cdk/test/integ/cli/cli.integtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,25 @@ integTest('cdk ls', withDefaultFixture(async (fixture) => {
}
}));

integTest('synthing a stage with errors leads to failure', withDefaultFixture(async (fixture) => {
const output = await fixture.cdk(['synth'], {
allowErrExit: true,
modEnv: {
INTEG_STACK_SET: 'stage-with-errors',
},
});

expect(output).toContain('This is an error');
}));

integTest('synthing a stage with errors can be suppressed', withDefaultFixture(async (fixture) => {
await fixture.cdk(['synth', '--no-validation'], {
modEnv: {
INTEG_STACK_SET: 'stage-with-errors',
},
});
}));

integTest('deploy stack without resource', withDefaultFixture(async (fixture) => {
// Deploy the stack without resources
await fixture.cdkDeploy('conditional-resource', { modEnv: { NO_RESOURCE: 'TRUE' } });
Expand Down