proxmox_gate_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // proxmox_gate_test.go - toggle server-side de la integración Proxmox (#968):
  2. // con settings.integrations.proxmox="false" el adapter NO hace ni un solo
  3. // request al PVE; ausente o "true" sondea con normalidad.
  4. package adapters
  5. import (
  6. "net/http"
  7. "net/http/httptest"
  8. "sync/atomic"
  9. "testing"
  10. "github.com/gnacho/netpulse/server-go/internal/pve"
  11. )
  12. func TestPveInventoryGateDropsRequests(t *testing.T) {
  13. var hits atomic.Int64
  14. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  15. hits.Add(1)
  16. w.Header().Set("Content-Type", "application/json")
  17. switch r.URL.Path {
  18. case "/api2/json/cluster/resources":
  19. _, _ = w.Write([]byte(`{"data":[{"id":"node/pve1","node":"pve1","type":"node","status":"online"}]}`))
  20. case "/api2/json/cluster/status":
  21. _, _ = w.Write([]byte(`{"data":[{"type":"node","name":"pve1","ip":"192.0.2.2","online":1}]}`))
  22. default:
  23. _, _ = w.Write([]byte(`{"data":{}}`))
  24. }
  25. }))
  26. defer srv.Close()
  27. d := openLiveTestDB(t)
  28. cfg := pve.Config{URL: srv.URL, TokenID: "netpulse@pam!t", Secret: "s"}
  29. if err := pve.SaveInstances(d.DB, []pve.Instance{{ID: "acasa", Name: "casa", Config: cfg}}); err != nil {
  30. t.Fatalf("SaveInstances: %v", err)
  31. }
  32. l := NewLive(nil, d, nil, nil)
  33. // Integración OFF: 0 requests aunque haya instancia configurada.
  34. if _, err := d.Exec("INSERT INTO kv (key, value) VALUES (?, ?)", pveIntegrationKey, "false"); err != nil {
  35. t.Fatalf("kv off: %v", err)
  36. }
  37. if inv := l.pveInventoryCached(); inv != nil {
  38. t.Fatalf("gate off: inventario %v, esperado nil", inv)
  39. }
  40. if n := hits.Load(); n != 0 {
  41. t.Fatalf("gate off: %d requests al PVE, esperado 0", n)
  42. }
  43. // Integración ON: sondea (resources + status + lo que pida el inventario).
  44. if _, err := d.Exec("UPDATE kv SET value = ? WHERE key = ?", "true", pveIntegrationKey); err != nil {
  45. t.Fatalf("kv on: %v", err)
  46. }
  47. l.pveClients = nil
  48. l.pveKey = ""
  49. if inv := l.pveInventoryCached(); inv == nil {
  50. t.Fatal("gate on: inventario nil, esperado datos")
  51. }
  52. if n := hits.Load(); n == 0 {
  53. t.Fatal("gate on: 0 requests al PVE, esperado sondeo real")
  54. }
  55. }
  56. func TestPveInventoryGateDefaultOn(t *testing.T) {
  57. // Clave ausente (instalación previa a #968) = integración activa.
  58. d := openLiveTestDB(t)
  59. l := NewLive(nil, d, nil, nil)
  60. if !l.pveIntegrationEnabled() {
  61. t.Fatal("clave ausente: integración desactivada, esperado activa")
  62. }
  63. if _, err := d.Exec("INSERT INTO kv (key, value) VALUES (?, ?)", pveIntegrationKey, "false"); err != nil {
  64. t.Fatalf("kv off: %v", err)
  65. }
  66. if l.pveIntegrationEnabled() {
  67. t.Fatal("clave false: integración activa, esperado desactivada")
  68. }
  69. }