Skip to content

Commit

Permalink
fix(pipelines): reuseCrossRegionSupportStacks=true does not fail when…
Browse files Browse the repository at this point in the history
… existing pipeline is used (#20423)

----
Added prop check for reuseCrossRegionSupportStacks in CodePipeline which fixes #20334 

Added unit tests for all three prop checks.

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)?
	* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
tejasmr authored Jul 14, 2022
1 parent 39a7c1f commit 9c0ccca
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/@aws-cdk/pipelines/lib/codepipeline/codepipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ export class CodePipeline extends PipelineBase {
if (this.props.crossAccountKeys !== undefined) {
throw new Error('Cannot set \'crossAccountKeys\' if an existing CodePipeline is given using \'codePipeline\'');
}
if (this.props.reuseCrossRegionSupportStacks !== undefined) {
throw new Error('Cannot set \'reuseCrossRegionSupportStacks\' if an existing CodePipeline is given using \'codePipeline\'');
}

this._pipeline = this.props.codePipeline;
} else {
Expand Down
59 changes: 57 additions & 2 deletions packages/@aws-cdk/pipelines/test/codepipeline/codepipeline.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Template, Annotations, Match } from '@aws-cdk/assertions';
import * as ccommit from '@aws-cdk/aws-codecommit';
import { Pipeline } from '@aws-cdk/aws-codepipeline';
import * as sqs from '@aws-cdk/aws-sqs';
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
Expand Down Expand Up @@ -71,6 +72,24 @@ describe('CodePipeline support stack reuse', () => {
});
});

describe('Providing codePipeline parameter and prop(s) of codePipeline parameter to CodePipeline constructor should throw error', () => {
test('Providing codePipeline parameter and pipelineName parameter should throw error', () => {
expect(() => new CodePipelinePropsCheckTest(app, 'CodePipeline', {
pipelineName: 'randomName',
}).create()).toThrowError('Cannot set \'pipelineName\' if an existing CodePipeline is given using \'codePipeline\'');
});
test('Providing codePipeline parameter and crossAccountKeys parameter should throw error', () => {
expect(() => new CodePipelinePropsCheckTest(app, 'CodePipeline', {
crossAccountKeys: true,
}).create()).toThrowError('Cannot set \'crossAccountKeys\' if an existing CodePipeline is given using \'codePipeline\'');
});
test('Providing codePipeline parameter and reuseCrossRegionSupportStacks parameter should throw error', () => {
expect(() => new CodePipelinePropsCheckTest(app, 'CodePipeline', {
reuseCrossRegionSupportStacks: true,
}).create()).toThrowError('Cannot set \'reuseCrossRegionSupportStacks\' if an existing CodePipeline is given using \'codePipeline\'');
});
});

test('Policy sizes do not exceed the maximum size', () => {
const pipelineStack = new cdk.Stack(app, 'PipelineStack', { env: PIPELINE_ENV });
pipelineStack.node.setContext('@aws-cdk/aws-iam:minimizePolicies', true);
Expand Down Expand Up @@ -112,7 +131,6 @@ test('Policy sizes do not exceed the maximum size', () => {
}
}


// Validate sizes
//
// Not entirely accurate, because our "Ref"s and "Fn::GetAtt"s actually need to be evaluated
Expand Down Expand Up @@ -217,4 +235,41 @@ class ReuseStack extends cdk.Stack {
super(scope, id, props);
new sqs.Queue(this, 'Queue');
}
}
}

interface CodePipelineStackProps extends cdk.StackProps {
pipelineName?: string;
crossAccountKeys?: boolean;
reuseCrossRegionSupportStacks?: boolean;
}

class CodePipelinePropsCheckTest extends cdk.Stack {
cProps: CodePipelineStackProps;
public constructor(scope: Construct, id: string, props: CodePipelineStackProps) {
super(scope, id, props);
this.cProps = props;
}
public create() {
if (this.cProps.pipelineName !== undefined) {
new cdkp.CodePipeline(this, 'CodePipeline1', {
pipelineName: this.cProps.pipelineName,
codePipeline: new Pipeline(this, 'Pipeline1'),
synth: new cdkp.ShellStep('Synth', { commands: ['ls'] }),
}).buildPipeline();
}
if (this.cProps.crossAccountKeys !== undefined) {
new cdkp.CodePipeline(this, 'CodePipeline2', {
crossAccountKeys: this.cProps.crossAccountKeys,
codePipeline: new Pipeline(this, 'Pipline2'),
synth: new cdkp.ShellStep('Synth', { commands: ['ls'] }),
}).buildPipeline();
}
if (this.cProps.reuseCrossRegionSupportStacks !== undefined) {
new cdkp.CodePipeline(this, 'CodePipeline3', {
reuseCrossRegionSupportStacks: this.cProps.reuseCrossRegionSupportStacks,
codePipeline: new Pipeline(this, 'Pipline3'),
synth: new cdkp.ShellStep('Synth', { commands: ['ls'] }),
}).buildPipeline();
}
}
}

0 comments on commit 9c0ccca

Please sign in to comment.