live_temp_threshold_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // live_temp_threshold_test.go — issue #716: umbral de temperatura alta por
  2. // router. El umbral configurado se usa en el estado hot del router, en la
  3. // salud global y en el emisor de alerta, cayendo al default 65 cuando no hay.
  4. package adapters
  5. import (
  6. "strings"
  7. "testing"
  8. )
  9. func TestRouterConfigTempThresholdValue(t *testing.T) {
  10. if got := (RouterConfig{}).TempThresholdValue(); got != DefaultTempThreshold {
  11. t.Fatalf("sin umbral: %d, esperaba %d", got, DefaultTempThreshold)
  12. }
  13. if got := (RouterConfig{TempThreshold: iptr(70)}).TempThresholdValue(); got != 70 {
  14. t.Fatalf("umbral 70: %d, esperaba 70", got)
  15. }
  16. if got := (RouterConfig{TempThreshold: iptr(0)}).TempThresholdValue(); got != DefaultTempThreshold {
  17. t.Fatalf("umbral 0: %d, esperaba default %d", got, DefaultTempThreshold)
  18. }
  19. }
  20. func TestBuildRouterUsesPerRouterTempThreshold(t *testing.T) {
  21. l := NewLive(nil, nil, nil, nil)
  22. // Umbral 70 y 68 °C → por debajo: online y sin hotMetric.
  23. p := &routerPolled{
  24. cfg: RouterConfig{ID: "rt1", Name: "RT1", Host: "192.168.1.2", TempThreshold: iptr(70)},
  25. temp: 68,
  26. polledAt: 1000,
  27. }
  28. r := l.buildRouter(p, nil)
  29. if r.Status != "online" {
  30. t.Fatalf("68 °C con umbral 70: status=%q, esperaba online", r.Status)
  31. }
  32. if r.HotMetric != "" {
  33. t.Fatalf("68 °C con umbral 70: hotMetric=%q, esperaba vacío", r.HotMetric)
  34. }
  35. if r.TempThreshold != 70 {
  36. t.Fatalf("TempThreshold resuelto=%d, esperaba 70", r.TempThreshold)
  37. }
  38. // 72 °C supera el umbral 70 → warn + hotMetric.
  39. p2 := &routerPolled{
  40. cfg: RouterConfig{ID: "rt1", Name: "RT1", Host: "192.168.1.2", TempThreshold: iptr(70)},
  41. temp: 72,
  42. polledAt: 1000,
  43. }
  44. r2 := l.buildRouter(p2, nil)
  45. if r2.Status != "warn" {
  46. t.Fatalf("72 °C con umbral 70: status=%q, esperaba warn", r2.Status)
  47. }
  48. if r2.HotMetric != "temp" {
  49. t.Fatalf("72 °C con umbral 70: hotMetric=%q, esperaba temp", r2.HotMetric)
  50. }
  51. }
  52. func TestBuildRouterTempThresholdFallsBackToDefault(t *testing.T) {
  53. l := NewLive(nil, nil, nil, nil)
  54. // Sin umbral configurado → 65. 66 °C dispara warn (comportamiento previo).
  55. p := &routerPolled{
  56. cfg: RouterConfig{ID: "rt1", Name: "RT1", Host: "192.168.1.2"},
  57. temp: 66,
  58. polledAt: 1000,
  59. }
  60. r := l.buildRouter(p, nil)
  61. if r.Status != "warn" || r.HotMetric != "temp" {
  62. t.Fatalf("66 °C sin umbral: status=%q hotMetric=%q, esperaba warn/temp", r.Status, r.HotMetric)
  63. }
  64. if r.TempThreshold != DefaultTempThreshold {
  65. t.Fatalf("TempThreshold resuelto=%d, esperaba %d", r.TempThreshold, DefaultTempThreshold)
  66. }
  67. }
  68. func TestEmitTempAlertUsesPerRouterThreshold(t *testing.T) {
  69. l := liveTestLive()
  70. cfg := RouterConfig{ID: "patio", Name: "Patio", TempThreshold: iptr(70)}
  71. router := Router{Name: "Patio", Temp: iptr(72), TempThreshold: 70}
  72. l.mu.Lock()
  73. l.emitTempAlert(cfg, router)
  74. l.mu.Unlock()
  75. list := l.engine.List()
  76. if len(list) != 1 {
  77. t.Fatalf("72 °C con umbral 70: %d alertas, esperaba 1", len(list))
  78. }
  79. if !strings.Contains(list[0].Description, "70 °C") {
  80. t.Fatalf("la descripción no refleja el umbral del router: %q", list[0].Description)
  81. }
  82. if list[0].Vars["threshold"] != "70" {
  83. t.Fatalf("Vars.threshold=%q, esperaba 70", list[0].Vars["threshold"])
  84. }
  85. }
  86. func TestEmitTempAlertNoAlertBelowThreshold(t *testing.T) {
  87. l := liveTestLive()
  88. cfg := RouterConfig{ID: "patio", Name: "Patio", TempThreshold: iptr(70)}
  89. router := Router{Name: "Patio", Temp: iptr(68), TempThreshold: 70}
  90. l.mu.Lock()
  91. l.emitTempAlert(cfg, router)
  92. l.mu.Unlock()
  93. if n := len(l.engine.List()); n != 0 {
  94. t.Fatalf("68 °C con umbral 70: %d alertas, esperaba 0", n)
  95. }
  96. }
  97. func TestEmitTempAlertFallsBackToDefault(t *testing.T) {
  98. l := liveTestLive()
  99. cfg := RouterConfig{ID: "patio", Name: "Patio"} // sin umbral → default 65
  100. router := Router{Name: "Patio", Temp: iptr(66), TempThreshold: DefaultTempThreshold}
  101. l.mu.Lock()
  102. l.emitTempAlert(cfg, router)
  103. l.mu.Unlock()
  104. list := l.engine.List()
  105. if len(list) != 1 {
  106. t.Fatalf("66 °C sin umbral: %d alertas, esperaba 1", len(list))
  107. }
  108. if !strings.Contains(list[0].Description, "65 °C") {
  109. t.Fatalf("la descripción no usa el default 65: %q", list[0].Description)
  110. }
  111. }
  112. func TestComputeHealthUsesPerRouterTempThreshold(t *testing.T) {
  113. // 72 °C con umbral 70 penaliza (-8); 68 °C con umbral 70 no.
  114. routers := []Router{
  115. {Name: "hot", Status: "online", Temp: iptr(72), TempThreshold: 70},
  116. {Name: "cool", Status: "online", Temp: iptr(68), TempThreshold: 70},
  117. }
  118. h := computeHealth(routers, nil, nil)
  119. if h.Score != 92 {
  120. t.Fatalf("score=%d, esperaba 92", h.Score)
  121. }
  122. }