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.

6.1 KB
Raw Blame
import { describe, expect, it } from 'vitest';
import type { ClusterEndpoint } from '@/types/nodes';
import {
  applyClusterEndpointOverridesLocally,
  applyClusterNodeDisplayNamesLocally,
  buildClusterEndpointOverridesPayload,
  buildClusterEndpointTrustOverridesPayload,
  buildClusterNodeDisplayNameOverridesPayload,
  PVE_MANUAL_PERMISSION_COMMAND,
} from '../nodeModalModel';

const endpoint = (nodeName: string, ipOverride?: string): ClusterEndpoint => ({
  nodeId: `node/${nodeName}`,
  nodeIdentity: `cluster-${nodeName}`,
  nodeName,
  host: `https://${nodeName}.local:8006`,
  ip: '10.0.0.1',
  ipOverride,
  online: true,
  lastSeen: '',
});

describe('cluster node display-name overrides', () => {
  it('writes immutable identities, permits duplicate labels, and sends an empty clear', () => {
    const endpoints = [{ ...endpoint('pve1'), displayName: 'Compute' }, endpoint('pve2')];
    expect(
      buildClusterNodeDisplayNameOverridesPayload(endpoints, {
        'cluster-pve1': '',
        'cluster-pve2': 'Compute',
      }),
    ).toEqual([
      { nodeIdentity: 'cluster-pve1', displayName: '' },
      { nodeIdentity: 'cluster-pve2', displayName: 'Compute' },
    ]);
  });

  it('patches the cached presentation without changing native names or addresses', () => {
    const endpoints = [endpoint('pve1')];
    const result = applyClusterNodeDisplayNamesLocally(endpoints, [
      { nodeIdentity: 'cluster-pve1', displayName: 'Render East' },
    ]);
    expect(result?.[0]).toMatchObject({
      nodeIdentity: 'cluster-pve1',
      nodeName: 'pve1',
      displayName: 'Render East',
      ip: '10.0.0.1',
    });
  });
});

describe('nodeModalModel', () => {
  it('prefers PVE 9 guest-agent privileges before legacy VM.Monitor', () => {
    expect(PVE_MANUAL_PERMISSION_COMMAND).toContain('VM.GuestAgent.Audit');
    expect(PVE_MANUAL_PERMISSION_COMMAND).toContain('VM.GuestAgent.FileRead');
    expect(PVE_MANUAL_PERMISSION_COMMAND).toContain('VM.Monitor');

    const guestBranch = PVE_MANUAL_PERMISSION_COMMAND.indexOf(
      'if [ "$HAS_GUEST_AGENT_AUDIT" = true ]; then',
    );
    const monitorBranch = PVE_MANUAL_PERMISSION_COMMAND.indexOf(
      'pveum role add PulseTmpVMMonitor -privs VM.Monitor',
    );

    expect(guestBranch).toBeGreaterThanOrEqual(0);
    expect(monitorBranch).toBeGreaterThanOrEqual(0);
    expect(guestBranch).toBeLessThan(monitorBranch);
  });
});

describe('buildClusterEndpointOverridesPayload', () => {
  it('includes only members whose value changed from the saved override', () => {
    const endpoints = [endpoint('pve1'), endpoint('pve2', '10.0.0.2')];
    const payload = buildClusterEndpointOverridesPayload(endpoints, {
      pve1: '10.0.0.11',
      pve2: '10.0.0.2',
    });
    expect(payload).toEqual([{ nodeName: 'pve1', ipOverride: '10.0.0.11' }]);
  });

  it('sends an empty value to clear a saved override', () => {
    const endpoints = [endpoint('pve2', '10.0.0.2')];
    const payload = buildClusterEndpointOverridesPayload(endpoints, { pve2: '' });
    expect(payload).toEqual([{ nodeName: 'pve2', ipOverride: '' }]);
  });

  it('returns undefined when nothing changed or there are no endpoints', () => {
    expect(buildClusterEndpointOverridesPayload([endpoint('pve1')], { pve1: '' })).toBeUndefined();
    expect(
      buildClusterEndpointOverridesPayload([endpoint('pve1')], { pve1: '   ' }),
    ).toBeUndefined();
    expect(buildClusterEndpointOverridesPayload([], { pve1: '10.0.0.1' })).toBeUndefined();
    expect(buildClusterEndpointOverridesPayload(undefined, {})).toBeUndefined();
  });

  it('ignores form entries for members no longer in the endpoint list', () => {
    const payload = buildClusterEndpointOverridesPayload([endpoint('pve1')], {
      removed: '10.0.0.9',
    });
    expect(payload).toBeUndefined();
  });
});

describe('applyClusterEndpointOverridesLocally', () => {
  it('patches named members and leaves the rest untouched', () => {
    const endpoints = [endpoint('pve1'), endpoint('pve2', '10.0.0.2')];
    const result = applyClusterEndpointOverridesLocally(endpoints, [
      { nodeName: 'pve1', ipOverride: '10.0.0.11' },
    ]);
    expect(result?.[0].ipOverride).toBe('10.0.0.11');
    expect(result?.[1]).toBe(endpoints[1]);
  });

  it('clears an override when the payload value is empty', () => {
    const result = applyClusterEndpointOverridesLocally(
      [endpoint('pve2', '10.0.0.2')],
      [{ nodeName: 'pve2', ipOverride: '' }],
    );
    expect(result?.[0].ipOverride).toBeUndefined();
  });

  it('passes endpoints through when there is nothing to apply', () => {
    const endpoints = [endpoint('pve1')];
    expect(applyClusterEndpointOverridesLocally(endpoints, undefined)).toBe(endpoints);
    expect(applyClusterEndpointOverridesLocally(undefined, [])).toBeUndefined();
  });
});

describe('endpoint-scoped TLS decisions', () => {
  it('does not copy the primary pin to peers or submit untouched trust', () => {
    const endpoints = [
      {
        ...endpoint('primary'),
        tlsEndpoint: 'https://primary.test:8006',
        usesPrimaryTLS: true,
        fingerprint: 'primary-pin',
      },
      { ...endpoint('peer'), tlsEndpoint: 'https://peer.test:8006', fingerprint: 'peer-pin' },
      { ...endpoint('other'), tlsEndpoint: 'https://other.test:8006', fingerprint: 'other-pin' },
    ];
    expect(buildClusterEndpointTrustOverridesPayload(endpoints, {})).toBeUndefined();
    expect(
      buildClusterEndpointTrustOverridesPayload(endpoints, {
        'https://primary.test:8006': 'new-primary',
        'https://peer.test:8006': '',
        'https://other.test:8006': 'other-pin',
      }),
    ).toEqual([{ endpoint: 'https://peer.test:8006', fingerprint: '' }]);
  });
  it('requires an explicit choice before turning unknown first-use trust into CA mode', () => {
    const peer = { ...endpoint('peer'), tlsEndpoint: 'https://peer.test:8006', tlsFirstUse: true };
    expect(
      buildClusterEndpointTrustOverridesPayload([peer], { [peer.tlsEndpoint]: '' }),
    ).toBeUndefined();
    expect(
      buildClusterEndpointTrustOverridesPayload(
        [peer],
        { [peer.tlsEndpoint]: '' },
        { [peer.tlsEndpoint]: true },
      ),
    ).toEqual([{ endpoint: peer.tlsEndpoint, fingerprint: '' }]);
  });
});