configured_1895_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package discover
  2. // #1895: el descubrimiento ofrecía como nuevas unidades que ya estaban dadas
  3. // de alta porque "configured" se calculaba comparando la cadena literal del
  4. // host configurado con la IP del barrido.
  5. import (
  6. "context"
  7. "errors"
  8. "testing"
  9. "github.com/gnacho/netpulse/server-go/internal/adapters"
  10. )
  11. func TestHostKeyNormaliza(t *testing.T) {
  12. cases := map[string]string{
  13. "192.168.1.2": "192.168.1.2",
  14. "192.168.1.2:2222": "192.168.1.2",
  15. "AP.Lan.": "ap.lan",
  16. " ap.lan ": "ap.lan",
  17. "[::1]:22": "::1",
  18. "": "",
  19. }
  20. for in, want := range cases {
  21. if got := hostKey(in); got != want {
  22. t.Errorf("hostKey(%q) = %q, quiero %q", in, got, want)
  23. }
  24. }
  25. }
  26. func TestConfiguredIndexReconoceUnidadesExistentes(t *testing.T) {
  27. routers := []adapters.RouterConfig{
  28. {ID: "gw", Host: "192.168.1.1"},
  29. {ID: "ap", Host: "ap.lan"},
  30. {ID: "rt3", Host: "RT3.LAN."},
  31. {ID: "legacy", Host: "192.168.1.4:2222"},
  32. {ID: "roto", Host: "no-resuelve.lan"},
  33. }
  34. lookup := func(_ context.Context, host string) ([]string, error) {
  35. switch host {
  36. case "ap.lan":
  37. return []string{"192.168.1.2"}, nil
  38. case "rt3.lan":
  39. return []string{"192.168.1.3"}, nil
  40. default:
  41. return nil, errors.New("sin DNS")
  42. }
  43. }
  44. got := configuredIndex(context.Background(), routers, lookup)
  45. for ip, want := range map[string]bool{
  46. "192.168.1.1": true, // gateway por IP literal
  47. "192.168.1.2": true, // AP configurado por nombre
  48. "192.168.1.3": true, // nombre con mayúsculas y punto final
  49. "192.168.1.4": true, // IP con puerto en el alta
  50. "192.168.1.50": false, // unidad realmente nueva
  51. } {
  52. if got[hostKey(ip)] != want {
  53. t.Errorf("configured[%s] = %v, quiero %v", ip, got[hostKey(ip)], want)
  54. }
  55. }
  56. // El nombre que no resuelve sigue marcado por su cadena literal, pero no
  57. // inventa ninguna IP.
  58. if !got["no-resuelve.lan"] {
  59. t.Error("el nombre sin DNS debería seguir marcado por cadena literal")
  60. }
  61. }