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
125 lines
3.5 KiB
Go
125 lines
3.5 KiB
Go
package kubernetesagent
|
|
|
|
import (
|
|
"testing"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
)
|
|
|
|
func TestIsProblemPod(t *testing.T) {
|
|
if !isProblemPod(corev1.Pod{Status: corev1.PodStatus{Phase: corev1.PodPending}}) {
|
|
t.Fatal("expected pending pod to be a problem")
|
|
}
|
|
if !isProblemPod(corev1.Pod{Status: corev1.PodStatus{Phase: corev1.PodFailed}}) {
|
|
t.Fatal("expected failed pod to be a problem")
|
|
}
|
|
if !isProblemPod(corev1.Pod{Status: corev1.PodStatus{Phase: corev1.PodUnknown}}) {
|
|
t.Fatal("expected unknown pod to be a problem")
|
|
}
|
|
|
|
okPod := corev1.Pod{
|
|
Status: corev1.PodStatus{
|
|
Phase: corev1.PodRunning,
|
|
ContainerStatuses: []corev1.ContainerStatus{
|
|
{
|
|
Ready: true,
|
|
State: corev1.ContainerState{Running: &corev1.ContainerStateRunning{}},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
if isProblemPod(okPod) {
|
|
t.Fatal("expected healthy running pod to be non-problem")
|
|
}
|
|
|
|
waitingPod := corev1.Pod{
|
|
Status: corev1.PodStatus{
|
|
Phase: corev1.PodRunning,
|
|
ContainerStatuses: []corev1.ContainerStatus{
|
|
{
|
|
Ready: false,
|
|
State: corev1.ContainerState{
|
|
Waiting: &corev1.ContainerStateWaiting{Reason: "ImagePullBackOff"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
if !isProblemPod(waitingPod) {
|
|
t.Fatal("expected waiting container to be a problem")
|
|
}
|
|
|
|
initFailedPod := corev1.Pod{
|
|
Status: corev1.PodStatus{
|
|
Phase: corev1.PodRunning,
|
|
InitContainerStatuses: []corev1.ContainerStatus{
|
|
{
|
|
Ready: false,
|
|
State: corev1.ContainerState{
|
|
Terminated: &corev1.ContainerStateTerminated{ExitCode: 1},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
if !isProblemPod(initFailedPod) {
|
|
t.Fatal("expected failed init container to be a problem")
|
|
}
|
|
}
|
|
|
|
func TestSummarizeContainerState(t *testing.T) {
|
|
status, reason, message := summarizeContainerState(corev1.ContainerStatus{
|
|
State: corev1.ContainerState{Running: &corev1.ContainerStateRunning{}},
|
|
})
|
|
if status != "running" || reason != "" || message != "" {
|
|
t.Fatalf("unexpected running summary: %s %s %s", status, reason, message)
|
|
}
|
|
|
|
status, reason, message = summarizeContainerState(corev1.ContainerStatus{
|
|
State: corev1.ContainerState{
|
|
Waiting: &corev1.ContainerStateWaiting{Reason: "CrashLoopBackOff", Message: " waiting "},
|
|
},
|
|
})
|
|
if status != "waiting" || reason != "CrashLoopBackOff" || message != "waiting" {
|
|
t.Fatalf("unexpected waiting summary: %s %s %s", status, reason, message)
|
|
}
|
|
|
|
status, reason, message = summarizeContainerState(corev1.ContainerStatus{
|
|
State: corev1.ContainerState{
|
|
Terminated: &corev1.ContainerStateTerminated{Reason: "Error", Message: " boom "},
|
|
},
|
|
})
|
|
if status != "terminated" || reason != "Error" || message != "boom" {
|
|
t.Fatalf("unexpected terminated summary: %s %s %s", status, reason, message)
|
|
}
|
|
|
|
status, reason, message = summarizeContainerState(corev1.ContainerStatus{})
|
|
if status != "unknown" || reason != "" || message != "" {
|
|
t.Fatalf("unexpected unknown summary: %s %s %s", status, reason, message)
|
|
}
|
|
}
|
|
|
|
func TestOwnerRef(t *testing.T) {
|
|
controller := true
|
|
refs := []metav1.OwnerReference{
|
|
{Kind: "ReplicaSet", Name: "rs-1"},
|
|
{Kind: "Deployment", Name: "deploy-1", Controller: &controller},
|
|
}
|
|
kind, name := ownerRef(refs)
|
|
if kind != "Deployment" || name != "deploy-1" {
|
|
t.Fatalf("expected controller ref, got %s %s", kind, name)
|
|
}
|
|
|
|
kind, name = ownerRef([]metav1.OwnerReference{
|
|
{Kind: "Job", Name: "job-1"},
|
|
})
|
|
if kind != "Job" || name != "job-1" {
|
|
t.Fatalf("expected first ref, got %s %s", kind, name)
|
|
}
|
|
|
|
kind, name = ownerRef(nil)
|
|
if kind != "" || name != "" {
|
|
t.Fatalf("expected empty ref, got %s %s", kind, name)
|
|
}
|
|
}
|