/
custom-resource-provider.test.ts
236 lines (211 loc) · 7.1 KB
/
custom-resource-provider.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import * as fs from 'fs';
import * as path from 'path';
import { nodeunitShim, Test } from 'nodeunit-shim';
import { App, AssetStaging, CustomResourceProvider, CustomResourceProviderRuntime, DockerImageAssetLocation, DockerImageAssetSource, Duration, FileAssetLocation, FileAssetSource, ISynthesisSession, Size, Stack } from '../../lib';
import { toCloudFormation } from '../util';
const TEST_HANDLER = `${__dirname}/mock-provider`;
nodeunitShim({
'minimal configuration'(test: Test) {
// GIVEN
const stack = new Stack();
// WHEN
CustomResourceProvider.getOrCreate(stack, 'Custom:MyResourceType', {
codeDirectory: TEST_HANDLER,
runtime: CustomResourceProviderRuntime.NODEJS_12,
});
// THEN
test.ok(fs.existsSync(path.join(TEST_HANDLER, '__entrypoint__.js')), 'expecting entrypoint to be copied to the handler directory');
const cfn = toCloudFormation(stack);
// The asset hash constantly changes, so in order to not have to chase it, just look
// it up from the output.
const staging = stack.node.tryFindChild('Custom:MyResourceTypeCustomResourceProvider')?.node.tryFindChild('Staging') as AssetStaging;
const assetHash = staging.sourceHash;
const paramNames = Object.keys(cfn.Parameters);
const bucketParam = paramNames[0];
const keyParam = paramNames[1];
const hashParam = paramNames[2];
test.deepEqual(cfn, {
Resources: {
CustomMyResourceTypeCustomResourceProviderRoleBD5E655F: {
Type: 'AWS::IAM::Role',
Properties: {
AssumeRolePolicyDocument: {
Version: '2012-10-17',
Statement: [
{
Action: 'sts:AssumeRole',
Effect: 'Allow',
Principal: {
Service: 'lambda.amazonaws.com',
},
},
],
},
ManagedPolicyArns: [
{
'Fn::Sub': 'arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole',
},
],
},
},
CustomMyResourceTypeCustomResourceProviderHandler29FBDD2A: {
Type: 'AWS::Lambda::Function',
Properties: {
Code: {
S3Bucket: { Ref: bucketParam },
S3Key: {
'Fn::Join': [
'',
[
{
'Fn::Select': [
0,
{
'Fn::Split': [
'||',
{ Ref: keyParam },
],
},
],
},
{
'Fn::Select': [
1,
{
'Fn::Split': [
'||',
{ Ref: keyParam },
],
},
],
},
],
],
},
},
Timeout: 900,
MemorySize: 128,
Handler: '__entrypoint__.handler',
Role: {
'Fn::GetAtt': [
'CustomMyResourceTypeCustomResourceProviderRoleBD5E655F',
'Arn',
],
},
Runtime: 'nodejs12.x',
},
DependsOn: [
'CustomMyResourceTypeCustomResourceProviderRoleBD5E655F',
],
},
},
Parameters: {
[bucketParam]: {
Type: 'String',
Description: `S3 bucket for asset "${assetHash}"`,
},
[keyParam]: {
Type: 'String',
Description: `S3 key for asset version "${assetHash}"`,
},
[hashParam]: {
Type: 'String',
Description: `Artifact hash for asset "${assetHash}"`,
},
},
});
test.done();
},
'custom resource provided creates asset in new-style synthesis with relative path'(test: Test) {
// GIVEN
let assetFilename : string | undefined;
const app = new App();
const stack = new Stack(app, 'Stack', {
synthesizer: {
bind(_stack: Stack): void { },
addFileAsset(asset: FileAssetSource): FileAssetLocation {
assetFilename = asset.fileName;
return { bucketName: '', httpUrl: '', objectKey: '', s3ObjectUrl: '', s3Url: '', kmsKeyArn: '' };
},
addDockerImageAsset(_asset: DockerImageAssetSource): DockerImageAssetLocation {
return { imageUri: '', repositoryName: '' };
},
synthesize(_session: ISynthesisSession): void { },
},
});
// WHEN
CustomResourceProvider.getOrCreate(stack, 'Custom:MyResourceType', {
codeDirectory: TEST_HANDLER,
runtime: CustomResourceProviderRuntime.NODEJS_12,
});
// THEN -- no exception
if (!assetFilename || assetFilename.startsWith(path.sep)) {
throw new Error(`Asset filename must be a relative path, got: ${assetFilename}`);
}
test.done();
},
'policyStatements can be used to add statements to the inline policy'(test: Test) {
// GIVEN
const stack = new Stack();
// WHEN
CustomResourceProvider.getOrCreate(stack, 'Custom:MyResourceType', {
codeDirectory: TEST_HANDLER,
runtime: CustomResourceProviderRuntime.NODEJS_12,
policyStatements: [
{ statement1: 123 },
{ statement2: { foo: 111 } },
],
});
// THEN
const template = toCloudFormation(stack);
const role = template.Resources.CustomMyResourceTypeCustomResourceProviderRoleBD5E655F;
test.deepEqual(role.Properties.Policies, [{
PolicyName: 'Inline',
PolicyDocument: {
Version: '2012-10-17',
Statement: [{ statement1: 123 }, { statement2: { foo: 111 } }],
},
}]);
test.done();
},
'memorySize and timeout'(test: Test) {
// GIVEN
const stack = new Stack();
// WHEN
CustomResourceProvider.getOrCreate(stack, 'Custom:MyResourceType', {
codeDirectory: TEST_HANDLER,
runtime: CustomResourceProviderRuntime.NODEJS_12,
memorySize: Size.gibibytes(2),
timeout: Duration.minutes(5),
});
// THEN
const template = toCloudFormation(stack);
const lambda = template.Resources.CustomMyResourceTypeCustomResourceProviderHandler29FBDD2A;
test.deepEqual(lambda.Properties.MemorySize, 2048);
test.deepEqual(lambda.Properties.Timeout, 300);
test.done();
},
'environment variables'(test: Test) {
// GIVEN
const stack = new Stack();
// WHEN
CustomResourceProvider.getOrCreate(stack, 'Custom:MyResourceType', {
codeDirectory: TEST_HANDLER,
runtime: CustomResourceProviderRuntime.NODEJS_12,
environment: {
B: 'b',
A: 'a',
},
});
// THEN
const template = toCloudFormation(stack);
const lambda = template.Resources.CustomMyResourceTypeCustomResourceProviderHandler29FBDD2A;
test.deepEqual(lambda.Properties.Environment, {
Variables: {
A: 'a',
B: 'b',
},
});
test.done();
},
});