-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathlambda-fixtures.ts
More file actions
87 lines (72 loc) · 2.71 KB
/
lambda-fixtures.ts
File metadata and controls
87 lines (72 loc) · 2.71 KB
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
import { test as base, expect } from '@playwright/test';
import { App } from 'aws-cdk-lib';
import * as tmp from 'tmp';
import { LocalLambdaStack, SAM_PORT, getHostIp } from '../src/stack';
import { writeFileSync } from 'node:fs';
import { spawn, execSync } from 'node:child_process';
import { LambdaClient } from '@aws-sdk/client-lambda';
const DOCKER_NETWORK_NAME = 'lambda-test-network';
const SAM_TEMPLATE_FILE = 'sam.template.yml';
export { expect };
export const test = base.extend<{ testEnvironment: LocalLambdaStack; lambdaClient: LambdaClient }>({
testEnvironment: [
async ({}, use) => {
console.log('[testEnvironment fixture] Setting up AWS Lambda test infrastructure');
execSync('docker network prune -f');
execSync(`docker network create --driver bridge ${DOCKER_NETWORK_NAME}`);
const hostIp = await getHostIp();
const app = new App();
const stack = new LocalLambdaStack(app, 'LocalLambdaStack', {}, hostIp);
const template = app.synth().getStackByName('LocalLambdaStack').template;
writeFileSync(SAM_TEMPLATE_FILE, JSON.stringify(template, null, 2));
const debugLog = tmp.fileSync({ prefix: 'sentry_aws_lambda_tests_sam_debug', postfix: '.log' });
console.log(`[test_environment fixture] Writing SAM debug log to: ${debugLog.name}`);
const args = [
'local',
'start-lambda',
'--debug',
'--template',
SAM_TEMPLATE_FILE,
'--warm-containers',
'EAGER',
'--docker-network',
DOCKER_NETWORK_NAME,
];
if (process.env.NODE_VERSION) {
args.push('--invoke-image', `public.ecr.aws/sam/build-nodejs${process.env.NODE_VERSION}.x:latest`);
}
console.log(`[testEnvironment fixture] Running SAM with args: ${args.join(' ')}`);
const samProcess = spawn('sam', args, {
stdio: ['ignore', debugLog.fd, debugLog.fd],
});
try {
await LocalLambdaStack.waitForStack();
await use(stack);
} finally {
console.log('[testEnvironment fixture] Tearing down AWS Lambda test infrastructure');
samProcess.kill('SIGTERM');
await new Promise(resolve => {
samProcess.once('exit', resolve);
setTimeout(() => {
if (!samProcess.killed) {
samProcess.kill('SIGKILL');
}
resolve(void 0);
}, 5000);
});
}
},
{ scope: 'worker', auto: true },
],
lambdaClient: async ({}, use) => {
const lambdaClient = new LambdaClient({
endpoint: `http://127.0.0.1:${SAM_PORT}`,
region: 'us-east-1',
credentials: {
accessKeyId: 'dummy',
secretAccessKey: 'dummy',
},
});
await use(lambdaClient);
},
});