@harperfast/integration-testing 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +291 -0
- package/dist/harperLifecycle.js +315 -0
- package/dist/harperLifecycle.js.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/loopbackAddressPool.js +337 -0
- package/dist/loopbackAddressPool.js.map +1 -0
- package/dist/run.js +94 -0
- package/dist/run.js.map +1 -0
- package/dist/targz.js +20 -0
- package/dist/targz.js.map +1 -0
- package/package.json +55 -0
- package/scripts/setup-loopback.sh +24 -0
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import { spawn, ChildProcess } from 'node:child_process';
|
|
2
|
+
import { createWriteStream, existsSync, rmSync } from 'node:fs';
|
|
3
|
+
import { join, basename } from 'node:path';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { mkdtemp, mkdir, rm, cp } from 'node:fs/promises';
|
|
6
|
+
import {} from 'node:test';
|
|
7
|
+
import { getNextAvailableLoopbackAddress, releaseLoopbackAddress } from "./loopbackAddressPool.js";
|
|
8
|
+
import { ok, equal } from 'node:assert';
|
|
9
|
+
import { createRequire } from 'node:module';
|
|
10
|
+
/**
|
|
11
|
+
* Creates a plain object satisfying HarperTestContext, for use outside node:test
|
|
12
|
+
* (e.g. as a Playwright worker fixture).
|
|
13
|
+
*
|
|
14
|
+
* @param name Optional name for log directory naming (e.g. Playwright worker index).
|
|
15
|
+
*/
|
|
16
|
+
export function createHarperContext(name) {
|
|
17
|
+
return { name };
|
|
18
|
+
}
|
|
19
|
+
// Constants
|
|
20
|
+
const HTTP_PORT = 9926;
|
|
21
|
+
const HTTPS_PORT = 9927;
|
|
22
|
+
export const OPERATIONS_API_PORT = 9925;
|
|
23
|
+
export const DEFAULT_ADMIN_USERNAME = 'admin';
|
|
24
|
+
export const DEFAULT_ADMIN_PASSWORD = 'Abc1234!';
|
|
25
|
+
export const DEFAULT_STARTUP_TIMEOUT_MS = process.env.HARPER_INTEGRATION_TEST_STARTUP_TIMEOUT_MS
|
|
26
|
+
? parseInt(process.env.HARPER_INTEGRATION_TEST_STARTUP_TIMEOUT_MS, 10)
|
|
27
|
+
: 30000;
|
|
28
|
+
const LOG_DIR = process.env.HARPER_INTEGRATION_TEST_LOG_DIR;
|
|
29
|
+
/**
|
|
30
|
+
* Gets the path to the Harper CLI script.
|
|
31
|
+
*
|
|
32
|
+
* Resolution order:
|
|
33
|
+
* 1. `harperBinPath` argument
|
|
34
|
+
* 2. `HARPER_INTEGRATION_TEST_INSTALL_SCRIPT` environment variable
|
|
35
|
+
* 3. Auto-resolved from 'harper' package in node_modules
|
|
36
|
+
*
|
|
37
|
+
* @returns The absolute path to the Harper CLI entry script
|
|
38
|
+
* @throws {AssertionError} If the script cannot be found
|
|
39
|
+
*/
|
|
40
|
+
function getHarperScript(harperBinPath) {
|
|
41
|
+
// 1. Explicit option
|
|
42
|
+
if (harperBinPath) {
|
|
43
|
+
ok(existsSync(harperBinPath), `Harper script not found at provided harperBinPath: ${harperBinPath}`);
|
|
44
|
+
return harperBinPath;
|
|
45
|
+
}
|
|
46
|
+
// 2. Environment variable
|
|
47
|
+
const envPath = process.env.HARPER_INTEGRATION_TEST_INSTALL_SCRIPT;
|
|
48
|
+
if (envPath) {
|
|
49
|
+
ok(existsSync(envPath), `Harper script not found at HARPER_INTEGRATION_TEST_INSTALL_SCRIPT path: ${envPath}`);
|
|
50
|
+
return envPath;
|
|
51
|
+
}
|
|
52
|
+
// 3. Auto-resolve from node_modules
|
|
53
|
+
try {
|
|
54
|
+
const require = createRequire(import.meta.url);
|
|
55
|
+
const resolved = require.resolve('harper/dist/bin/harper.js');
|
|
56
|
+
if (existsSync(resolved)) {
|
|
57
|
+
return resolved;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// harper package not found in node_modules
|
|
62
|
+
}
|
|
63
|
+
throw new Error(`Harper CLI script not found. Provide the path via:\n` +
|
|
64
|
+
` - harperBinPath option: startHarper(ctx, { harperBinPath: '/path/to/dist/bin/harper.js' })\n` +
|
|
65
|
+
` - HARPER_INTEGRATION_TEST_INSTALL_SCRIPT environment variable\n` +
|
|
66
|
+
` - Install 'harper' as a dependency in your project`);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Sanitizes a string for use as a filesystem directory name.
|
|
70
|
+
*/
|
|
71
|
+
function sanitizeForFilesystem(name) {
|
|
72
|
+
return name
|
|
73
|
+
.replace(/[^a-zA-Z0-9_-]/g, '_')
|
|
74
|
+
.replace(/_+/g, '_')
|
|
75
|
+
.substring(0, 100);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Runs a Harper CLI command and captures output.
|
|
79
|
+
*
|
|
80
|
+
* When `logDir` is provided, stdout and stderr are also written to files
|
|
81
|
+
* (`stdout.log` and `stderr.log`) in that directory.
|
|
82
|
+
*
|
|
83
|
+
* @throws {AssertionError} If the command exits with a non-zero status code
|
|
84
|
+
*/
|
|
85
|
+
function runHarperCommand({ args, env, completionMessage, logDir, harperBinPath, }) {
|
|
86
|
+
const harperScript = getHarperScript(harperBinPath);
|
|
87
|
+
const proc = spawn('node', ['--trace-warnings', '--force-node-api-uncaught-exceptions-policy=true', harperScript, ...args], {
|
|
88
|
+
env: { ...process.env, ...env },
|
|
89
|
+
});
|
|
90
|
+
let stdoutStream;
|
|
91
|
+
let stderrStream;
|
|
92
|
+
if (logDir) {
|
|
93
|
+
stdoutStream = createWriteStream(join(logDir, 'stdout.log'));
|
|
94
|
+
stderrStream = createWriteStream(join(logDir, 'stderr.log'));
|
|
95
|
+
}
|
|
96
|
+
return new Promise((resolve, reject) => {
|
|
97
|
+
let stdout = '';
|
|
98
|
+
let stderr = '';
|
|
99
|
+
let timer = setTimeout(() => {
|
|
100
|
+
let errorMessage = `Harper process timed out after ${DEFAULT_STARTUP_TIMEOUT_MS}ms`;
|
|
101
|
+
if (stdout) {
|
|
102
|
+
errorMessage += `\n\nstdout:\n${stdout}`;
|
|
103
|
+
}
|
|
104
|
+
if (stderr) {
|
|
105
|
+
errorMessage += `\n\nstderr:\n${stderr}`;
|
|
106
|
+
}
|
|
107
|
+
reject(errorMessage);
|
|
108
|
+
proc.kill();
|
|
109
|
+
}, DEFAULT_STARTUP_TIMEOUT_MS);
|
|
110
|
+
proc.stdout?.on('data', (data) => {
|
|
111
|
+
const dataString = data.toString();
|
|
112
|
+
stdoutStream?.write(data);
|
|
113
|
+
if (completionMessage && dataString.includes(completionMessage)) {
|
|
114
|
+
clearTimeout(timer);
|
|
115
|
+
resolve(proc);
|
|
116
|
+
}
|
|
117
|
+
stdout += dataString;
|
|
118
|
+
});
|
|
119
|
+
proc.stderr?.on('data', (data) => {
|
|
120
|
+
stderrStream?.write(data);
|
|
121
|
+
stderr += data.toString();
|
|
122
|
+
});
|
|
123
|
+
proc.on('error', (error) => {
|
|
124
|
+
reject(error);
|
|
125
|
+
});
|
|
126
|
+
proc.on('exit', (statusCode, signal) => {
|
|
127
|
+
clearTimeout(timer);
|
|
128
|
+
if (statusCode === 0) {
|
|
129
|
+
resolve(proc);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
let errorMessage = `Harper process failed with exit code/signal ${statusCode ?? signal}`;
|
|
133
|
+
stderrStream?.write(errorMessage);
|
|
134
|
+
if (stderr) {
|
|
135
|
+
errorMessage += `\n\nstderr:\n${stderr}`;
|
|
136
|
+
}
|
|
137
|
+
reject(errorMessage);
|
|
138
|
+
}
|
|
139
|
+
stdoutStream?.end();
|
|
140
|
+
stderrStream?.end();
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Sets up a Harper instance with a component pre-installed from a local directory.
|
|
146
|
+
*
|
|
147
|
+
* Copies `fixturePath` into `{dataRootDir}/components/{name}` before Harper starts,
|
|
148
|
+
* so the component is available on the first request without a post-startup deploy.
|
|
149
|
+
* Use this when tests need a known route available at startup (e.g. mTLS cert tests).
|
|
150
|
+
*
|
|
151
|
+
* @param ctx - The test context to populate with Harper instance details
|
|
152
|
+
* @param fixturePath - Absolute path to the component directory to pre-install
|
|
153
|
+
* @param options - Optional configuration for the setup process
|
|
154
|
+
*/
|
|
155
|
+
export async function setupHarperWithFixture(ctx, fixturePath, options) {
|
|
156
|
+
const dataRootDirPrefix = join(process.env.HARPER_INTEGRATION_TEST_INSTALL_PARENT_DIR || tmpdir(), 'harper-integration-test-');
|
|
157
|
+
const dataRootDir = await mkdtemp(dataRootDirPrefix);
|
|
158
|
+
await cp(fixturePath, join(dataRootDir, 'components', basename(fixturePath)), { recursive: true });
|
|
159
|
+
ctx.harper = { dataRootDir };
|
|
160
|
+
return startHarper(ctx, options);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Sets up and starts a Harper instance for testing.
|
|
164
|
+
*
|
|
165
|
+
* @param ctx - The test context to populate with Harper instance details
|
|
166
|
+
* @param options - Optional configuration for the setup process
|
|
167
|
+
* @returns The context with the `harper` property populated
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```ts
|
|
171
|
+
* suite('My tests', (ctx: ContextWithHarper) => {
|
|
172
|
+
* before(async () => {
|
|
173
|
+
* await startHarper(ctx);
|
|
174
|
+
* });
|
|
175
|
+
*
|
|
176
|
+
* after(async () => {
|
|
177
|
+
* await teardownHarper(ctx);
|
|
178
|
+
* });
|
|
179
|
+
*
|
|
180
|
+
* test('can connect', async () => {
|
|
181
|
+
* const response = await fetch(ctx.harper.httpURL);
|
|
182
|
+
* // ...
|
|
183
|
+
* });
|
|
184
|
+
* });
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
export async function startHarper(ctx, options) {
|
|
188
|
+
const dataRootDirPrefix = join(process.env.HARPER_INTEGRATION_TEST_INSTALL_PARENT_DIR || tmpdir(), `harper-integration-test-`);
|
|
189
|
+
const dataRootDir = ctx.harper?.dataRootDir ?? (await mkdtemp(dataRootDirPrefix));
|
|
190
|
+
const loopbackAddress = ctx.harper?.hostname ?? (await getNextAvailableLoopbackAddress());
|
|
191
|
+
// Set up per-suite log directory when HARPER_INTEGRATION_TEST_LOG_DIR is configured
|
|
192
|
+
let logDir;
|
|
193
|
+
if (LOG_DIR) {
|
|
194
|
+
const suiteName = sanitizeForFilesystem(ctx.name || 'unknown');
|
|
195
|
+
logDir = join(LOG_DIR, `${suiteName}-${sanitizeForFilesystem(loopbackAddress)}`);
|
|
196
|
+
await mkdir(logDir, { recursive: true });
|
|
197
|
+
}
|
|
198
|
+
// Point Harper's log directory to the suite log dir so hdb.log is preserved for upload
|
|
199
|
+
const config = { ...options?.config };
|
|
200
|
+
if (logDir) {
|
|
201
|
+
config.logging = { ...config.logging, root: logDir };
|
|
202
|
+
// Clean up log directory on successful exit — only keep logs when tests fail
|
|
203
|
+
process.on('exit', (code) => {
|
|
204
|
+
if (code === 0) {
|
|
205
|
+
try {
|
|
206
|
+
rmSync(logDir, { recursive: true, force: true });
|
|
207
|
+
}
|
|
208
|
+
catch { }
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
const args = [
|
|
213
|
+
`--ROOTPATH=${dataRootDir}`,
|
|
214
|
+
'--DEFAULTS_MODE=dev',
|
|
215
|
+
`--HDB_ADMIN_USERNAME=${DEFAULT_ADMIN_USERNAME}`,
|
|
216
|
+
`--HDB_ADMIN_PASSWORD=${DEFAULT_ADMIN_PASSWORD}`,
|
|
217
|
+
'--THREADS_COUNT=1',
|
|
218
|
+
'--THREADS_DEBUG=false',
|
|
219
|
+
`--NODE_HOSTNAME=${loopbackAddress}`,
|
|
220
|
+
`--HTTP_PORT=${loopbackAddress}:${HTTP_PORT}`,
|
|
221
|
+
`--OPERATIONSAPI_NETWORK_PORT=${loopbackAddress}:${OPERATIONS_API_PORT}`,
|
|
222
|
+
'--LOGGING_LEVEL=debug',
|
|
223
|
+
'--LOGGING_STDSTREAMS=false',
|
|
224
|
+
];
|
|
225
|
+
// Bind secure port if HTTPS is needed (mTLS or other TLS config present)
|
|
226
|
+
if (options?.config?.http?.mtls !== undefined || options?.config?.tls !== undefined) {
|
|
227
|
+
args.push(`--HTTP_SECUREPORT=${loopbackAddress}:${HTTPS_PORT}`);
|
|
228
|
+
}
|
|
229
|
+
// HARPER_SET_CONFIG must be passed as an environment variable, not a CLI arg,
|
|
230
|
+
// because applyRuntimeEnvVarConfig reads from process.env.HARPER_SET_CONFIG
|
|
231
|
+
const harperEnv = {
|
|
232
|
+
HARPER_SET_CONFIG: JSON.stringify(config),
|
|
233
|
+
...options?.env,
|
|
234
|
+
};
|
|
235
|
+
const harperProcess = await runHarperCommand({
|
|
236
|
+
args,
|
|
237
|
+
env: harperEnv,
|
|
238
|
+
completionMessage: 'successfully started',
|
|
239
|
+
logDir,
|
|
240
|
+
harperBinPath: options?.harperBinPath,
|
|
241
|
+
});
|
|
242
|
+
ctx.harper = {
|
|
243
|
+
dataRootDir,
|
|
244
|
+
admin: {
|
|
245
|
+
username: DEFAULT_ADMIN_USERNAME,
|
|
246
|
+
password: DEFAULT_ADMIN_PASSWORD,
|
|
247
|
+
},
|
|
248
|
+
httpURL: `http://${loopbackAddress}:${HTTP_PORT}`,
|
|
249
|
+
operationsAPIURL: `http://${loopbackAddress}:${OPERATIONS_API_PORT}`,
|
|
250
|
+
hostname: loopbackAddress,
|
|
251
|
+
process: harperProcess,
|
|
252
|
+
logDir,
|
|
253
|
+
};
|
|
254
|
+
return ctx;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Kill harper process (can be used for teardown, or killing it before a restart)
|
|
258
|
+
* @param ctx
|
|
259
|
+
*/
|
|
260
|
+
export async function killHarper(ctx) {
|
|
261
|
+
await new Promise((resolve) => {
|
|
262
|
+
let timer;
|
|
263
|
+
ctx.harper.process.on('exit', () => {
|
|
264
|
+
resolve();
|
|
265
|
+
clearTimeout(timer);
|
|
266
|
+
});
|
|
267
|
+
ctx.harper.process.kill();
|
|
268
|
+
timer = setTimeout(() => {
|
|
269
|
+
try {
|
|
270
|
+
ctx.harper.process.kill('SIGKILL');
|
|
271
|
+
}
|
|
272
|
+
catch {
|
|
273
|
+
// possible that the process terminated but the exit event hasn't fired yet
|
|
274
|
+
}
|
|
275
|
+
resolve();
|
|
276
|
+
}, 200);
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Tears down a Harper instance and cleans up all resources.
|
|
281
|
+
*
|
|
282
|
+
* This function stops the Harper instance, releases the loopback address,
|
|
283
|
+
* and removes the installation directory.
|
|
284
|
+
* @param ctx - The test context with Harper instance details
|
|
285
|
+
*
|
|
286
|
+
* @example
|
|
287
|
+
* ```ts
|
|
288
|
+
* suite('My tests', (ctx: ContextWithHarper) => {
|
|
289
|
+
* before(async () => {
|
|
290
|
+
* await startHarper(ctx);
|
|
291
|
+
* });
|
|
292
|
+
*
|
|
293
|
+
* after(async () => {
|
|
294
|
+
* await teardownHarper(ctx);
|
|
295
|
+
* });
|
|
296
|
+
* });
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
export async function teardownHarper(ctx) {
|
|
300
|
+
await killHarper(ctx);
|
|
301
|
+
await releaseLoopbackAddress(ctx.harper.hostname);
|
|
302
|
+
// a few retries are typically necessary, might take a sec for a process to finish, especially since rocksdb may be flushing
|
|
303
|
+
await rm(ctx.harper.dataRootDir, { recursive: true, force: true, maxRetries: 4 });
|
|
304
|
+
}
|
|
305
|
+
export async function sendOperation(context, operation) {
|
|
306
|
+
const response = await fetch(context.operationsAPIURL, {
|
|
307
|
+
method: 'POST',
|
|
308
|
+
headers: { 'Content-Type': 'application/json' },
|
|
309
|
+
body: JSON.stringify(operation),
|
|
310
|
+
});
|
|
311
|
+
const responseData = await response.json();
|
|
312
|
+
equal(response.status, 200, JSON.stringify(responseData));
|
|
313
|
+
return responseData;
|
|
314
|
+
}
|
|
315
|
+
//# sourceMappingURL=harperLifecycle.js.map
|
|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AAM,sBAAsB,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAElD,4HAA4H;IAC5H,MAAM,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,WAAW,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC,CAAC;AACnF,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,OAAsB,EAAE,SAAc;IACzE,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,OAAO,CAAC,gBAAgB,EAAE;QACtD,MAAM,EAAE,MAAM;QACd,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;QAC/C,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC;KAC/B,CAAC,CAAC;IACH,MAAM,YAAY,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;IAC3C,KAAK,CAAC,QAAQ,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC,CAAC;IAC1D,OAAO,YAAY,CAAC;AACrB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { startHarper, setupHarperWithFixture, killHarper, teardownHarper, sendOperation, createHarperContext, OPERATIONS_API_PORT, DEFAULT_ADMIN_USERNAME, DEFAULT_ADMIN_PASSWORD, DEFAULT_STARTUP_TIMEOUT_MS, } from "./harperLifecycle.js";
|
|
2
|
+
export { validateLoopbackAddressPool, getNextAvailableLoopbackAddress, releaseLoopbackAddress, releaseAllLoopbackAddressesForCurrentProcess, } from "./loopbackAddressPool.js";
|
|
3
|
+
export { targz } from "./targz.js";
|
|
4
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,WAAW,EACX,sBAAsB,EACtB,UAAU,EACV,cAAc,EACd,aAAa,EACb,mBAAmB,EACnB,mBAAmB,EACnB,sBAAsB,EACtB,sBAAsB,EACtB,0BAA0B,GAM1B,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EACN,2BAA2B,EAC3B,+BAA+B,EAC/B,sBAAsB,EACtB,4CAA4C,GAC5C,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
import { setTimeout as sleep } from 'node:timers/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { open, readFile, stat, unlink, writeFile } from 'node:fs/promises';
|
|
5
|
+
import { createServer } from 'node:net';
|
|
6
|
+
// Configuration constants
|
|
7
|
+
const HARPER_LOOPBACK_POOL_COUNT = process.env.HARPER_INTEGRATION_TEST_LOOPBACK_POOL_COUNT
|
|
8
|
+
? parseInt(process.env.HARPER_INTEGRATION_TEST_LOOPBACK_POOL_COUNT, 10)
|
|
9
|
+
: 32;
|
|
10
|
+
if (HARPER_LOOPBACK_POOL_COUNT < 1 || HARPER_LOOPBACK_POOL_COUNT > 255) {
|
|
11
|
+
throw new Error('HARPER_INTEGRATION_TEST_LOOPBACK_POOL_COUNT must be between 1 and 255');
|
|
12
|
+
}
|
|
13
|
+
const HARPER_LOOPBACK_POOL_PATH = join(tmpdir(), 'harper-integration-test-loopback-pool.json');
|
|
14
|
+
const HARPER_LOOPBACK_POOL_LOCK_PATH = join(tmpdir(), 'harper-integration-test-loopback-pool.lock');
|
|
15
|
+
// Constants for timeouts and retries
|
|
16
|
+
const LOCK_STALE_TIMEOUT_MS = 10000;
|
|
17
|
+
const RETRY_DELAY_MS = 1000;
|
|
18
|
+
// Custom error classes
|
|
19
|
+
class LoopbackAddressValidationError extends Error {
|
|
20
|
+
constructor(address, cause) {
|
|
21
|
+
super(`Failed to validate loopback address ${address}. This likely means your system does not have the required loopback addresses configured/enabled. Refer to the Harper Integration Test documentation (integrationTests/README.md) for more information.`);
|
|
22
|
+
this.name = 'LoopbackAddressValidationError';
|
|
23
|
+
if (cause) {
|
|
24
|
+
this.cause = cause;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
class InvalidLoopbackAddressError extends Error {
|
|
29
|
+
constructor(address) {
|
|
30
|
+
super(`Invalid loopback address format: ${address}. Expected format: 127.0.0.X where X is between 1 and ${HARPER_LOOPBACK_POOL_COUNT}`);
|
|
31
|
+
this.name = 'InvalidLoopbackAddressError';
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Acquires a file-based lock by creating the lock file. This enables safe concurrent
|
|
36
|
+
* access to the loopback pool across multiple test processes.
|
|
37
|
+
*
|
|
38
|
+
* Uses the 'wx' file flag which atomically fails if the file already exists, providing
|
|
39
|
+
* a simple but effective cross-process mutex. Handles stale locks by removing lock files
|
|
40
|
+
* older than LOCK_STALE_TIMEOUT_MS (10 seconds).
|
|
41
|
+
*
|
|
42
|
+
* @returns A promise that resolves when the lock is acquired
|
|
43
|
+
*/
|
|
44
|
+
async function acquireLock() {
|
|
45
|
+
while (true) {
|
|
46
|
+
try {
|
|
47
|
+
// The 'wx' flag causes the open to fail if the file already exists
|
|
48
|
+
const lockFileHandle = await open(HARPER_LOOPBACK_POOL_LOCK_PATH, 'wx');
|
|
49
|
+
// We have the lock - close the handle as we don't intend to write to it
|
|
50
|
+
await lockFileHandle.close();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
// If the lock file already exists, it's either stale or we wait for it to be released
|
|
55
|
+
if (error.code === 'EEXIST') {
|
|
56
|
+
try {
|
|
57
|
+
const lockFileStat = await stat(HARPER_LOOPBACK_POOL_LOCK_PATH);
|
|
58
|
+
// If the lock file is older than the timeout, consider it stale and remove it
|
|
59
|
+
if (Date.now() - lockFileStat.mtimeMs > LOCK_STALE_TIMEOUT_MS) {
|
|
60
|
+
await unlink(HARPER_LOOPBACK_POOL_LOCK_PATH);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
// Lock file may have been removed by another process, continue
|
|
65
|
+
}
|
|
66
|
+
await sleep(RETRY_DELAY_MS);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
// Rethrow other errors
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Releases the file-based lock by deleting the lock file.
|
|
76
|
+
*/
|
|
77
|
+
async function releaseLock() {
|
|
78
|
+
try {
|
|
79
|
+
await unlink(HARPER_LOOPBACK_POOL_LOCK_PATH);
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
// Ignore errors if lock file is already gone
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Executes a callback function while holding the lock. Automatically acquires
|
|
87
|
+
* and releases the lock, ensuring the lock is always released even if the callback
|
|
88
|
+
* throws an error.
|
|
89
|
+
*
|
|
90
|
+
* @param callback The async function to execute while holding the lock
|
|
91
|
+
* @returns The result of the callback function
|
|
92
|
+
*/
|
|
93
|
+
async function withLock(callback) {
|
|
94
|
+
await acquireLock();
|
|
95
|
+
try {
|
|
96
|
+
return await callback();
|
|
97
|
+
}
|
|
98
|
+
finally {
|
|
99
|
+
await releaseLock();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Reads the loopback pool from the pool file. The pool is a JSON array where each
|
|
104
|
+
* index represents a loopback address (127.0.0.1, 127.0.0.2, etc.) and the value
|
|
105
|
+
* is either null (available) or a process PID (in use).
|
|
106
|
+
*
|
|
107
|
+
* If the file doesn't exist, creates and returns a new empty pool with all addresses
|
|
108
|
+
* marked as available (null).
|
|
109
|
+
*
|
|
110
|
+
* @returns The loopback pool array
|
|
111
|
+
*/
|
|
112
|
+
async function readPoolFile() {
|
|
113
|
+
try {
|
|
114
|
+
const content = await readFile(HARPER_LOOPBACK_POOL_PATH, 'utf-8');
|
|
115
|
+
return JSON.parse(content);
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
if (error.code === 'ENOENT') {
|
|
119
|
+
// If the pool file doesn't exist yet, create it with null entries
|
|
120
|
+
return Array(HARPER_LOOPBACK_POOL_COUNT).fill(null);
|
|
121
|
+
}
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Writes the loopback pool to the pool file as JSON.
|
|
127
|
+
*
|
|
128
|
+
* @param pool The loopback pool array to persist
|
|
129
|
+
*/
|
|
130
|
+
async function writePoolFile(pool) {
|
|
131
|
+
await writeFile(HARPER_LOOPBACK_POOL_PATH, JSON.stringify(pool));
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Finds the first available (null) index in the pool. This implements a simple
|
|
135
|
+
* first-available allocation strategy.
|
|
136
|
+
*
|
|
137
|
+
* @param pool The loopback pool array
|
|
138
|
+
* @returns The first available index, or null if the pool is full
|
|
139
|
+
*/
|
|
140
|
+
function findAvailableIndex(pool) {
|
|
141
|
+
for (let i = 0; i < pool.length; i++) {
|
|
142
|
+
if (pool[i] === null) {
|
|
143
|
+
return i;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Validates the format of a loopback address and extracts the pool index.
|
|
150
|
+
*
|
|
151
|
+
* Expects addresses in the format "127.0.0.X" where X is between 1 and the pool count.
|
|
152
|
+
* The returned index is 0-based (e.g., "127.0.0.1" returns index 0).
|
|
153
|
+
*
|
|
154
|
+
* @param address The loopback address to parse (e.g., "127.0.0.2")
|
|
155
|
+
* @returns The 0-based pool index for this address
|
|
156
|
+
* @throws {InvalidLoopbackAddressError} If the address format is invalid or out of range
|
|
157
|
+
*/
|
|
158
|
+
function parseLoopbackAddress(address) {
|
|
159
|
+
const parts = address.split('.');
|
|
160
|
+
if (parts.length !== 4 || parts[0] !== '127' || parts[1] !== '0' || parts[2] !== '0') {
|
|
161
|
+
throw new InvalidLoopbackAddressError(address);
|
|
162
|
+
}
|
|
163
|
+
const index = parseInt(parts[3], 10) - 1;
|
|
164
|
+
if (isNaN(index) || index < 0 || index >= HARPER_LOOPBACK_POOL_COUNT) {
|
|
165
|
+
throw new InvalidLoopbackAddressError(address);
|
|
166
|
+
}
|
|
167
|
+
return index;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Validates that a given loopback address can be bound to by creating a temporary
|
|
171
|
+
* TCP server on that address. This ensures the loopback address is actually configured
|
|
172
|
+
* and available on the system before allocating it to a test process.
|
|
173
|
+
*
|
|
174
|
+
* The server is bound to port 0 (random port) just to verify the address exists,
|
|
175
|
+
* then immediately closed.
|
|
176
|
+
*
|
|
177
|
+
* @param loopbackAddress The loopback IP address to validate (e.g., "127.0.0.2")
|
|
178
|
+
* @returns A promise that resolves with the address if valid
|
|
179
|
+
* @throws An error with the loopbackAddress property if binding fails
|
|
180
|
+
*/
|
|
181
|
+
function validateLoopbackAddress(loopbackAddress) {
|
|
182
|
+
return new Promise((resolve, reject) => {
|
|
183
|
+
const server = createServer();
|
|
184
|
+
server.once('error', (error) => {
|
|
185
|
+
const enhancedError = error;
|
|
186
|
+
enhancedError.loopbackAddress = loopbackAddress;
|
|
187
|
+
reject(enhancedError);
|
|
188
|
+
});
|
|
189
|
+
server.listen(0, loopbackAddress, () => {
|
|
190
|
+
server.close(() => {
|
|
191
|
+
resolve(loopbackAddress);
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* This method attempts to validate all loopback addresses in the pool by trying to
|
|
198
|
+
* bind to each one. It returns an object containing arrays of successfully bound
|
|
199
|
+
* loopback addresses and those that failed along with their errors.
|
|
200
|
+
*
|
|
201
|
+
* It will check all loopback addresses from 127.0.0.1 to 127.0.0.32 (by default).
|
|
202
|
+
*
|
|
203
|
+
* Use the HARPER_INTEGRATION_TEST_LOOPBACK_POOL_COUNT environment variable to
|
|
204
|
+
* adjust the number of loopback addresses to validate (up to 255).
|
|
205
|
+
*/
|
|
206
|
+
export async function validateLoopbackAddressPool() {
|
|
207
|
+
return Promise.allSettled(Array.from({ length: HARPER_LOOPBACK_POOL_COUNT }, (_, i) => validateLoopbackAddress(`127.0.0.${i + 1}`))).then((results) => results.reduce((acc, result) => {
|
|
208
|
+
if (result.status === 'fulfilled') {
|
|
209
|
+
acc.successful.push(result.value);
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
const error = result.reason;
|
|
213
|
+
acc.failed.push({ loopbackAddress: error.loopbackAddress, error });
|
|
214
|
+
}
|
|
215
|
+
return acc;
|
|
216
|
+
}, { successful: [], failed: [] }));
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Retrieves the next available loopback address from the pool using a file-based
|
|
220
|
+
* locking mechanism to safely allocate addresses across concurrent test processes.
|
|
221
|
+
*
|
|
222
|
+
* **How it works:**
|
|
223
|
+
* 1. Acquires a file-based lock to prevent race conditions with other processes
|
|
224
|
+
* 2. Reads the pool state (a JSON array of process IDs, with null for available slots)
|
|
225
|
+
* 3. Finds the first available (null) slot and assigns the current process PID to it
|
|
226
|
+
* 4. Writes the updated pool back to disk and releases the lock
|
|
227
|
+
* 5. Validates that the allocated address can actually be bound to
|
|
228
|
+
* 6. Returns the loopback address (e.g., "127.0.0.2")
|
|
229
|
+
*
|
|
230
|
+
* If no addresses are available, waits and retries until one becomes available.
|
|
231
|
+
*
|
|
232
|
+
* **Pool file location:** `${tmpdir()}/harper-integration-test-loopback-pool.json`
|
|
233
|
+
* **Lock file location:** `${tmpdir()}/harper-integration-test-loopback-pool.lock`
|
|
234
|
+
*
|
|
235
|
+
* @returns A promise that resolves with an allocated loopback address
|
|
236
|
+
* @throws {LoopbackAddressValidationError} If the allocated address cannot be bound to
|
|
237
|
+
*/
|
|
238
|
+
export async function getNextAvailableLoopbackAddress() {
|
|
239
|
+
// Each index+1 is a different loopback address that a test process will be assigned to
|
|
240
|
+
// So if the first test process number is 42, it would be assigned to index 0 associated with address 127.0.0.1
|
|
241
|
+
// [42, null, null, ...];
|
|
242
|
+
// Then the next process (call is 43) gets the next available, so index 1 -> 127.0.0.2
|
|
243
|
+
// [42, 43, null, ...];
|
|
244
|
+
// And so on...
|
|
245
|
+
// As processes exit and release their loopback addresses, those addresses become available for new processes to use
|
|
246
|
+
// [42, null, 44, ...];
|
|
247
|
+
// Next process (45) gets index 1 again ->
|
|
248
|
+
// [42, 45, 44, ...];
|
|
249
|
+
// This continues until all loopback addresses are used, at which point new processes will wait until an address becomes available
|
|
250
|
+
// Since multiple processes may be trying to get a loopback address at the same time, we need to implement a simple file-based locking mechanism to prevent race conditions
|
|
251
|
+
while (true) {
|
|
252
|
+
const assignedIndex = await withLock(async () => {
|
|
253
|
+
// Read the pool file
|
|
254
|
+
const loopbackPool = await readPoolFile();
|
|
255
|
+
// Find the first available index
|
|
256
|
+
const index = findAvailableIndex(loopbackPool);
|
|
257
|
+
if (index === null) {
|
|
258
|
+
// No available addresses - remove any dead processes from the pool and wait for one to become available
|
|
259
|
+
removeDeadProcessesFromPool(loopbackPool);
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
// Assign the process PID to that index to mark it as used
|
|
263
|
+
loopbackPool[index] = process.pid;
|
|
264
|
+
}
|
|
265
|
+
// Write the updated pool back to the file
|
|
266
|
+
await writePoolFile(loopbackPool);
|
|
267
|
+
return index;
|
|
268
|
+
});
|
|
269
|
+
// If we got an index, validate and return the address
|
|
270
|
+
if (assignedIndex !== null) {
|
|
271
|
+
const loopbackAddress = `127.0.0.${assignedIndex + 1}`;
|
|
272
|
+
try {
|
|
273
|
+
await validateLoopbackAddress(loopbackAddress);
|
|
274
|
+
return loopbackAddress;
|
|
275
|
+
}
|
|
276
|
+
catch (error) {
|
|
277
|
+
// Validation failed - throw a proper error instead of breaking
|
|
278
|
+
throw new LoopbackAddressValidationError(loopbackAddress, error);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
// No available addresses; wait and retry
|
|
282
|
+
await sleep(RETRY_DELAY_MS);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Removes any dead processes from the loopback pool.
|
|
287
|
+
* @param loopbackPool
|
|
288
|
+
*/
|
|
289
|
+
function removeDeadProcessesFromPool(loopbackPool) {
|
|
290
|
+
loopbackPool.forEach((pid, index) => {
|
|
291
|
+
if (pid === null)
|
|
292
|
+
return;
|
|
293
|
+
try {
|
|
294
|
+
process.kill(pid, 0);
|
|
295
|
+
}
|
|
296
|
+
catch {
|
|
297
|
+
loopbackPool[index] = null;
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Releases a loopback address back to the pool, making it available for other processes.
|
|
303
|
+
*
|
|
304
|
+
* @param address The loopback address to release (e.g., "127.0.0.1")
|
|
305
|
+
* @throws InvalidLoopbackAddressError if the address format is invalid
|
|
306
|
+
*/
|
|
307
|
+
export async function releaseLoopbackAddress(address) {
|
|
308
|
+
// Validate and parse the address
|
|
309
|
+
const index = parseLoopbackAddress(address);
|
|
310
|
+
await withLock(async () => {
|
|
311
|
+
// Read the pool file
|
|
312
|
+
const loopbackPool = await readPoolFile();
|
|
313
|
+
// Release the address by setting it to null
|
|
314
|
+
loopbackPool[index] = null;
|
|
315
|
+
// Write the updated pool back to the file
|
|
316
|
+
await writePoolFile(loopbackPool);
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Releases all loopback addresses assigned to the current process.
|
|
321
|
+
* Useful for cleanup during graceful shutdown.
|
|
322
|
+
*/
|
|
323
|
+
export async function releaseAllLoopbackAddressesForCurrentProcess() {
|
|
324
|
+
await withLock(async () => {
|
|
325
|
+
// Read the pool file
|
|
326
|
+
const loopbackPool = await readPoolFile();
|
|
327
|
+
// Find and release all addresses assigned to this process
|
|
328
|
+
for (let i = 0; i < loopbackPool.length; i++) {
|
|
329
|
+
if (loopbackPool[i] === process.pid) {
|
|
330
|
+
loopbackPool[i] = null;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
// Write the updated pool back to the file
|
|
334
|
+
await writePoolFile(loopbackPool);
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
//# sourceMappingURL=loopbackAddressPool.js.map
|