Skip to content

Commit 2d80baa

Browse files
committed
Add E2E test coverage checking return values pre- and post-expiration
Includes update to e2e lambda config type that allows test writers to specify a test function duration. Needed because to wait for parameter expiration, we needed a longer-than-default timeout.
1 parent 0b90940 commit 2d80baa

File tree

3 files changed

+73
-6
lines changed

3 files changed

+73
-6
lines changed

Diff for: packages/commons/tests/utils/e2eUtils.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* E2E utils is used by e2e tests. They are helper function that calls either CDK or SDK
66
* to interact with services.
77
*/
8-
import { App, CfnOutput, Stack } from 'aws-cdk-lib';
8+
import { App, CfnOutput, Stack, Duration } from 'aws-cdk-lib';
99
import {
1010
NodejsFunction,
1111
NodejsFunctionProps
@@ -37,6 +37,7 @@ export type StackWithLambdaFunctionOptions = {
3737
runtime: string
3838
bundling?: NodejsFunctionProps['bundling']
3939
layers?: NodejsFunctionProps['layers']
40+
timeout?: Duration
4041
};
4142

4243
type FunctionPayload = {[key: string]: string | boolean | number};
@@ -55,6 +56,7 @@ export const createStackWithLambdaFunction = (params: StackWithLambdaFunctionOpt
5556
bundling: params.bundling,
5657
layers: params.layers,
5758
logRetention: RetentionDays.ONE_DAY,
59+
timeout: params.timeout
5860
});
5961

6062
if (params.logGroupOutputKey) {

Diff for: packages/parameters/tests/e2e/appConfigProvider.class.test.functionCode.ts

+40-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,16 @@ export const handler = async (_event: unknown, _context: Context): Promise<void>
8181
try {
8282
providerWithMiddleware.clearCache();
8383
middleware.counter = 0;
84-
await providerWithMiddleware.get(freeFormBase64encodedPlainText);
85-
await providerWithMiddleware.get(freeFormBase64encodedPlainText);
84+
const result1 = await providerWithMiddleware.get(freeFormBase64encodedPlainText);
85+
const result2 = await providerWithMiddleware.get(freeFormBase64encodedPlainText);
86+
logger.log({
87+
test: 'get-cached-result1',
88+
value: result1
89+
});
90+
logger.log({
91+
test: 'get-cached-result2',
92+
value: result2
93+
});
8694
logger.log({
8795
test: 'get-cached',
8896
value: middleware.counter // should be 1
@@ -111,4 +119,34 @@ export const handler = async (_event: unknown, _context: Context): Promise<void>
111119
error: err.message
112120
});
113121
}
122+
123+
// Test 8
124+
// get parameter twice, but wait long enough that cache expires count SDK calls and return values
125+
try {
126+
providerWithMiddleware.clearCache();
127+
middleware.counter = 0;
128+
const expiredResult1 = await providerWithMiddleware.get(freeFormBase64encodedPlainText, { maxAge: 5 });
129+
130+
// Wait
131+
await new Promise(resolve => setTimeout(resolve, 6000));
132+
133+
const expiredResult2 = await providerWithMiddleware.get(freeFormBase64encodedPlainText, { maxAge: 5 });
134+
logger.log({
135+
test: 'get-expired-result1',
136+
value: expiredResult1
137+
});
138+
logger.log({
139+
test: 'get-expired-result2',
140+
value: expiredResult2
141+
});
142+
logger.log({
143+
test: 'get-expired',
144+
value: middleware.counter // should be 2
145+
});
146+
} catch (err) {
147+
logger.log({
148+
test: 'get-expired',
149+
error: err.message
150+
});
151+
}
114152
};

Diff for: packages/parameters/tests/e2e/appConfigProvider.class.test.ts

+30-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @group e2e/parameters/appconfig/class
55
*/
66
import path from 'path';
7-
import { App, Stack, Aspects } from 'aws-cdk-lib';
7+
import { App, Stack, Aspects, Duration } from 'aws-cdk-lib';
88
import { toBase64 } from '@aws-sdk/util-base64-node';
99
import { v4 } from 'uuid';
1010
import {
@@ -111,6 +111,7 @@ let stack: Stack;
111111
* Test 6
112112
* get parameter twice, but force fetch 2nd time, we count number of SDK requests and
113113
* check that we made two API calls
114+
* check that we got matching results
114115
*
115116
* Note: To avoid race conditions, we add a dependency between each pair of configuration profiles.
116117
* This allows us to influence the order of creation and ensure that each configuration profile
@@ -141,6 +142,7 @@ describe(`parameters E2E tests (appConfigProvider) for runtime ${runtime}`, () =
141142
FEATURE_FLAG_NAME: featureFlagName,
142143
},
143144
runtime,
145+
timeout: Duration.seconds(10),
144146
});
145147

146148
// Create the base resources for an AppConfig application.
@@ -291,8 +293,12 @@ describe(`parameters E2E tests (appConfigProvider) for runtime ${runtime}`, () =
291293
it('should retrieve single parameter cached', () => {
292294

293295
const logs = invocationLogs[0].getFunctionLogs();
294-
const testLog = InvocationLogs.parseFunctionLog(logs[4]);
295296

297+
const resultLog1 = InvocationLogs.parseFunctionLog(logs[4]);
298+
const resultLog2 = InvocationLogs.parseFunctionLog(logs[5]);
299+
expect(resultLog1.value).toStrictEqual(resultLog2.value);
300+
301+
const testLog = InvocationLogs.parseFunctionLog(logs[6]);
296302
expect(testLog).toStrictEqual({
297303
test: 'get-cached',
298304
value: 1
@@ -305,7 +311,7 @@ describe(`parameters E2E tests (appConfigProvider) for runtime ${runtime}`, () =
305311
it('should retrieve single parameter twice without caching', async () => {
306312

307313
const logs = invocationLogs[0].getFunctionLogs();
308-
const testLog = InvocationLogs.parseFunctionLog(logs[5]);
314+
const testLog = InvocationLogs.parseFunctionLog(logs[7]);
309315

310316
expect(testLog).toStrictEqual({
311317
test: 'get-forced',
@@ -314,6 +320,27 @@ describe(`parameters E2E tests (appConfigProvider) for runtime ${runtime}`, () =
314320

315321
}, TEST_CASE_TIMEOUT);
316322

323+
// Test 7 - get parameter twice, wait for expiration betweenn
324+
// we count number of SDK requests and check that we made two API calls
325+
// and check that the values match
326+
it('should retrieve single parameter twice, with expiration between and matching values', async () => {
327+
328+
const logs = invocationLogs[0].getFunctionLogs();
329+
330+
const resultLog1 = InvocationLogs.parseFunctionLog(logs[8]);
331+
const resultLog2 = InvocationLogs.parseFunctionLog(logs[9]);
332+
expect(resultLog1.test).toBe('get-expired-result1');
333+
expect(resultLog2.test).toBe('get-expired-result2');
334+
expect(resultLog1.value).toStrictEqual(resultLog2.value);
335+
336+
const testLog = InvocationLogs.parseFunctionLog(logs[10]);
337+
expect(testLog).toStrictEqual({
338+
test: 'get-expired',
339+
value: 2
340+
});
341+
342+
}, TEST_CASE_TIMEOUT);
343+
317344
});
318345

319346
afterAll(async () => {

0 commit comments

Comments
 (0)