4
4
* @group e2e/layers/all
5
5
*/
6
6
import { App } from 'aws-cdk-lib' ;
7
- import { LayerVersion , Tracing } from 'aws-cdk-lib/aws-lambda' ;
7
+ import { LayerVersion } from 'aws-cdk-lib/aws-lambda' ;
8
8
import { LayerPublisherStack } from '../../src/layer-publisher-stack' ;
9
9
import {
10
+ TestNodejsFunction ,
10
11
TestStack ,
11
- defaultRuntime ,
12
+ TestInvocationLogs ,
13
+ invokeFunctionOnce ,
14
+ generateTestUniqueName ,
12
15
} from '@aws-lambda-powertools/testing-utils' ;
13
- import {
14
- generateUniqueName ,
15
- invokeFunction ,
16
- isValidRuntimeKey ,
17
- createStackWithLambdaFunction ,
18
- } from '../../../packages/commons/tests/utils/e2eUtils' ;
19
16
import {
20
17
RESOURCE_NAME_PREFIX ,
21
18
SETUP_TIMEOUT ,
22
19
TEARDOWN_TIMEOUT ,
23
20
TEST_CASE_TIMEOUT ,
24
21
} from './constants' ;
25
- import {
26
- LEVEL ,
27
- InvocationLogs ,
28
- } from '../../../packages/commons/tests/utils/InvocationLogs' ;
29
- import { v4 } from 'uuid' ;
30
- import path from 'path' ;
22
+ import { join } from 'node:path' ;
31
23
import packageJson from '../../package.json' ;
32
24
33
- const runtime : string = process . env . RUNTIME || defaultRuntime ;
25
+ /**
26
+ * This test has two stacks:
27
+ * 1. LayerPublisherStack - publishes a layer version using the LayerPublisher construct and containing the Powertools utilities from the repo
28
+ * 2. TestStack - uses the layer published in the first stack and contains a lambda function that uses the Powertools utilities from the layer
29
+ *
30
+ * 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.
31
+ */
32
+ describe ( `Layers E2E tests, publisher stack` , ( ) => {
33
+ const testStack = new TestStack ( {
34
+ stackNameProps : {
35
+ stackNamePrefix : RESOURCE_NAME_PREFIX ,
36
+ testName : 'functionStack' ,
37
+ } ,
38
+ } ) ;
34
39
35
- if ( ! isValidRuntimeKey ( runtime ) ) {
36
- throw new Error ( `Invalid runtime key: ${ runtime } ` ) ;
37
- }
40
+ let invocationLogs : TestInvocationLogs ;
38
41
39
- describe ( `layers E2E tests (LayerPublisherStack) for runtime: ${ runtime } ` , ( ) => {
40
- const uuid = v4 ( ) ;
41
- let invocationLogs : InvocationLogs [ ] ;
42
- const stackNameLayers = generateUniqueName (
43
- RESOURCE_NAME_PREFIX ,
44
- uuid ,
45
- runtime ,
46
- 'layerStack'
47
- ) ;
48
- const stackNameFunction = generateUniqueName (
49
- RESOURCE_NAME_PREFIX ,
50
- uuid ,
51
- runtime ,
52
- 'functionStack'
53
- ) ;
54
- const functionName = generateUniqueName (
55
- RESOURCE_NAME_PREFIX ,
56
- uuid ,
57
- runtime ,
58
- 'function'
59
- ) ;
60
- const ssmParameterLayerName = generateUniqueName (
61
- RESOURCE_NAME_PREFIX ,
62
- uuid ,
63
- runtime ,
64
- 'parameter'
42
+ const ssmParameterLayerName = generateTestUniqueName ( {
43
+ testPrefix : `${ RESOURCE_NAME_PREFIX } ` ,
44
+ testName : 'parameter' ,
45
+ } ) ;
46
+
47
+ // Location of the lambda function code
48
+ const lambdaFunctionCodeFilePath = join (
49
+ __dirname ,
50
+ 'layerPublisher.class.test.functionCode.ts'
65
51
) ;
66
- const lambdaFunctionCodeFile = 'layerPublisher.class.test.functionCode.ts' ;
67
52
68
- const invocationCount = 1 ;
69
53
const powerToolsPackageVersion = packageJson . version ;
70
- const layerName = generateUniqueName (
71
- RESOURCE_NAME_PREFIX ,
72
- uuid ,
73
- runtime ,
74
- 'layer'
75
- ) ;
76
54
77
- const testStack = new TestStack ( stackNameFunction ) ;
78
55
const layerApp = new App ( ) ;
79
- const layerStack = new LayerPublisherStack ( layerApp , stackNameLayers , {
80
- layerName,
56
+ const layerId = generateTestUniqueName ( {
57
+ testPrefix : RESOURCE_NAME_PREFIX ,
58
+ testName : 'layerStack' ,
59
+ } ) ;
60
+ const layerStack = new LayerPublisherStack ( layerApp , layerId , {
61
+ layerName : layerId ,
81
62
powertoolsPackageVersion : powerToolsPackageVersion ,
82
63
ssmParameterLayerArn : ssmParameterLayerName ,
64
+ removeLayerVersion : true ,
65
+ } ) ;
66
+ const testLayerStack = new TestStack ( {
67
+ stackNameProps : {
68
+ stackNamePrefix : RESOURCE_NAME_PREFIX ,
69
+ testName : 'layerStack' ,
70
+ } ,
71
+ app : layerApp ,
72
+ stack : layerStack ,
83
73
} ) ;
84
- const testLayerStack = new TestStack ( stackNameLayers , layerApp , layerStack ) ;
85
74
86
75
beforeAll ( async ( ) => {
87
- const outputs = await testLayerStack . deploy ( ) ;
76
+ await testLayerStack . deploy ( ) ;
88
77
89
78
const layerVersion = LayerVersion . fromLayerVersionArn (
90
79
testStack . stack ,
91
80
'LayerVersionArnReference' ,
92
- outputs [ 'LatestLayerArn' ]
81
+ testLayerStack . findAndGetStackOutputValue ( 'LatestLayerArn' )
93
82
) ;
94
- createStackWithLambdaFunction ( {
95
- stack : testStack . stack ,
96
- functionName : functionName ,
97
- functionEntry : path . join ( __dirname , lambdaFunctionCodeFile ) ,
98
- tracing : Tracing . ACTIVE ,
99
- environment : {
100
- UUID : uuid ,
101
- POWERTOOLS_PACKAGE_VERSION : powerToolsPackageVersion ,
102
- POWERTOOLS_SERVICE_NAME : 'LayerPublisherStack' ,
103
- } ,
104
- runtime : runtime ,
105
- bundling : {
106
- externalModules : [
107
- '@aws-lambda-powertools/commons' ,
108
- '@aws-lambda-powertools/logger' ,
109
- '@aws-lambda-powertools/metrics' ,
110
- '@aws-lambda-powertools/tracer' ,
111
- ] ,
83
+ new TestNodejsFunction (
84
+ testStack ,
85
+ {
86
+ entry : lambdaFunctionCodeFilePath ,
87
+ environment : {
88
+ POWERTOOLS_PACKAGE_VERSION : powerToolsPackageVersion ,
89
+ POWERTOOLS_SERVICE_NAME : 'LayerPublisherStack' ,
90
+ } ,
91
+ bundling : {
92
+ externalModules : [
93
+ '@aws-lambda-powertools/commons' ,
94
+ '@aws-lambda-powertools/logger' ,
95
+ '@aws-lambda-powertools/metrics' ,
96
+ '@aws-lambda-powertools/tracer' ,
97
+ ] ,
98
+ } ,
99
+ layers : [ layerVersion ] ,
112
100
} ,
113
- layers : [ layerVersion ] ,
114
- } ) ;
101
+ {
102
+ nameSuffix : 'testFn' ,
103
+ }
104
+ ) ;
115
105
116
106
await testStack . deploy ( ) ;
117
107
118
- invocationLogs = await invokeFunction (
108
+ const functionName = testStack . findAndGetStackOutputValue ( 'testFn' ) ;
109
+
110
+ invocationLogs = await invokeFunctionOnce ( {
119
111
functionName,
120
- invocationCount ,
121
- 'SEQUENTIAL'
122
- ) ;
112
+ } ) ;
123
113
} , SETUP_TIMEOUT ) ;
124
114
125
115
describe ( 'LayerPublisherStack usage' , ( ) => {
126
116
it (
127
117
'should have no errors in the logs, which indicates the pacakges version matches the expected one' ,
128
118
( ) => {
129
- const logs = invocationLogs [ 0 ] . getFunctionLogs ( LEVEL . ERROR ) ;
119
+ const logs = invocationLogs . getFunctionLogs ( ' ERROR' ) ;
130
120
131
121
expect ( logs . length ) . toBe ( 0 ) ;
132
122
} ,
@@ -136,7 +126,7 @@ describe(`layers E2E tests (LayerPublisherStack) for runtime: ${runtime}`, () =>
136
126
it (
137
127
'should have one warning related to missing Metrics namespace' ,
138
128
( ) => {
139
- const logs = invocationLogs [ 0 ] . getFunctionLogs ( LEVEL . WARN ) ;
129
+ const logs = invocationLogs . getFunctionLogs ( ' WARN' ) ;
140
130
141
131
expect ( logs . length ) . toBe ( 1 ) ;
142
132
expect ( logs [ 0 ] ) . toContain ( 'Namespace should be defined, default used' ) ;
@@ -147,7 +137,7 @@ describe(`layers E2E tests (LayerPublisherStack) for runtime: ${runtime}`, () =>
147
137
it (
148
138
'should have one info log related to coldstart metric' ,
149
139
( ) => {
150
- const logs = invocationLogs [ 0 ] . getFunctionLogs ( LEVEL . INFO ) ;
140
+ const logs = invocationLogs . getFunctionLogs ( ' INFO' ) ;
151
141
152
142
expect ( logs . length ) . toBe ( 1 ) ;
153
143
expect ( logs [ 0 ] ) . toContain ( 'ColdStart' ) ;
@@ -158,7 +148,7 @@ describe(`layers E2E tests (LayerPublisherStack) for runtime: ${runtime}`, () =>
158
148
it (
159
149
'should have one debug log that says Hello World!' ,
160
150
( ) => {
161
- const logs = invocationLogs [ 0 ] . getFunctionLogs ( LEVEL . DEBUG ) ;
151
+ const logs = invocationLogs . getFunctionLogs ( ' DEBUG' ) ;
162
152
163
153
expect ( logs . length ) . toBe ( 1 ) ;
164
154
expect ( logs [ 0 ] ) . toContain ( 'Hello World!' ) ;
0 commit comments