//go:build linux // +build linux package monitor import ( "testing" ) // TestNewMonitor tests the creation of a new Monitor func TestNewMonitor(t *testing.T) { // Test that we can create a monitor without errors mon := NewMonitor("test0", "Test Interface A", "test1", "Test Interface B", "/tmp/test.log") if mon == nil { t.Fatal("NewMonitor returned nil") } // Check interfaces if mon.interfaceA == nil { t.Fatal("interfaceA is nil") } if mon.interfaceB == nil { t.Fatal("interfaceB is nil") } if mon.interfaceA.Name != "test0" { t.Errorf("Expected interfaceA.Name to be 'test0', got '%s'", mon.interfaceA.Name) } if mon.interfaceB.Name != "test1" { t.Errorf("Expected interfaceB.Name to be 'test1', got '%s'", mon.interfaceB.Name) } // Check default configuration if mon.ICMPTimeout.Milliseconds() != 500 { t.Errorf("Expected ICMPTimeout to be 500ms, got %dms", mon.ICMPTimeout.Milliseconds()) } if mon.PacketLossPings != 20 { t.Errorf("Expected PacketLossPings to be 20, got %d", mon.PacketLossPings) } // Check that host lists are initialized 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") } } // TestAddHosts tests adding hosts to the monitor func TestAddHosts(t *testing.T) { mon := NewMonitor("test0", "Test A", "test1", "Test B", "") // Test adding reachability hosts mon.AddReachabilityHost("8.8.8.8") mon.AddReachabilityHost("google.com") if len(mon.reachabilityHosts) != 2 { t.Errorf("Expected 2 reachability hosts, got %d", len(mon.reachabilityHosts)) } // Test adding packet loss hosts mon.AddPacketLossHost("github.com") if len(mon.packetLossHosts) != 1 { t.Errorf("Expected 1 packet loss host, got %d", len(mon.packetLossHosts)) } // Test adding TCP hosts mon.AddTCPHost("google.com:443") mon.AddTCPHost("github.com:443") if len(mon.tcpHosts) != 2 { t.Errorf("Expected 2 TCP hosts, got %d", len(mon.tcpHosts)) } } // TestMinMaxAvgStd tests the statistics calculation function func TestMinMaxAvgStd(t *testing.T) { tests := []struct { name string data []float64 wantMin, wantMax, wantAvg float64 }{ { name: "empty slice", data: []float64{}, wantMin: 0, wantMax: 0, wantAvg: 0, }, { name: "single value", data: []float64{5.0}, wantMin: 5.0, wantMax: 5.0, wantAvg: 5.0, }, { name: "multiple values", data: []float64{1.0, 2.0, 3.0, 4.0, 5.0}, wantMin: 1.0, wantMax: 5.0, wantAvg: 3.0, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { min, max, avg, _ := MinMaxAvgStd(tt.data) if min != tt.wantMin { t.Errorf("MinMaxAvgStd() min = %v, want %v", min, tt.wantMin) } if max != tt.wantMax { t.Errorf("MinMaxAvgStd() max = %v, want %v", max, tt.wantMax) } if avg != tt.wantAvg { t.Errorf("MinMaxAvgStd() avg = %v, want %v", avg, tt.wantAvg) } }) } }