55 lines
2.0 KiB
Go
55 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestAgentActionAllowlistAndLifecycle(t *testing.T) {
|
|
store, err := openStore(":memory:")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
report, _ := json.Marshal(AgentReport{Services: []AgentService{{Name: "gitea.service"}}, Containers: []AgentContainer{{ID: "abc123", Name: "gitea"}}})
|
|
_, err = store.db.Exec(`INSERT INTO agents(id,name,secret_hash,enrolled_at,last_seen,report_json) VALUES('a','agent','x',?,?,?)`, time.Now().Unix(), time.Now().Unix(), string(report))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = store.QueueAgentAction("a", "systemd-restart", "../../bad.service"); err == nil {
|
|
t.Fatal("unsafe unit accepted")
|
|
}
|
|
action, err := store.QueueAgentAction("a", "systemd-restart", "gitea.service")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
commands, err := store.DispatchAgentActions("a")
|
|
if err != nil || len(commands) != 1 || commands[0].ID != action.ID {
|
|
t.Fatalf("commands=%#v err=%v", commands, err)
|
|
}
|
|
store.CompleteAgentActions("a", []AgentActionResult{{ID: action.ID, Success: true, Message: "ok"}})
|
|
items, err := store.AgentActions(10)
|
|
if err != nil || len(items) != 1 || items[0].Status != "completed" {
|
|
t.Fatalf("items=%#v err=%v", items, err)
|
|
}
|
|
}
|
|
|
|
func TestManualServiceLinkGroupsOfflineAlerts(t *testing.T) {
|
|
metrics := dashboardMetrics{Agents: []ManagedAgent{{ID: "a", Name: "app", VMID: 105, Online: false}}, ServiceLinks: []ServiceLink{{ServiceID: 7, AgentID: "a", VMID: 105}}, Services: ServicesMetrics{Services: []ServiceStatus{{MonitoredService: MonitoredService{ID: 7, Name: "Gitea"}, Endpoints: []ServiceEndpointStatus{{Kind: "local", Up: false}}}}}}
|
|
alerts := []Alert{{ID: "agent-offline-a-105"}, {ID: "service-7-local"}, {ID: "other"}}
|
|
grouped := groupLinkedAlerts(metrics, alerts)
|
|
if len(grouped) != 2 {
|
|
t.Fatalf("grouped=%#v", grouped)
|
|
}
|
|
found := false
|
|
for _, alert := range grouped {
|
|
if alert.ID == "group-agent-offline-a-105" {
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("group alert missing: %#v", grouped)
|
|
}
|
|
}
|