-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest10MinioArtifactStorage.js
More file actions
239 lines (212 loc) · 7.69 KB
/
Copy pathtest10MinioArtifactStorage.js
File metadata and controls
239 lines (212 loc) · 7.69 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import { DeleteBucketCommand, DeleteObjectCommand, GetObjectCommand, S3Client } from '@aws-sdk/client-s3';
import { execFileSync, spawn } from 'child_process';
import crypto from 'crypto';
import net from 'net';
import path from 'path';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';
import fetch from 'node-fetch';
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
dotenv.config({ path: path.join(repoRoot, '.env') });
const args = parseArgs(process.argv.slice(2));
const children = [];
let shuttingDown = false;
const endpoint = args.endpoint || process.env.ARTIFACT_S3_ENDPOINT || 'http://127.0.0.1:9000';
const accessKeyId = args.accessKey || process.env.AWS_ACCESS_KEY_ID || process.env.MINIO_ROOT_USER || 'rtms_minio';
const secretAccessKey = args.secretKey || process.env.AWS_SECRET_ACCESS_KEY || process.env.MINIO_ROOT_PASSWORD || 'rtms_minio_password';
const bucket = args.bucket || `rtms-artifacts-test-${process.pid}`;
const region = args.region || process.env.AWS_REGION || 'us-east-1';
const shouldStartComposeMinio = !args.noCompose && isLoopbackEndpoint(endpoint);
try {
if (shouldStartComposeMinio) {
execFileSync('docker', ['compose', 'up', '-d', 'object-storage'], {
cwd: repoRoot,
stdio: args.verbose ? 'inherit' : 'ignore'
});
}
await waitForMinio(endpoint, accessKeyId, secretAccessKey, region);
console.log(`PASS minio_ready endpoint=${endpoint}`);
const port = await getFreePort();
const serviceUrl = `http://127.0.0.1:${port}`;
children.push(spawnService('08-artifact-storage/server.js', {
ARTIFACT_STORAGE_PORT: String(port),
ARTIFACT_STORAGE_PROVIDER: 'minio',
ARTIFACT_BUCKET: bucket,
AWS_REGION: region,
ARTIFACT_S3_ENDPOINT: endpoint,
ARTIFACT_S3_FORCE_PATH_STYLE: 'true',
ARTIFACT_S3_CREATE_BUCKET: 'true',
AWS_ACCESS_KEY_ID: accessKeyId,
AWS_SECRET_ACCESS_KEY: secretAccessKey
}));
const health = await waitForJson(`${serviceUrl}/health`, 'artifact storage health');
assert(health.storage?.provider === 's3-compatible', 'artifact storage did not use S3-compatible provider');
assert(health.storage?.createBucket === true, 'artifact storage did not enable bucket creation');
console.log(`PASS artifact_storage_minio_health port=${port}`);
const streamId = `minio-artifact-stream-${Date.now()}`;
const markdown = '# MinIO Final Summary\n\nThis is a MinIO artifact test.\n';
const upload = await postJson(`${serviceUrl}/artifacts`, {
streamId,
rtmsId: 'minio-artifact-rtms',
regionCode: 'SJC',
productType: 'meeting',
artifactType: 'summary_final',
fileName: 'final.md',
contentType: 'text/markdown',
content: markdown,
metadata: {
test: true,
provider: 'minio'
}
});
const artifact = upload.artifact;
assert(artifact.blobUri === `s3://${bucket}/${artifact.objectKey}`, 'unexpected MinIO blobUri');
assert(artifact.provider === 's3-compatible', 'unexpected provider name');
assert(artifact.sha256 === sha256(markdown), 'sha mismatch');
assert(artifact.objectKey.includes('artifact_type=summary_final'), 'object key missing artifact type partition');
assert(artifact.objectKey.includes(`stream_id=${streamId}`), 'object key missing stream id partition');
console.log(`PASS minio_artifact_uploaded blobUri=${artifact.blobUri}`);
const client = createClient(endpoint, accessKeyId, secretAccessKey, region);
const object = await client.send(new GetObjectCommand({
Bucket: bucket,
Key: artifact.objectKey
}));
const body = await streamToString(object.Body);
assert(body === markdown, 'MinIO object content mismatch');
console.log(`PASS minio_artifact_downloaded key=${artifact.objectKey}`);
if (!args.keepBucket) {
await client.send(new DeleteObjectCommand({ Bucket: bucket, Key: artifact.objectKey }));
await client.send(new DeleteBucketCommand({ Bucket: bucket }));
console.log(`PASS minio_bucket_cleaned bucket=${bucket}`);
}
console.log('10 MinIO artifact storage tester passed: 4/4');
} finally {
shuttingDown = true;
for (const child of children.reverse()) {
child.kill('SIGTERM');
}
}
function spawnService(script, env) {
const child = spawn(process.execPath, [path.join(repoRoot, script)], {
cwd: repoRoot,
env: {
...process.env,
...env
},
stdio: ['ignore', 'pipe', 'pipe']
});
child.stdout.on('data', (chunk) => {
if (args.verbose || process.env.TEST_VERBOSE) process.stdout.write(chunk);
});
child.stderr.on('data', (chunk) => {
if (args.verbose || process.env.TEST_VERBOSE) process.stderr.write(chunk);
});
child.on('exit', (code, signal) => {
if (!shuttingDown && code !== 0) {
console.error(`[test10] ${script} exited code=${code} signal=${signal || ''}`);
}
});
return child;
}
async function waitForMinio(endpoint, accessKeyId, secretAccessKey, region) {
const client = createClient(endpoint, accessKeyId, secretAccessKey, region);
await waitForCondition(async () => {
try {
await client.config.credentials();
const response = await fetch(`${endpoint.replace(/\/+$/, '')}/minio/health/ready`);
return response.ok;
} catch (_error) {
return false;
}
}, 'MinIO readiness', 120);
}
function createClient(endpoint, accessKeyId, secretAccessKey, region) {
return new S3Client({
region,
endpoint,
forcePathStyle: true,
credentials: {
accessKeyId,
secretAccessKey
}
});
}
async function getFreePort() {
return new Promise((resolve, reject) => {
const server = net.createServer();
server.once('error', reject);
server.listen(0, '127.0.0.1', () => {
const { port } = server.address();
server.close(() => resolve(port));
});
});
}
async function waitForJson(url, label) {
return waitForCondition(async () => {
try {
const response = await fetch(url);
if (!response.ok) return null;
return response.json();
} catch (_error) {
return null;
}
}, label);
}
async function waitForCondition(check, label, attempts = 80) {
for (let i = 0; i < attempts; i += 1) {
const value = await check();
if (value) return value;
await sleep(250);
}
throw new Error(`Timed out waiting for ${label}`);
}
async function postJson(url, body) {
const response = await fetch(url, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(body)
});
if (!response.ok) throw new Error(`POST ${url} failed status=${response.status}: ${await response.text()}`);
return response.json();
}
async function streamToString(stream) {
if (typeof stream?.transformToString === 'function') {
return stream.transformToString();
}
const chunks = [];
for await (const chunk of stream) {
chunks.push(Buffer.from(chunk));
}
return Buffer.concat(chunks).toString('utf8');
}
function parseArgs(argv) {
const parsed = {};
for (let i = 0; i < argv.length; i += 1) {
const arg = argv[i];
if (arg === '--no-compose') parsed.noCompose = true;
else if (arg === '--keep-bucket') parsed.keepBucket = true;
else if (arg === '--verbose') parsed.verbose = true;
else if (arg.startsWith('--')) {
parsed[arg.slice(2).replace(/-([a-z])/g, (_match, char) => char.toUpperCase())] = argv[i + 1];
i += 1;
}
}
return parsed;
}
function isLoopbackEndpoint(value) {
try {
const url = new URL(value);
return ['127.0.0.1', 'localhost', '::1'].includes(url.hostname);
} catch (_error) {
return false;
}
}
function sha256(value) {
return crypto.createHash('sha256').update(value).digest('hex');
}
function assert(condition, message) {
if (!condition) throw new Error(message);
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}