mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-04-29 03:50:18 +00:00
New test files with expanded coverage: API tests: - ai_handler_test.go: AI handler unit tests with mocking - agent_profiles_tools_test.go: Profile management tests - alerts_endpoints_test.go: Alert API endpoint tests - alerts_test.go: Updated for interface changes - audit_handlers_test.go: Audit handler tests - frontend_embed_test.go: Frontend embedding tests - metadata_handlers_test.go, metadata_provider_test.go: Metadata tests - notifications_test.go: Updated for interface changes - profile_suggestions_test.go: Profile suggestion tests - saml_service_test.go: SAML authentication tests - sensor_proxy_gate_test.go: Sensor proxy tests - updates_test.go: Updated for interface changes Agent tests: - dockeragent/signature_test.go: Docker agent signature tests - hostagent/agent_metrics_test.go: Host agent metrics tests - hostagent/commands_test.go: Command execution tests - hostagent/network_helpers_test.go: Network helper tests - hostagent/proxmox_setup_test.go: Updated setup tests - kubernetesagent/*_test.go: Kubernetes agent tests Core package tests: - monitoring/kubernetes_agents_test.go, reload_test.go - remoteconfig/client_test.go, signature_test.go - sensors/collector_test.go - updates/adapter_installsh_*_test.go: Install adapter tests - updates/manager_*_test.go: Update manager tests - websocket/hub_*_test.go: WebSocket hub tests Library tests: - pkg/audit/export_test.go: Audit export tests - pkg/metrics/store_test.go: Metrics store tests - pkg/proxmox/*_test.go: Proxmox client tests - pkg/reporting/reporting_test.go: Reporting tests - pkg/server/*_test.go: Server tests - pkg/tlsutil/extra_test.go: TLS utility tests Total: ~8000 lines of new test code
168 lines
4.6 KiB
Go
168 lines
4.6 KiB
Go
package remoteconfig
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestClient_Fetch(t *testing.T) {
|
|
t.Run("successful fetch with full config", func(t *testing.T) {
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/api/agents/host/agent-1/config" {
|
|
t.Errorf("Expected path /api/agents/host/agent-1/config, got %s", r.URL.Path)
|
|
w.WriteHeader(http.StatusNotFound)
|
|
return
|
|
}
|
|
if r.Header.Get("Authorization") != "Bearer token-123" {
|
|
t.Errorf("Expected Authorization header 'Bearer token-123', got %s", r.Header.Get("Authorization"))
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write([]byte(`{
|
|
"success": true,
|
|
"hostId": "agent-1",
|
|
"config": {
|
|
"commandsEnabled": true,
|
|
"settings": {
|
|
"interval": "1m",
|
|
"enable_docker": false
|
|
}
|
|
}
|
|
}`))
|
|
}))
|
|
defer ts.Close()
|
|
|
|
client := New(Config{
|
|
PulseURL: ts.URL,
|
|
APIToken: "token-123",
|
|
AgentID: "agent-1",
|
|
})
|
|
|
|
settings, commandsEnabled, err := client.Fetch(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("Fetch failed: %v", err)
|
|
}
|
|
|
|
if commandsEnabled == nil || *commandsEnabled != true {
|
|
t.Errorf("Expected commandsEnabled=true, got %v", commandsEnabled)
|
|
}
|
|
|
|
if settings["interval"] != "1m" {
|
|
t.Errorf("Expected interval='1m', got %v", settings["interval"])
|
|
}
|
|
if settings["enable_docker"] != false {
|
|
t.Errorf("Expected enable_docker=false, got %v", settings["enable_docker"])
|
|
}
|
|
})
|
|
|
|
t.Run("signature required without signature", func(t *testing.T) {
|
|
t.Setenv("PULSE_AGENT_CONFIG_SIGNATURE_REQUIRED", "true")
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write([]byte(`{
|
|
"success": true,
|
|
"hostId": "agent-1",
|
|
"config": {
|
|
"commandsEnabled": true,
|
|
"settings": {
|
|
"interval": "1m"
|
|
}
|
|
}
|
|
}`))
|
|
}))
|
|
defer ts.Close()
|
|
|
|
client := New(Config{
|
|
PulseURL: ts.URL,
|
|
APIToken: "token-123",
|
|
AgentID: "agent-1",
|
|
})
|
|
|
|
_, _, err := client.Fetch(context.Background())
|
|
if err == nil {
|
|
t.Fatal("Expected error, got nil")
|
|
}
|
|
if !strings.Contains(err.Error(), "signature required") {
|
|
t.Fatalf("Expected signature required error, got %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("server error", func(t *testing.T) {
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}))
|
|
defer ts.Close()
|
|
|
|
client := New(Config{PulseURL: ts.URL, APIToken: "t", AgentID: "a"})
|
|
_, _, err := client.Fetch(context.Background())
|
|
if err == nil {
|
|
t.Fatal("Expected error, got nil")
|
|
}
|
|
})
|
|
|
|
t.Run("missing agent ID", func(t *testing.T) {
|
|
client := New(Config{PulseURL: "http://localhost", APIToken: "t", AgentID: ""})
|
|
_, _, err := client.Fetch(context.Background())
|
|
if err == nil {
|
|
t.Fatal("Expected error for missing agent ID, got nil")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestClient_ResolveHostID(t *testing.T) {
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/api/agents/host/lookup" {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
switch r.URL.Query().Get("hostname") {
|
|
case "known":
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write([]byte(`{"success":true,"host":{"id":"host-123"}}`))
|
|
case "unknown":
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write([]byte(`{"success":false}`))
|
|
case "bad":
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
case "invalid":
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write([]byte(`not-json`))
|
|
default:
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}
|
|
}))
|
|
defer ts.Close()
|
|
|
|
client := New(Config{
|
|
PulseURL: ts.URL,
|
|
APIToken: "token",
|
|
})
|
|
|
|
if got, err := client.resolveHostID(context.Background()); err != nil || got != "" {
|
|
t.Fatalf("expected empty hostID for blank hostname, got %q err=%v", got, err)
|
|
}
|
|
|
|
client.cfg.Hostname = "known"
|
|
if got, err := client.resolveHostID(context.Background()); err != nil || got != "host-123" {
|
|
t.Fatalf("expected host-123, got %q err=%v", got, err)
|
|
}
|
|
|
|
client.cfg.Hostname = "unknown"
|
|
if got, err := client.resolveHostID(context.Background()); err != nil || got != "" {
|
|
t.Fatalf("expected empty hostID, got %q err=%v", got, err)
|
|
}
|
|
|
|
client.cfg.Hostname = "bad"
|
|
if _, err := client.resolveHostID(context.Background()); err == nil {
|
|
t.Fatal("expected error for server failure")
|
|
}
|
|
|
|
client.cfg.Hostname = "invalid"
|
|
if _, err := client.resolveHostID(context.Background()); err == nil {
|
|
t.Fatal("expected error for invalid JSON")
|
|
}
|
|
}
|