-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathlayerPublisher.test.ts
176 lines (158 loc) · 4.54 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
/**
* Test LayerPublisherStack class
*
* @group e2e/layers/all
*/
import { App } from 'aws-cdk-lib';
import { LayerVersion, Tracing } from 'aws-cdk-lib/aws-lambda';
import { LayerPublisherStack } from '../../src/layer-publisher-stack';
import {
TestStack,
defaultRuntime,
} from '@aws-lambda-powertools/testing-utils';
import {
generateUniqueName,
invokeFunction,
isValidRuntimeKey,
createStackWithLambdaFunction,
} from '../../../packages/commons/tests/utils/e2eUtils';
import {
RESOURCE_NAME_PREFIX,
SETUP_TIMEOUT,
TEARDOWN_TIMEOUT,
TEST_CASE_TIMEOUT,
} from './constants';
import {
LEVEL,
InvocationLogs,
} from '../../../packages/commons/tests/utils/InvocationLogs';
import { v4 } from 'uuid';
import path from 'path';
import packageJson from '../../package.json';
const runtime: string = process.env.RUNTIME || defaultRuntime;
if (!isValidRuntimeKey(runtime)) {
throw new Error(`Invalid runtime key: ${runtime}`);
}
describe(`layers E2E tests (LayerPublisherStack) for runtime: ${runtime}`, () => {
const uuid = v4();
let invocationLogs: InvocationLogs[];
const stackNameLayers = generateUniqueName(
RESOURCE_NAME_PREFIX,
uuid,
runtime,
'layerStack'
);
const stackNameFunction = generateUniqueName(
RESOURCE_NAME_PREFIX,
uuid,
runtime,
'functionStack'
);
const functionName = generateUniqueName(
RESOURCE_NAME_PREFIX,
uuid,
runtime,
'function'
);
const ssmParameterLayerName = generateUniqueName(
RESOURCE_NAME_PREFIX,
uuid,
runtime,
'parameter'
);
const lambdaFunctionCodeFile = 'layerPublisher.class.test.functionCode.ts';
const invocationCount = 1;
const powerToolsPackageVersion = packageJson.version;
const layerName = generateUniqueName(
RESOURCE_NAME_PREFIX,
uuid,
runtime,
'layer'
);
const testStack = new TestStack(stackNameFunction);
const layerApp = new App();
const layerStack = new LayerPublisherStack(layerApp, stackNameLayers, {
layerName,
powertoolsPackageVersion: powerToolsPackageVersion,
ssmParameterLayerArn: ssmParameterLayerName,
});
const testLayerStack = new TestStack(stackNameLayers, layerApp, layerStack);
beforeAll(async () => {
const outputs = await testLayerStack.deploy();
const layerVersion = LayerVersion.fromLayerVersionArn(
testStack.stack,
'LayerVersionArnReference',
outputs['LatestLayerArn']
);
createStackWithLambdaFunction({
stack: testStack.stack,
functionName: functionName,
functionEntry: path.join(__dirname, lambdaFunctionCodeFile),
tracing: Tracing.ACTIVE,
environment: {
UUID: uuid,
POWERTOOLS_PACKAGE_VERSION: powerToolsPackageVersion,
POWERTOOLS_SERVICE_NAME: 'LayerPublisherStack',
},
runtime: runtime,
bundling: {
externalModules: [
'@aws-lambda-powertools/commons',
'@aws-lambda-powertools/logger',
'@aws-lambda-powertools/metrics',
'@aws-lambda-powertools/tracer',
],
},
layers: [layerVersion],
});
await testStack.deploy();
invocationLogs = await invokeFunction(
functionName,
invocationCount,
'SEQUENTIAL'
);
}, SETUP_TIMEOUT);
describe('LayerPublisherStack usage', () => {
it(
'should have no errors in the logs, which indicates the pacakges version matches the expected one',
() => {
const logs = invocationLogs[0].getFunctionLogs(LEVEL.ERROR);
expect(logs.length).toBe(0);
},
TEST_CASE_TIMEOUT
);
it(
'should have one warning related to missing Metrics namespace',
() => {
const logs = invocationLogs[0].getFunctionLogs(LEVEL.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[0].getFunctionLogs(LEVEL.INFO);
expect(logs.length).toBe(1);
expect(logs[0]).toContain('ColdStart');
},
TEST_CASE_TIMEOUT
);
it(
'should have one debug log that says Hello World!',
() => {
const logs = invocationLogs[0].getFunctionLogs(LEVEL.DEBUG);
expect(logs.length).toBe(1);
expect(logs[0]).toContain('Hello World!');
},
TEST_CASE_TIMEOUT
);
});
afterAll(async () => {
if (!process.env.DISABLE_TEARDOWN) {
await testLayerStack.destroy();
await testStack.destroy();
}
}, TEARDOWN_TIMEOUT);
});