device_reservation_hostname_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // device_reservation_hostname_test.go — issue #800: aplicar el nombre visible
  2. // del dispositivo como hostname de su reserva DHCP. Con el fake SSHRunner
  3. // (scripteable); NINGÚN router real se toca. Cubre crear/actualizar/no-op,
  4. // dry-run, conflicto de IP, validación y rollback ante fallo de reload.
  5. package httpapi_test
  6. import (
  7. "fmt"
  8. "net/http"
  9. "strings"
  10. "testing"
  11. )
  12. // dhcpDevHostNoName: reserva existente SIN opción name (rollback = delete).
  13. const dhcpDevHostNoName = `dhcp.np_host_aabbccddeeff=host
  14. dhcp.np_host_aabbccddeeff.mac='aa:bb:cc:dd:ee:ff'
  15. dhcp.np_host_aabbccddeeff.ip='192.168.1.60'
  16. `
  17. func hostnameBody(ip, hostname string) string {
  18. return fmt.Sprintf(`{"ip":%q,"hostname":%q}`, ip, hostname)
  19. }
  20. func TestReservationHostnameCreate(t *testing.T) {
  21. ssh := newSSH(dhcpEmpty, fwEmpty)
  22. ts := makeDeviceActionsTestServer(t, ssh)
  23. // Apply real: crea la sección host con name/mac/ip.
  24. res := deviceReq(t, "PUT", ts.URL, "/api/devices/"+devMAC+"/reservation-hostname", ts.cookie, hostnameBody("192.168.1.60", "tv-salon"))
  25. if res.StatusCode != http.StatusOK {
  26. t.Fatalf("status: %d", res.StatusCode)
  27. }
  28. body := decodeBody(t, res)
  29. if body["created"] != true {
  30. t.Fatalf("created: %v", body["created"])
  31. }
  32. if !ssh.saw("uci set dhcp.np_host_aabbccddeeff=host") {
  33. t.Fatalf("no creó la sección: %v", ssh.cmdsSnapshot())
  34. }
  35. if !ssh.saw(".name='tv-salon'") || !ssh.saw(".ip='192.168.1.60'") {
  36. t.Fatalf("faltan set name/ip: %v", ssh.cmdsSnapshot())
  37. }
  38. if !ssh.saw("uci commit dhcp") || !ssh.saw("/etc/init.d/dnsmasq restart") {
  39. t.Fatalf("falta commit/reload: %v", ssh.cmdsSnapshot())
  40. }
  41. }
  42. func TestReservationHostnameCreateDryRun(t *testing.T) {
  43. ssh := newSSH(dhcpEmpty, fwEmpty)
  44. ts := makeDeviceActionsTestServer(t, ssh)
  45. res := deviceReq(t, "PUT", ts.URL, "/api/devices/"+devMAC+"/reservation-hostname?dry_run=1", ts.cookie, hostnameBody("192.168.1.60", "tv-salon"))
  46. if res.StatusCode != http.StatusOK {
  47. t.Fatalf("status: %d", res.StatusCode)
  48. }
  49. body := decodeBody(t, res)
  50. if body["dryRun"] != true {
  51. t.Fatalf("dryRun: %v", body["dryRun"])
  52. }
  53. apply, _ := body["apply"].([]any)
  54. if len(apply) != 4 {
  55. t.Fatalf("apply: %v", apply)
  56. }
  57. if ssh.saw("uci commit dhcp") {
  58. t.Fatalf("dry-run ejecutó comandos: %v", ssh.cmdsSnapshot())
  59. }
  60. }
  61. func TestReservationHostnameUpdate(t *testing.T) {
  62. ssh := newSSH(dhcpDevHost, fwEmpty)
  63. ts := makeDeviceActionsTestServer(t, ssh)
  64. // Dry-run: apply solo toca el name y el rollback restaura el name anterior.
  65. res := deviceReq(t, "PUT", ts.URL, "/api/devices/"+devMAC+"/reservation-hostname?dry_run=1", ts.cookie, hostnameBody("192.168.1.60", "movil-nacho"))
  66. if res.StatusCode != http.StatusOK {
  67. t.Fatalf("dry-run status: %d", res.StatusCode)
  68. }
  69. plan := decodeBody(t, res)
  70. apply, _ := plan["apply"].([]any)
  71. rollback, _ := plan["rollback"].([]any)
  72. if len(apply) != 1 || !strings.Contains(fmt.Sprint(apply[0]), "name='movil-nacho'") {
  73. t.Fatalf("apply: %v", apply)
  74. }
  75. if len(rollback) != 1 || !strings.Contains(fmt.Sprint(rollback[0]), "name='tv-salon'") {
  76. t.Fatalf("rollback: %v", rollback)
  77. }
  78. // Apply real: actualiza el name; no recrea la sección.
  79. res = deviceReq(t, "PUT", ts.URL, "/api/devices/"+devMAC+"/reservation-hostname", ts.cookie, hostnameBody("192.168.1.60", "movil-nacho"))
  80. if res.StatusCode != http.StatusOK {
  81. t.Fatalf("status: %d", res.StatusCode)
  82. }
  83. body := decodeBody(t, res)
  84. if body["created"] != false {
  85. t.Fatalf("created: %v", body["created"])
  86. }
  87. if !ssh.saw("uci set dhcp.np_host_aabbccddeeff.name='movil-nacho'") {
  88. t.Fatalf("no actualizó el name: %v", ssh.cmdsSnapshot())
  89. }
  90. if ssh.saw("np_host_aabbccddeeff=host") {
  91. t.Fatalf("recreate innecesario de la sección: %v", ssh.cmdsSnapshot())
  92. }
  93. }
  94. func TestReservationHostnameUpdateNoNameRollback(t *testing.T) {
  95. ssh := newSSH(dhcpEmpty+dhcpDevHostNoName, fwEmpty)
  96. ts := makeDeviceActionsTestServer(t, ssh)
  97. // La sección no tenía name: el dry-run planea BORRAR la opción en el rollback.
  98. res := deviceReq(t, "PUT", ts.URL, "/api/devices/"+devMAC+"/reservation-hostname?dry_run=1", ts.cookie, hostnameBody("192.168.1.60", "tv-salon"))
  99. if res.StatusCode != http.StatusOK {
  100. t.Fatalf("status: %d", res.StatusCode)
  101. }
  102. plan := decodeBody(t, res)
  103. rollback, _ := plan["rollback"].([]any)
  104. if len(rollback) != 1 || !strings.Contains(fmt.Sprint(rollback[0]), "uci delete dhcp.np_host_aabbccddeeff.name") {
  105. t.Fatalf("rollback: %v", rollback)
  106. }
  107. }
  108. func TestReservationHostnameNoop(t *testing.T) {
  109. ssh := newSSH(dhcpDevHost, fwEmpty)
  110. ts := makeDeviceActionsTestServer(t, ssh)
  111. res := deviceReq(t, "PUT", ts.URL, "/api/devices/"+devMAC+"/reservation-hostname", ts.cookie, hostnameBody("192.168.1.60", "tv-salon"))
  112. if res.StatusCode != http.StatusOK {
  113. t.Fatalf("status: %d", res.StatusCode)
  114. }
  115. body := decodeBody(t, res)
  116. if body["noop"] != true {
  117. t.Fatalf("noop: %v", body["noop"])
  118. }
  119. if ssh.saw("uci commit dhcp") {
  120. t.Fatalf("no-op ejecutó escritura: %v", ssh.cmdsSnapshot())
  121. }
  122. }
  123. func TestReservationHostnameConflict(t *testing.T) {
  124. ssh := newSSH(dhcpOtherHost, fwEmpty)
  125. ts := makeDeviceActionsTestServer(t, ssh)
  126. res := deviceReq(t, "PUT", ts.URL, "/api/devices/"+devMAC+"/reservation-hostname", ts.cookie, hostnameBody("192.168.1.50", "tv-salon"))
  127. if res.StatusCode != http.StatusConflict {
  128. t.Fatalf("status: %d", res.StatusCode)
  129. }
  130. if ssh.saw("uci commit dhcp") {
  131. t.Fatalf("conflicto ejecutó escritura: %v", ssh.cmdsSnapshot())
  132. }
  133. }
  134. func TestReservationHostnameApplyValidation(t *testing.T) {
  135. ssh := newSSH(dhcpEmpty, fwEmpty)
  136. ts := makeDeviceActionsTestServer(t, ssh)
  137. for _, body := range []string{
  138. hostnameBody("192.168.1.60", "tv salón"), // espacio: no es hostname DNS
  139. hostnameBody("192.168.1.60", ""), // hostname vacío
  140. `{"ip":"nop","hostname":"tv-salon"}`, // IP inválida
  141. } {
  142. res := deviceReq(t, "PUT", ts.URL, "/api/devices/"+devMAC+"/reservation-hostname", ts.cookie, body)
  143. if res.StatusCode != http.StatusBadRequest {
  144. t.Fatalf("body %s: status %d", body, res.StatusCode)
  145. }
  146. res.Body.Close()
  147. }
  148. if ssh.saw("uci commit dhcp") {
  149. t.Fatalf("validación ejecutó escritura: %v", ssh.cmdsSnapshot())
  150. }
  151. }
  152. func TestReservationHostnameReloadFailureRollsBack(t *testing.T) {
  153. ssh := newSSH(dhcpEmpty, fwEmpty, sshRule{
  154. contains: "/etc/init.d/dnsmasq restart",
  155. err: fmt.Errorf("boom"),
  156. })
  157. ts := makeDeviceActionsTestServer(t, ssh)
  158. res := deviceReq(t, "PUT", ts.URL, "/api/devices/"+devMAC+"/reservation-hostname", ts.cookie, hostnameBody("192.168.1.60", "tv-salon"))
  159. if res.StatusCode != http.StatusInternalServerError {
  160. t.Fatalf("status: %d", res.StatusCode)
  161. }
  162. res.Body.Close()
  163. // El rollback borra la sección creada.
  164. if !ssh.saw("uci delete dhcp.np_host_aabbccddeeff") {
  165. t.Fatalf("falta rollback: %v", ssh.cmdsSnapshot())
  166. }
  167. if !strings.Contains(strings.Join(ssh.cmdsSnapshot(), "\n"), "uci commit dhcp") {
  168. t.Fatalf("falta commit del rollback: %v", ssh.cmdsSnapshot())
  169. }
  170. }