-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathfunction.test.ts
147 lines (125 loc) · 4 KB
/
function.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
import '@aws-cdk/assert-internal/jest';
import * as path from 'path';
import { Runtime } from '@aws-cdk/aws-lambda';
import { Stack } from '@aws-cdk/core';
import { GoFunction } from '../lib';
import { Bundling } from '../lib/bundling';
jest.mock('../lib/bundling', () => {
return {
Bundling: {
bundle: jest.fn().mockReturnValue({
bind: () => {
return { s3Location: 'code' };
},
bindToResource: () => { return; },
}),
},
};
});
let stack: Stack;
beforeEach(() => {
stack = new Stack();
jest.clearAllMocks();
});
test('GoFunction with defaults', () => {
// WHEN
new GoFunction(stack, 'handler', {
entry: path.join(__dirname, 'lambda-handler-vendor/cmd/api'),
});
expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({
entry: expect.stringMatching(/@aws-cdk\/aws-lambda-go\/test\/lambda-handler-vendor\/cmd\/api$/),
}));
expect(stack).toHaveResource('AWS::Lambda::Function', {
Handler: 'bootstrap',
Runtime: 'provided.al2',
});
});
test('GoFunction with using provided runtime', () => {
// WHEN
new GoFunction(stack, 'handler', {
entry: 'test/lambda-handler-vendor/cmd/api',
runtime: Runtime.PROVIDED,
});
expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({
entry: expect.stringMatching(/@aws-cdk\/aws-lambda-go\/test\/lambda-handler-vendor\/cmd\/api$/),
}));
expect(stack).toHaveResource('AWS::Lambda::Function', {
Handler: 'bootstrap',
Runtime: 'provided',
});
});
test('GoFunction with using golang runtime', () => {
// WHEN
new GoFunction(stack, 'handler', {
entry: 'test/lambda-handler-vendor/cmd/api',
runtime: Runtime.GO_1_X,
});
expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({
entry: expect.stringMatching(/@aws-cdk\/aws-lambda-go\/test\/lambda-handler-vendor\/cmd\/api$/),
}));
expect(stack).toHaveResource('AWS::Lambda::Function', {
Handler: 'bootstrap',
Runtime: 'go1.x',
});
});
test('GoFunction with container env vars', () => {
// WHEN
new GoFunction(stack, 'handler', {
entry: 'test/lambda-handler-vendor/cmd/api',
bundling: {
environment: {
KEY: 'VALUE',
},
},
});
expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({
environment: {
KEY: 'VALUE',
},
}));
});
test('throws with the wrong runtime family', () => {
expect(() => new GoFunction(stack, 'handler', {
entry: 'test/lambda-handler-vendor/cmd/api',
runtime: Runtime.PYTHON_3_8,
})).toThrow(/Only `go` and `provided` runtimes are supported/);
});
test('resolves entry to an absolute path', () => {
// WHEN
new GoFunction(stack, 'fn', {
entry: 'test/lambda-handler-vendor/cmd/api/main.go',
});
expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({
entry: expect.stringMatching(/@aws-cdk\/aws-lambda-go\/test\/lambda-handler-vendor\/cmd\/api\/main.go$/),
}));
});
test('throws with no existing go.mod file', () => {
expect(() => new GoFunction(stack, 'handler', {
entry: 'test/lambda-handler-vendor/cmd/api',
moduleDir: '/does/not/exist/go.mod',
})).toThrow(/go.mod file at \/does\/not\/exist\/go.mod doesn't exist/);
});
test('throws with incorrect moduleDir file', () => {
expect(() => new GoFunction(stack, 'handler', {
entry: 'test/lambda-handler-vendor/cmd/api',
moduleDir: '/does/not/exist.mod',
})).toThrow(/moduleDir is specifying a file that is not go.mod/);
});
test('custom moduleDir can be used', () => {
new GoFunction(stack, 'handler', {
entry: 'test/lambda-handler-vendor/cmd/api',
moduleDir: 'test/lambda-handler-vendor',
});
expect(stack).toHaveResource('AWS::Lambda::Function', {
Handler: 'bootstrap',
});
});
test('custom moduleDir with file path can be used', () => {
new GoFunction(stack, 'handler', {
entry: 'test/lambda-handler-vendor/cmd/api',
moduleDir: 'test/lambda-handler-vendor/go.mod',
});
expect(stack).toHaveResource('AWS::Lambda::Function', {
Handler: 'bootstrap',
});
});