ddns_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package orchestr
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/gnacho/netpulse/agent/executor"
  6. )
  7. // Fixture: router apk con ddns-scripts instalado y sección activa.
  8. // Formato de `uci show ddns` (no de fichero config).
  9. const ddnsOutApkActivo = `===PKG_MGR===
  10. apk
  11. ===INSTALLED===
  12. yes
  13. ===DDNS_UCI===
  14. ddns.global=ddns
  15. ddns.global.ddns_dateformat='%F %R'
  16. ddns.global.ddns_loglines='250'
  17. ddns.global.upd_privateip='0'
  18. ddns.myddns=service
  19. ddns.myddns.enabled='1'
  20. ddns.myddns.service_name='duckdns.org'
  21. ddns.myddns.lookup_host='mi.duckdns.org'
  22. ddns.myddns.domain='mi.duckdns.org'
  23. ddns.myddns.username='token'
  24. ddns.myddns.password='token123'
  25. ddns.myddns.ip_source='network'
  26. ddns.myddns.ip_network='wan'
  27. ===END===`
  28. // TestParseDdnsApkActivo: detecta apk, instalado y sección activa.
  29. func TestParseDdnsApkActivo(t *testing.T) {
  30. sc := parseDdns(ddnsOutApkActivo)
  31. if sc.Manager != "apk" {
  32. t.Errorf("Manager: got %q want apk", sc.Manager)
  33. }
  34. if !sc.Installed {
  35. t.Error("Installed: esperaba true")
  36. }
  37. if !sc.SectionExists {
  38. t.Error("SectionExists: esperaba true")
  39. }
  40. if !sc.ActiveSection {
  41. t.Error("ActiveSection: esperaba true (enabled='1')")
  42. }
  43. }
  44. // TestParseDdnsNoInstalado: sin ddns-scripts ni sección.
  45. func TestParseDdnsNoInstalado(t *testing.T) {
  46. out := `===PKG_MGR===
  47. opkg
  48. ===INSTALLED===
  49. no
  50. ===DDNS_UCI===
  51. ===END===`
  52. sc := parseDdns(out)
  53. if sc.Manager != "opkg" {
  54. t.Errorf("Manager: got %q want opkg", sc.Manager)
  55. }
  56. if sc.Installed {
  57. t.Error("Installed: esperaba false")
  58. }
  59. if sc.SectionExists || sc.ActiveSection {
  60. t.Error("no debería haber sección activa sin ddns-scripts")
  61. }
  62. }
  63. // TestDdnsOpsEnableInstalaYConfigura: enable instala + configura + arranca.
  64. func TestDdnsOpsEnableInstalaYConfigura(t *testing.T) {
  65. sc := parseDdns(`===PKG_MGR===
  66. apk
  67. ===INSTALLED===
  68. no
  69. ===DDNS_UCI===
  70. ===END===`)
  71. ops := DdnsOps(DdnsDesired{
  72. Enabled: true, ServiceName: "duckdns.org",
  73. Domain: "mi.duckdns.org", Username: "token", Password: "token123",
  74. }, sc)
  75. if err := validateDdnsOps(ops); err != nil {
  76. t.Fatalf("ops enable no validan en executor: %v", err)
  77. }
  78. // Debe instalar con apk_install y crear la sección con uci_set_named.
  79. kinds := map[string]bool{}
  80. for _, o := range ops {
  81. kinds[o.Kind] = true
  82. }
  83. if !kinds["apk_install"] {
  84. t.Error("enable sin apk_install (ddns-scripts no instalado)")
  85. }
  86. if !kinds["uci_set_named"] {
  87. t.Error("enable sin uci_set_named (sección ddns a crear)")
  88. }
  89. if !kinds["service"] {
  90. t.Error("enable sin service (start/enable ddns)")
  91. }
  92. // Verificar que configura los campos clave.
  93. found := false
  94. for _, o := range ops {
  95. if o.Kind == "uci_set" && o.Args["option"] == "domain" && o.Args["value"] == "mi.duckdns.org" {
  96. found = true
  97. }
  98. }
  99. if !found {
  100. t.Error("enable sin uci_set del dominio")
  101. }
  102. }
  103. // TestDdnsOpsEnableYaInstalado: no re-instala si ya está.
  104. func TestDdnsOpsEnableYaInstalado(t *testing.T) {
  105. sc := parseDdns(ddnsOutApkActivo)
  106. ops := DdnsOps(DdnsDesired{
  107. Enabled: true, ServiceName: "duckdns.org",
  108. Domain: "mi.duckdns.org", Username: "token", Password: "token123",
  109. }, sc)
  110. if err := validateDdnsOps(ops); err != nil {
  111. t.Fatalf("ops no validan: %v", err)
  112. }
  113. for _, o := range ops {
  114. if o.Kind == "apk_install" || o.Kind == "install" {
  115. t.Error("no debería re-instalar ddns-scripts si ya está")
  116. }
  117. if o.Kind == "uci_set_named" {
  118. t.Error("no debería recrear la sección si ya existe")
  119. }
  120. }
  121. }
  122. // TestDdnsOpsDisable: desactiva (enabled=0 + stop + disable) sin desinstalar.
  123. func TestDdnsOpsDisable(t *testing.T) {
  124. sc := parseDdns(ddnsOutApkActivo)
  125. ops := DdnsOps(DdnsDesired{Enabled: false}, sc)
  126. if err := validateDdnsOps(ops); err != nil {
  127. t.Fatalf("ops disable no validan: %v", err)
  128. }
  129. joined := strings.Join(kindsOf(ops), ",")
  130. if !strings.Contains(joined, "uci_set") {
  131. t.Error("disable sin uci_set enabled=0")
  132. }
  133. if !strings.Contains(joined, "service") {
  134. t.Error("disable sin service stop/disable")
  135. }
  136. }
  137. func kindsOf(ops []executor.Op) []string {
  138. out := make([]string, len(ops))
  139. for i, o := range ops {
  140. out[i] = o.Kind
  141. }
  142. return out
  143. }