| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- package pathanalysis
- import (
- "testing"
- "time"
- "github.com/gnacho/netpulse/server-go/internal/db"
- )
- func setup(t *testing.T) *Store {
- t.Helper()
- d, err := db.Open(t.TempDir())
- if err != nil {
- t.Fatal(err)
- }
- t.Cleanup(func() { _ = d.Close() })
- if err := EnsureSchema(d); err != nil {
- t.Fatal(err)
- }
- return NewStore(d)
- }
- func TestInsertAndLatest(t *testing.T) {
- s := setup(t)
- err := s.Insert(PathResult{
- RouterID: "patio", Destination: "8.8.8.8", TS: db.NowMS(),
- HopCount: 5, TotalMs: 25.3,
- Hops: []Hop{
- {Index: 1, Host: "192.168.1.1", AvgMs: 1.2, LossPct: 0},
- {Index: 2, Host: "10.0.0.1", AvgMs: 5.1, LossPct: 0},
- {Index: 3, Host: "172.16.0.1", AvgMs: 8.4, LossPct: 2.5},
- {Index: 4, Host: "216.239.0.1", AvgMs: 15.2, LossPct: 0},
- {Index: 5, Host: "8.8.8.8", AvgMs: 25.3, LossPct: 0},
- },
- })
- if err != nil {
- t.Fatal(err)
- }
- r, err := s.Latest("patio", "8.8.8.8")
- if err != nil {
- t.Fatal(err)
- }
- if r.HopCount != 5 || r.TotalMs != 25.3 {
- t.Fatalf("unexpected result: %+v", r)
- }
- if len(r.Hops) != 5 {
- t.Fatalf("expected 5 hops, got %d", len(r.Hops))
- }
- if r.Hops[2].LossPct != 2.5 {
- t.Fatalf("hop 3 loss should be 2.5, got %.1f", r.Hops[2].LossPct)
- }
- }
- func TestLatestNotFound(t *testing.T) {
- s := setup(t)
- _, err := s.Latest("nonexistent", "8.8.8.8")
- if err == nil {
- t.Fatal("expected error for nonexistent path")
- }
- }
- func TestHistory(t *testing.T) {
- s := setup(t)
- now := db.NowMS()
- for i := 0; i < 5; i++ {
- _ = s.Insert(PathResult{
- RouterID: "patio", Destination: "1.1.1.1",
- TS: now - int64(i)*3600000, HopCount: 3, TotalMs: 10 + float64(i),
- Hops: []Hop{{Index: 1, Host: "gw", AvgMs: 1}},
- })
- }
- results, err := s.History("patio", "1.1.1.1", 10)
- if err != nil {
- t.Fatal(err)
- }
- if len(results) != 5 {
- t.Fatalf("expected 5 results, got %d", len(results))
- }
- }
- func TestSummaries(t *testing.T) {
- s := setup(t)
- _ = s.Insert(PathResult{
- RouterID: "patio", Destination: "8.8.8.8", TS: db.NowMS(),
- HopCount: 4, TotalMs: 20,
- Hops: []Hop{
- {Index: 1, Host: "gw", LossPct: 0},
- {Index: 2, Host: "isp", LossPct: 5.0},
- {Index: 3, Host: "google", LossPct: 0},
- },
- })
- _ = s.Insert(PathResult{
- RouterID: "patio", Destination: "1.1.1.1", TS: db.NowMS(),
- HopCount: 3, TotalMs: 15,
- Hops: []Hop{{Index: 1, Host: "gw", LossPct: 0}},
- })
- summaries, err := s.Summaries("patio")
- if err != nil {
- t.Fatal(err)
- }
- if len(summaries) != 2 {
- t.Fatalf("expected 2 summaries, got %d", len(summaries))
- }
- }
- func TestPurge(t *testing.T) {
- s := setup(t)
- _ = s.Insert(PathResult{RouterID: "r1", Destination: "d1", TS: db.NowMS() - 86400000*30, HopCount: 1, Hops: []Hop{}})
- _ = s.Insert(PathResult{RouterID: "r1", Destination: "d1", TS: db.NowMS(), HopCount: 1, Hops: []Hop{}})
- n, err := s.Purge(7 * 24 * time.Hour)
- if err != nil {
- t.Fatal(err)
- }
- if n != 1 {
- t.Fatalf("expected 1 purged, got %d", n)
- }
- }
|