44 * @group e2e/layers/all
55 */
66import { App } from 'aws-cdk-lib' ;
7- import { LayerVersion , Tracing } from 'aws-cdk-lib/aws-lambda' ;
7+ import { LayerVersion } from 'aws-cdk-lib/aws-lambda' ;
88import { LayerPublisherStack } from '../../src/layer-publisher-stack' ;
99import {
10+ TestNodejsFunction ,
1011 TestStack ,
11- defaultRuntime ,
12+ TestInvocationLogs ,
13+ invokeFunctionOnce ,
14+ generateTestUniqueName ,
1215} from '@aws-lambda-powertools/testing-utils' ;
13- import {
14- generateUniqueName ,
15- invokeFunction ,
16- isValidRuntimeKey ,
17- createStackWithLambdaFunction ,
18- } from '../../../packages/commons/tests/utils/e2eUtils' ;
1916import {
2017 RESOURCE_NAME_PREFIX ,
2118 SETUP_TIMEOUT ,
2219 TEARDOWN_TIMEOUT ,
2320 TEST_CASE_TIMEOUT ,
2421} 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' ;
3123import packageJson from '../../package.json' ;
3224
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+ } ) ;
3439
35- if ( ! isValidRuntimeKey ( runtime ) ) {
36- throw new Error ( `Invalid runtime key: ${ runtime } ` ) ;
37- }
40+ let invocationLogs : TestInvocationLogs ;
3841
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'
6551 ) ;
66- const lambdaFunctionCodeFile = 'layerPublisher.class.test.functionCode.ts' ;
6752
68- const invocationCount = 1 ;
6953 const powerToolsPackageVersion = packageJson . version ;
70- const layerName = generateUniqueName (
71- RESOURCE_NAME_PREFIX ,
72- uuid ,
73- runtime ,
74- 'layer'
75- ) ;
7654
77- const testStack = new TestStack ( stackNameFunction ) ;
7855 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 ,
8162 powertoolsPackageVersion : powerToolsPackageVersion ,
8263 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 ,
8373 } ) ;
84- const testLayerStack = new TestStack ( stackNameLayers , layerApp , layerStack ) ;
8574
8675 beforeAll ( async ( ) => {
87- const outputs = await testLayerStack . deploy ( ) ;
76+ await testLayerStack . deploy ( ) ;
8877
8978 const layerVersion = LayerVersion . fromLayerVersionArn (
9079 testStack . stack ,
9180 'LayerVersionArnReference' ,
92- outputs [ 'LatestLayerArn' ]
81+ testLayerStack . findAndGetStackOutputValue ( 'LatestLayerArn' )
9382 ) ;
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 ] ,
112100 } ,
113- layers : [ layerVersion ] ,
114- } ) ;
101+ {
102+ nameSuffix : 'testFn' ,
103+ }
104+ ) ;
115105
116106 await testStack . deploy ( ) ;
117107
118- invocationLogs = await invokeFunction (
108+ const functionName = testStack . findAndGetStackOutputValue ( 'testFn' ) ;
109+
110+ invocationLogs = await invokeFunctionOnce ( {
119111 functionName,
120- invocationCount ,
121- 'SEQUENTIAL'
122- ) ;
112+ } ) ;
123113 } , SETUP_TIMEOUT ) ;
124114
125115 describe ( 'LayerPublisherStack usage' , ( ) => {
126116 it (
127117 'should have no errors in the logs, which indicates the pacakges version matches the expected one' ,
128118 ( ) => {
129- const logs = invocationLogs [ 0 ] . getFunctionLogs ( LEVEL . ERROR ) ;
119+ const logs = invocationLogs . getFunctionLogs ( ' ERROR' ) ;
130120
131121 expect ( logs . length ) . toBe ( 0 ) ;
132122 } ,
@@ -136,7 +126,7 @@ describe(`layers E2E tests (LayerPublisherStack) for runtime: ${runtime}`, () =>
136126 it (
137127 'should have one warning related to missing Metrics namespace' ,
138128 ( ) => {
139- const logs = invocationLogs [ 0 ] . getFunctionLogs ( LEVEL . WARN ) ;
129+ const logs = invocationLogs . getFunctionLogs ( ' WARN' ) ;
140130
141131 expect ( logs . length ) . toBe ( 1 ) ;
142132 expect ( logs [ 0 ] ) . toContain ( 'Namespace should be defined, default used' ) ;
@@ -147,7 +137,7 @@ describe(`layers E2E tests (LayerPublisherStack) for runtime: ${runtime}`, () =>
147137 it (
148138 'should have one info log related to coldstart metric' ,
149139 ( ) => {
150- const logs = invocationLogs [ 0 ] . getFunctionLogs ( LEVEL . INFO ) ;
140+ const logs = invocationLogs . getFunctionLogs ( ' INFO' ) ;
151141
152142 expect ( logs . length ) . toBe ( 1 ) ;
153143 expect ( logs [ 0 ] ) . toContain ( 'ColdStart' ) ;
@@ -158,7 +148,7 @@ describe(`layers E2E tests (LayerPublisherStack) for runtime: ${runtime}`, () =>
158148 it (
159149 'should have one debug log that says Hello World!' ,
160150 ( ) => {
161- const logs = invocationLogs [ 0 ] . getFunctionLogs ( LEVEL . DEBUG ) ;
151+ const logs = invocationLogs . getFunctionLogs ( ' DEBUG' ) ;
162152
163153 expect ( logs . length ) . toBe ( 1 ) ;
164154 expect ( logs [ 0 ] ) . toContain ( 'Hello World!' ) ;
0 commit comments