-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathcontent.test.ts
212 lines (181 loc) · 7.05 KB
/
content.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
import { Vpc } from '../../aws-ec2';
import * as elbv2 from '../../aws-elasticloadbalancingv2';
import * as iam from '../../aws-iam';
import * as lambda from '../../aws-lambda';
import * as s3 from '../../aws-s3';
import { Lazy, Stack, Fn, CfnMapping } from '../../core';
import { Source } from '../lib';
import { renderData } from '../lib/render-data';
test('simple string', () => {
const stack = new Stack();
expect(renderData(stack, 'foo')).toStrictEqual({
markers: {},
text: 'foo',
});
});
test('string with a "Ref" token', () => {
const stack = new Stack();
const bucket = new s3.Bucket(stack, 'Bucket');
expect(renderData(stack, `foo-${bucket.bucketName}`)).toStrictEqual({
text: 'foo-<<marker:0xbaba:0>>',
markers: { '<<marker:0xbaba:0>>': { Ref: 'Bucket83908E77' } },
});
});
test('string is a single "Ref" token', () => {
const stack = new Stack();
const bucket = new s3.Bucket(stack, 'Bucket');
expect(renderData(stack, bucket.bucketName)).toStrictEqual({
text: '<<marker:0xbaba:0>>',
markers: { '<<marker:0xbaba:0>>': { Ref: 'Bucket83908E77' } },
});
});
test('string is a single "Fn::GetAtt" token', () => {
const stack = new Stack();
const bucket = new s3.Bucket(stack, 'Bucket');
expect(renderData(stack, bucket.bucketRegionalDomainName)).toStrictEqual({
text: '<<marker:0xbaba:0>>',
markers: { '<<marker:0xbaba:0>>': { 'Fn::GetAtt': ['Bucket83908E77', 'RegionalDomainName'] } },
});
});
test('string with a "Fn::GetAtt" token', () => {
const stack = new Stack();
const bucket = new s3.Bucket(stack, 'Bucket');
expect(renderData(stack, `foo-${bucket.bucketArn}`)).toStrictEqual({
text: 'foo-<<marker:0xbaba:0>>',
markers: { '<<marker:0xbaba:0>>': { 'Fn::GetAtt': ['Bucket83908E77', 'Arn'] } },
});
});
test('string is a single "Fn::Select" token', () => {
const stack = new Stack();
const bucket = new s3.Bucket(stack, 'Bucket');
expect(renderData(stack, bucket.bucketWebsiteDomainName)).toStrictEqual({
text: '<<marker:0xbaba:0>>',
markers: { '<<marker:0xbaba:0>>': { 'Fn::Select': [2, { 'Fn::Split': ['/', { 'Fn::GetAtt': ['Bucket83908E77', 'WebsiteURL'] }] }] } },
});
});
test('string with a "Fn::Select" token', () => {
const stack = new Stack();
const bucket = new s3.Bucket(stack, 'Bucket');
expect(renderData(stack, `test.${bucket.bucketWebsiteDomainName}`)).toStrictEqual({
text: 'test.<<marker:0xbaba:0>>',
markers: { '<<marker:0xbaba:0>>': { 'Fn::Select': [2, { 'Fn::Split': ['/', { 'Fn::GetAtt': ['Bucket83908E77', 'WebsiteURL'] }] }] } },
});
});
test('multiple markers', () => {
const stack = new Stack();
const bucket = new s3.Bucket(stack, 'Bucket');
expect(renderData(stack, `boom-${bucket.bucketName}-bam-${bucket.bucketArn}`)).toStrictEqual({
text: 'boom-<<marker:0xbaba:0>>-bam-<<marker:0xbaba:1>>',
markers: {
'<<marker:0xbaba:0>>': { Ref: 'Bucket83908E77' },
'<<marker:0xbaba:1>>': { 'Fn::GetAtt': ['Bucket83908E77', 'Arn'] },
},
});
});
test('json-encoded string', () => {
const stack = new Stack();
const bucket = new s3.Bucket(stack, 'Bucket');
const tg = new elbv2.ApplicationTargetGroup(stack, 'TargetGroup');
const lb = new elbv2.ApplicationLoadBalancer(stack, 'LB', { vpc: new Vpc(stack, 'vpc') });
lb.addListener('Listener', { port: 80, defaultTargetGroups: [tg] });
const json = {
BucketArn: bucket.bucketArn,
BucketName: bucket.bucketName,
ComplexFnSelect: tg.firstLoadBalancerFullName + '/' + tg.targetGroupFullName,
};
const expectedTextResult = {
BucketArn: '<<marker:0xbaba:0>>',
BucketName: '<<marker:0xbaba:1>>',
ComplexFnSelect: '<<marker:0xbaba:2>>/<<marker:0xbaba:3>>/<<marker:0xbaba:4>>/<<marker:0xbaba:5>>',
};
expect(renderData(stack, JSON.stringify(json))).toStrictEqual({
text: JSON.stringify(expectedTextResult),
markers: {
'<<marker:0xbaba:0>>': { 'Fn::GetAtt': ['Bucket83908E77', 'Arn'] },
'<<marker:0xbaba:1>>': { Ref: 'Bucket83908E77' },
'<<marker:0xbaba:2>>': { 'Fn::Select': [1, { 'Fn::Split': ['/', { Ref: 'LBListener49E825B4' }] }] },
'<<marker:0xbaba:3>>': { 'Fn::Select': [2, { 'Fn::Split': ['/', { Ref: 'LBListener49E825B4' }] }] },
'<<marker:0xbaba:4>>': { 'Fn::Select': [3, { 'Fn::Split': ['/', { Ref: 'LBListener49E825B4' }] }] },
'<<marker:0xbaba:5>>': { 'Fn::GetAtt': ['TargetGroup3D7CD9B8', 'TargetGroupFullName'] },
},
});
});
test('markers are returned in the source config', () => {
const stack = new Stack();
const handler = new lambda.Function(stack, 'Handler', { runtime: lambda.Runtime.NODEJS_LATEST, code: lambda.Code.fromInline('foo'), handler: 'index.handler' });
const actual = Source.data('file1.txt', `boom-${stack.account}`).bind(stack, { handlerRole: handler.role! });
expect(actual.markers).toStrictEqual({
'<<marker:0xbaba:0>>': { Ref: 'AWS::AccountId' },
});
});
test('lazy string which can be fully resolved', () => {
const stack = new Stack();
expect(renderData(stack, Lazy.string({ produce: () => 'resolved!' }))).toStrictEqual({
text: 'resolved!',
markers: { },
});
});
test('lazy within a string which can be fully resolved', () => {
const stack = new Stack();
const token = Lazy.string({ produce: () => 'resolved!' });
expect(renderData(stack, `hello, ${token}`)).toStrictEqual({
text: 'hello, resolved!',
markers: { },
});
});
test('lazy string which resolves to something with a deploy-time value', () => {
const stack = new Stack();
const token = Lazy.string({ produce: () => 'resolved!' });
expect(renderData(stack, `hello, ${token}`)).toStrictEqual({
text: 'hello, resolved!',
markers: { },
});
});
test('supports Fn::FindInMap in deploy-time json data', () => {
const stack = new Stack();
const mapping = new CfnMapping(stack, 'TestMapping', {
mapping: {
responseTemplate: { full: 'example value' },
},
});
const source = Source.jsonData('file.json', {
key: Fn.findInMap('TestMapping', 'responseTemplate', 'full'),
});
expect(() =>
source.bind(stack, {
handlerRole: new iam.Role(stack, 'Role', {
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
}),
}),
).not.toThrow();
});
test('renderData returns plain string untouched', () => {
const stack = new Stack();
const input = 'plain-string';
const result = renderData(stack, input);
expect(result.text).toBe('plain-string');
expect(result.markers).toEqual({});
});
test('renderData parses Fn::Join with string and Ref parts', () => {
const stack = new Stack();
const input = {
'Fn::Join': ['', [
'prefix-',
{ Ref: 'MyParam' },
'-suffix'
]]
};
const result = renderData(stack, input as any);
expect(result.text).toContain('prefix-');
expect(result.text).toContain('-suffix');
expect(Object.values(result.markers)[0]).toEqual({ Ref: 'MyParam' });
});
test('throws on invalid marker object key', () => {
const stack = new Stack();
const input = {
'Fn::Join': ['', [
{ 'Fn::Wrong': ['oops'] }
]]
};
expect(() => renderData(stack, input as any)).toThrow(/Invalid CloudFormation reference/);
});