46 lines
2.8 KiB
TypeScript
46 lines
2.8 KiB
TypeScript
import { BadRequestException } from '@nestjs/common';
|
|
import { DEFAULT_ALERT_THRESHOLDS, InfrastructureAlertSettingsService } from './infrastructure-alert-settings.service';
|
|
import { readFileSync } from 'node:fs';
|
|
import { resolve } from 'node:path';
|
|
import { FILESYSTEM_INODE_USAGE_PERCENT, FILESYSTEM_USAGE_PERCENT } from './filesystem-metrics';
|
|
|
|
describe('InfrastructureAlertSettingsService', () => {
|
|
const service = new InfrastructureAlertSettingsService({} as never, { get: () => undefined } as never);
|
|
|
|
it('accepts the fixed threshold whitelist and renders managed rules', () => {
|
|
const validated = (service as unknown as { validate(value: unknown): unknown }).validate(DEFAULT_ALERT_THRESHOLDS);
|
|
const rules = (service as unknown as { renderRules(value: unknown): string }).renderRules(validated);
|
|
expect(rules).toContain('HostCpuUsageWarning');
|
|
expect(rules).toContain('CmppGatewayQueueDelayedCritical');
|
|
expect(rules).toContain('threshold: "120秒"');
|
|
expect(rules).toContain('redis_memory_max_bytes > 0');
|
|
expect(rules).toContain('sum(increase(cmpp_api_http_requests_total');
|
|
expect(rules).not.toContain('mountpoint="/"');
|
|
expect(rules).toContain('device=~"/dev/.+"');
|
|
expect(rules).not.toContain('{{ $labels.mountpoint }}');
|
|
expect(rules).toContain('{{ $labels.device }}');
|
|
expect(rules).toContain('{{ $labels.fstype }}');
|
|
expect(rules).toContain(FILESYSTEM_USAGE_PERCENT);
|
|
});
|
|
|
|
it('keeps static capacity and inode rules aligned with filesystem-level deduplication', () => {
|
|
for (const file of ['cmpp-alerts.yml', 'cmpp-managed-alerts.yml']) {
|
|
const content = readFileSync(resolve(__dirname, '../../../tools/monitoring', file), 'utf8');
|
|
const blocks = [...content.matchAll(/ - alert: (HostRoot(?:Disk|Inode)Usage(?:Warning|Critical))\r?\n([\s\S]*?)(?=\r?\n - alert:|$)/g)];
|
|
expect(blocks).toHaveLength(file === 'cmpp-alerts.yml' ? 4 : 2);
|
|
for (const [, name, body] of blocks) {
|
|
const expression = body.match(/^\s+expr: (.+)$/m)![1].trim();
|
|
const base = name.includes('Inode') ? FILESYSTEM_INODE_USAGE_PERCENT : FILESYSTEM_USAGE_PERCENT;
|
|
expect(expression).toBe(name.endsWith('Warning') ? `(${base} > 80) and (${base} <= 90)` : `${base} > 90`);
|
|
expect(body).not.toContain('$labels.mountpoint');
|
|
expect(body).toContain('$labels.device');
|
|
}
|
|
}
|
|
});
|
|
|
|
it('rejects unknown keys and warning thresholds that are not below critical', () => {
|
|
expect(() => (service as unknown as { validate(value: unknown): unknown }).validate({ ...DEFAULT_ALERT_THRESHOLDS, promql: { warning: 1, critical: 2 } })).toThrow(BadRequestException);
|
|
expect(() => (service as unknown as { validate(value: unknown): unknown }).validate({ ...DEFAULT_ALERT_THRESHOLDS, hostCpu: { warning: 90, critical: 90 } })).toThrow(BadRequestException);
|
|
});
|
|
});
|