mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-02-18 00:17:39 +01: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
106 lines
2.6 KiB
Go
106 lines
2.6 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestShouldAutoImport(t *testing.T) {
|
|
dir := t.TempDir()
|
|
t.Setenv("PULSE_DATA_DIR", dir)
|
|
t.Setenv("PULSE_INIT_CONFIG_DATA", "")
|
|
t.Setenv("PULSE_INIT_CONFIG_FILE", "")
|
|
|
|
if ShouldAutoImport() {
|
|
t.Fatal("expected auto-import false without config")
|
|
}
|
|
|
|
t.Setenv("PULSE_INIT_CONFIG_DATA", "payload")
|
|
if !ShouldAutoImport() {
|
|
t.Fatal("expected auto-import true with data")
|
|
}
|
|
|
|
t.Setenv("PULSE_INIT_CONFIG_DATA", "")
|
|
t.Setenv("PULSE_INIT_CONFIG_FILE", "/tmp/file")
|
|
if !ShouldAutoImport() {
|
|
t.Fatal("expected auto-import true with file")
|
|
}
|
|
|
|
file := filepath.Join(dir, "nodes.enc")
|
|
if err := os.WriteFile(file, []byte("x"), 0600); err != nil {
|
|
t.Fatalf("write nodes: %v", err)
|
|
}
|
|
if ShouldAutoImport() {
|
|
t.Fatal("expected auto-import false when nodes.enc exists")
|
|
}
|
|
}
|
|
|
|
func TestNormalizeImportPayload(t *testing.T) {
|
|
if _, err := NormalizeImportPayload([]byte("")); err == nil {
|
|
t.Fatal("expected error for empty payload")
|
|
}
|
|
|
|
raw := []byte("hello")
|
|
encoded := base64.StdEncoding.EncodeToString(raw)
|
|
out, err := NormalizeImportPayload([]byte(encoded))
|
|
if err != nil {
|
|
t.Fatalf("normalize error: %v", err)
|
|
}
|
|
if out != encoded {
|
|
t.Fatalf("unexpected output: %s", out)
|
|
}
|
|
|
|
double := base64.StdEncoding.EncodeToString([]byte(encoded))
|
|
out, err = NormalizeImportPayload([]byte(double))
|
|
if err != nil {
|
|
t.Fatalf("normalize error: %v", err)
|
|
}
|
|
if out != encoded {
|
|
t.Fatalf("unexpected output: %s", out)
|
|
}
|
|
|
|
out, err = NormalizeImportPayload([]byte("not-base64"))
|
|
if err != nil {
|
|
t.Fatalf("normalize error: %v", err)
|
|
}
|
|
if out == "not-base64" {
|
|
t.Fatal("expected payload to be encoded")
|
|
}
|
|
}
|
|
|
|
func TestLooksLikeBase64(t *testing.T) {
|
|
if LooksLikeBase64("") {
|
|
t.Fatal("expected false for empty")
|
|
}
|
|
if !LooksLikeBase64("aGVsbG8=") {
|
|
t.Fatal("expected true for base64")
|
|
}
|
|
if LooksLikeBase64("nope!!") {
|
|
t.Fatal("expected false for invalid")
|
|
}
|
|
}
|
|
|
|
func TestPerformAutoImportErrors(t *testing.T) {
|
|
t.Setenv("PULSE_INIT_CONFIG_DATA", "data")
|
|
t.Setenv("PULSE_INIT_CONFIG_FILE", "")
|
|
t.Setenv("PULSE_INIT_CONFIG_PASSPHRASE", "")
|
|
if err := PerformAutoImport(); err == nil {
|
|
t.Fatal("expected error without passphrase")
|
|
}
|
|
|
|
t.Setenv("PULSE_INIT_CONFIG_PASSPHRASE", "pass")
|
|
t.Setenv("PULSE_INIT_CONFIG_FILE", "/tmp/missing-file")
|
|
t.Setenv("PULSE_INIT_CONFIG_DATA", "")
|
|
if err := PerformAutoImport(); err == nil {
|
|
t.Fatal("expected error for missing file")
|
|
}
|
|
|
|
t.Setenv("PULSE_INIT_CONFIG_FILE", "")
|
|
t.Setenv("PULSE_INIT_CONFIG_DATA", "")
|
|
if err := PerformAutoImport(); err == nil {
|
|
t.Fatal("expected error for missing data")
|
|
}
|
|
}
|