mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-30 17:42:32 +01:00
Canonical resource queries could return no backups because inventory compared raw guest IDs from provider caches. Read org-scoped recovery artifacts with shared identity, repository, verification and provenance fields instead. Preserve global pagination across providers and surface read failures. Remove the obsolete vendor summaries and unrelated recent-task cache from this read.
384 lines
12 KiB
Go
384 lines
12 KiB
Go
package tools
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/models"
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
|
|
)
|
|
|
|
type stubBackupProvider struct {
|
|
backups models.Backups
|
|
pbs []models.PBSInstance
|
|
}
|
|
|
|
func (s *stubBackupProvider) GetBackups() models.Backups {
|
|
return s.backups
|
|
}
|
|
|
|
func (s *stubBackupProvider) GetPBSInstances() []models.PBSInstance {
|
|
return s.pbs
|
|
}
|
|
|
|
type stubDiskHealthProvider struct {
|
|
hosts []*unifiedresources.HostView
|
|
}
|
|
|
|
type stubUpdatesProvider struct {
|
|
pending []ContainerUpdateInfo
|
|
enabled bool
|
|
triggerCalled bool
|
|
lastTriggerHost string
|
|
lastUpdateHost string
|
|
lastUpdateID string
|
|
lastUpdateName string
|
|
triggerStatus DockerCommandStatus
|
|
triggerErr error
|
|
updateStatus DockerCommandStatus
|
|
updateErr error
|
|
updateCheckEnabled bool
|
|
}
|
|
|
|
func (s *stubUpdatesProvider) GetPendingUpdates(hostID string) []ContainerUpdateInfo {
|
|
s.lastTriggerHost = hostID
|
|
return s.pending
|
|
}
|
|
|
|
func (s *stubUpdatesProvider) TriggerUpdateCheck(hostID string) (DockerCommandStatus, error) {
|
|
s.triggerCalled = true
|
|
s.lastTriggerHost = hostID
|
|
return s.triggerStatus, s.triggerErr
|
|
}
|
|
|
|
func (s *stubUpdatesProvider) UpdateContainer(hostID, containerID, containerName string) (DockerCommandStatus, error) {
|
|
s.lastUpdateHost = hostID
|
|
s.lastUpdateID = containerID
|
|
s.lastUpdateName = containerName
|
|
return s.updateStatus, s.updateErr
|
|
}
|
|
|
|
func (s *stubUpdatesProvider) GetCommandStatus(commandID string) (DockerCommandStatus, bool) {
|
|
if s.updateStatus.ID == commandID {
|
|
return s.updateStatus, true
|
|
}
|
|
return DockerCommandStatus{}, false
|
|
}
|
|
|
|
func (s *stubUpdatesProvider) IsUpdateActionsEnabled() bool {
|
|
return s.enabled
|
|
}
|
|
|
|
func TestExecuteListStorage(t *testing.T) {
|
|
executor := NewPulseToolExecutor(ExecutorConfig{StateProvider: &mockStateProvider{}})
|
|
// Storage pools and Ceph data come from the unified resource provider.
|
|
storageUsedBytes := int64(1024 * 1024 * 1024)
|
|
storageTotalBytes := int64(4 * 1024 * 1024 * 1024)
|
|
cephUsedBytes := int64(2 * 1024 * 1024 * 1024 * 1024)
|
|
cephTotalBytes := int64(4 * 1024 * 1024 * 1024 * 1024)
|
|
executor.unifiedResourceProvider = &stubUnifiedResourceProvider{
|
|
resources: []unifiedresources.Resource{
|
|
{
|
|
ID: "store1",
|
|
Name: "store1",
|
|
Type: unifiedresources.ResourceTypeStorage,
|
|
Status: unifiedresources.StatusOnline,
|
|
Proxmox: &unifiedresources.ProxmoxData{
|
|
NodeName: "node1",
|
|
Instance: "pve1",
|
|
},
|
|
Storage: &unifiedresources.StorageMeta{
|
|
Type: "zfs",
|
|
Content: "images",
|
|
Shared: false,
|
|
IsZFS: true,
|
|
},
|
|
Metrics: &unifiedresources.ResourceMetrics{
|
|
Disk: &unifiedresources.MetricValue{
|
|
Used: &storageUsedBytes,
|
|
Total: &storageTotalBytes,
|
|
Percent: 25.0,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Name: "ceph1",
|
|
Type: unifiedresources.ResourceTypeCeph,
|
|
Ceph: &unifiedresources.CephMeta{
|
|
HealthStatus: "HEALTH_OK",
|
|
HealthMessage: "ok",
|
|
NumOSDs: 3,
|
|
NumOSDsUp: 3,
|
|
NumOSDsIn: 3,
|
|
NumMons: 1,
|
|
NumMgrs: 1,
|
|
},
|
|
Metrics: &unifiedresources.ResourceMetrics{
|
|
Disk: &unifiedresources.MetricValue{
|
|
Used: &cephUsedBytes,
|
|
Total: &cephTotalBytes,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
result, _ := executor.executeListStorage(context.Background(), map[string]interface{}{})
|
|
var storageResp StorageResponse
|
|
if err := json.Unmarshal([]byte(result.Content[0].Text), &storageResp); err != nil {
|
|
t.Fatalf("decode storage response: %v", err)
|
|
}
|
|
if len(storageResp.Pools) != 1 {
|
|
t.Fatalf("unexpected storage pools: %+v", storageResp.Pools)
|
|
}
|
|
// Verify UsagePercent is passed through directly (not double-multiplied).
|
|
if storageResp.Pools[0].UsagePercent != 25.0 {
|
|
t.Errorf("storage pool UsagePercent = %v, want 25.0 (was double-multiplied before fix)", storageResp.Pools[0].UsagePercent)
|
|
}
|
|
if len(storageResp.CephClusters) != 1 || storageResp.CephClusters[0].UsedTB != 2 {
|
|
t.Fatalf("unexpected ceph clusters: %+v", storageResp.CephClusters)
|
|
}
|
|
}
|
|
|
|
func TestStorageUsagePercentNotDoubled(t *testing.T) {
|
|
// Regression test: Usage is already 0-100 from safePercentage().
|
|
// The bug was multiplying by 100 again, turning 25% into 2500%.
|
|
executor := NewPulseToolExecutor(ExecutorConfig{StateProvider: &mockStateProvider{}})
|
|
|
|
// Storage pool path
|
|
storageUsedBytes := int64(1024 * 1024 * 1024)
|
|
storageTotalBytes := int64(4 * 1024 * 1024 * 1024)
|
|
executor.unifiedResourceProvider = &stubUnifiedResourceProvider{
|
|
resources: []unifiedresources.Resource{
|
|
{
|
|
ID: "s1",
|
|
Name: "s1",
|
|
Type: unifiedresources.ResourceTypeStorage,
|
|
Status: unifiedresources.StatusOnline,
|
|
Storage: &unifiedresources.StorageMeta{
|
|
Type: "dir",
|
|
Content: "images",
|
|
},
|
|
Metrics: &unifiedresources.ResourceMetrics{
|
|
Disk: &unifiedresources.MetricValue{
|
|
Used: &storageUsedBytes,
|
|
Total: &storageTotalBytes,
|
|
Percent: 45.7,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
result2, _ := executor.executeListStorage(context.Background(), map[string]interface{}{})
|
|
var storageResp StorageResponse
|
|
if err := json.Unmarshal([]byte(result2.Content[0].Text), &storageResp); err != nil {
|
|
t.Fatalf("decode storage response: %v", err)
|
|
}
|
|
if len(storageResp.Pools) != 1 {
|
|
t.Fatalf("unexpected pools: %+v", storageResp.Pools)
|
|
}
|
|
got2 := storageResp.Pools[0].UsagePercent
|
|
if got2 != 45.7 {
|
|
t.Errorf("storage pool UsagePercent = %v, want 45.7 (double-multiplied = %v)", got2, 45.7*100)
|
|
}
|
|
}
|
|
|
|
func TestStoragePoolSummaryReadsStorageFacetFlags(t *testing.T) {
|
|
storage := func(enabled, active bool) *unifiedresources.StorageMeta {
|
|
return &unifiedresources.StorageMeta{Type: "dir", Enabled: enabled, Active: active}
|
|
}
|
|
cases := []struct {
|
|
name string
|
|
status unifiedresources.ResourceStatus
|
|
storage *unifiedresources.StorageMeta
|
|
wantEnabled bool
|
|
wantActive bool
|
|
listSkipsRow bool
|
|
}{
|
|
// Proxmox storage that reports active=0 is unavailable, not disabled.
|
|
{"unavailable storage stays enabled", unifiedresources.StatusOffline, storage(true, false), true, false, false},
|
|
{"disabled storage", unifiedresources.StatusWarning, storage(false, false), false, false, false},
|
|
{"online storage", unifiedresources.StatusOnline, storage(true, true), true, true, false},
|
|
// PBS datastores report Active even while unavailable.
|
|
{"offline row whose source reports active", unifiedresources.StatusOffline, storage(true, true), true, false, false},
|
|
{"no facet online", unifiedresources.StatusOnline, nil, true, true, true},
|
|
{"no facet offline", unifiedresources.StatusOffline, nil, false, false, true},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
resource := unifiedresources.Resource{
|
|
ID: "local-lvm",
|
|
Name: "local-lvm",
|
|
Type: unifiedresources.ResourceTypeStorage,
|
|
Status: tc.status,
|
|
Storage: tc.storage,
|
|
}
|
|
|
|
got := storagePoolSummaryFromResource(resource)
|
|
if got.Enabled != tc.wantEnabled || got.Active != tc.wantActive {
|
|
t.Fatalf("summary enabled/active = %v/%v, want %v/%v", got.Enabled, got.Active, tc.wantEnabled, tc.wantActive)
|
|
}
|
|
if tc.listSkipsRow {
|
|
return
|
|
}
|
|
|
|
executor := NewPulseToolExecutor(ExecutorConfig{StateProvider: &mockStateProvider{}})
|
|
executor.unifiedResourceProvider = &stubUnifiedResourceProvider{resources: []unifiedresources.Resource{resource}}
|
|
result, err := executor.executeListStorage(context.Background(), map[string]interface{}{})
|
|
if err != nil {
|
|
t.Fatalf("executeListStorage: %v", err)
|
|
}
|
|
var resp StorageResponse
|
|
if err := json.Unmarshal([]byte(result.Content[0].Text), &resp); err != nil {
|
|
t.Fatalf("decode storage response: %v", err)
|
|
}
|
|
if len(resp.Pools) != 1 {
|
|
t.Fatalf("pools = %+v, want one", resp.Pools)
|
|
}
|
|
if resp.Pools[0].Enabled != tc.wantEnabled || resp.Pools[0].Active != tc.wantActive {
|
|
t.Fatalf("listed enabled/active = %v/%v, want %v/%v", resp.Pools[0].Enabled, resp.Pools[0].Active, tc.wantEnabled, tc.wantActive)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestStorageResponsesUseCanonicalEmptyCollections(t *testing.T) {
|
|
backupsPayload, err := json.Marshal(EmptyBackupsResponse())
|
|
if err != nil {
|
|
t.Fatalf("marshal empty backups response: %v", err)
|
|
}
|
|
var backupsMap map[string]any
|
|
if err := json.Unmarshal(backupsPayload, &backupsMap); err != nil {
|
|
t.Fatalf("decode empty backups response: %v", err)
|
|
}
|
|
for _, key := range []string{"points"} {
|
|
values, ok := backupsMap[key].([]any)
|
|
if !ok || len(values) != 0 {
|
|
t.Fatalf("expected %s to serialize as an empty array, got %T (%v)", key, backupsMap[key], backupsMap[key])
|
|
}
|
|
}
|
|
|
|
storagePayload, err := json.Marshal(EmptyStorageResponse())
|
|
if err != nil {
|
|
t.Fatalf("marshal empty storage response: %v", err)
|
|
}
|
|
var storageMap map[string]any
|
|
if err := json.Unmarshal(storagePayload, &storageMap); err != nil {
|
|
t.Fatalf("decode empty storage response: %v", err)
|
|
}
|
|
for _, key := range []string{"pools", "ceph_clusters"} {
|
|
values, ok := storageMap[key].([]any)
|
|
if !ok || len(values) != 0 {
|
|
t.Fatalf("expected %s to serialize as an empty array, got %T (%v)", key, storageMap[key], storageMap[key])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStorageDiagnosticsResponsesUseCanonicalEmptyCollections(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
raw any
|
|
keys []string
|
|
}{
|
|
{name: "host_raid", raw: EmptyHostRAIDStatusResponse(), keys: []string{"hosts"}},
|
|
{name: "host_ceph", raw: EmptyHostCephDetailsResponse(), keys: []string{"hosts"}},
|
|
{name: "resource_disks", raw: EmptyResourceDisksResponse(), keys: []string{"resources"}},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
payload, err := json.Marshal(tc.raw)
|
|
if err != nil {
|
|
t.Fatalf("marshal %s response: %v", tc.name, err)
|
|
}
|
|
|
|
var decoded map[string]any
|
|
if err := json.Unmarshal(payload, &decoded); err != nil {
|
|
t.Fatalf("decode %s response: %v", tc.name, err)
|
|
}
|
|
|
|
for _, key := range tc.keys {
|
|
values, ok := decoded[key].([]any)
|
|
if !ok || len(values) != 0 {
|
|
t.Fatalf("expected %s.%s to be an empty array, got %T (%v)", tc.name, key, decoded[key], decoded[key])
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDockerUpdateTools(t *testing.T) {
|
|
state := models.StateSnapshot{
|
|
DockerHosts: []models.DockerHost{
|
|
{
|
|
ID: "host1",
|
|
Hostname: "docker1",
|
|
DisplayName: "Docker One",
|
|
Containers: []models.DockerContainer{
|
|
{ID: "c1", Name: "/nginx"},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
stateProv := &mockStateProvider{}
|
|
stateProv.On("ReadSnapshot").Return(state)
|
|
executor := NewPulseToolExecutor(ExecutorConfig{
|
|
StateProvider: stateProv,
|
|
ControlLevel: ControlLevelControlled,
|
|
})
|
|
updates := &stubUpdatesProvider{
|
|
pending: []ContainerUpdateInfo{
|
|
{TargetID: "host1", ContainerID: "c1", ContainerName: "nginx", UpdateAvailable: true},
|
|
},
|
|
enabled: true,
|
|
triggerStatus: DockerCommandStatus{
|
|
ID: "cmd1",
|
|
Type: "check",
|
|
Status: "queued",
|
|
},
|
|
updateStatus: DockerCommandStatus{
|
|
ID: "cmd2",
|
|
Type: "update",
|
|
Status: "queued",
|
|
},
|
|
}
|
|
executor.updatesProvider = updates
|
|
|
|
result, _ := executor.executeListDockerUpdates(context.Background(), map[string]interface{}{"host": "Docker One"})
|
|
var listResp DockerUpdatesResponse
|
|
if err := json.Unmarshal([]byte(result.Content[0].Text), &listResp); err != nil {
|
|
t.Fatalf("decode docker updates: %v", err)
|
|
}
|
|
if listResp.TargetID != "host1" || listResp.Total != 1 {
|
|
t.Fatalf("unexpected docker updates response: %+v", listResp)
|
|
}
|
|
|
|
result, _ = executor.executeCheckDockerUpdates(context.Background(), map[string]interface{}{"host": "Docker One"})
|
|
var checkResp DockerCheckUpdatesResponse
|
|
if err := json.Unmarshal([]byte(result.Content[0].Text), &checkResp); err != nil {
|
|
t.Fatalf("decode docker check response: %v", err)
|
|
}
|
|
if checkResp.CommandID != "cmd1" || checkResp.TargetID != "host1" {
|
|
t.Fatalf("unexpected check response: %+v", checkResp)
|
|
}
|
|
}
|
|
|
|
func TestDockerUpdatesResponseUsesCanonicalEmptyCollections(t *testing.T) {
|
|
payload, err := json.Marshal(EmptyDockerUpdatesResponse())
|
|
if err != nil {
|
|
t.Fatalf("marshal docker updates response: %v", err)
|
|
}
|
|
|
|
var decoded map[string]any
|
|
if err := json.Unmarshal(payload, &decoded); err != nil {
|
|
t.Fatalf("decode docker updates response: %v", err)
|
|
}
|
|
|
|
updates, ok := decoded["updates"].([]any)
|
|
if !ok || len(updates) != 0 {
|
|
t.Fatalf("expected updates to be an empty array, got %T (%v)", decoded["updates"], decoded["updates"])
|
|
}
|
|
}
|