Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

7.7 KB
Raw Blame
import { describe, expect, it } from 'vitest';

import type { PBSJobHealthEvidence } from '@/types/api';

import type { Resource } from '@/types/resource';

import {
  collectDatastoreJobFailures,
  describeDatastoreJobs,
  summarizeDatastoreJobs,
} from '../proxmoxBackupServerJobs';

const job = (overrides: Partial<PBSJobHealthEvidence>): PBSJobHealthEvidence => ({
  id: 'job',
  family: 'verify',
  store: 'main',
  enabled: true,
  confidence: 'config-last-run',
  posture: 'healthy',
  postureReason: 'last-run-success',
  ...overrides,
});

describe('summarizeDatastoreJobs', () => {
  it('says OK only when every reported job on the store last succeeded', () => {
    const summary = summarizeDatastoreJobs(
      [
        job({ id: 'garbage:main', family: 'garbage' }),
        job({ id: 'v-1', family: 'verify' }),
        job({ id: 's-1', family: 'sync', postureReason: 'running' }),
      ],
      'main',
    );
    expect(summary.state).toBe('ok');
    expect(summary.label).toBe('OK');
    expect(describeDatastoreJobs(summary)).toBe('GC OK · Verify OK · Sync running');
  });

  it('leads with the worst backend judgment and lists the rest in the detail', () => {
    const summary = summarizeDatastoreJobs(
      [
        job({ id: 'p-1', family: 'prune', posture: 'warning', postureReason: 'job-overdue' }),
        job({ id: 's-1', family: 'sync', posture: 'critical', postureReason: 'last-run-failed' }),
        job({ id: 'v-1', family: 'verify' }),
      ],
      'main',
    );
    expect(summary.state).toBe('critical');
    expect(summary.label).toBe('Sync failed');
    expect(describeDatastoreJobs(summary)).toBe('Sync failed · Prune late · Verify OK');
  });

  it('ignores other stores, disabled jobs and backup jobs', () => {
    const summary = summarizeDatastoreJobs(
      [
        job({ id: 's-other', family: 'sync', store: 'offsite', posture: 'critical' }),
        job({ id: 'v-off', family: 'verify', enabled: false, posture: 'critical' }),
        job({ id: 'b-1', family: 'backup', posture: 'critical' }),
      ],
      'main',
    );
    expect(summary).toEqual({ state: 'none', label: '—', entries: [] });
  });

  it('lets the newest run of a repeated job speak for it', () => {
    const summary = summarizeDatastoreJobs(
      [
        job({
          id: 'verify:v-1',
          posture: 'critical',
          postureReason: 'last-run-failed',
          'last-run-endtime': 1_000,
        }),
        job({ id: 'verify:v-1', 'last-run-endtime': 2_000 }),
      ],
      'main',
    );
    expect(summary.label).toBe('OK');
    expect(summary.entries).toHaveLength(1);
  });

  it('keeps families that share a job id apart', () => {
    const summary = summarizeDatastoreJobs(
      [
        job({ id: 'daily', family: 'sync', posture: 'critical', postureReason: 'last-run-failed' }),
        job({ id: 'daily', family: 'verify', 'last-run-endtime': 2_000 }),
      ],
      'main',
    );
    expect(summary.label).toBe('Sync failed');
    expect(summary.entries).toHaveLength(2);
  });

  it('never reports a family as OK when its server-wide read failed', () => {
    const summary = summarizeDatastoreJobs(
      [
        job({ id: 'v-1', family: 'verify' }),
        job({
          id: 'garbage:partial',
          family: 'garbage',
          store: undefined,
          posture: 'unknown',
          error: 'permission denied',
        }),
      ],
      'main',
    );
    expect(summary.state).toBe('unknown');
    expect(summary.label).toBe('GC unknown');
  });

  it('does not let storeless task history mark a family unread', () => {
    // A real PBS: configured prune jobs report OK, while bounded task history
    // also returns every past run with no store, one of them with an unclear
    // status. That history is not a failed read of the prune family.
    const summary = summarizeDatastoreJobs(
      [
        job({ id: 'prune-main-delly', family: 'prune' }),
        job({
          id: 'prune:main:delly',
          family: 'prune',
          store: undefined,
          confidence: 'task-history-only',
          evidenceScope: 'observed-task',
          posture: 'unknown',
          postureReason: 'last-run-state-unknown',
        }),
      ],
      'main',
    );
    expect(summary.state).toBe('ok');
    expect(summary.label).toBe('OK');
  });

  it('keeps a failed run from storeless task history visible', () => {
    const summary = summarizeDatastoreJobs(
      [
        job({ id: 'prune-main-delly', family: 'prune' }),
        job({
          id: 'prune:main:manual',
          family: 'prune',
          store: undefined,
          confidence: 'task-history-only',
          evidenceScope: 'observed-task',
          posture: 'critical',
          postureReason: 'last-run-failed',
        }),
      ],
      'main',
    );
    expect(summary.state).toBe('unknown');
    expect(summary.label).toBe('Prune unknown');
  });

  it('still treats a storeless partial read as an unread family', () => {
    const summary = summarizeDatastoreJobs(
      [
        job({ id: 'prune-main-delly', family: 'prune' }),
        job({
          id: 'prune:partial',
          family: 'prune',
          store: undefined,
          confidence: 'partial-permission',
          evidenceScope: 'partial-read',
          posture: 'unknown',
        }),
      ],
      'main',
    );
    expect(summary.state).toBe('unknown');
    expect(summary.label).toBe('Prune unknown');
  });

  it('names a job that has not run yet without calling it healthy', () => {
    const summary = summarizeDatastoreJobs(
      [job({ id: 'v-1', posture: 'unknown', postureReason: 'no-run-observed' })],
      'main',
    );
    expect(summary.state).toBe('unknown');
    expect(summary.label).toBe('Verify not run');
  });

  it('has nothing to say without a datastore or evidence', () => {
    expect(summarizeDatastoreJobs(undefined, 'main').label).toBe('—');
    expect(summarizeDatastoreJobs([job({})], undefined).label).toBe('—');
  });
});

describe('collectDatastoreJobFailures', () => {
  const server = (name: string, evidence: PBSJobHealthEvidence[]): Resource =>
    ({
      id: name,
      name,
      type: 'pbs',
      pbs: {
        datastores: [
          { name: 'main', total: 1, used: 0 },
          { name: 'offsite', total: 1, used: 0 },
        ],
        jobHealthEvidence: evidence,
      },
    }) as unknown as Resource;

  it('groups failed jobs by family and names the datastores they hit (#1815)', () => {
    const groups = collectDatastoreJobFailures([
      server('vault', [
        job({ id: 'b-1', family: 'backup', store: 'main' }),
        job({ id: 's-1', family: 'sync', store: 'offsite', posture: 'critical' }),
        job({ id: 'v-1', family: 'verify', store: 'offsite', posture: 'warning' }),
        job({ id: 'garbage:main', family: 'garbage', store: 'main', posture: 'critical' }),
      ]),
    ]);
    expect(groups).toEqual([
      { family: 'sync', label: 'Sync failed', datastores: ['offsite'] },
      { family: 'garbage', label: 'GC failed', datastores: ['main'] },
    ]);
  });

  it('leaves out storeless failures and a job whose newest run succeeded', () => {
    const groups = collectDatastoreJobFailures([
      server('vault', [
        job({ id: 'sync:task', family: 'sync', store: undefined, posture: 'critical' }),
        job({ id: 'v-1', posture: 'critical', 'last-run-endtime': 1_000 }),
        job({ id: 'v-1', 'last-run-endtime': 2_000 }),
      ]),
    ]);
    expect(groups).toEqual([]);
  });

  it('names the server once more than one is connected', () => {
    const failed = [job({ id: 's-1', family: 'sync', store: 'offsite', posture: 'critical' })];
    const groups = collectDatastoreJobFailures([server('vault', failed), server('dr', failed)]);
    expect(groups[0].datastores).toEqual(['offsite (dr)', 'offsite (vault)']);
  });
});