mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-04-28 11:30:15 +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
80 lines
1.8 KiB
Go
80 lines
1.8 KiB
Go
package updates
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestInstallShAdapter_DetectServiceName(t *testing.T) {
|
|
dir := t.TempDir()
|
|
systemctl := filepath.Join(dir, "systemctl")
|
|
script := `#!/bin/sh
|
|
if [ "$1" = "is-active" ] && [ "$2" = "pulse-backend" ]; then
|
|
echo "active"
|
|
exit 0
|
|
fi
|
|
echo "inactive"
|
|
exit 0
|
|
`
|
|
if err := os.WriteFile(systemctl, []byte(script), 0755); err != nil {
|
|
t.Fatalf("write systemctl: %v", err)
|
|
}
|
|
|
|
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
|
|
adapter := &InstallShAdapter{}
|
|
name, err := adapter.detectServiceName()
|
|
if err != nil {
|
|
t.Fatalf("detectServiceName error: %v", err)
|
|
}
|
|
if name != "pulse-backend" {
|
|
t.Fatalf("expected pulse-backend, got %q", name)
|
|
}
|
|
}
|
|
|
|
func TestInstallShAdapter_DownloadInstallScript(t *testing.T) {
|
|
content := "echo hi"
|
|
sum := sha256.Sum256([]byte(content))
|
|
checksum := hex.EncodeToString(sum[:])
|
|
|
|
dir := t.TempDir()
|
|
curl := filepath.Join(dir, "curl")
|
|
script := strings.Join([]string{
|
|
"#!/bin/sh",
|
|
`out=""`,
|
|
`url=""`,
|
|
`while [ "$#" -gt 0 ]; do`,
|
|
` if [ "$1" = "-o" ]; then`,
|
|
` out="$2"`,
|
|
` shift 2`,
|
|
` continue`,
|
|
` fi`,
|
|
` url="$1"`,
|
|
` shift`,
|
|
`done`,
|
|
`if echo "$url" | grep -q ".sha256$"; then`,
|
|
` echo "` + checksum + ` install.sh" > "$out"`,
|
|
`else`,
|
|
` printf '%s' "` + content + `" > "$out"`,
|
|
`fi`,
|
|
``,
|
|
}, "\n")
|
|
if err := os.WriteFile(curl, []byte(script), 0755); err != nil {
|
|
t.Fatalf("write curl: %v", err)
|
|
}
|
|
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
|
|
adapter := &InstallShAdapter{installScriptURL: "http://example/install.sh"}
|
|
out, err := adapter.downloadInstallScript(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("downloadInstallScript error: %v", err)
|
|
}
|
|
if out != content {
|
|
t.Fatalf("unexpected script content: %q", out)
|
|
}
|
|
}
|