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.

11.6 KB
Raw Blame
import { describe, expect, it } from 'vitest';
import type { Resource } from '@/types/resource';
import {
  PROXMOX_TAB_SPECS,
  buildProxmoxPageModel,
  buildVisibleProxmoxTabSpecs,
  filterProxmoxNodes,
  getResourceVersion,
  resolveProxmoxPlatformScope,
} from '../proxmoxPageModel';

const makeResource = (resource: Partial<Resource> & Pick<Resource, 'id' | 'type'>): Resource => ({
  name: resource.id,
  displayName: resource.id,
  platformId: 'lab',
  platformType: 'proxmox-pve',
  sourceType: 'api',
  status: 'online',
  lastSeen: 1_700_000_000_000,
  ...resource,
});

describe('proxmoxPageModel', () => {
  it('does not infer PVE membership from a generic NAS sharing cluster labels', () => {
    // #1930 boundary check, not a reproduction of the reporter's API payload.
    // A missing PVE version must not hide a real node either.
    const node = makeResource({
      id: 'pve-node',
      type: 'agent',
      proxmox: { nodeName: 'pve-node', clusterName: 'test-cluster' },
    });
    const nas = makeResource({
      id: 'synthetic-nas',
      type: 'agent',
      platformType: 'generic',
      sourceType: 'agent',
      platformId: node.platformId,
      clusterId: 'test-cluster',
      identity: { hostname: 'synthetic-nas', clusterName: 'test-cluster' },
      sources: ['host-agent'],
    });

    expect(resolveProxmoxPlatformScope(nas)).toBeNull();
    expect(getResourceVersion(node)).toBe('');
    const model = buildProxmoxPageModel([nas, node]);
    expect(model.pveNodes).toEqual([node]);
    expect(model.resources).toEqual([node]);
    expect(model.clusterGroups).toHaveLength(1);
    expect(model.clusterGroups[0].nodes).toEqual([node]);
    expect(model.summary.nodeCount).toBe(1);
  });

  it('keeps the platform-native section set aligned with the legacy Proxmox workspace', () => {
    expect(PROXMOX_TAB_SPECS.map((tab) => tab.id)).toEqual([
      'overview',
      'storage',
      'replication',
      'backups',
      'ceph',
      'mail',
    ]);
  });

  it('builds a Proxmox-first estate model from canonical v6 resources', () => {
    const model = buildProxmoxPageModel([
      makeResource({
        id: 'pve-node-1',
        type: 'agent',
        displayName: 'pve-node-1',
        proxmox: { nodeName: 'pve-node-1', clusterName: 'alpha' },
      }),
      makeResource({
        id: 'vm-101',
        type: 'vm',
        displayName: 'database',
        parentName: 'pve-node-1',
        status: 'running',
        proxmox: { vmid: 101, nodeName: 'pve-node-1' },
        platformData: {
          proxmox: { lastBackup: 1_700_000_000_000 },
        },
      }),
      makeResource({
        id: 'local-zfs',
        type: 'storage',
        displayName: 'local-zfs',
        parentName: 'pve-node-1',
        storage: { type: 'zfs', isCeph: false },
      }),
      makeResource({
        id: 'ceph-alpha',
        type: 'ceph',
        displayName: 'ceph-alpha',
        platformData: { ceph: { healthStatus: 'healthy' } },
      }),
      makeResource({
        id: 'pbs-main',
        type: 'pbs',
        displayName: 'pbs-main',
        platformType: 'proxmox-pbs',
        pbs: { version: '3.2' },
      }),
      makeResource({
        id: 'pmg-main',
        type: 'pmg',
        displayName: 'pmg-main',
        platformType: 'proxmox-pmg',
        platformData: { pmg: { version: '8.1' } },
      }),
      makeResource({
        id: 'docker-host',
        type: 'docker-host',
        displayName: 'docker-host',
        platformType: 'docker',
      }),
    ]);

    expect(model.summary).toMatchObject({
      clusterCount: 1,
      nodeCount: 1,
      guestCount: 1,
      runningGuestCount: 1,
      degradedGuestCount: 0,
      stoppedGuestCount: 0,
      storageCount: 1,
      pbsCount: 1,
      pmgCount: 1,
      cephCount: 1,
    });
    expect(model.clusterGroups).toHaveLength(1);
    expect(model.clusterGroups[0]).toMatchObject({
      label: 'alpha',
      nodes: [expect.objectContaining({ id: 'pve-node-1' })],
      guests: [expect.objectContaining({ id: 'vm-101' })],
      storage: [expect.objectContaining({ id: 'local-zfs' })],
    });
    expect(model.resources.map((resource) => resource.id)).not.toContain('docker-host');
    // Replication is gated on the fetched job count, not on anything in the
    // resource model: the jobs bypass the unified-resource pipeline.
    expect(buildVisibleProxmoxTabSpecs(model, 1).map((tab) => tab.id)).toEqual([
      'overview',
      'storage',
      'replication',
      'backups',
      'ceph',
      'mail',
    ]);
  });

  it('hides Replication for a PVE estate without replication jobs', () => {
    const model = buildProxmoxPageModel([
      makeResource({
        id: 'pve-node-1',
        type: 'agent',
        displayName: 'pve-node-1',
        proxmox: { nodeName: 'pve-node-1', clusterName: 'alpha' },
      }),
      makeResource({
        id: 'vm-101',
        type: 'vm',
        displayName: 'database',
        parentName: 'pve-node-1',
        status: 'running',
        proxmox: { vmid: 101, nodeName: 'pve-node-1' },
      }),
    ]);

    expect(buildVisibleProxmoxTabSpecs(model, 0).map((tab) => tab.id)).toEqual(['overview']);
  });

  it('keeps same-named clusters and nodes isolated by provider identity', () => {
    const model = buildProxmoxPageModel([
      makeResource({
        id: 'site-a-node',
        type: 'agent',
        platformId: 'site-a',
        proxmox: { instance: 'site-a', nodeName: 'pve-1', clusterName: 'production' },
      }),
      makeResource({
        id: 'site-b-node',
        type: 'agent',
        platformId: 'site-b',
        proxmox: { instance: 'site-b', nodeName: 'pve-1', clusterName: 'production' },
      }),
      makeResource({
        id: 'site-a-vm',
        type: 'vm',
        platformId: 'site-a',
        proxmox: { instance: 'site-a', nodeName: 'pve-1', vmid: 101 },
      }),
      makeResource({
        id: 'site-b-vm',
        type: 'vm',
        platformId: 'site-b',
        proxmox: { instance: 'site-b', nodeName: 'pve-1', vmid: 101 },
      }),
    ]);

    expect(model.summary.clusterCount).toBe(2);
    expect(model.clusterGroups).toHaveLength(2);
    expect(model.clusterGroups.map((group) => group.label)).toEqual(['production', 'production']);
    expect(
      model.clusterGroups.map((group) => ({
        id: group.id,
        nodes: group.nodes.map((node) => node.id),
        guests: group.guests.map((guest) => guest.id),
      })),
    ).toEqual([
      { id: 'site-a::production', nodes: ['site-a-node'], guests: ['site-a-vm'] },
      { id: 'site-b::production', nodes: ['site-b-node'], guests: ['site-b-vm'] },
    ]);
  });

  it('resolves Proxmox suite scope from canonical platform hints', () => {
    expect(resolveProxmoxPlatformScope(makeResource({ id: 'pbs', type: 'pbs' }))).toBe(
      'proxmox-pbs',
    );
    expect(resolveProxmoxPlatformScope(makeResource({ id: 'pmg', type: 'pmg' }))).toBe(
      'proxmox-pmg',
    );
    expect(
      resolveProxmoxPlatformScope(
        makeResource({ id: 'pve-source', type: 'agent', sources: ['proxmox'] }),
      ),
    ).toBe('proxmox-pve');
  });

  it('surfaces compact PVE versions from canonical Proxmox resource metadata', () => {
    expect(
      getResourceVersion(
        makeResource({
          id: 'pve-node',
          type: 'agent',
          proxmox: { pveVersion: 'pve-manager/9.1.9/ee7bad0a3d1546c9' },
        }),
      ),
    ).toBe('9.1.9');

    expect(
      getResourceVersion(
        makeResource({
          id: 'pve-node-platform-data',
          type: 'agent',
          platformData: {
            proxmox: { pveVersion: 'pve-manager/8.3.3/bbba1c53a1a65b24' },
          },
        }),
      ),
    ).toBe('8.3.3');
  });

  it('filters nodes by native identity, cluster, aliases, and matching guests', () => {
    const nodes = [
      makeResource({
        id: 'friendly-east',
        type: 'agent',
        displayName: 'East estate',
        platformId: 'site-east',
        proxmox: {
          nodeName: 'pve-east-01',
          nodeAliases: ['old-east-name'],
          clusterName: 'production',
        },
      }),
      makeResource({
        id: 'friendly-west',
        type: 'agent',
        displayName: 'West estate',
        platformId: 'site-west',
        proxmox: { nodeName: 'pve-west-01', clusterName: 'production' },
      }),
    ];
    const guests = [
      makeResource({
        id: 'vm-database',
        type: 'vm',
        displayName: 'database-primary',
        parentName: 'pve-east-01',
        proxmox: { nodeName: 'pve-east-01', instance: 'site-east', vmid: 101 },
      }),
    ];

    expect(filterProxmoxNodes(nodes, guests, 'pve-east-01').map((node) => node.id)).toEqual([
      'friendly-east',
    ]);
    expect(filterProxmoxNodes(nodes, guests, 'old-east-name').map((node) => node.id)).toEqual([
      'friendly-east',
    ]);
    expect(filterProxmoxNodes(nodes, guests, 'database-primary').map((node) => node.id)).toEqual([
      'friendly-east',
    ]);
    expect(filterProxmoxNodes(nodes, guests, 'pve-east-01, pve-west-01')).toHaveLength(2);
  });

  it('preserves exclusion search semantics and does not cross provider boundaries', () => {
    const nodes = [
      makeResource({
        id: 'site-a-node',
        type: 'agent',
        platformId: 'site-a',
        proxmox: { instance: 'site-a', nodeName: 'pve-1', clusterName: 'production' },
      }),
      makeResource({
        id: 'site-b-node',
        type: 'agent',
        platformId: 'site-b',
        proxmox: { instance: 'site-b', nodeName: 'pve-1', clusterName: 'production' },
      }),
    ];
    const guests = [
      makeResource({
        id: 'site-b-vm',
        type: 'vm',
        displayName: 'unique-west-guest',
        platformId: 'site-b',
        proxmox: { instance: 'site-b', nodeName: 'pve-1', vmid: 202 },
      }),
    ];

    expect(
      filterProxmoxNodes(nodes, guests, 'pve-1 -unique-west-guest').map((node) => node.id),
    ).toEqual(['site-a-node']);
    expect(filterProxmoxNodes(nodes, guests, 'unique-west-guest').map((node) => node.id)).toEqual([
      'site-b-node',
    ]);
  });

  it('does not retain nodes whose only match is an opaque resource id', () => {
    const nodes = [
      makeResource({
        id: 'agent-node-315-hidden',
        type: 'agent',
        name: 'Branch Services D',
        displayName: 'Branch Services D',
        proxmox: { instance: 'branch-services', nodeName: 'pve46' },
      }),
      makeResource({
        id: 'analytics-a',
        type: 'agent',
        name: 'Analytics A',
        displayName: 'Analytics A',
        proxmox: { instance: 'analytics', nodeName: 'pve31' },
      }),
      makeResource({
        id: 'production-east-e',
        type: 'agent',
        name: 'East Production E',
        displayName: 'East Production E',
        proxmox: { instance: 'production-east', nodeName: 'pve11' },
      }),
    ];
    const guests = [
      makeResource({
        id: 'system-container-bbf38a3159484caf',
        type: 'system-container',
        name: 'auth-service-218',
        displayName: 'auth-service-218',
        proxmox: { instance: 'branch-services', nodeName: 'pve46', vmid: 467 },
      }),
      makeResource({
        id: 'system-container-visible-name',
        type: 'system-container',
        name: 'billing-worker-315',
        displayName: 'billing-worker-315',
        proxmox: { instance: 'analytics', nodeName: 'pve31', vmid: 680 },
      }),
      makeResource({
        id: 'system-container-visible-vmid',
        type: 'system-container',
        name: 'reporting-api-124',
        displayName: 'reporting-api-124',
        proxmox: { instance: 'production-east', nodeName: 'pve11', vmid: 315 },
      }),
    ];

    expect(filterProxmoxNodes(nodes, guests, '315').map((node) => node.id)).toEqual([
      'analytics-a',
      'production-east-e',
    ]);
  });
});