fix: handle cold dev startup and versioned dependency removal

This commit is contained in:
whyour committed 2026-11-21 13:33:39 +08:00
1 parent a65d56a980
commit 0841e4e134
6 files changed
+211 -17

No files matched your search

+10 -5
View File
@@ -28,14 +28,18 @@ const compiled = ts.transpileModule(
},
).outputText;
for (const [arch, armVersion, timeout] of [
['arm', 6, 120000],
['arm', 7, 30000],
['arm64', undefined, 30000],
for (const [arch, armVersion, mode, timeout] of [
['arm', 6, 'production', 120000],
['arm', 7, 'production', 30000],
['arm64', undefined, 'production', 30000],
['x64', undefined, 'production', 30000],
['x64', undefined, undefined, 30000],
['x64', undefined, 'development', 120000],
['arm64', undefined, 'development', 120000],
]) {
test(`${arch} v${
armVersion ?? ''
} waits for scheduler readiness before HTTP and job recovery`, async () => {
} ${mode ?? 'default'} waits for scheduler readiness before HTTP and job recovery`, async () => {
const cluster = new EventEmitter();
const module = { exports: {} };
const logger = { info() {}, warn() {}, error() {} };
@@ -48,6 +52,7 @@ for (const [arch, armVersion, timeout] of [
compiled,
)(module, cluster, logger, String, {
arch,
env: { NODE_ENV: mode },
config: { variables: { arm_version: armVersion } },
exit() {
assert.fail('unexpected application exit');
@@ -0,0 +1,117 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { EventEmitter } = require('node:events');
const load = require('../helpers/load-security-module.cjs');
const types = { nodejs: 0, python3: 1, linux: 2 };
const util = load('back/config/util.ts', {
'./index': {},
'./const': {},
'../loaders/logger': {},
'../shared/utils': {},
'../data/dependence': { DependenceTypes: types },
});
for (const specifier of [
'tree', ' tree=2.3.2-r0 ', 'tree>=2.3', 'tree<=2.3', 'tree~2.3',
'tree>2', 'tree<3', 'tree@testing', 'tree@testing=2.3.2-r0',
]) {
test(`apk uninstall removes the constraint from ${specifier}`, () => {
assert.equal(util.getUninstallCommand(2, specifier, 'Alpine'), 'apk del -- tree');
});
}
for (const osType of ['Debian', 'Ubuntu']) {
for (const specifier of ['tree', 'tree=2.1.0-1', 'tree:amd64=1:2.1.0-1+b1', 'tree/stable']) {
test(`${osType} uninstall preserves ${specifier}, including sudo selection`, (t) => {
const original = process.env.QL_CONTAINER;
t.after(() => {
if (original === undefined) delete process.env.QL_CONTAINER;
else process.env.QL_CONTAINER = original;
});
for (const container of ['false', 'true']) {
process.env.QL_CONTAINER = container;
assert.equal(util.getUninstallCommand(2, specifier, osType),
`${container === 'true' ? 'sudo ' : ''}apt-get remove -y ${specifier}`);
}
});
}
}
test('standalone Linux uninstall uses the detected OS when no override is supplied', (t) => {
const original = process.env.QL_OS_TYPE;
t.after(() => {
if (original === undefined) delete process.env.QL_OS_TYPE;
else process.env.QL_OS_TYPE = original;
});
process.env.QL_OS_TYPE = 'Alpine';
assert.equal(util.getUninstallCommand(2, 'tree=2.3.2-r0'), 'apk del -- tree');
});
for (const osType of ['Alpine', 'Debian', 'Ubuntu']) {
for (const specifier of ['tree', 'tree=2.3.2-r0']) {
test(`${osType} service installs, uninstalls and cancels ${specifier} consistently`, async (t) => {
const original = process.env.QL_CONTAINER;
process.env.QL_CONTAINER = 'false';
t.after(() => {
if (original === undefined) delete process.env.QL_CONTAINER;
else process.env.QL_CONTAINER = original;
});
const install = `${osType === 'Alpine' ? 'apk add --no-check-certificate' : 'apt-get install -y'} ${specifier}`;
const uninstall = osType === 'Alpine' ? 'apk del -- tree' : `apt-get remove -y ${specifier}`;
const spawned = [], lookedUp = [], killed = [], removed = [];
const dep = { id: 1, name: specifier, type: 2, status: 1, log: [] };
const Service = load('back/services/dependence.ts', {
'../config': {},
'../config/util': {
...util,
detectOS: async () => osType,
fileExist: async () => false,
getPid: async (command) => {
lookedUp.push(command);
return command === uninstall ? 77 : undefined;
},
killTask: async (pid) => killed.push(pid),
},
'../data/dependence': {
Dependence: class {},
DependenceTypes: types,
DependenceStatus: { installed: 1, installing: 0, removing: 3, removed: 4, removeFailed: 5, cancelled: 7 },
versionDependenceCommandTypes: { 0: '@', 1: '==', 2: '=' },
DependenceModel: {
findAll: async () => [dep],
update: async (values) => Object.assign(dep, values),
destroy: async ({ where }) => removed.push(...where.id),
},
},
'../shared/pLimit': {
firstDependencyId: 1,
runDependeny: (_dep, fn) => fn(),
removeQueuedDependency() {},
updateDepLog: (fn) => fn(),
},
'cross-spawn': {
spawn: (command) => {
spawned.push(command.trim());
const child = new EventEmitter();
child.stdout = new EventEmitter();
child.stderr = new EventEmitter();
setImmediate(() => child.emit('exit', 0));
return child;
},
},
'./sock': class {},
'../shared/i18n': { t: (text) => text, tf: (text) => text },
}).default;
const service = new Service({}, { sendMessage() {} });
await service.cancel([1]);
assert.deepEqual(lookedUp, [install, uninstall]);
assert.deepEqual(killed, [77]);
await service.installOrUninstallDependency(dep, true, true);
assert.equal(dep.status, 1);
await service.installOrUninstallDependency(dep, false);
assert.deepEqual(spawned, [install, uninstall]);
assert.deepEqual(removed, [1]);
});
}
}
@@ -0,0 +1,49 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { execFileSync, spawn } = require('node:child_process');
const load = require('../helpers/load-security-module.cjs');
const api = load('back/config/util.ts', {
'./index': {},
'./const': {},
'../loaders/logger': {},
'../shared/utils': {},
'../data/dependence': {
DependenceTypes: { nodejs: 0, python3: 1, linux: 2 },
},
});
for (const [specifier, installedName] of [
['is-number@7.0.0', 'is-number'],
[' is-number@^7.0.0 ', 'is-number'],
['is-number@latest', 'is-number'],
['is-number', 'is-number'],
['@scope/name@1.2.3-beta.1', '@scope/name'],
['@scope/name@next', '@scope/name'],
['@scope/name', '@scope/name'],
['alias@npm:is-number@7.0.0', 'alias'],
["literal'$(printf INJECTED)", "literal'$(printf INJECTED)"],
]) {
test(`Node uninstall passes the installed name literally: ${specifier}`, () => {
const command = api.getUninstallCommand(0, specifier);
const args = execFileSync('/bin/sh', [
'-c',
'pnpm() { printf "%s\\0" "$@"; }; ' + command,
]).toString().split('\0').slice(0, -1);
assert.deepEqual(args, ['remove', '-g', '--', installedName]);
});
}
test('cancel can locate an uninstall process after shell argument parsing', async (t) => {
const name = `@ql-uninstall-fixture/worker-${process.pid}`;
const command = api.getUninstallCommand(0, `${name}@1.0.0`);
const worker = spawn(process.execPath, [
'-e', 'setInterval(() => {}, 1000)', 'pnpm', 'remove', '-g', '--', name,
], { stdio: 'ignore' });
t.after(() => worker.kill());
await new Promise((resolve, reject) => {
worker.once('spawn', resolve);
worker.once('error', reject);
});
assert.equal(await api.getPid(command), worker.pid);
});