mirror of
https://github.com/nexu-io/openmotion.git
synced 2026-12-02 00:20:06 +08:00
68 lines
2.9 KiB
JavaScript
68 lines
2.9 KiB
JavaScript
// Run through stdin inside the built Lambda image with a disposable Redis server.
|
|
import assert from 'node:assert/strict';
|
|
import { spawn } from 'node:child_process';
|
|
import { createServer } from 'node:http';
|
|
import { Redis } from 'ioredis';
|
|
|
|
const requestId = '00000000-0000-4000-8000-000000000009';
|
|
const redisUrl = process.env.LOCAL_TEST_REDIS;
|
|
assert.ok(redisUrl, 'LOCAL_TEST_REDIS is required');
|
|
const response = Promise.withResolvers();
|
|
let issued = false;
|
|
const server = createServer(async (req, res) => {
|
|
if (req.url.endsWith('/next')) {
|
|
if (issued) return;
|
|
issued = true;
|
|
res.writeHead(200, {
|
|
'content-type': 'application/json',
|
|
'lambda-runtime-aws-request-id': requestId,
|
|
'lambda-runtime-deadline-ms': String(Date.now() + 30_000),
|
|
'lambda-runtime-invoked-function-arn': 'arn:aws:lambda:us-east-1:123456789012:function:test',
|
|
});
|
|
res.end(JSON.stringify({ Records: [{ eventSource: 'aws:sqs', body: 'openmotion.render.wake.v1' }] }));
|
|
return;
|
|
}
|
|
if (req.headers['x-amz-target']?.endsWith('.GetSecretValue')) {
|
|
res.writeHead(200, { 'content-type': 'application/x-amz-json-1.1' });
|
|
res.end(JSON.stringify({ SecretString: JSON.stringify({
|
|
DATABASE_URL: 'postgres://fixture@127.0.0.1/fixture', REDIS_URL: redisUrl,
|
|
}) }));
|
|
return;
|
|
}
|
|
let body = '';
|
|
for await (const chunk of req) body += chunk;
|
|
res.writeHead(202);
|
|
res.end();
|
|
if (req.url.endsWith('/response')) response.resolve();
|
|
else response.reject(new Error(`${req.url} ${body}`));
|
|
});
|
|
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
|
const address = `127.0.0.1:${server.address().port}`;
|
|
const runtime = spawn(process.execPath, ['/var/runtime/index.mjs', 'handler.handler'], {
|
|
stdio: 'inherit',
|
|
env: {
|
|
...process.env, AWS_LAMBDA_RUNTIME_API: address, AWS_REGION: 'us-east-1',
|
|
AWS_LAMBDA_FUNCTION_NAME: 'local-smoke', AWS_LAMBDA_FUNCTION_VERSION: '1',
|
|
AWS_LAMBDA_FUNCTION_MEMORY_SIZE: '4096', AWS_LAMBDA_LOG_GROUP_NAME: 'local', AWS_LAMBDA_LOG_STREAM_NAME: 'local',
|
|
AWS_ACCESS_KEY_ID: 'fixture', AWS_SECRET_ACCESS_KEY: 'fixture', RENDER_RUNTIME_SECRET_ARN: 'local-fixture',
|
|
AWS_ENDPOINT_URL_SECRETS_MANAGER: `http://${address}`, S3_BUCKET: 'fixture',
|
|
APP_PUBLIC_ORIGIN: 'https://example.test', OPENMOTION_TELEMETRY: '0',
|
|
},
|
|
});
|
|
runtime.on('exit', code => response.reject(new Error(`Runtime exited ${code}`)));
|
|
const timer = setTimeout(() => response.reject(new Error('Runtime did not finish the invocation')), 25_000);
|
|
const redis = new Redis(redisUrl);
|
|
try {
|
|
await response.promise;
|
|
const health = await redis.hget('om:workers:render:health', `lambda-${requestId}`);
|
|
assert.ok(health, 'Studio must see the invocation heartbeat');
|
|
assert.equal(JSON.parse(health).activeJobs, 0);
|
|
console.log('Verified SQS invocation, Redis worker heartbeat and clean return to idle.');
|
|
} finally {
|
|
clearTimeout(timer);
|
|
runtime.kill('SIGKILL');
|
|
server.closeAllConnections();
|
|
server.close();
|
|
await redis.quit();
|
|
}
|