device_actions_dryrun_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // device_actions_dryrun_test.go — #754: los PUT de reserva y bloqueo con
  2. // ?dry_run=1 devuelven el plan de comandos SIN ejecutar nada en el router.
  3. package httpapi_test
  4. import (
  5. "strings"
  6. "testing"
  7. )
  8. func TestReservationDryRunPlansWithoutExecuting(t *testing.T) {
  9. ssh := newSSH(dhcpEmpty, fwEmpty)
  10. ts := makeDeviceActionsTestServer(t, ssh)
  11. res := deviceReq(t, "PUT", ts.URL, "/api/devices/"+devMAC+"/reservation?dry_run=1", ts.cookie,
  12. `{"ip":"192.168.1.60","hostname":"tv-salon"}`)
  13. if res.StatusCode != 200 {
  14. t.Fatalf("PUT dry_run: %d (body %v)", res.StatusCode, decodeBody(t, res))
  15. }
  16. body := decodeBody(t, res)
  17. res.Body.Close()
  18. if body["dryRun"] != true {
  19. t.Fatalf("la respuesta debe ser dryRun: %v", body)
  20. }
  21. apply, _ := body["apply"].([]any)
  22. if len(apply) < 4 {
  23. t.Fatalf("plan apply incompleto: %v", apply)
  24. }
  25. joined := ""
  26. for _, c := range apply {
  27. if s, ok := c.(string); ok {
  28. joined += s + "\n"
  29. }
  30. }
  31. if !strings.Contains(joined, "uci set dhcp.np_host_aabbccddeeff=host") ||
  32. !strings.Contains(joined, "uci set dhcp.np_host_aabbccddeeff.ip='192.168.1.60'") {
  33. t.Fatalf("apply no contiene la reserva: %v", apply)
  34. }
  35. rb, _ := body["rollback"].([]any)
  36. if len(rb) == 0 || !strings.Contains(rb[0].(string), "uci delete dhcp.np_host_aabbccddeeff") {
  37. t.Fatalf("rollback esperado (borrar sección): %v", rb)
  38. }
  39. // NADA se ejecutó: solo la lectura uci show del sondeo de conflictos.
  40. for _, forbidden := range []string{"uci set", "uci commit", "uci delete", "/etc/init.d"} {
  41. if ssh.saw(forbidden) {
  42. t.Fatalf("dry-run ejecutó comandos (%q): %v", forbidden, ssh.cmdsSnapshot())
  43. }
  44. }
  45. }
  46. func TestBlockDryRunPlansWithoutExecuting(t *testing.T) {
  47. ssh := newSSH(dhcpEmpty, fwEmpty)
  48. ts := makeDeviceActionsTestServer(t, ssh)
  49. res := deviceReq(t, "PUT", ts.URL, "/api/devices/"+devMAC+"/block?dry_run=1", ts.cookie,
  50. `{"router":"gateway"}`)
  51. if res.StatusCode != 200 {
  52. t.Fatalf("PUT block dry_run: %d (body %v)", res.StatusCode, decodeBody(t, res))
  53. }
  54. body := decodeBody(t, res)
  55. res.Body.Close()
  56. if body["dryRun"] != true {
  57. t.Fatalf("la respuesta debe ser dryRun: %v", body)
  58. }
  59. apply, _ := body["apply"].([]any)
  60. if len(apply) == 0 {
  61. t.Fatalf("plan apply vacío: %v", apply)
  62. }
  63. joined := ""
  64. for _, c := range apply {
  65. if s, ok := c.(string); ok {
  66. joined += s + "\n"
  67. }
  68. }
  69. if !strings.Contains(joined, "uci set firewall.np_block_aabbccddeeff=rule") {
  70. t.Fatalf("apply no contiene la regla de bloqueo: %v", apply)
  71. }
  72. for _, forbidden := range []string{"uci set", "uci commit", "uci delete", "/etc/init.d"} {
  73. if ssh.saw(forbidden) {
  74. t.Fatalf("dry-run ejecutó comandos (%q): %v", forbidden, ssh.cmdsSnapshot())
  75. }
  76. }
  77. }