mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-30 01:32:00 +01:00
79 lines
3.7 KiB
Go
79 lines
3.7 KiB
Go
package ai
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/models"
|
|
)
|
|
|
|
func TestGetResourceCurrentState(t *testing.T) {
|
|
ps := NewPatrolService(nil, nil)
|
|
|
|
state := models.StateSnapshot{
|
|
Storage: []models.Storage{{ID: "storage-1", Name: "local", Usage: 42, Status: "active"}},
|
|
Nodes: []models.Node{{ID: "node-1", Name: "node-1", CPU: 0.2, Memory: models.Memory{Usage: 33}, Status: "online"}},
|
|
Hosts: []models.Host{
|
|
{ID: "host-1", Hostname: "host-1", CPUUsage: 27.5, Memory: models.Memory{Usage: 41.0}, Status: "online"},
|
|
{ID: "truenas-1", Hostname: "truenas-1", DisplayName: "TrueNAS Main", Platform: "truenas", CPUUsage: 18.0, Memory: models.Memory{Usage: 52.0}, Status: "online"},
|
|
},
|
|
VMs: []models.VM{{ID: "vm-1", Name: "vm-1", CPU: 0.1, Memory: models.Memory{Usage: 44}, Status: "running"}},
|
|
Containers: []models.Container{
|
|
{ID: "ct-1", Name: "ct-1", CPU: 0.3, Memory: models.Memory{Usage: 55}, Status: "running"},
|
|
},
|
|
DockerHosts: []models.DockerHost{{
|
|
ID: "docker-1",
|
|
Hostname: "dockhost",
|
|
Status: "online",
|
|
Containers: []models.DockerContainer{{ID: "dc-1", Name: "web", CPUPercent: 12.3, MemoryPercent: 45.6, State: "running"}},
|
|
}},
|
|
}
|
|
|
|
cases := []struct {
|
|
alert AlertInfo
|
|
assert string
|
|
}{
|
|
{alert: AlertInfo{ResourceType: "storage", ResourceID: "storage-1", ResourceName: "local"}, assert: "Storage 'local'"},
|
|
{alert: AlertInfo{ResourceType: "usage", Type: "usage", ResourceID: "storage-1", ResourceName: "local"}, assert: "Storage 'local'"},
|
|
{alert: AlertInfo{ResourceType: "node", ResourceID: "node-1", ResourceName: "node-1"}, assert: "Node 'node-1'"},
|
|
{alert: AlertInfo{ResourceType: "agent", ResourceID: "host-1", ResourceName: "host-1"}, assert: "Agent host 'host-1'"},
|
|
{alert: AlertInfo{ResourceType: "truenas", ResourceID: "truenas-1", ResourceName: "TrueNAS Main"}, assert: "TrueNAS system 'truenas-1'"},
|
|
{alert: AlertInfo{ResourceType: "vm", ResourceID: "vm-1", ResourceName: "vm-1"}, assert: "VM 'vm-1'"},
|
|
{alert: AlertInfo{ResourceType: "system-container", ResourceID: "ct-1", ResourceName: "ct-1"}, assert: "Container 'ct-1'"},
|
|
{alert: AlertInfo{ResourceType: "app-container", ResourceID: "dc-1", ResourceName: "web"}, assert: "Docker container 'web'"},
|
|
{alert: AlertInfo{ResourceType: "unknown", ResourceID: "x"}, assert: "Resource state unknown"},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
stateText := ps.getResourceCurrentStateState(tc.alert, patrolRuntimeStateForTest(ps, state))
|
|
if !strings.Contains(stateText, tc.assert) {
|
|
t.Fatalf("expected %q in state text, got %q", tc.assert, stateText)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGetCurrentMetricValue_DockerAndStorage(t *testing.T) {
|
|
ps := NewPatrolService(nil, nil)
|
|
state := models.StateSnapshot{
|
|
Storage: []models.Storage{{ID: "storage-1", Name: "local", Usage: 77}},
|
|
DockerHosts: []models.DockerHost{{
|
|
Containers: []models.DockerContainer{{ID: "dc-1", Name: "web", CPUPercent: 12.3, MemoryPercent: 45.6}},
|
|
}},
|
|
}
|
|
|
|
storageAlert := AlertInfo{ResourceType: "storage", ResourceID: "storage-1"}
|
|
if got := ps.getCurrentMetricValueState(storageAlert, patrolRuntimeStateForTest(ps, state)); got != 77 {
|
|
t.Fatalf("expected storage usage 77, got %.1f", got)
|
|
}
|
|
|
|
dockerCPU := AlertInfo{ResourceType: "app-container", ResourceID: "dc-1", Type: "cpu"}
|
|
if got := ps.getCurrentMetricValueState(dockerCPU, patrolRuntimeStateForTest(ps, state)); got != 12.3 {
|
|
t.Fatalf("expected docker cpu 12.3, got %.1f", got)
|
|
}
|
|
|
|
dockerMem := AlertInfo{ResourceType: "app-container", ResourceID: "dc-1", Type: "memory"}
|
|
if got := ps.getCurrentMetricValueState(dockerMem, patrolRuntimeStateForTest(ps, state)); got != 45.6 {
|
|
t.Fatalf("expected docker memory 45.6, got %.1f", got)
|
|
}
|
|
}
|