/
application-associator.test.ts
213 lines (193 loc) · 8.73 KB
/
application-associator.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { Annotations, Template } from '@aws-cdk/assertions';
import * as codecommit from '@aws-cdk/aws-codecommit';
import * as cdk from '@aws-cdk/core';
import * as codepipeline from '@aws-cdk/pipelines';
import { Construct } from 'constructs';
import * as appreg from '../lib';
describe('Scope based Associations with Application within Same Account', () => {
let app: cdk.App;
beforeEach(() => {
app = new cdk.App({
context: {
'@aws-cdk/core:newStyleStackSynthesis': false,
},
});
});
test('ApplicationAssociator will associate allStacks created inside cdkApp', () => {
const appAssociator = new appreg.ApplicationAssociator(app, 'MyApplication', {
applications: [appreg.TargetApplication.createApplicationStack({
applicationName: 'MyAssociatedApplication',
stackName: 'MyAssociatedApplicationStack',
})],
});
const anotherStack = new AppRegistrySampleStack(app, 'SampleStack');
Template.fromStack(appAssociator.appRegistryApplication().stack).resourceCountIs('AWS::ServiceCatalogAppRegistry::Application', 1);
Template.fromStack(appAssociator.appRegistryApplication().stack).hasResourceProperties('AWS::ServiceCatalogAppRegistry::Application', {
Name: 'MyAssociatedApplication',
Tags: { managedBy: 'CDK_Application_Associator' },
});
Template.fromStack(anotherStack).resourceCountIs('AWS::ServiceCatalogAppRegistry::ResourceAssociation', 1);
Template.fromStack(anotherStack).hasResourceProperties('AWS::ServiceCatalogAppRegistry::ResourceAssociation', {
Application: 'MyAssociatedApplication',
Resource: { Ref: 'AWS::StackId' },
});
});
});
describe('Scope based Associations with Application with Cross Region/Account', () => {
let app: cdk.App;
beforeEach(() => {
app = new cdk.App({
context: {
'@aws-cdk/core:newStyleStackSynthesis': false,
},
});
});
test('ApplicationAssociator in cross-account associates all stacks created inside cdk app', () => {
new appreg.ApplicationAssociator(app, 'MyApplication', {
applications: [appreg.TargetApplication.createApplicationStack({
applicationName: 'MyAssociatedApplication',
stackName: 'MyAssociatedApplicationStack',
})],
});
const firstStack = new cdk.Stack(app, 'testStack', {
env: { account: 'account2', region: 'region' },
});
const nestedStack = new cdk.Stack(firstStack, 'MyFirstStack', {
env: { account: 'account2', region: 'region' },
});
Template.fromStack(firstStack).resourceCountIs('AWS::ServiceCatalogAppRegistry::ResourceAssociation', 1);
Template.fromStack(nestedStack).resourceCountIs('AWS::ServiceCatalogAppRegistry::ResourceAssociation', 1);
});
test('ApplicationAssociator creation failed when neither Application name nor ARN is provided', () => {
expect(() => {
new appreg.ApplicationAssociator(app, 'MyApplication', {
applications: [],
});
}).toThrow('Please pass exactly 1 instance of TargetApplication.createApplicationStack() or TargetApplication.existingApplicationFromArn() into the "applications" property');
});
test('ApplicationAssociator creation failed when both Application name and ARN is provided', () => {
expect(() => {
new appreg.ApplicationAssociator(app, 'MyApplication', {
applications: [appreg.TargetApplication.existingApplicationFromArn({
applicationArnValue: 'arn:aws:servicecatalog:us-east-1:482211128593:/applications/0a17wtxeg5vilok0sbxfozwpq9',
}),
appreg.TargetApplication.createApplicationStack({
applicationName: 'MyAssociatedApplication',
})],
});
}).toThrow('Please pass exactly 1 instance of TargetApplication.createApplicationStack() or TargetApplication.existingApplicationFromArn() into the "applications" property');
});
test('associate resource on imported application', () => {
const resource = new cdk.Stack(app, 'MyStack');
new appreg.ApplicationAssociator(app, 'MyApplication', {
applications: [appreg.TargetApplication.existingApplicationFromArn({
applicationArnValue: 'arn:aws:servicecatalog:us-east-1:482211128593:/applications/0a17wtxeg5vilok0sbxfozwpq9',
})],
});
Template.fromStack(resource).hasResourceProperties('AWS::ServiceCatalogAppRegistry::ResourceAssociation', {
Application: '0a17wtxeg5vilok0sbxfozwpq9',
Resource: { Ref: 'AWS::StackId' },
});
}),
test('ApplicationAssociator with cross region stacks inside cdkApp throws error', () => {
new appreg.ApplicationAssociator(app, 'MyApplication', {
applications: [appreg.TargetApplication.createApplicationStack({
applicationName: 'MyAssociatedApplication',
stackName: 'MyAssociatedApplicationStack',
env: { account: 'account2', region: 'region2' },
})],
});
const crossRegionStack = new cdk.Stack(app, 'crossRegionStack', {
env: { account: 'account', region: 'region' },
});
Annotations.fromStack(crossRegionStack).hasWarning('*', 'AppRegistry does not support cross region associations, deployment might fail if there is cross region stacks in the app.'
+ ' Application region region2, stack region region');
});
test('Environment Agnostic ApplicationAssociator with cross region stacks inside cdkApp gives warning', () => {
new appreg.ApplicationAssociator(app, 'MyApplication', {
applications: [appreg.TargetApplication.createApplicationStack({
applicationName: 'MyAssociatedApplication',
stackName: 'MyAssociatedApplicationStack',
})],
});
const crossRegionStack = new cdk.Stack(app, 'crossRegionStack', {
env: { account: 'account', region: 'region' },
});
Annotations.fromStack(crossRegionStack).hasWarning('*', 'Environment agnostic stack determined, AppRegistry association might not work as expected in case you deploy cross-region or cross-account stack.');
});
test('Cdk App Containing Pipeline with stage but stage not associated throws error', () => {
const application = new appreg.ApplicationAssociator(app, 'MyApplication', {
applications: [appreg.TargetApplication.createApplicationStack({
applicationName: 'MyAssociatedApplication',
stackName: 'MyAssociatedApplicationStack',
})],
});
const pipelineStack = new AppRegistrySampleCodePipelineStack(app, 'PipelineStackA', {
application: application,
associateStage: false,
});
app.synth();
Annotations.fromStack(pipelineStack).hasError('*',
'Associate Stage: SampleStage to ensure all stacks in your cdk app are associated with AppRegistry. You can use ApplicationAssociator.associateStage to associate any stage.');
});
test('Cdk App Containing Pipeline with stage and stage associated successfully gets synthesized', () => {
const application = new appreg.ApplicationAssociator(app, 'MyApplication', {
applications: [appreg.TargetApplication.createApplicationStack({
applicationName: 'MyAssociatedApplication',
stackName: 'MyAssociatedApplicationStack',
})],
});
const pipelineStack = new AppRegistrySampleCodePipelineStack(app, 'PipelineStackA', {
application: application,
associateStage: true,
});
app.synth();
Template.fromStack(pipelineStack).resourceCountIs('AWS::ServiceCatalogAppRegistry::ResourceAssociation', 1);
});
});
interface AppRegistrySampleCodePipelineStackProps extends cdk.StackProps {
application: appreg.ApplicationAssociator;
associateStage: boolean;
}
class AppRegistrySampleCodePipelineStack extends cdk.Stack {
public constructor(scope: Construct, id: string, props: AppRegistrySampleCodePipelineStackProps ) {
super(scope, id, props);
const repo = new codecommit.Repository(this, 'Repo', {
repositoryName: 'MyRepo',
});
const pipeline = new codepipeline.CodePipeline(this, 'Pipeline', {
pipelineName: 'MyPipeline',
synth: new codepipeline.CodeBuildStep('SynthStep', {
input: codepipeline.CodePipelineSource.codeCommit(repo, 'main'),
installCommands: [
'npm install -g aws-cdk',
],
commands: [
'npm ci',
'npm run build',
'npx cdk synth',
],
},
),
});
const stage = new AppRegistrySampleStage(
this,
'SampleStage',
);
if (props.associateStage) {
props.application.associateStage(stage);
}
pipeline.addStage(stage);
}
}
class AppRegistrySampleStage extends cdk.Stage {
public constructor(scope: Construct, id: string, props?: cdk.StageProps) {
super(scope, id, props);
new AppRegistrySampleStack(this, 'SampleStack', {});
}
}
class AppRegistrySampleStack extends cdk.Stack {
public constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
}
}