Files
mtusi2026lab4/test/warm-upload-replicas.mjs
qrtx dc46ac213d feat(storage): add RustFS attachments and presigned uploads
Add two API replicas, safe object reconciliation, upload validation, regression tests and documented load experiments.
2026-11-12 10:16:39 +03:00

120 lines
3.7 KiB
JavaScript

// Run as a temporary docker-exec client. Secrets and signed URLs arrive only on stdin.
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import http from 'node:http';
let input = '';
for await (const chunk of process.stdin) input += chunk;
const { targets, owner, reader, chat, imageBase64, mode } = JSON.parse(input);
const image = Buffer.from(imageBase64, 'base64');
const digest = createHash('sha256').update(image).digest('hex');
assert.ok(['proxy', 'direct'].includes(mode));
assert.equal(targets.length, 2);
async function signedPut(capability) {
const signed = new URL(capability.url);
// Connect to the same Nginx PUT listener inside Docker, preserving the signed Host.
const status = await new Promise((resolve, reject) => {
const request = http.request(
{
hostname: 'nginx',
port: 9000,
method: 'PUT',
path: signed.pathname + signed.search,
headers: { ...capability.headers, Host: signed.host },
timeout: 60000,
},
(response) => {
response.resume();
response.once('end', () => resolve(response.statusCode));
response.once('error', reject);
},
);
request.once('timeout', () =>
request.destroy(new Error('Warm PUT timed out')),
);
request.once('error', reject);
request.end(image);
});
assert.equal(status, 200, 'Warm PUT failed');
}
const counts = {};
for (const target of targets) {
assert.match(target.address, /^\d{1,3}(\.\d{1,3}){3}$/);
const base = `http://${target.address}:8080`;
async function call(path, token, options = {}, expected = 200) {
const response = await fetch(base + path, {
...options,
headers: { Authorization: `Bearer ${token}`, ...options.headers },
signal: AbortSignal.timeout(60000),
});
assert.equal(response.status, expected, 'Warm API request failed');
assert.equal(
response.headers.get('x-instance-id'),
target.instance,
'Warm-up reached a different API',
);
return response;
}
for (let index = 0; index < 2; index++) {
let response;
if (mode === 'proxy') {
response = await call(
`/api/v1/chats/${chat.id}/attachments?filename=warmup.png`,
owner.token,
{
method: 'POST',
headers: { 'Content-Type': 'image/png' },
body: image,
},
201,
);
} else {
const capability = await (
await call(
`/api/v1/chats/${chat.id}/attachment-uploads`,
owner.token,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
filename: 'warmup.png',
content_type: 'image/png',
size_bytes: image.length,
sha256: digest,
}),
},
201,
)
).json();
await signedPut(capability);
response = await call(
capability.complete_url,
owner.token,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: '{}',
},
201,
);
}
const attachment = await response.json();
assert.equal(attachment.sha256, digest);
assert.equal(attachment.size_bytes, image.length);
const meta = await (
await call(`/api/v1/attachments/${attachment.id}`, reader.token)
).json();
assert.deepEqual(meta, attachment);
const downloaded = Buffer.from(
await (
await call(`/api/v1/attachments/${attachment.id}/content`, reader.token)
).arrayBuffer(),
);
assert.deepEqual(downloaded, image);
counts[target.instance] = (counts[target.instance] ?? 0) + 1;
}
}
console.log(JSON.stringify({ counts, verifiedDownloads: 4 }));