Files
rtnetmon/internal/monitor/monitor_test.go
T
sneak 18b27dc48c
check / check (push) Failing after 0s
Adopt repo standards: scaffold, policies, lint-clean (closes #1)
Add the standard scaffold and bring the tree to a clean lint under the
vendored `default: all` config: `script/` Scripts-to-Rule-Them-All
entrypoints with the `Makefile` as thin shims; a `Dockerfile` whose
`lint` and `test` phases gate the build; `.gitea/workflows/` CI running
`script/cibuild`; `REPO_POLICIES.md`, `.editorconfig`, `.dockerignore`,
`LICENSE` (WTFPL), `TODO.md`, `.gitignore`. The `.golangci.yml` is
byte-identical to the canonical copy in the `prompts` repo
(`https://git.eeqj.de/sneak/prompts/raw/branch/main/.golangci.yml`).

The 211 lint findings were fixed, not suppressed: package globals became
functions/fields/a command constructor, magic numbers became named
constants, `ctx` threads into the probes, loop functions were split to
cut complexity. Behavior is unchanged; the log file mode stays `0644`.
Four `//nolint:gosec` remain — G204 on the fixed-argv subprocess calls,
G304 on the operator-chosen log file — matching the reference repos.

`make check` is green (lint and tests run in Docker).

Model: opus-4-8
2026-09-21 07:13:37 +00:00

112 lines
2.4 KiB
Go

//go:build linux
package monitor_test
import (
"testing"
"git.eeqj.de/sneak/rtnetmon/internal/monitor"
)
func TestNewMonitor(t *testing.T) {
t.Parallel()
mon := monitor.NewMonitor("test0", "Test Interface A", "test1",
"Test Interface B", "/tmp/test.log")
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("interfaceA.Name = %q, want %q", mon.InterfaceA().Name, "test0")
}
if mon.InterfaceB().Name != "test1" {
t.Errorf("interfaceB.Name = %q, want %q", mon.InterfaceB().Name, "test1")
}
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 TestAddHosts(t *testing.T) {
t.Parallel()
mon := monitor.NewMonitor("test0", "Test A", "test1", "Test B", "")
mon.AddReachabilityHost("8.8.8.8")
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)
}
})
}
}