store_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package pathanalysis
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/gnacho/netpulse/server-go/internal/db"
  6. )
  7. func setup(t *testing.T) *Store {
  8. t.Helper()
  9. d, err := db.Open(t.TempDir())
  10. if err != nil {
  11. t.Fatal(err)
  12. }
  13. t.Cleanup(func() { _ = d.Close() })
  14. if err := EnsureSchema(d); err != nil {
  15. t.Fatal(err)
  16. }
  17. return NewStore(d)
  18. }
  19. func TestInsertAndLatest(t *testing.T) {
  20. s := setup(t)
  21. err := s.Insert(PathResult{
  22. RouterID: "patio", Destination: "8.8.8.8", TS: db.NowMS(),
  23. HopCount: 5, TotalMs: 25.3,
  24. Hops: []Hop{
  25. {Index: 1, Host: "192.168.1.1", AvgMs: 1.2, LossPct: 0},
  26. {Index: 2, Host: "10.0.0.1", AvgMs: 5.1, LossPct: 0},
  27. {Index: 3, Host: "172.16.0.1", AvgMs: 8.4, LossPct: 2.5},
  28. {Index: 4, Host: "216.239.0.1", AvgMs: 15.2, LossPct: 0},
  29. {Index: 5, Host: "8.8.8.8", AvgMs: 25.3, LossPct: 0},
  30. },
  31. })
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. r, err := s.Latest("patio", "8.8.8.8")
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. if r.HopCount != 5 || r.TotalMs != 25.3 {
  40. t.Fatalf("unexpected result: %+v", r)
  41. }
  42. if len(r.Hops) != 5 {
  43. t.Fatalf("expected 5 hops, got %d", len(r.Hops))
  44. }
  45. if r.Hops[2].LossPct != 2.5 {
  46. t.Fatalf("hop 3 loss should be 2.5, got %.1f", r.Hops[2].LossPct)
  47. }
  48. }
  49. func TestLatestNotFound(t *testing.T) {
  50. s := setup(t)
  51. _, err := s.Latest("nonexistent", "8.8.8.8")
  52. if err == nil {
  53. t.Fatal("expected error for nonexistent path")
  54. }
  55. }
  56. func TestHistory(t *testing.T) {
  57. s := setup(t)
  58. now := db.NowMS()
  59. for i := 0; i < 5; i++ {
  60. _ = s.Insert(PathResult{
  61. RouterID: "patio", Destination: "1.1.1.1",
  62. TS: now - int64(i)*3600000, HopCount: 3, TotalMs: 10 + float64(i),
  63. Hops: []Hop{{Index: 1, Host: "gw", AvgMs: 1}},
  64. })
  65. }
  66. results, err := s.History("patio", "1.1.1.1", 10)
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. if len(results) != 5 {
  71. t.Fatalf("expected 5 results, got %d", len(results))
  72. }
  73. }
  74. func TestSummaries(t *testing.T) {
  75. s := setup(t)
  76. _ = s.Insert(PathResult{
  77. RouterID: "patio", Destination: "8.8.8.8", TS: db.NowMS(),
  78. HopCount: 4, TotalMs: 20,
  79. Hops: []Hop{
  80. {Index: 1, Host: "gw", LossPct: 0},
  81. {Index: 2, Host: "isp", LossPct: 5.0},
  82. {Index: 3, Host: "google", LossPct: 0},
  83. },
  84. })
  85. _ = s.Insert(PathResult{
  86. RouterID: "patio", Destination: "1.1.1.1", TS: db.NowMS(),
  87. HopCount: 3, TotalMs: 15,
  88. Hops: []Hop{{Index: 1, Host: "gw", LossPct: 0}},
  89. })
  90. summaries, err := s.Summaries("patio")
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. if len(summaries) != 2 {
  95. t.Fatalf("expected 2 summaries, got %d", len(summaries))
  96. }
  97. }
  98. func TestPurge(t *testing.T) {
  99. s := setup(t)
  100. _ = s.Insert(PathResult{RouterID: "r1", Destination: "d1", TS: db.NowMS() - 86400000*30, HopCount: 1, Hops: []Hop{}})
  101. _ = s.Insert(PathResult{RouterID: "r1", Destination: "d1", TS: db.NowMS(), HopCount: 1, Hops: []Hop{}})
  102. n, err := s.Purge(7 * 24 * time.Hour)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. if n != 1 {
  107. t.Fatalf("expected 1 purged, got %d", n)
  108. }
  109. }