ownership_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package orchestr
  2. import (
  3. "testing"
  4. "github.com/gnacho/netpulse/agent/executor"
  5. )
  6. func TestWrapOwnershipEmpty(t *testing.T) {
  7. got := WrapOwnership(nil)
  8. if len(got) != 0 {
  9. t.Fatalf("esperaba [] vacío, got %d", len(got))
  10. }
  11. }
  12. func TestWrapOwnershipAddsModeAndManaged(t *testing.T) {
  13. ops := []executor.Op{
  14. {Kind: "uci_set", Args: map[string]string{"config": "dhcp", "section": "@dnsmasq[0]", "option": "server", "value": "1.1.1.1"}},
  15. {Kind: "uci_commit", Args: map[string]string{"config": "dhcp"}},
  16. }
  17. got := WrapOwnership(ops)
  18. if len(got) != 4 {
  19. t.Fatalf("esperaba 4 ops, got %d", len(got))
  20. }
  21. if got[0].Kind != "ownership_mode" || got[0].Args["enforce"] != "true" {
  22. t.Errorf("op[0] debería ser ownership_mode enforce=true, got %+v", got[0])
  23. }
  24. if got[1].Kind != "uci_set_managed" {
  25. t.Errorf("op[1] debería ser uci_set_managed, got %+v", got[1])
  26. }
  27. if got[2].Kind != "uci_set" {
  28. t.Errorf("op[2] debería ser uci_set original, got %+v", got[2])
  29. }
  30. }
  31. func TestWrapOwnershipDeduplicatesSections(t *testing.T) {
  32. ops := []executor.Op{
  33. {Kind: "uci_set", Args: map[string]string{"config": "wireless", "section": "guest", "option": "ssid", "value": "A"}},
  34. {Kind: "uci_set", Args: map[string]string{"config": "wireless", "section": "guest", "option": "key", "value": "B"}},
  35. {Kind: "uci_commit", Args: map[string]string{"config": "wireless"}},
  36. }
  37. got := WrapOwnership(ops)
  38. managed := 0
  39. for _, op := range got {
  40. if op.Kind == "uci_set_managed" {
  41. managed++
  42. }
  43. }
  44. if managed != 1 {
  45. t.Fatalf("esperaba 1 uci_set_managed, got %d", managed)
  46. }
  47. }
  48. func TestWrapOwnershipSkipsNonUci(t *testing.T) {
  49. ops := []executor.Op{
  50. {Kind: "service", Args: map[string]string{"name": "network", "action": "reload"}},
  51. }
  52. got := WrapOwnership(ops)
  53. if len(got) != 1 || got[0].Kind != "service" {
  54. t.Fatalf("no debería añadir ownership a ops no-UCI: %+v", got)
  55. }
  56. }
  57. func TestWrapOwnershipCoversDeleteSection(t *testing.T) {
  58. ops := []executor.Op{
  59. {Kind: "uci_delete_section", Args: map[string]string{"config": "wireless", "section": "guest"}},
  60. }
  61. got := WrapOwnership(ops)
  62. if got[0].Kind != "ownership_mode" || got[1].Kind != "uci_set_managed" {
  63. t.Fatalf("esperaba ownership_mode + uci_set_managed + delete, got %+v", got)
  64. }
  65. }