|
1 | 1 | import { Template, Annotations, Match } from '@aws-cdk/assertions'; |
2 | 2 | import * as ccommit from '@aws-cdk/aws-codecommit'; |
3 | 3 | import { Pipeline } from '@aws-cdk/aws-codepipeline'; |
| 4 | +import * as iam from '@aws-cdk/aws-iam'; |
4 | 5 | import * as sqs from '@aws-cdk/aws-sqs'; |
5 | 6 | import * as cdk from '@aws-cdk/core'; |
| 7 | +import { Stack } from '@aws-cdk/core'; |
6 | 8 | import { Construct } from 'constructs'; |
7 | 9 | import * as cdkp from '../../lib'; |
| 10 | +import { CodePipeline } from '../../lib'; |
8 | 11 | import { PIPELINE_ENV, TestApp, ModernTestGitHubNpmPipeline, FileAssetApp } from '../testhelpers'; |
9 | 12 |
|
10 | 13 | let app: TestApp; |
@@ -88,6 +91,15 @@ describe('Providing codePipeline parameter and prop(s) of codePipeline parameter |
88 | 91 | reuseCrossRegionSupportStacks: true, |
89 | 92 | }).create()).toThrowError('Cannot set \'reuseCrossRegionSupportStacks\' if an existing CodePipeline is given using \'codePipeline\''); |
90 | 93 | }); |
| 94 | + test('Providing codePipeline parameter and role parameter should throw error', () => { |
| 95 | + const stack = new Stack(app, 'Stack'); |
| 96 | + |
| 97 | + expect(() => new CodePipelinePropsCheckTest(stack, 'CodePipeline', { |
| 98 | + role: new iam.Role(stack, 'Role', { |
| 99 | + assumedBy: new iam.ServicePrincipal('codebuild.amazonaws.com'), |
| 100 | + }), |
| 101 | + }).create()).toThrowError('Cannot set \'role\' if an existing CodePipeline is given using \'codePipeline\''); |
| 102 | + }); |
91 | 103 | }); |
92 | 104 |
|
93 | 105 | test('Policy sizes do not exceed the maximum size', () => { |
@@ -180,6 +192,51 @@ test('CodeBuild action role has the right AssumeRolePolicyDocument', () => { |
180 | 192 | }); |
181 | 193 | }); |
182 | 194 |
|
| 195 | +test('CodePipeline supports use of existing role', () => { |
| 196 | + const pipelineStack = new cdk.Stack(app, 'PipelineStack', { env: PIPELINE_ENV }); |
| 197 | + const repo = new ccommit.Repository(pipelineStack, 'Repo', { |
| 198 | + repositoryName: 'MyRepo', |
| 199 | + }); |
| 200 | + const cdkInput = cdkp.CodePipelineSource.codeCommit( |
| 201 | + repo, |
| 202 | + 'main', |
| 203 | + ); |
| 204 | + |
| 205 | + new CodePipeline(pipelineStack, 'Pipeline', { |
| 206 | + synth: new cdkp.ShellStep('Synth', { |
| 207 | + input: cdkInput, |
| 208 | + installCommands: ['npm ci'], |
| 209 | + commands: [ |
| 210 | + 'npm run build', |
| 211 | + 'npx cdk synth', |
| 212 | + ], |
| 213 | + }), |
| 214 | + role: new iam.Role(pipelineStack, 'CustomRole', { |
| 215 | + assumedBy: new iam.ServicePrincipal('codepipeline.amazonaws.com'), |
| 216 | + roleName: 'MyCustomPipelineRole', |
| 217 | + }), |
| 218 | + }); |
| 219 | + |
| 220 | + const template = Template.fromStack(pipelineStack); |
| 221 | + template.hasResourceProperties('AWS::IAM::Role', { |
| 222 | + AssumeRolePolicyDocument: { |
| 223 | + Statement: [ |
| 224 | + { |
| 225 | + Action: 'sts:AssumeRole', |
| 226 | + Effect: 'Allow', |
| 227 | + Principal: { |
| 228 | + Service: 'codepipeline.amazonaws.com', |
| 229 | + }, |
| 230 | + }, |
| 231 | + ], |
| 232 | + }, |
| 233 | + RoleName: 'MyCustomPipelineRole', |
| 234 | + }); |
| 235 | + template.hasResourceProperties('AWS::CodePipeline::Pipeline', { |
| 236 | + RoleArn: { 'Fn::GetAtt': ['CustomRole6D8E6809', 'Arn'] }, |
| 237 | + }); |
| 238 | +}); |
| 239 | + |
183 | 240 | interface ReuseCodePipelineStackProps extends cdk.StackProps { |
184 | 241 | reuseCrossRegionSupportStacks?: boolean; |
185 | 242 | } |
@@ -241,6 +298,7 @@ interface CodePipelineStackProps extends cdk.StackProps { |
241 | 298 | pipelineName?: string; |
242 | 299 | crossAccountKeys?: boolean; |
243 | 300 | reuseCrossRegionSupportStacks?: boolean; |
| 301 | + role?: iam.IRole; |
244 | 302 | } |
245 | 303 |
|
246 | 304 | class CodePipelinePropsCheckTest extends cdk.Stack { |
@@ -271,5 +329,12 @@ class CodePipelinePropsCheckTest extends cdk.Stack { |
271 | 329 | synth: new cdkp.ShellStep('Synth', { commands: ['ls'] }), |
272 | 330 | }).buildPipeline(); |
273 | 331 | } |
| 332 | + if (this.cProps.role !== undefined) { |
| 333 | + new cdkp.CodePipeline(this, 'CodePipeline4', { |
| 334 | + role: this.cProps.role, |
| 335 | + codePipeline: new Pipeline(this, 'Pipline4'), |
| 336 | + synth: new cdkp.ShellStep('Synth', { commands: ['ls'] }), |
| 337 | + }).buildPipeline(); |
| 338 | + } |
274 | 339 | } |
275 | 340 | } |
0 commit comments