fix: read ql runtime logs from syslog

This commit is contained in:
whyour
2026-08-20 09:41:42 +08:00
parent 88200f2805
commit 134cd8337c
2 changed files with 132 additions and 11 deletions
+81 -9
View File
@@ -16,9 +16,10 @@ function writeExecutable(filePath, content) {
fs.writeFileSync(filePath, content, { mode: 0o755 });
}
test('ql log scopes PM2 output to qinglong and forwards log options', () => {
const shellDir = path.join(fixtureRoot, 'shell');
const binDir = path.join(fixtureRoot, 'bin');
function prepareFixture(name) {
const testRoot = path.join(fixtureRoot, name);
const shellDir = path.join(testRoot, 'shell');
const binDir = path.join(testRoot, 'bin');
fs.mkdirSync(shellDir, { recursive: true });
fs.mkdirSync(binDir, { recursive: true });
@@ -28,29 +29,100 @@ test('ql log scopes PM2 output to qinglong and forwards log options', () => {
);
fs.writeFileSync(
path.join(shellDir, 'share.sh'),
'load_ql_envs() { :; }\nimport_config() { :; }\n',
[
'dir_data="${QL_DATA_DIR:-$QL_DIR/data}"',
'load_ql_envs() { :; }',
'import_config() { :; }',
'',
].join('\n'),
);
fs.writeFileSync(path.join(shellDir, 'api.sh'), '');
fs.writeFileSync(path.join(shellDir, 'env.sh'), '');
writeExecutable(
path.join(binDir, 'pm2'),
'#!/usr/bin/env bash\nprintf "%s\\n" "$@"\n',
'#!/usr/bin/env bash\necho "pm2 must not be called" >&2\nexit 99\n',
);
return { binDir, shellDir, testRoot };
}
test('ql log reads the requested system log history without PM2', () => {
const { binDir, shellDir, testRoot } = prepareFixture('host');
const systemLogDir = path.join(testRoot, 'data/syslog');
fs.mkdirSync(systemLogDir, { recursive: true });
fs.writeFileSync(
path.join(systemLogDir, '2026-08-20.log'),
'system-one\nsystem-two\nsystem-three\n',
);
const result = spawnSync(
'/bin/bash',
[path.join(shellDir, 'update.sh'), 'log', '--lines', '25', '--nostream'],
[path.join(shellDir, 'update.sh'), 'log', '--lines', '2', '--nostream'],
{
encoding: 'utf8',
env: {
...process.env,
PATH: `${binDir}:${process.env.PATH}`,
QL_DIR: fixtureRoot,
QL_CONTAINER: 'false',
QL_DIR: testRoot,
},
},
);
assert.equal(result.status, 0, result.stderr);
assert.equal(result.stdout, 'logs\nqinglong\n--lines\n25\n--nostream\n');
assert.equal(fs.existsSync(path.join(fixtureRoot, 'data/log/log')), false);
assert.equal(result.stdout, 'system-two\nsystem-three\n');
assert.equal(fs.existsSync(path.join(testRoot, 'data/log/log')), false);
});
test('ql log follows the current Winston system log without PM2', () => {
const { binDir, shellDir, testRoot } = prepareFixture('container');
const systemLogDir = path.join(testRoot, 'data/syslog');
const currentDate = spawnSync('date', ['+%F'], { encoding: 'utf8' }).stdout.trim();
const currentLog = path.join(systemLogDir, `${currentDate}.log`);
fs.mkdirSync(systemLogDir, { recursive: true });
fs.writeFileSync(currentLog, 'system-one\n');
writeExecutable(
path.join(binDir, 'tail'),
'#!/usr/bin/env bash\nprintf "tail"\nprintf " <%s>" "$@"\nprintf "\\n"\n',
);
const result = spawnSync(
'/bin/bash',
[path.join(shellDir, 'update.sh'), 'log', '--lines', '0'],
{
encoding: 'utf8',
env: {
...process.env,
PATH: `${binDir}:${process.env.PATH}`,
QL_CONTAINER: 'true',
QL_DIR: testRoot,
},
},
);
assert.equal(result.status, 0, result.stderr);
assert.equal(result.stdout, `tail <-n> <0> <--retry> <-F> <${currentLog}>\n`);
assert.equal(fs.existsSync(path.join(testRoot, 'data/log/log')), false);
});
test('ql log reports when no persisted system log exists', () => {
const { binDir, shellDir, testRoot } = prepareFixture('container-nostream');
const result = spawnSync(
'/bin/bash',
[path.join(shellDir, 'update.sh'), 'log', '--nostream'],
{
encoding: 'utf8',
env: {
...process.env,
PATH: `${binDir}:${process.env.PATH}`,
QL_CONTAINER: 'true',
QL_DIR: testRoot,
},
},
);
assert.equal(result.status, 0);
assert.equal(result.stdout, '');
assert.match(result.stderr, /no system log found/);
});