Pulse/pkg/discovery/scan_helpers_test.go
2026-03-18 16:06:30 +00:00

157 lines
4.3 KiB
Go

package discovery
import (
"context"
"net"
"testing"
"time"
"github.com/rcourtman/pulse-go-rewrite/pkg/discovery/envdetect"
)
func TestCollectExtraTargets(t *testing.T) {
scanner := &Scanner{policy: envdetect.DefaultScanPolicy()}
seen := map[string]struct{}{
"10.0.0.2": {},
}
profile := &envdetect.EnvironmentProfile{
ExtraTargets: []net.IP{
net.ParseIP("10.0.0.1"),
net.ParseIP("10.0.0.2"),
net.ParseIP("2001:db8::1"),
nil,
},
}
targets := scanner.collectExtraTargets(profile, seen)
if len(targets) != 1 || targets[0] != "10.0.0.1" {
t.Fatalf("unexpected targets: %v", targets)
}
if _, ok := seen["10.0.0.1"]; !ok {
t.Fatalf("expected seen to include new target")
}
}
func TestCollectExtraTargetsNilProfile(t *testing.T) {
scanner := &Scanner{policy: envdetect.DefaultScanPolicy()}
seen := map[string]struct{}{}
if targets := scanner.collectExtraTargets(nil, seen); targets != nil {
t.Fatalf("expected nil targets for nil profile, got %v", targets)
}
}
func TestExpandPhaseIPs(t *testing.T) {
scanner := &Scanner{policy: envdetect.DefaultScanPolicy()}
seen := map[string]struct{}{
"192.168.1.1": {},
}
_, subnet30, err := net.ParseCIDR("192.168.1.0/30")
if err != nil {
t.Fatalf("failed to parse subnet: %v", err)
}
_, subnet6, err := net.ParseCIDR("2001:db8::/64")
if err != nil {
t.Fatalf("failed to parse ipv6 subnet: %v", err)
}
targets, count := scanner.expandPhaseIPs(envdetect.SubnetPhase{
Subnets: []net.IPNet{*subnet30, *subnet6},
}, seen)
if count != 2 {
t.Fatalf("expected 2 subnets counted, got %d", count)
}
if len(targets) != 1 || targets[0] != "192.168.1.2" {
t.Fatalf("unexpected targets: %v", targets)
}
}
func TestShouldSkipPhase(t *testing.T) {
policy := envdetect.DefaultScanPolicy()
policy.DialTimeout = time.Second
scanner := &Scanner{policy: policy}
ctxShort, cancel := context.WithDeadline(context.Background(), time.Now().Add(500*time.Millisecond))
defer cancel()
phaseLowConfidence := envdetect.SubnetPhase{Name: "low", Confidence: 0.2}
if !scanner.shouldSkipPhase(ctxShort, phaseLowConfidence) {
t.Fatalf("expected phase to be skipped with short deadline")
}
phaseHighConfidence := envdetect.SubnetPhase{Name: "high", Confidence: 0.8}
if scanner.shouldSkipPhase(ctxShort, phaseHighConfidence) {
t.Fatalf("expected high confidence phase to run")
}
if scanner.shouldSkipPhase(context.Background(), phaseLowConfidence) {
t.Fatalf("expected phase to run without deadline")
}
}
func TestShouldSkipPhaseDefaultBudget(t *testing.T) {
policy := envdetect.DefaultScanPolicy()
policy.DialTimeout = 0
scanner := &Scanner{policy: policy}
ctxShort, cancel := context.WithDeadline(context.Background(), time.Now().Add(2*time.Second))
defer cancel()
phaseLowConfidence := envdetect.SubnetPhase{Name: "low", Confidence: 0.2}
if !scanner.shouldSkipPhase(ctxShort, phaseLowConfidence) {
t.Fatalf("expected phase to be skipped with default budget")
}
}
func TestBuildEnvironmentInfoCopiesData(t *testing.T) {
profile := &envdetect.EnvironmentProfile{
Type: envdetect.DockerBridge,
Confidence: 0.75,
Warnings: []string{"warning-one"},
Metadata: map[string]string{
"container_type": "docker",
},
Phases: []envdetect.SubnetPhase{
{
Name: "phase-a",
Confidence: 0.9,
Subnets: []net.IPNet{},
},
},
}
info := buildEnvironmentInfo(profile)
if info == nil {
t.Fatal("expected environment info, got nil")
}
if info.Type != "docker-bridge" || info.Confidence != 0.75 {
t.Fatalf("unexpected info: %+v", info)
}
if len(info.Warnings) != 1 || info.Warnings[0] != "warning-one" {
t.Fatalf("unexpected warnings: %v", info.Warnings)
}
if info.Metadata["container_type"] != "docker" {
t.Fatalf("unexpected metadata: %v", info.Metadata)
}
if len(info.Phases) != 1 || info.Phases[0].Name != "phase-a" {
t.Fatalf("unexpected phase info: %v", info.Phases)
}
info.Metadata["container_type"] = "mutated"
info.Warnings[0] = "mutated-warning"
if profile.Metadata["container_type"] != "docker" {
t.Fatalf("expected metadata copy, got %v", profile.Metadata)
}
if profile.Warnings[0] != "warning-one" {
t.Fatalf("expected warnings copy, got %v", profile.Warnings)
}
}
func TestBuildEnvironmentInfoNilProfile(t *testing.T) {
if info := buildEnvironmentInfo(nil); info != nil {
t.Fatalf("expected nil info for nil profile, got %+v", info)
}
}