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.

2.0 KB
Raw Blame
package discovery

import (
	"testing"
)

func TestDiscoveryResultAddError_PopulatesStructuredAndBoundaryLegacy(t *testing.T) {
	t.Parallel()

	var r DiscoveryResult
	r.AddError("docker_bridge_network", "timeout", "request timed out", "192.168.1.10", 8006)

	if len(r.StructuredErrors) != 1 {
		t.Fatalf("StructuredErrors len = %d, want %d", len(r.StructuredErrors), 1)
	}
	se := r.StructuredErrors[0]
	if se.Phase != "docker_bridge_network" {
		t.Fatalf("StructuredErrors[0].Phase = %q, want %q", se.Phase, "docker_bridge_network")
	}
	if se.ErrorType != "timeout" {
		t.Fatalf("StructuredErrors[0].ErrorType = %q, want %q", se.ErrorType, "timeout")
	}
	if se.Message != "request timed out" {
		t.Fatalf("StructuredErrors[0].Message = %q, want %q", se.Message, "request timed out")
	}
	if se.IP != "192.168.1.10" || se.Port != 8006 {
		t.Fatalf("StructuredErrors[0] address = %s:%d, want %s:%d", se.IP, se.Port, "192.168.1.10", 8006)
	}
	if se.Timestamp.IsZero() {
		t.Fatalf("StructuredErrors[0].Timestamp should be set")
	}

	legacy := r.LegacyErrors()
	if len(legacy) != 1 {
		t.Fatalf("LegacyErrors len = %d, want %d", len(legacy), 1)
	}
	if legacy[0] != "Docker bridge network [192.168.1.10:8006]: request timed out" {
		t.Fatalf("LegacyErrors[0] = %q", legacy[0])
	}
}

func TestDiscoveryResultLegacyErrors_FormattingVariants(t *testing.T) {
	t.Parallel()

	t.Run("ip-only", func(t *testing.T) {
		var r DiscoveryResult
		r.AddError("extra_targets", "phase_error", "failed", "10.0.0.1", 0)
		legacy := r.LegacyErrors()
		if len(legacy) != 1 {
			t.Fatalf("LegacyErrors len = %d, want %d", len(legacy), 1)
		}
		if legacy[0] != "Additional targets [10.0.0.1]: failed" {
			t.Fatalf("LegacyErrors[0] = %q", legacy[0])
		}
	})

	t.Run("no-address", func(t *testing.T) {
		var r DiscoveryResult
		r.AddError("unknown_phase", "phase_error", "failed", "", 0)
		legacy := r.LegacyErrors()
		if len(legacy) != 1 {
			t.Fatalf("LegacyErrors len = %d, want %d", len(legacy), 1)
		}
		if legacy[0] != "unknown_phase: failed" {
			t.Fatalf("LegacyErrors[0] = %q", legacy[0])
		}
	})
}