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.4 KB
Raw Blame
import { describe, expect, it } from 'vitest';
import type { Node } from '@/types/api';
import type { WorkloadGuest } from '@/types/workloads';
import {
  buildGuestParentNodeMap,
  buildNodeByInstance,
  getWorkloadContainerHostId,
  getKubernetesContextKey,
  getWorkloadAlertPolicyTags,
  getWorkloadDockerHostId,
  workloadNodeScopeId,
} from '@/components/Workloads/workloadTopology';

const makeGuest = (i: number, overrides?: Partial<WorkloadGuest>): WorkloadGuest => ({
  id: `guest-${i}`,
  vmid: 100 + i,
  name: `workload-${i}`,
  node: `node-${i % 5}`,
  instance: `cluster-${i % 3}`,
  status: i % 7 === 0 ? 'stopped' : 'running',
  type: i % 4 === 0 ? 'lxc' : i % 3 === 0 ? 'app-container' : 'vm',
  cpu: (i % 100) / 100,
  cpus: 2,
  memory: { total: 4096, used: ((i % 80) / 100) * 4096, free: 0, usage: (i % 80) / 100 },
  disk: { total: 102400, used: ((i % 60) / 100) * 102400, free: 0, usage: (i % 60) / 100 },
  networkIn: i * 100,
  networkOut: i * 50,
  diskRead: i * 10,
  diskWrite: i * 5,
  uptime: i * 3600,
  template: false,
  lastBackup: 0,
  tags: [],
  lock: '',
  lastSeen: new Date().toISOString(),
  workloadType: (i % 4 === 0 ? 'lxc' : i % 3 === 0 ? 'app-container' : 'vm') as any,
  ...overrides,
});

const makeNode = (id: string, instance: string, name: string): Node => ({
  id,
  name,
  displayName: name,
  instance,
  host: `${name}.local`,
  status: 'online',
  type: 'pve',
  cpu: 0,
  memory: { total: 1, used: 0, free: 1, usage: 0 },
  disk: { total: 1, used: 0, free: 1, usage: 0 },
  uptime: 1,
  loadAverage: [0, 0, 0],
  kernelVersion: 'test',
  pveVersion: 'test',
  cpuInfo: { model: 'test', cores: 1, sockets: 1, mhz: '1' },
  lastSeen: new Date().toISOString(),
  connectionHealth: 'online',
});

describe('workloadTopology', () => {
  describe('buildNodeByInstance and buildGuestParentNodeMap', () => {
    it('maps unique native IDs without aliasing duplicate instance/node tuples', () => {
      const nodeA = makeNode('cluster-a-node-a', 'cluster-a', 'node-a');
      const nodeAAlt = makeNode('custom-node-id', 'cluster-a', 'node-a');
      const nodeB = makeNode('cluster-b-node-b', 'cluster-b', 'node-b');

      const map = buildNodeByInstance([nodeA, nodeAAlt, nodeB]);

      expect(map['cluster-a-node-a']).toBe(nodeA);
      expect(map['custom-node-id']).toBe(nodeAAlt);
      expect(map['cluster-a-node-a']).toBe(nodeA);
      expect(map['cluster-b-node-b']).toBe(nodeB);
    });

    it('maps each supplied guest ID to its current provider parent', () => {
      const nodeA = makeNode('cluster-a-node-a', 'cluster-a', 'node-a');
      const nodeB = makeNode('cluster-b-node-b', 'cluster-b', 'node-b');
      const nodeMap = buildNodeByInstance([nodeA, nodeB]);

      const guestWithIdLookup = makeGuest(1, {
        id: 'cluster-a-node-a-101',
        vmid: 101,
        type: 'vm',
        workloadType: 'vm',
        instance: 'cluster-a',
        node: 'node-a',
      });
      const guestWithFallback = makeGuest(2, {
        id: 'unmatched-id',
        vmid: 102,
        type: 'vm',
        workloadType: 'vm',
        instance: 'cluster-b',
        node: 'node-b',
      });
      const guestWithoutParent = makeGuest(3, {
        id: 'unknown-103',
        vmid: 103,
        type: 'vm',
        workloadType: 'vm',
        instance: 'cluster-c',
        node: 'node-c',
      });

      const mapping = buildGuestParentNodeMap(
        [guestWithIdLookup, guestWithFallback, guestWithoutParent],
        nodeMap,
      );

      expect(mapping[guestWithIdLookup.id]).toBe(nodeA);
      expect(mapping[guestWithFallback.id]).toBe(nodeB);
      expect(mapping[guestWithoutParent.id]).toBeUndefined();
    });
  });

  describe('workload identity helpers', () => {
    it('builds an encoded instance/node scope with trimming', () => {
      const guest = makeGuest(1, { instance: ' cluster-a ', node: ' node-a ' });
      expect(workloadNodeScopeId(guest)).toBe('node|cluster-a|node-a');
    });

    it('returns first non-empty kubernetes context candidate', () => {
      const guest = makeGuest(1, {
        contextLabel: ' ',
        instance: 'cluster-a',
        node: 'worker-a',
        namespace: 'default',
      });
      expect(getKubernetesContextKey(guest)).toBe('cluster-a');
    });
  });

  describe('workload action IDs', () => {
    it('keeps Docker action hosts explicit and uses broader host fallback for container hosts', () => {
      const dockerWithHostId = makeGuest(1, {
        type: 'app-container',
        workloadType: 'app-container',
        platformType: 'docker',
        dockerHostId: 'docker-host-1',
        node: 'node-a',
        instance: 'inst-a',
      });
      const dockerFallback = makeGuest(2, {
        type: 'app-container',
        workloadType: 'app-container',
        platformType: 'docker',
        dockerHostId: '',
        node: 'node-b',
        instance: 'inst-b',
      });
      const truenasApp = makeGuest(3, {
        type: 'app-container',
        workloadType: 'app-container',
        platformType: 'truenas',
        dockerHostId: '',
        node: 'truenas-main',
        instance: 'truenas-main',
      });
      const appContainerWithHostLabel = makeGuest(4, {
        type: 'app-container',
        workloadType: 'app-container',
        platformType: 'docker',
        dockerHostId: '',
        contextLabel: 'tower.local',
        node: 'node-c',
        instance: 'inst-c',
      });
      expect(getWorkloadDockerHostId(dockerWithHostId)).toBe('docker-host-1');
      expect(getWorkloadDockerHostId(dockerFallback)).toBe('');
      expect(getWorkloadDockerHostId(truenasApp)).toBe('');
      expect(getWorkloadContainerHostId(dockerFallback)).toBe('node-b');
      expect(getWorkloadContainerHostId(truenasApp)).toBe('truenas-main');
      expect(getWorkloadContainerHostId(appContainerWithHostLabel)).toBe('tower.local');
    });
  });

  describe('alert policy tags', () => {
    it('passes only Proxmox guest tags to the threshold resolver', () => {
      const tags = ['batch', 'pulse-relaxed'];
      const proxmoxVm = makeGuest(1, {
        type: 'vm',
        workloadType: 'vm',
        platformType: 'proxmox-pve',
        tags,
      });
      const proxmoxLxc = makeGuest(2, {
        type: 'lxc',
        workloadType: 'system-container',
        platformScopes: ['proxmox-pve', 'agent'],
        tags,
      });
      const legacyVm = makeGuest(3, { type: 'vm', workloadType: 'vm', tags });
      const vsphereVm = makeGuest(4, {
        type: 'vm',
        workloadType: 'vm',
        platformType: 'vmware-vsphere',
        tags,
      });
      const appContainer = makeGuest(5, {
        type: 'app-container',
        workloadType: 'app-container',
        platformType: 'docker',
        tags,
      });

      expect(getWorkloadAlertPolicyTags(proxmoxVm)).toEqual(tags);
      expect(getWorkloadAlertPolicyTags(proxmoxLxc)).toEqual(tags);
      expect(getWorkloadAlertPolicyTags(legacyVm)).toEqual(tags);
      // The alert engine never reads Pulse control tags from these.
      expect(getWorkloadAlertPolicyTags(vsphereVm)).toEqual([]);
      expect(getWorkloadAlertPolicyTags(appContainer)).toEqual([]);
    });

    it('splits a legacy raw tag string', () => {
      const guest = makeGuest(6, {
        type: 'vm',
        workloadType: 'vm',
        tags: 'batch;pulse-relaxed' as unknown as string[],
      });
      expect(getWorkloadAlertPolicyTags(guest)).toEqual(['batch', 'pulse-relaxed']);
      expect(getWorkloadAlertPolicyTags(makeGuest(7, { type: 'vm', tags: null as never }))).toEqual(
        [],
      );
    });
  });
});