hostResources_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // hostResources_test.go: parsers de HOST-RESOURCES con la muestra real de un
  2. // CRS305 (RouterOS 7.25) capturada por #1725, y los casos en los que el árbol
  3. // no existe o no dice nada útil.
  4. package snmp
  5. import (
  6. "math/big"
  7. "testing"
  8. "github.com/gosnmp/gosnmp"
  9. )
  10. // Walk real (192.168.2.15, community del SNMP del equipo):
  11. //
  12. // hrProcessorLoad.1 = 1 (cpu0)
  13. // hrProcessorLoad.2 = 1 (cpu1)
  14. // hrStorageDescr.65536 = "main memory" hrStorageSize.65536 = 524288
  15. // hrStorageUsed.65536 = 89840 allocationUnits = 1024 B
  16. // hrStorageDescr.131072 = "system disk" (16384 KB)
  17. //
  18. // → CPU 1 %, RAM 89840/524288 = 17 %. El propio API de RouterOS decía
  19. // cpu-load=1 y free-memory=446193664 de total=536870912 (17 % usado): las dos
  20. // fuentes cuadran.
  21. func TestParseHrProcessorLoadsRealRouterOS(t *testing.T) {
  22. cpu, ok := ParseHrProcessorLoads([]int{1, 1})
  23. if !ok || cpu != 1 {
  24. t.Fatalf("cpu = %d/%v, want 1/true", cpu, ok)
  25. }
  26. }
  27. // El máximo, no la media: un núcleo al 73 % en un dual es un equipo ocupado, y
  28. // la media (37) escondería justo lo que la tarjeta tiene que avisar.
  29. func TestParseHrProcessorLoadsTakesMax(t *testing.T) {
  30. cpu, ok := ParseHrProcessorLoads([]int{2, 73, 15})
  31. if !ok || cpu != 73 {
  32. t.Fatalf("cpu = %d/%v, want 73 (máximo por núcleo)", cpu, ok)
  33. }
  34. }
  35. func TestParseHrProcessorLoadsRejectsGarbage(t *testing.T) {
  36. if cpu, ok := ParseHrProcessorLoads([]int{200, 300}); ok || cpu != 0 {
  37. t.Errorf("fuera de 0..100 no es una utilización: (%d,%v)", cpu, ok)
  38. }
  39. if _, ok := ParseHrProcessorLoads(nil); ok {
  40. t.Error("sin filas ok debe ser false (null en la UI, no 0)")
  41. }
  42. // Un agente que publica un bicho suelto sí tiene lectura: el descarte no
  43. // puede arrastrar a las filas válidas.
  44. if cpu, ok := ParseHrProcessorLoads([]int{42, 7000}); !ok || cpu != 42 {
  45. t.Errorf("cpu = %d/%v, want 42/true", cpu, ok)
  46. }
  47. }
  48. func TestParseHrStorageRAMRealRouterOS(t *testing.T) {
  49. rows := []HrStorageRow{
  50. {Descr: "virtual memory", Size: 524288, Used: 0},
  51. {Descr: "main memory", Size: 524288, Used: 89840},
  52. {Descr: "system disk", Size: 16384, Used: 4096},
  53. }
  54. pct, ok := ParseHrStorageRAM(rows)
  55. if !ok || pct != 17 {
  56. t.Fatalf("ram = %d/%v, want 17 (89840/524288)", pct, ok)
  57. }
  58. }
  59. // La fila se reconoce por su descripción, nunca por el índice: si no hay
  60. // "main memory", la RAM se queda en null, aunque el equipo publique discos y
  61. // memoria virtual. Un 100 % de "system disk" pintado como RAM es peor que un
  62. // null.
  63. func TestParseHrStorageRAMNoMainMemoryRowStaysUnknown(t *testing.T) {
  64. rows := []HrStorageRow{
  65. {Descr: "system disk", Size: 16384, Used: 15000},
  66. {Descr: "virtual memory", Size: 524288, Used: 10},
  67. }
  68. if pct, ok := ParseHrStorageRAM(rows); ok || pct != 0 {
  69. t.Fatalf("ram = %d/%v, want 0/false sin fila de memoria principal", pct, ok)
  70. }
  71. }
  72. func TestParseHrStorageRAMVariantsAndBrokenRows(t *testing.T) {
  73. if pct, ok := ParseHrStorageRAM([]HrStorageRow{{Descr: "Main Memory", Size: 1000, Used: 250}}); !ok || pct != 25 {
  74. t.Errorf("la descripción se compara sin importar mayúsculas: (%d,%v)", pct, ok)
  75. }
  76. if _, ok := ParseHrStorageRAM([]HrStorageRow{{Descr: "main memory", Size: 0, Used: 0}}); ok {
  77. t.Error("size 0 = fila sin dimensionar, no 0 % ni división por cero")
  78. }
  79. if _, ok := ParseHrStorageRAM([]HrStorageRow{{Descr: "main memory", Size: 100, Used: 140}}); ok {
  80. t.Error("used > size es un contador roto: no se publica un 140 %")
  81. }
  82. // Redondeo: 1/3 = 33,3 %; 2/3 = 66,7 %.
  83. if pct, _ := ParseHrStorageRAM([]HrStorageRow{{Descr: "main memory", Size: 3, Used: 1}}); pct != 33 {
  84. t.Errorf("1/3 = %d, want 33", pct)
  85. }
  86. if pct, _ := ParseHrStorageRAM([]HrStorageRow{{Descr: "main memory", Size: 3, Used: 2}}); pct != 67 {
  87. t.Errorf("2/3 = %d, want 67", pct)
  88. }
  89. }
  90. func TestPduIntAceptaNumericosRechazaLoDemas(t *testing.T) {
  91. if v, ok := pduInt(gosnmp.SnmpPDU{Type: gosnmp.Gauge32, Value: big.NewInt(73)}); !ok || v != 73 {
  92. t.Errorf("gauge 73: (%d,%v)", v, ok)
  93. }
  94. if _, ok := pduInt(gosnmp.SnmpPDU{Type: gosnmp.OctetString, Value: "73"}); ok {
  95. t.Error("un string no es una utilización")
  96. }
  97. if _, ok := pduInt(gosnmp.SnmpPDU{Type: gosnmp.NoSuchInstance}); ok {
  98. t.Error("NoSuchInstance debe dar ok=false")
  99. }
  100. if _, ok := pduInt(gosnmp.SnmpPDU{Type: gosnmp.Counter32, Value: big.NewInt(-1)}); ok {
  101. t.Error("un negativo no es una utilización")
  102. }
  103. }