4
0

orchestr_apply_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // orchestr_apply_test.go — tests del endpoint /api/orchestr/apply (#451) con
  2. // el contrato F-06: las ops (ya envueltas con ownership) van al agente por
  3. // SSE y no hay ninguna petición HTTP al executor de NetGrip.
  4. package httpapi_test
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "io"
  9. "net/http"
  10. "sync/atomic"
  11. "testing"
  12. "github.com/gnacho/netpulse/agent/executor"
  13. )
  14. func TestOrchestrApplyCreatesAndAppliesPlan(t *testing.T) {
  15. ts, _ := newSSEPlanServer(t, nil)
  16. netgripMock, hits := countingNetGripMock(t)
  17. addPlanRouter(t, ts, "flint2", netgripMock.Listener.Addr().String(), true, "netgrip-secret")
  18. _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
  19. events := connectAgentStream(t, ts.URL, "flint2")
  20. ops := []executor.Op{{
  21. Kind: "uci_set",
  22. Args: map[string]string{"config": "dhcp", "section": "@dnsmasq[0]", "option": "server", "value": "1.1.1.1"},
  23. Desc: "set dns",
  24. }}
  25. body, _ := json.Marshal(map[string]any{
  26. "routerId": "flint2",
  27. "resource": "guestwifi",
  28. "diff": ops,
  29. })
  30. req, _ := http.NewRequest("POST", ts.URL+"/api/orchestr/apply", bytes.NewReader(body))
  31. req.Header.Set("Cookie", "session="+cookie)
  32. req.Header.Set("Content-Type", "application/json")
  33. res, err := http.DefaultClient.Do(req)
  34. if err != nil {
  35. t.Fatalf("apply: %v", err)
  36. }
  37. defer res.Body.Close()
  38. if res.StatusCode != http.StatusAccepted {
  39. b, _ := io.ReadAll(res.Body)
  40. t.Fatalf("apply: esperaba 202, got %d: %s", res.StatusCode, string(b))
  41. }
  42. if got := atomic.LoadInt32(hits); got != 0 {
  43. t.Fatalf("NetGrip recibió %d peticiones HTTP; el apply debe ir por SSE", got)
  44. }
  45. // El agente recibe el plan completo, con las ops de ownership delante.
  46. ev := waitAgentEvent(t, events, "apply")
  47. var payload struct {
  48. PlanID string `json:"plan_id"`
  49. Ops []executor.Op `json:"ops"`
  50. }
  51. if err := json.Unmarshal([]byte(ev.data), &payload); err != nil {
  52. t.Fatalf("payload SSE apply inválido (%q): %v", ev.data, err)
  53. }
  54. if len(payload.Ops) < 3 {
  55. t.Fatalf("el agente debería recibir al menos 3 ops, got %d", len(payload.Ops))
  56. }
  57. if payload.Ops[0].Kind != "ownership_mode" || payload.Ops[0].Args["enforce"] != "true" {
  58. t.Errorf("op[0] debería ser ownership_mode enforce=true, got %+v", payload.Ops[0])
  59. }
  60. if payload.Ops[1].Kind != "uci_set_managed" {
  61. t.Errorf("op[1] debería ser uci_set_managed, got %+v", payload.Ops[1])
  62. }
  63. var planID string
  64. if err := ts.db.QueryRow("SELECT id FROM orchestr_plans ORDER BY created_at DESC LIMIT 1").Scan(&planID); err != nil {
  65. t.Fatalf("select plan: %v", err)
  66. }
  67. if payload.PlanID != planID {
  68. t.Errorf("plan_id SSE: got %q, want %q", payload.PlanID, planID)
  69. }
  70. if got := planStatus(t, ts, planID); got != "applying" {
  71. t.Fatalf("plan status: esperaba applying, got %s", got)
  72. }
  73. }