61 lines
2.7 KiB
JavaScript
61 lines
2.7 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 本地自测桩:模拟远端只读探测输出,验证 check-root-logs.cjs 的
|
||
* --clean dry-run 删除计划逻辑(不连接任何真实服务器)。
|
||
*
|
||
* 用法:node script/tmp-test-clean-plan.cjs --clean
|
||
*/
|
||
|
||
const path = require('node:path');
|
||
|
||
const deployLibPath = require.resolve('./deploy-lib.cjs');
|
||
|
||
/**
|
||
* 构造模拟的远端探测输出:
|
||
* 总大小约 6.37 GiB(超过 4 GiB 阈值),包含活跃日志、7 个轮转日志、
|
||
* 其他命名模式的日志与 .gz 文件,用于验证排除规则与删除计划
|
||
*
|
||
* @return {string} 模拟的远端探测标准输出
|
||
*/
|
||
function buildFakeProbeOutput() {
|
||
const files = [
|
||
// 正在写入的活跃日志(无日期后缀),必须被排除
|
||
{ mtime: 1757780000, size: 1181116006, filePath: '/root/logs/wms/wms-web.log' },
|
||
// 历史轮转日志(mtime 升序,最旧的 4 个较大)
|
||
{ mtime: 1756150000, size: 1288490188, filePath: '/root/logs/wms/wms-web.log.2026-08-24' },
|
||
{ mtime: 1756236400, size: 1288490188, filePath: '/root/logs/wms/wms-web.log.2026-08-25' },
|
||
{ mtime: 1756322800, size: 1288490188, filePath: '/root/logs/wms/wms-web.log.2026-08-26' },
|
||
{ mtime: 1756409200, size: 1288490188, filePath: '/root/logs/wms/wms-web.log.2026-08-27' },
|
||
{ mtime: 1756495600, size: 214748365, filePath: '/root/logs/wms/wms-web.log.2026-08-28' },
|
||
{ mtime: 1756582000, size: 107374182, filePath: '/root/logs/wms/wms-web.log.2026-08-29' },
|
||
{ mtime: 1756668400, size: 107374182, filePath: '/root/logs/wms/wms-web.log.2026-08-30' },
|
||
// 非 wms-web 轮转命名模式的文件,必须被排除
|
||
{ mtime: 1756583000, size: 29360128, filePath: '/root/logs/wms/common-error.log.2026-08-29' },
|
||
{ mtime: 1756720000, size: 12582912, filePath: '/root/logs/wms/egg-schedule.log.2026-08-31' },
|
||
// .gz 后缀文件不符合严格命名模式,必须被排除
|
||
{ mtime: 1756730000, size: 31457280, filePath: '/root/logs/wms/wms-web.log.2026-08-31.gz' },
|
||
];
|
||
const totalBytes = files.reduce((sum, item) => sum + item.size, 0);
|
||
return [
|
||
`PROBE_TOTAL_BYTES=${totalBytes}`,
|
||
...files.map(item => `${item.mtime}\t${item.size}\t${item.filePath}`),
|
||
].join('\n');
|
||
}
|
||
|
||
// 将 deploy-lib.cjs 替换为本地桩,避免真实 SSH 连接
|
||
require.cache[deployLibPath] = {
|
||
id: deployLibPath,
|
||
filename: deployLibPath,
|
||
loaded: true,
|
||
exports: {
|
||
ensureCommandAvailable() {},
|
||
loadDeployConfig: () => ({ envName: 'production', username: 'root', host: '127.0.0.1', port: 22 }),
|
||
quoteForBash: value => `'${String(value)}'`,
|
||
runSsh: () => buildFakeProbeOutput(),
|
||
},
|
||
};
|
||
|
||
// 加载被测脚本(其 main() 会读取当前进程参数,即 --clean)
|
||
require(path.resolve(__dirname, 'check-root-logs.cjs'));
|