package monitor_test import ( "testing" "git.eeqj.de/sneak/rtnetmon/internal/monitor" ) // Interface names and hosts reused across the monitor package tests. const ( ifaceTest0 = "test0" ifaceTest1 = "test1" ifaceEth0 = "eth0" host8888 = "8.8.8.8" ) func TestNewMonitor(t *testing.T) { t.Parallel() mon := monitor.NewMonitor([]monitor.IfaceSpec{ {Name: ifaceTest0, Label: "Test Interface A"}, {Name: ifaceTest1, Label: "Test Interface B"}, }, "/tmp/test.log") ifaces := mon.Interfaces() if len(ifaces) != 2 { t.Fatalf("interfaces = %d, want 2", len(ifaces)) } if ifaces[0].Name != ifaceTest0 { t.Errorf("interfaces[0].Name = %q, want %q", ifaces[0].Name, ifaceTest0) } if ifaces[1].Name != ifaceTest1 { t.Errorf("interfaces[1].Name = %q, want %q", ifaces[1].Name, ifaceTest1) } if mon.ICMPTimeout.Milliseconds() != 500 { t.Errorf("ICMPTimeout = %dms, want 500ms", mon.ICMPTimeout.Milliseconds()) } if mon.PacketLossPings != 20 { t.Errorf("PacketLossPings = %d, want 20", mon.PacketLossPings) } if mon.ReachabilityHosts() == nil { t.Error("reachabilityHosts is nil") } if mon.PacketLossHosts() == nil { t.Error("packetLossHosts is nil") } if mon.TCPHosts() == nil { t.Error("tcpHosts is nil") } } func TestNewMonitorSingle(t *testing.T) { t.Parallel() mon := monitor.NewMonitor( []monitor.IfaceSpec{{Name: ifaceEth0, Label: "default route"}}, "") ifaces := mon.Interfaces() if len(ifaces) != 1 { t.Fatalf("interfaces = %d, want 1", len(ifaces)) } if ifaces[0].Name != ifaceEth0 { t.Errorf("interfaces[0].Name = %q, want %q", ifaces[0].Name, ifaceEth0) } } func TestAddHosts(t *testing.T) { t.Parallel() mon := monitor.NewMonitor([]monitor.IfaceSpec{ {Name: ifaceTest0, Label: "Test A"}, {Name: ifaceTest1, Label: "Test B"}, }, "") mon.AddReachabilityHost(host8888) mon.AddReachabilityHost("google.com") if len(mon.ReachabilityHosts()) != 2 { t.Errorf("reachability hosts = %d, want 2", len(mon.ReachabilityHosts())) } mon.AddPacketLossHost("github.com") if len(mon.PacketLossHosts()) != 1 { t.Errorf("packet loss hosts = %d, want 1", len(mon.PacketLossHosts())) } mon.AddTCPHost("google.com:443") mon.AddTCPHost("github.com:443") if len(mon.TCPHosts()) != 2 { t.Errorf("tcp hosts = %d, want 2", len(mon.TCPHosts())) } } func TestMinMaxAvgStd(t *testing.T) { t.Parallel() tests := []struct { name string data []float64 wantMin, wantMax, wantAvg float64 }{ {"empty slice", []float64{}, 0, 0, 0}, {"single value", []float64{5.0}, 5.0, 5.0, 5.0}, {"multiple values", []float64{1.0, 2.0, 3.0, 4.0, 5.0}, 1.0, 5.0, 3.0}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() mn, mx, avg, _ := monitor.MinMaxAvgStd(tt.data) if mn != tt.wantMin { t.Errorf("min = %v, want %v", mn, tt.wantMin) } if mx != tt.wantMax { t.Errorf("max = %v, want %v", mx, tt.wantMax) } if avg != tt.wantAvg { t.Errorf("avg = %v, want %v", avg, tt.wantAvg) } }) } }