telemetry_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // telemetry_test.go — #822: el ping anónimo solo lleva id+versión+os/arch,
  2. // se desactiva con NETPULSE_TELEMETRY=0 y no reenvía antes de 23 h.
  3. package telemetry
  4. import (
  5. "context"
  6. "net/http"
  7. "net/http/httptest"
  8. "strings"
  9. "testing"
  10. "time"
  11. "github.com/gnacho/netpulse/server-go/internal/db"
  12. )
  13. func openTestDB(t *testing.T) *db.DB {
  14. t.Helper()
  15. d, err := db.Open(t.TempDir())
  16. if err != nil {
  17. t.Fatalf("db.Open: %v", err)
  18. }
  19. t.Cleanup(func() { d.Close() })
  20. return d
  21. }
  22. func TestDisabledByEnvAndDemo(t *testing.T) {
  23. t.Setenv("NETPULSE_TELEMETRY", "0")
  24. if New(openTestDB(t), "v1", false).Enabled() {
  25. t.Fatal("NETPULSE_TELEMETRY=0 debería desactivar el aviso")
  26. }
  27. t.Setenv("NETPULSE_TELEMETRY", "1")
  28. if New(openTestDB(t), "v1", true).Enabled() {
  29. t.Fatal("DEMO_MODE debería desactivar el aviso")
  30. }
  31. }
  32. func TestSendOncePayloadAndLastSent(t *testing.T) {
  33. var gotPath, gotQuery, gotUA string
  34. var hits int
  35. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  36. hits++
  37. gotPath, gotQuery, gotUA = r.URL.Path, r.URL.RawQuery, r.UserAgent()
  38. w.WriteHeader(http.StatusNoContent)
  39. }))
  40. defer srv.Close()
  41. t.Setenv("NETPULSE_TELEMETRY", "1")
  42. t.Setenv("NETPULSE_TELEMETRY_URL", srv.URL+"/instances")
  43. d := openTestDB(t)
  44. a := New(d, "v9.9.9", false)
  45. a.newID = func() (string, error) { return "abc123", nil }
  46. fixed := time.Date(2026, 9, 22, 12, 0, 0, 0, time.UTC)
  47. a.now = func() time.Time { return fixed }
  48. if err := a.SendOnce(context.Background()); err != nil {
  49. t.Fatalf("SendOnce: %v", err)
  50. }
  51. if hits != 1 {
  52. t.Fatalf("hits = %d, esperaba 1", hits)
  53. }
  54. if gotPath != "/instances/abc123" {
  55. t.Fatalf("path = %q", gotPath)
  56. }
  57. if gotUA != "netpulse-instance" {
  58. t.Fatalf("ua = %q", gotUA)
  59. }
  60. // El query solo puede llevar v y os.
  61. for _, kv := range strings.Split(gotQuery, "&") {
  62. if kv == "" {
  63. continue
  64. }
  65. k := strings.SplitN(kv, "=", 2)[0]
  66. if k != "v" && k != "os" {
  67. t.Fatalf("parámetro inesperado en el ping: %q", k)
  68. }
  69. }
  70. if !strings.Contains(gotQuery, "v=v9.9.9") {
  71. t.Fatalf("falta la versión: %q", gotQuery)
  72. }
  73. // Tras enviar, no vuelve a tocar hasta pasadas 23 h.
  74. if a.Due() {
  75. t.Fatal("no debería tocar reenviar justo después")
  76. }
  77. a.now = func() time.Time { return fixed.Add(24 * time.Hour) }
  78. if !a.Due() {
  79. t.Fatal("debería tocar reenviar a las 24 h")
  80. }
  81. if err := a.SendOnce(context.Background()); err != nil {
  82. t.Fatalf("SendOnce 2: %v", err)
  83. }
  84. if hits != 2 {
  85. t.Fatalf("hits = %d, esperaba 2", hits)
  86. }
  87. }
  88. func TestInstallIDStableAndPersisted(t *testing.T) {
  89. t.Setenv("NETPULSE_TELEMETRY", "1")
  90. d := openTestDB(t)
  91. a := New(d, "v1", false)
  92. id1, err := a.installID()
  93. if err != nil || len(id1) != 32 {
  94. t.Fatalf("installID = %q, %v", id1, err)
  95. }
  96. id2, _ := a.installID()
  97. if id1 != id2 {
  98. t.Fatalf("el id no es estable: %q != %q", id1, id2)
  99. }
  100. // Un agente nuevo sobre la misma BD reutiliza el id.
  101. a2 := New(d, "v1", false)
  102. id3, _ := a2.installID()
  103. if id3 != id1 {
  104. t.Fatalf("el id debería persistir entre arranques: %q != %q", id3, id1)
  105. }
  106. }
  107. func TestHTTPErrorDoesNotMarkSent(t *testing.T) {
  108. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  109. w.WriteHeader(http.StatusNotFound)
  110. }))
  111. defer srv.Close()
  112. t.Setenv("NETPULSE_TELEMETRY", "1")
  113. t.Setenv("NETPULSE_TELEMETRY_URL", srv.URL+"/instances")
  114. d := openTestDB(t)
  115. a := New(d, "v1", false)
  116. if err := a.SendOnce(context.Background()); err == nil {
  117. t.Fatal("un 404 debería devolver error")
  118. }
  119. if !a.Due() {
  120. t.Fatal("un fallo no debe marcar como enviado (se reintentará)")
  121. }
  122. }