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.

1.9 KB
Raw Blame
package proxmox

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
	"time"
)

func TestClusterClient_GetNodePendingUpdates_NoHealthyNodes(t *testing.T) {
	cc := &ClusterClient{
		name:       "test",
		endpoints:  []string{"node1", "node2"},
		nodeHealth: map[string]bool{"node1": false, "node2": false},
		lastError:  make(map[string]string),
		lastHealthCheck: map[string]time.Time{
			"node1": time.Now(),
			"node2": time.Now(),
		},
		rateLimitUntil: make(map[string]time.Time),
	}

	updates, err := cc.GetNodePendingUpdates(context.Background(), "node1")
	if err == nil {
		t.Fatal("expected unavailable cluster error to remain distinguishable from a confirmed empty update list")
	}
	if len(updates) != 0 {
		t.Fatalf("expected empty updates on no healthy nodes, got %+v", updates)
	}
}

func TestClusterClient_GetVMAgentVersion_QemuGA(t *testing.T) {
	isolateGuestAgentCapacity(t)
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Guest reads require fresh operation-lock evidence, independently of their payload fixture.
		if strings.HasSuffix(r.URL.Path, "/config") {
			fmt.Fprint(w, `{"data":{}}`)
			return
		}
		w.Header().Set("Content-Type", "application/json")
		if r.URL.Path == "/api2/json/nodes" {
			fmt.Fprint(w, `{"data":[{"node":"node1","status":"online"}]}`)
			return
		}
		if r.URL.Path == "/api2/json/nodes/node1/qemu/100/agent/info" {
			fmt.Fprint(w, `{"data":{"result":{"qemu-ga":{"version":"7.2.0"}}}}`)
			return
		}
		w.WriteHeader(http.StatusNotFound)
	}))
	defer server.Close()

	cfg := ClientConfig{Host: server.URL, TokenName: "u@p!t", TokenValue: "v"}
	cc := NewClusterClient("test", cfg, []string{server.URL}, nil)

	version, err := cc.GetVMAgentVersion(context.Background(), "node1", 100)
	if err != nil {
		t.Fatalf("GetVMAgentVersion failed: %v", err)
	}
	if version != "7.2.0" {
		t.Fatalf("unexpected version: %s", version)
	}
}