Skip to content

Commit

Permalink
fix(cli): 'cdk synth' not able to fail if stacks have errors (#14475)
Browse files Browse the repository at this point in the history
The environment variable `STACKS_TO_VALIDATE` contains the stack hierarchical ids that should be validated after synthesis. By default (when the variable is undefined), it validates only the printed stacks, keeping backward compatibility.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
otaviomacedo authored May 4, 2021
1 parent 3c53cfa commit 963d1c7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,15 @@ export class CdkToolkit {

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

const idsToValidate = process.env.STACKS_TO_VALIDATE ? process.env.STACKS_TO_VALIDATE.split(';') : stackNames;
const stacksToValidate = await this.selectStacksForList(idsToValidate);
await this.validateStacks(stacksToValidate);

return assembly.selectStacks(stackNames, {
extend: exclusively ? ExtendedStackSelection.None : ExtendedStackSelection.Upstream,
defaultBehavior: DefaultSelection.MainAssembly,
});

await this.validateStacks(stacks);

return stacks;
}

private async selectStacksForDestroy(stackNames: string[], exclusively?: boolean) {
Expand Down
48 changes: 48 additions & 0 deletions packages/aws-cdk/test/cdk-toolkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,40 @@ describe('synth', () => {
// THEN
await expect(toolkit.synth(['Test-Stack-A'], false, true)).resolves.toBeUndefined();
});

describe('post-synth validation', () => {
beforeEach(() => {
cloudExecutable = new MockCloudExecutable({
stacks: [
MockStack.MOCK_STACK_A,
MockStack.MOCK_STACK_B,
],
nestedAssemblies: [{
stacks: [MockStack.MOCK_STACK_WITH_ERROR],
}],
});
});
});

afterEach(() => {
process.env.STACKS_TO_VALIDATE = undefined;
});

test('with STACKS_TO_VALIDATE containing a failed stack', async() => {
process.env.STACKS_TO_VALIDATE = 'Test-Stack-A;Test-Stack-A/witherrors';

const toolkit = defaultToolkitSetup();

await expect(toolkit.synth(['Test-Stack-A'], false, true)).rejects.toBeDefined();
});

test('with STACKS_TO_VALIDATE not containing a failed stack', async() => {
process.env.STACKS_TO_VALIDATE = 'Test-Stack-A';

const toolkit = defaultToolkitSetup();

await toolkit.synth(['Test-Stack-A'], false, true);
});
});

class MockStack {
Expand Down Expand Up @@ -208,6 +242,20 @@ class MockStack {
},
displayName: 'Test-Stack-A/Test-Stack-C',
};
public static readonly MOCK_STACK_WITH_ERROR: TestStackArtifact = {
stackName: 'witherrors',
env: 'aws://123456789012/bermuda-triangle-1',
template: { resource: 'errorresource' },
metadata: {
'/resource': [
{
type: cxschema.ArtifactMetadataEntryType.ERROR,
data: 'this is an error',
},
],
},
displayName: 'Test-Stack-A/witherrors',
}
}

class FakeCloudFormation extends CloudFormationDeployments {
Expand Down

0 comments on commit 963d1c7

Please sign in to comment.