node-os-utils
Version:
Advanced cross-platform operating system monitoring utilities with TypeScript support
306 lines • 9.84 kB
JavaScript
;
/**
* 平台特定的测试工具和配置
* 为不同操作系统提供专门的测试支持
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlatformTestDataGenerator = exports.CrossPlatformValidator = exports.WindowsTestUtils = exports.MacOSTestUtils = exports.LinuxTestUtils = void 0;
const test_base_1 = require("./test-base");
/**
* Linux特定的测试工具
*/
class LinuxTestUtils {
/**
* 检查是否可以访问/proc文件系统
*/
static canAccessProc() {
try {
const fs = require('fs');
return fs.existsSync(this.PROC_MEMINFO);
}
catch {
return false;
}
}
/**
* 验证Linux的负载平均值
*/
static validateLoadAverage(loads) {
if (!Array.isArray(loads) || loads.length !== 3)
return false;
return loads.every(load => test_base_1.TestValidators.isValidNumber(load) && load >= 0);
}
/**
* 获取预期的Linux命令列表
*/
static getExpectedCommands() {
return [
'free',
'df',
'vmstat',
'netstat',
'ps',
'top',
'iotop',
'lscpu'
];
}
/**
* 验证Linux内存信息的结构
*/
static validateMemoryInfo(info) {
const requiredFields = ['totalMemMb', 'usedMemMb', 'freeMemMb', 'usedMemPercentage', 'freeMemPercentage'];
return requiredFields.every(field => info.hasOwnProperty(field) && test_base_1.TestValidators.isValidNumber(parseFloat(info[field])));
}
}
exports.LinuxTestUtils = LinuxTestUtils;
/**
* Linux特定的系统文件路径
*/
LinuxTestUtils.PROC_MEMINFO = '/proc/meminfo';
LinuxTestUtils.PROC_CPUINFO = '/proc/cpuinfo';
LinuxTestUtils.PROC_STAT = '/proc/stat';
LinuxTestUtils.PROC_LOADAVG = '/proc/loadavg';
/**
* macOS特定的测试工具
*/
class MacOSTestUtils {
/**
* 验证macOS的vm_stat输出格式
*/
static validateVmStatOutput(output) {
return output.includes('Pages free:') &&
output.includes('Pages active:') &&
output.includes('Pages inactive:');
}
/**
* 验证macOS的负载平均值(应该正常工作)
*/
static validateLoadAverage(loads) {
if (!Array.isArray(loads) || loads.length !== 3)
return false;
return loads.every(load => test_base_1.TestValidators.isValidNumber(load) && load >= 0);
}
/**
* 获取预期的macOS系统命令列表
*/
static getExpectedCommands() {
return [
'vm_stat',
'top',
'df',
'netstat',
'ps',
'system_profiler',
'sysctl'
];
}
/**
* 验证macOS磁盘信息格式
*/
static validateDiskInfo(info) {
const requiredFields = ['totalGb', 'usedGb', 'freeGb', 'usedPercentage', 'freePercentage'];
return requiredFields.every(field => info.hasOwnProperty(field) && test_base_1.TestValidators.isValidNumber(parseFloat(info[field])));
}
}
exports.MacOSTestUtils = MacOSTestUtils;
/**
* macOS特定的系统命令
*/
MacOSTestUtils.VM_STAT_CMD = 'vm_stat';
MacOSTestUtils.TOP_CMD = 'top';
MacOSTestUtils.DF_CMD = 'df';
MacOSTestUtils.NETSTAT_CMD = 'netstat';
/**
* Windows特定的测试工具
*/
class WindowsTestUtils {
/**
* 验证Windows的负载平均值(通常返回[0,0,0]或不支持)
*/
static validateLoadAverage(loads) {
// Windows上可能返回[0,0,0]或"not supported"
if (!Array.isArray(loads))
return false;
return loads.length === 3 && loads.every(load => test_base_1.TestValidators.isValidNumber(load) && load >= 0);
}
/**
* 检查功能是否在Windows上不被支持
*/
static isFeatureUnsupported(feature) {
return this.UNSUPPORTED_FEATURES.includes(feature);
}
/**
* 获取预期的Windows系统命令列表
*/
static getExpectedCommands() {
return [
'wmic',
'tasklist',
'systeminfo',
'netstat',
'powershell'
];
}
/**
* 验证Windows内存信息的结构
*/
static validateMemoryInfo(info) {
const requiredFields = ['totalMemMb', 'usedMemMb', 'freeMemMb'];
return requiredFields.every(field => info.hasOwnProperty(field) && test_base_1.TestValidators.isValidNumber(parseFloat(info[field])));
}
/**
* 验证Windows磁盘信息(可能使用不同的单位)
*/
static validateDiskInfo(info) {
const requiredFields = ['totalGb', 'usedGb', 'freeGb'];
return requiredFields.every(field => info.hasOwnProperty(field) && test_base_1.TestValidators.isValidNumber(parseFloat(info[field])));
}
}
exports.WindowsTestUtils = WindowsTestUtils;
/**
* Windows特定的系统命令
*/
WindowsTestUtils.WMIC_CMD = 'wmic';
WindowsTestUtils.TASKLIST_CMD = 'tasklist';
WindowsTestUtils.SYSTEMINFO_CMD = 'systeminfo';
/**
* Windows上某些功能可能不支持
*/
WindowsTestUtils.UNSUPPORTED_FEATURES = [
'loadavg', // Windows不支持负载平均值
'uptime' // Windows的uptime计算方式不同
];
/**
* 跨平台功能验证工具
*/
class CrossPlatformValidator {
/**
* 根据当前平台选择合适的验证器
*/
static validateSystemInfo(info) {
if (!test_base_1.TestValidators.isValidObject(info)) {
return false;
}
const requiredStringFields = ['hostname', 'platform', 'release'];
for (const field of requiredStringFields) {
if (!test_base_1.TestValidators.isNonEmptyString(info[field])) {
return false;
}
}
const optionalStringFields = ['version', 'arch'];
for (const field of optionalStringFields) {
if (field in info && !test_base_1.TestValidators.isNonEmptyString(info[field])) {
return false;
}
}
const numericFields = [
['uptime', true],
['uptimeSeconds', false],
['bootTime', false]
];
for (const [field, required] of numericFields) {
if (field in info) {
if (!test_base_1.TestValidators.isValidNumber(info[field]) || info[field] < 0) {
return false;
}
}
else if (required) {
return false;
}
}
if ('loadAverage' in info) {
const load = info.loadAverage;
if (!test_base_1.TestValidators.isValidObject(load)) {
return false;
}
const loads = [load.load1, load.load5, load.load15];
const platform = info.platform;
if (platform === 'darwin') {
if (!MacOSTestUtils.validateLoadAverage(loads)) {
return false;
}
}
else if (platform === 'win32') {
if (!WindowsTestUtils.validateLoadAverage(loads)) {
return false;
}
}
else {
if (!LinuxTestUtils.validateLoadAverage(loads)) {
return false;
}
}
}
return true;
}
/**
* 验证负载平均值(考虑平台差异)
*/
static validateLoadAverage(loads) {
if (test_base_1.PlatformUtils.isWindows()) {
return WindowsTestUtils.validateLoadAverage(loads);
}
else if (test_base_1.PlatformUtils.isMacOS()) {
return MacOSTestUtils.validateLoadAverage(loads);
}
else if (test_base_1.PlatformUtils.isLinux()) {
return LinuxTestUtils.validateLoadAverage(loads);
}
return false;
}
/**
* 获取当前平台的预期命令列表
*/
static getExpectedCommands() {
if (test_base_1.PlatformUtils.isLinux()) {
return LinuxTestUtils.getExpectedCommands();
}
else if (test_base_1.PlatformUtils.isMacOS()) {
return MacOSTestUtils.getExpectedCommands();
}
else if (test_base_1.PlatformUtils.isWindows()) {
return WindowsTestUtils.getExpectedCommands();
}
return [];
}
/**
* 检查功能是否在当前平台上被支持
*/
static isFeatureSupported(feature) {
if (test_base_1.PlatformUtils.isWindows()) {
return !WindowsTestUtils.isFeatureUnsupported(feature);
}
// Linux和macOS通常支持大多数功能
return true;
}
}
exports.CrossPlatformValidator = CrossPlatformValidator;
/**
* 平台特定的测试数据生成器
*/
class PlatformTestDataGenerator {
/**
* 生成平台特定的测试用例
*/
static generatePlatformTestCases() {
const platform = test_base_1.PlatformUtils.getCurrentPlatform();
const platformName = test_base_1.PlatformUtils.getPlatformName();
return {
platform,
platformName,
expectedCommands: CrossPlatformValidator.getExpectedCommands(),
supportedFeatures: {
loadavg: CrossPlatformValidator.isFeatureSupported('loadavg'),
uptime: CrossPlatformValidator.isFeatureSupported('uptime')
},
testConfig: {
memoryTestTimeout: test_base_1.PlatformUtils.isWindows() ? 15000 : 10000,
diskTestTimeout: test_base_1.PlatformUtils.isLinux() ? 8000 : 12000,
networkTestTimeout: 10000
}
};
}
}
exports.PlatformTestDataGenerator = PlatformTestDataGenerator;
//# sourceMappingURL=platform-specific.js.map