/
codepipeline.test.ts
153 lines (135 loc) · 5.02 KB
/
codepipeline.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { Template, Annotations, Match } from '@aws-cdk/assertions';
import * as ccommit from '@aws-cdk/aws-codecommit';
import * as sqs from '@aws-cdk/aws-sqs';
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
import * as cdkp from '../../lib';
import { PIPELINE_ENV, TestApp } from '../testhelpers';
let app: TestApp;
beforeEach(() => {
app = new TestApp();
});
afterEach(() => {
app.cleanup();
});
const testStageEnv: Required<cdk.Environment> = {
account: '123456789012',
region: 'us-east-1',
};
describe('CodePipeline support stack reuse', () => {
test('CodePipeline generates the same support stack containing the replication Bucket without the need to bootstrap in that environment for multiple pipelines', () => {
new ReuseCodePipelineStack(app, 'PipelineStackA', {
env: PIPELINE_ENV,
});
new ReuseCodePipelineStack(app, 'PipelineStackB', {
env: PIPELINE_ENV,
});
const assembly = app.synth();
// 2 Pipeline Stacks and 1 support stack for both pipeline stacks.
expect(assembly.stacks.length).toEqual(3);
assembly.getStackByName(`PipelineStackA-support-${testStageEnv.region}`);
expect(() => {
assembly.getStackByName(`PipelineStackB-support-${testStageEnv.region}`);
}).toThrowError(/Unable to find stack with stack name/);
});
test('CodePipeline generates the unique support stack containing the replication Bucket without the need to bootstrap in that environment for multiple pipelines', () => {
new ReuseCodePipelineStack(app, 'PipelineStackA', {
env: PIPELINE_ENV,
reuseCrossRegionSupportStacks: false,
});
new ReuseCodePipelineStack(app, 'PipelineStackB', {
env: PIPELINE_ENV,
reuseCrossRegionSupportStacks: false,
});
const assembly = app.synth();
// 2 Pipeline Stacks and 1 support stack for each pipeline stack.
expect(assembly.stacks.length).toEqual(4);
const supportStackAArtifact = assembly.getStackByName(`PipelineStackA-support-${testStageEnv.region}`);
const supportStackBArtifact = assembly.getStackByName(`PipelineStackB-support-${testStageEnv.region}`);
const supportStackATemplate = Template.fromJSON(supportStackAArtifact.template);
supportStackATemplate.hasResourceProperties('AWS::S3::Bucket', {
BucketName: 'pipelinestacka-support-useplicationbucket80db3753a0ebbf052279',
});
supportStackATemplate.hasResourceProperties('AWS::KMS::Alias', {
AliasName: 'alias/pport-ustencryptionalias5cad45754e1ff088476b',
});
const supportStackBTemplate = Template.fromJSON(supportStackBArtifact.template);
supportStackBTemplate.hasResourceProperties('AWS::S3::Bucket', {
BucketName: 'pipelinestackb-support-useplicationbucket1d556ec7f959b336abf8',
});
supportStackBTemplate.hasResourceProperties('AWS::KMS::Alias', {
AliasName: 'alias/pport-ustencryptionalias668c7ffd0de17c9867b0',
});
});
});
test('CodeBuild action role has the right AssumeRolePolicyDocument', () => {
const pipelineStack = new cdk.Stack(app, 'PipelineStack', { env: PIPELINE_ENV });
new ModernTestGitHubNpmPipeline(pipelineStack, 'Cdk');
const template = Template.fromStack(pipelineStack);
template.hasResourceProperties('AWS::IAM::Role', {
AssumeRolePolicyDocument: {
Statement: [
{
Action: 'sts:AssumeRole',
Principal: {
AWS: { 'Fn::Join': ['', ['arn:', { Ref: 'AWS::Partition' }, ':iam::123pipeline:root']] },
},
Condition: {
Bool: {
'aws:ViaAWSService': 'codepipeline.amazonaws.com',
},
},
},
],
},
});
});
interface ReuseCodePipelineStackProps extends cdk.StackProps {
reuseCrossRegionSupportStacks?: boolean;
}
class ReuseCodePipelineStack extends cdk.Stack {
public constructor(scope: Construct, id: string, props: ReuseCodePipelineStackProps ) {
super(scope, id, props);
const repo = new ccommit.Repository(this, 'Repo', {
repositoryName: 'MyRepo',
});
const cdkInput = cdkp.CodePipelineSource.codeCommit(
repo,
'main',
);
const synthStep = new cdkp.ShellStep('Synth', {
input: cdkInput,
installCommands: ['npm ci'],
commands: [
'npm run build',
'npx cdk synth',
],
});
const pipeline = new cdkp.CodePipeline(this, 'Pipeline', {
synth: synthStep,
selfMutation: true,
crossAccountKeys: true,
reuseCrossRegionSupportStacks: props.reuseCrossRegionSupportStacks,
});
const stage = new ReuseStage(
this,
`SampleStage-${testStageEnv.account}-${testStageEnv.region}`,
{
env: testStageEnv,
},
);
pipeline.addStage(stage);
}
}
class ReuseStage extends cdk.Stage {
public constructor(scope: Construct, id: string, props: cdk.StageProps) {
super(scope, id, props);
new ReuseStack(this, 'SampleStack', {});
}
}
class ReuseStack extends cdk.Stack {
public constructor(scope: Construct, id: string, props: cdk.StackProps) {
super(scope, id, props);
new sqs.Queue(this, 'Queue');
}
}