mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-29 01:08:56 +01:00
pulse_control only plans typed resource actions into Pulse Actions (executeControlResource -> typedActionPlanner). The handlers that sat behind the retired model-authored command, file and container paths stayed in the tree with no production caller: run command, guest control, app-container control, Docker control and container update, Kubernetes scale/restart/delete-pod/exec, and file append/write. registerFileTools was never called either, so pulse_file_edit is not registered at all; calls to it are denied by the mutation registry before any tool lookup. A whole-program reachability run over cmd/... lists each of them as unreachable, and so is everything only they used: approval-record and preflight authoring, the command and native-action audit lanes with their remediation-lock gate and plan-hash helpers, the Docker host resolvers, and the file write verification. The executor's autonomous flag, whose only readers were those handlers, goes with them (the executor's SetAutonomousMode and the flag argument of SetContext). Delete them with their tests and the constants, types and test helpers they stranded. The contract prose that described the removed broker now says that plan freshness and the operator remediation lock are enforced by internal/actionlifecycle. Two source pins that named deleted code go (the publishActionCompleted dispatch sites and the pulse_file_edit declaration), the standards tests drop their expectations of an ActionPlan construction in action_audit.go and of the legacy app_container rejection in the deleted file-edit test, and the RA35 evidence entry for the deleted run-command test goes with them. Left alone on purpose: the saved autonomous control level and its entitlement and approval-admission checks, which stay as legacy compatibility, Service.SetAutonomousMode and the chat clamp, SetOnActionCompleted and its router wiring (no dispatcher is left, the contract prose says so), and the protected-guests plumbing. Rebuilt on current main: the first version had 213 commits of drift. Two conflicts, both on code this change deletes. tools_control.go takes the deletion over the Protected guests cleanup that landed inside executeRunCommand. action_audit_remediation_lock_test.go goes with the audit lanes, including the test added for the dispatch gate's read through a succession; that gate is deleted here, and TestLockMovedBySuccessionStillRefusesTheIDThePreviousGenerationLists in internal/actionlifecycle covers the same refusal on the live path.
478 lines
15 KiB
Go
478 lines
15 KiB
Go
package tools
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
"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 TestExecuteListBackupsAndStorage(t *testing.T) {
|
|
executor := NewPulseToolExecutor(ExecutorConfig{StateProvider: &mockStateProvider{}})
|
|
executor.backupProvider = &stubBackupProvider{
|
|
backups: models.Backups{
|
|
PBS: []models.PBSBackup{
|
|
{
|
|
VMID: "100",
|
|
BackupType: "vm",
|
|
BackupTime: time.Unix(1000, 0),
|
|
Instance: "pbs1",
|
|
Datastore: "ds1",
|
|
Size: 1024 * 1024 * 1024,
|
|
Verified: true,
|
|
Protected: true,
|
|
},
|
|
},
|
|
PVE: models.PVEBackups{
|
|
StorageBackups: []models.StorageBackup{
|
|
{
|
|
VMID: 101,
|
|
Time: time.Unix(1100, 0),
|
|
Size: 2 * 1024 * 1024 * 1024,
|
|
Storage: "local",
|
|
},
|
|
},
|
|
BackupTasks: []models.BackupTask{
|
|
{
|
|
VMID: 101,
|
|
Node: "node1",
|
|
Status: "OK",
|
|
StartTime: time.Unix(1200, 0),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
pbs: []models.PBSInstance{
|
|
{
|
|
Name: "pbs1",
|
|
Host: "10.0.0.1",
|
|
Status: "online",
|
|
Datastores: []models.PBSDatastore{
|
|
{
|
|
Name: "ds1",
|
|
Usage: 50.0,
|
|
Free: 1024 * 1024 * 1024,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
result, _ := executor.executeListBackups(context.Background(), map[string]interface{}{})
|
|
var backupsResp BackupsResponse
|
|
if err := json.Unmarshal([]byte(result.Content[0].Text), &backupsResp); err != nil {
|
|
t.Fatalf("decode backups response: %v", err)
|
|
}
|
|
if len(backupsResp.PBS) != 1 || backupsResp.PBS[0].SizeGB != 1 {
|
|
t.Fatalf("unexpected PBS backups: %+v", backupsResp.PBS)
|
|
}
|
|
if len(backupsResp.PVE) != 1 || backupsResp.PVE[0].SizeGB != 2 {
|
|
t.Fatalf("unexpected PVE backups: %+v", backupsResp.PVE)
|
|
}
|
|
if len(backupsResp.PBSServers) != 1 || len(backupsResp.PBSServers[0].Datastores) != 1 {
|
|
t.Fatalf("unexpected PBS servers: %+v", backupsResp.PBSServers)
|
|
}
|
|
if len(backupsResp.RecentTasks) != 1 {
|
|
t.Fatalf("unexpected recent tasks: %+v", backupsResp.RecentTasks)
|
|
}
|
|
|
|
// 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{}})
|
|
|
|
// PBS datastore path
|
|
executor.backupProvider = &stubBackupProvider{
|
|
pbs: []models.PBSInstance{
|
|
{
|
|
Name: "pbs1",
|
|
Host: "10.0.0.1",
|
|
Status: "online",
|
|
Datastores: []models.PBSDatastore{
|
|
{Name: "ds1", Usage: 12.3, Free: 1024 * 1024 * 1024},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
result, _ := executor.executeListBackups(context.Background(), map[string]interface{}{})
|
|
var backupsResp BackupsResponse
|
|
if err := json.Unmarshal([]byte(result.Content[0].Text), &backupsResp); err != nil {
|
|
t.Fatalf("decode backups response: %v", err)
|
|
}
|
|
if len(backupsResp.PBSServers) != 1 || len(backupsResp.PBSServers[0].Datastores) != 1 {
|
|
t.Fatalf("unexpected PBS servers: %+v", backupsResp.PBSServers)
|
|
}
|
|
got := backupsResp.PBSServers[0].Datastores[0].UsagePercent
|
|
if got != 12.3 {
|
|
t.Errorf("PBS datastore UsagePercent = %v, want 12.3 (double-multiplied = %v)", got, 12.3*100)
|
|
}
|
|
|
|
// 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{"pbs", "pve", "pbs_servers", "recent_tasks"} {
|
|
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"])
|
|
}
|
|
}
|