-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test(e2e): Add e2e test for AWS lambda in ESM mode #12833
Changes from 3 commits
abbfb21
d14934e
9b282ed
9d0c186
6f93dc3
8d1fa8f
a69a664
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@sentry:registry=http://127.0.0.1:4873 | ||
@sentry-internal:registry=http://127.0.0.1:4873 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "node-express-app", | ||
"version": "1.0.0", | ||
"private": true, | ||
"scripts": { | ||
"start": "node src/run.mjs", | ||
"test": "playwright test", | ||
"clean": "npx rimraf node_modules pnpm-lock.yaml", | ||
"test:build": "pnpm install", | ||
"test:assert": "pnpm test" | ||
}, | ||
"dependencies": { | ||
"@sentry/aws-serverless": "* || latest" | ||
}, | ||
"devDependencies": { | ||
"@sentry-internal/test-utils": "link:../../../test-utils", | ||
"@playwright/test": "^1.41.1", | ||
"wait-port": "1.0.4", | ||
"rimraf": "^5.0.8" | ||
}, | ||
"volta": { | ||
"extends": "../../package.json" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import type { PlaywrightTestConfig } from '@playwright/test'; | ||
import { devices } from '@playwright/test'; | ||
|
||
// Fix urls not resolving to localhost on Node v17+ | ||
// See: https://github.com/axios/axios/issues/3821#issuecomment-1413727575 | ||
import { setDefaultResultOrder } from 'dns'; | ||
setDefaultResultOrder('ipv4first'); | ||
|
||
const eventProxyPort = 3031; | ||
const lambdaPort = 3030; | ||
|
||
/** | ||
* See https://playwright.dev/docs/test-configuration. | ||
*/ | ||
const config: PlaywrightTestConfig = { | ||
testDir: './tests', | ||
/* Maximum time one test can run for. */ | ||
timeout: 150_000, | ||
expect: { | ||
/** | ||
* Maximum time expect() should wait for the condition to be met. | ||
* For example in `await expect(locator).toHaveText();` | ||
*/ | ||
timeout: 5000, | ||
}, | ||
/* Run tests in files in parallel */ | ||
fullyParallel: true, | ||
/* Fail the build on CI if you accidentally left test.only in the source code. */ | ||
forbidOnly: !!process.env.CI, | ||
/* Retry on CI only */ | ||
retries: 0, | ||
/* Reporter to use. See https://playwright.dev/docs/test-reporters */ | ||
reporter: 'list', | ||
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ | ||
use: { | ||
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */ | ||
actionTimeout: 0, | ||
|
||
/* Base URL to use in actions like `await page.goto('/')`. */ | ||
baseURL: `http://localhost:${lambdaPort}`, | ||
|
||
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ | ||
trace: 'on-first-retry', | ||
}, | ||
|
||
/* Configure projects for major browsers */ | ||
projects: [ | ||
{ | ||
name: 'chromium', | ||
use: { | ||
...devices['Desktop Chrome'], | ||
}, | ||
}, | ||
// For now we only test Chrome! | ||
// { | ||
// name: 'firefox', | ||
// use: { | ||
// ...devices['Desktop Firefox'], | ||
// }, | ||
// }, | ||
// { | ||
// name: 'webkit', | ||
// use: { | ||
// ...devices['Desktop Safari'], | ||
// }, | ||
// }, | ||
], | ||
|
||
/* Run your local dev server before starting the tests */ | ||
webServer: [ | ||
{ | ||
command: `node start-event-proxy.mjs && pnpm wait-port ${eventProxyPort}`, | ||
port: eventProxyPort, | ||
stdout: 'pipe', | ||
}, | ||
], | ||
}; | ||
|
||
export default config; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import * as Sentry from '@sentry/aws-serverless'; | ||
|
||
Sentry.init({ | ||
dsn: 'http://public@localhost:3031/1337', | ||
tracesSampleRate: 1.0, | ||
debug: true, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import * as http from 'node:http'; | ||
import * as Sentry from '@sentry/aws-serverless'; | ||
|
||
const handler = Sentry.wrapHandler(async () => { | ||
await new Promise(resolve => { | ||
const req = http.request( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. took me ages to find out why the previously used Anyway, this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice find |
||
{ | ||
host: 'example.com', | ||
}, | ||
res => { | ||
res.on('data', d => { | ||
process.stdout.write(d); | ||
}); | ||
|
||
res.on('end', () => { | ||
resolve(); | ||
}); | ||
}, | ||
); | ||
req.end(); | ||
}); | ||
|
||
Sentry.startSpan({ name: 'manual-span', op: 'manual' }, () => {}); | ||
}); | ||
|
||
export { handler }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"//": "This is a mock package.json file which is usually created by AWS when deploying the lambda. OTEL instrumentation tries to read this file to get the lambda version", | ||
"name": "lambda", | ||
"version": "1.0.0" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { handler } from './lambda-function.mjs'; | ||
|
||
// Simulate minimal event and context objects being passed to the handler by the AWS runtime | ||
const event = {}; | ||
const context = { | ||
invokedFunctionArn: 'arn:aws:lambda:us-east-1:123453789012:function:my-lambda', | ||
functionName: 'my-lambda', | ||
}; | ||
|
||
await handler(event, context); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import child_process from 'child_process'; | ||
|
||
child_process.execSync('node ./src/run-lambda.mjs', { | ||
stdio: 'inherit', | ||
env: { | ||
...process.env, | ||
// On AWS, LAMBDA_TASK_ROOT is usually /var/task but for testing, we set it to the CWD to correctly apply our handler | ||
LAMBDA_TASK_ROOT: process.cwd(), | ||
_HANDLER: 'src/lambda-function.handler', | ||
|
||
NODE_OPTIONS: '--import ./src/instrument.mjs', | ||
}, | ||
cwd: process.cwd(), | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { startEventProxyServer } from '@sentry-internal/test-utils'; | ||
|
||
startEventProxyServer({ | ||
port: 3031, | ||
proxyServerName: 'aws-serverless-esm', | ||
forwardToSentry: false, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import * as child_process from 'child_process'; | ||
import { expect, test } from '@playwright/test'; | ||
import { waitForTransaction } from '@sentry-internal/test-utils'; | ||
|
||
test('AWS Serverless SDK sends events in ESM mode', async ({ request }) => { | ||
const transactionEventPromise = waitForTransaction('aws-serverless-esm', transactionEvent => { | ||
return transactionEvent?.transaction === 'my-lambda'; | ||
}); | ||
|
||
// Waiting for 1s here because attaching the listener for events in `waitForTransaction` is not synchronous | ||
// Since in this test, we don't start a browser via playwright, we don't have the usual delays (page.goto, etc) | ||
// which are usually enough for us to never have noticed this race condition before. | ||
// This is a workaround but probably sufficient as long as we only experience it in this test. | ||
await new Promise<void>(resolve => | ||
setTimeout(() => { | ||
resolve(); | ||
}, 1000), | ||
); | ||
|
||
child_process.execSync('pnpm start', { | ||
stdio: 'ignore', | ||
}); | ||
|
||
const transactionEvent = await transactionEventPromise; | ||
|
||
// shows the SDK sent a transaction | ||
expect(transactionEvent.transaction).toEqual('my-lambda'); // name should be the function name | ||
expect(transactionEvent.contexts?.trace).toEqual({ | ||
data: { | ||
'sentry.sample_rate': 1, | ||
'sentry.source': 'component', | ||
'sentry.origin': 'auto.function.serverless', | ||
'sentry.op': 'function.aws.lambda', | ||
'otel.kind': 'INTERNAL', | ||
}, | ||
op: 'function.aws.lambda', | ||
origin: 'auto.function.serverless', | ||
span_id: expect.any(String), | ||
status: 'ok', | ||
trace_id: expect.any(String), | ||
}); | ||
|
||
expect(transactionEvent.spans).toHaveLength(2); | ||
|
||
// shows that the Otel Http instrumentation is working | ||
expect(transactionEvent.spans).toContainEqual( | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
'sentry.op': 'http.client', | ||
'sentry.origin': 'auto.http.otel.http', | ||
url: 'http://example.com/', | ||
}), | ||
description: 'GET http://example.com/', | ||
op: 'http.client', | ||
}), | ||
); | ||
|
||
// shows that the manual span creation is working | ||
expect(transactionEvent.spans).toContainEqual( | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
'sentry.op': 'manual', | ||
'sentry.origin': 'manual', | ||
}), | ||
description: 'manual-span', | ||
op: 'manual', | ||
}), | ||
); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure why but without adding rimraf explicitly here, I can't use the
yarn test:run
command to run e2e tests locally as rimraf isn't found otherwise 🤔