-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathlayerPublisher.test.ts
192 lines (171 loc) · 5.49 KB
/
layerPublisher.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
/**
* Test LayerPublisherStack class
*
* @group e2e/layers/all
*/
import { App } from 'aws-cdk-lib';
import { LayerVersion } from 'aws-cdk-lib/aws-lambda';
import { LayerPublisherStack } from '../../src/layer-publisher-stack';
import {
TestNodejsFunction,
TestStack,
TestInvocationLogs,
invokeFunctionOnce,
generateTestUniqueName,
} from '@aws-lambda-powertools/testing-utils';
import {
RESOURCE_NAME_PREFIX,
SETUP_TIMEOUT,
TEARDOWN_TIMEOUT,
TEST_CASE_TIMEOUT,
} from './constants';
import { join } from 'node:path';
import packageJson from '../../package.json';
jest.spyOn(console, 'log').mockImplementation();
/**
* This test has two stacks:
* 1. LayerPublisherStack - publishes a layer version using the LayerPublisher construct and containing the Powertools utilities from the repo
* 2. TestStack - uses the layer published in the first stack and contains a lambda function that uses the Powertools utilities from the layer
*
* The lambda function is invoked once and the logs are collected. The goal of the test is to verify that the layer creation and usage works as expected.
*/
describe(`Layers E2E tests, publisher stack`, () => {
const testStack = new TestStack({
stackNameProps: {
stackNamePrefix: RESOURCE_NAME_PREFIX,
testName: 'functionStack',
},
});
let invocationLogs: TestInvocationLogs;
const ssmParameterLayerName = generateTestUniqueName({
testPrefix: `${RESOURCE_NAME_PREFIX}`,
testName: 'parameter',
});
// Location of the lambda function code
const lambdaFunctionCodeFilePath = join(
__dirname,
'layerPublisher.class.test.functionCode.ts'
);
const powerToolsPackageVersion = packageJson.version;
const layerApp = new App();
const layerId = generateTestUniqueName({
testPrefix: RESOURCE_NAME_PREFIX,
testName: 'layerStack',
});
const layerStack = new LayerPublisherStack(layerApp, layerId, {
layerName: layerId,
powertoolsPackageVersion: powerToolsPackageVersion,
buildFromLocal: true,
ssmParameterLayerArn: ssmParameterLayerName,
});
const testLayerStack = new TestStack({
stackNameProps: {
stackNamePrefix: RESOURCE_NAME_PREFIX,
testName: 'layerStack',
},
app: layerApp,
stack: layerStack,
});
beforeAll(async () => {
await testLayerStack.deploy();
const layerVersion = LayerVersion.fromLayerVersionArn(
testStack.stack,
'LayerVersionArnReference',
testLayerStack.findAndGetStackOutputValue('LatestLayerArn')
);
new TestNodejsFunction(
testStack,
{
entry: lambdaFunctionCodeFilePath,
environment: {
LAYERS_PATH: '/opt/nodejs/node_modules',
POWERTOOLS_PACKAGE_VERSION: powerToolsPackageVersion,
POWERTOOLS_SERVICE_NAME: 'LayerPublisherStack',
},
bundling: {
externalModules: [
'@aws-lambda-powertools/commons',
'@aws-lambda-powertools/logger',
'@aws-lambda-powertools/metrics',
'@aws-lambda-powertools/tracer',
'@aws-lambda-powertools/parameter',
'@aws-lambda-powertools/idempotency',
'@aws-lambda-powertools/batch',
],
},
layers: [layerVersion],
},
{
nameSuffix: 'testFn',
}
);
await testStack.deploy();
const functionName = testStack.findAndGetStackOutputValue('testFn');
invocationLogs = await invokeFunctionOnce({
functionName,
});
}, SETUP_TIMEOUT);
describe('package version and path check', () => {
it(
'should have no errors in the logs, which indicates the pacakges version matches the expected one',
() => {
const logs = invocationLogs.getFunctionLogs('ERROR');
expect(logs.length).toBe(0);
},
TEST_CASE_TIMEOUT
);
});
describe('utilities usage', () => {
it(
'should have one warning related to missing Metrics namespace',
() => {
const logs = invocationLogs.getFunctionLogs('WARN');
expect(logs.length).toBe(1);
expect(logs[0]).toContain('Namespace should be defined, default used');
},
TEST_CASE_TIMEOUT
);
it(
'should have one info log related to coldstart metric',
() => {
const logs = invocationLogs.getFunctionLogs('INFO');
expect(logs.length).toBe(1);
expect(logs[0]).toContain('ColdStart');
},
TEST_CASE_TIMEOUT
);
it(
'should have one debug log with tracer subsegment info',
() => {
const logs = invocationLogs.getFunctionLogs('DEBUG');
expect(logs.length).toBe(1);
const logEntry = TestInvocationLogs.parseFunctionLog(logs[0]);
expect(logEntry.message).toContain('subsegment');
expect(logEntry.subsegment).toBeDefined();
const subsegment = JSON.parse(logEntry.subsegment as string);
const traceIdFromLog = subsegment.trace_id;
expect(subsegment).toEqual(
expect.objectContaining({
id: expect.any(String),
name: '### index.handler',
start_time: expect.any(Number),
end_time: expect.any(Number),
type: 'subsegment',
annotations: {
ColdStart: true,
},
parent_id: expect.any(String),
trace_id: traceIdFromLog,
})
);
},
TEST_CASE_TIMEOUT
);
});
afterAll(async () => {
if (!process.env.DISABLE_TEARDOWN) {
await testLayerStack.destroy();
await testStack.destroy();
}
}, TEARDOWN_TIMEOUT);
});