reinstall_output_1894_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package httpapi
  2. // #1894: la salida del script de reinstalación vuelve en la respuesta (éxito y
  3. // fallo) en vez de perderse. Antes el handler la descartaba con `_` y el
  4. // operador solo veía "exit status 1" en rojo durante unos segundos.
  5. import (
  6. "encoding/json"
  7. "errors"
  8. "net/http/httptest"
  9. "strings"
  10. "testing"
  11. "time"
  12. "github.com/gnacho/netpulse/server-go/internal/config"
  13. "github.com/gnacho/netpulse/server-go/internal/db"
  14. "github.com/gnacho/netpulse/server-go/internal/routerstore"
  15. )
  16. type reinstallFakePool struct {
  17. out string
  18. err error
  19. }
  20. func (p *reinstallFakePool) Run(host, cmd string, timeout time.Duration) (string, error) {
  21. return p.out, p.err
  22. }
  23. func makeReinstallOutputServer(t *testing.T, out string, runErr error) (*server, string) {
  24. t.Helper()
  25. dataDir := t.TempDir()
  26. cfg, err := config.Load(map[string]string{
  27. "AUTH_USER": "admin", "AUTH_PASS": "test123456",
  28. "DEMO_MODE": "0", "DATA_DIR": dataDir, "NODE_ENV": "test",
  29. }, dataDir)
  30. if err != nil {
  31. t.Fatalf("config: %v", err)
  32. }
  33. d, err := db.Open(dataDir)
  34. if err != nil {
  35. t.Fatalf("db: %v", err)
  36. }
  37. t.Cleanup(func() { d.Close() })
  38. r, err := routerstore.AddRouter(d.DB, routerstore.AddInput{
  39. Name: "RecRouter", Host: "192.168.1.60", Type: "openwrt",
  40. })
  41. if err != nil {
  42. t.Fatalf("add router: %v", err)
  43. }
  44. return &server{cfg: cfg, db: d, pool: &reinstallFakePool{out: out, err: runErr}}, r.ID
  45. }
  46. func TestReinstallFailureReturnsScriptOutput(t *testing.T) {
  47. out := "descargando binario\ncurl: (7) Failed to connect\nno space left on device"
  48. s, id := makeReinstallOutputServer(t, out, errors.New("exit status 1"))
  49. req := httptest.NewRequest("POST", "/api/agents/"+id+"/reinstall", nil)
  50. req.SetPathValue("slug", id)
  51. rec := httptest.NewRecorder()
  52. s.handleAgentReinstall(rec, req)
  53. if rec.Code != 502 {
  54. t.Fatalf("status = %d, body %s", rec.Code, rec.Body.String())
  55. }
  56. var body struct {
  57. Error string `json:"error"`
  58. Message string `json:"message"`
  59. Output string `json:"output"`
  60. }
  61. if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
  62. t.Fatalf("json: %v", err)
  63. }
  64. if body.Error != "ssh_failed" || !strings.Contains(body.Message, "exit status 1") {
  65. t.Fatalf("error/message inesperados: %+v", body)
  66. }
  67. if !strings.Contains(body.Output, "no space left on device") {
  68. t.Fatalf("la salida del script no llegó al operador: %+v", body)
  69. }
  70. }
  71. func TestReinstallSuccessReturnsScriptOutput(t *testing.T) {
  72. s, id := makeReinstallOutputServer(t, "instalando netpulse-agent\nagente iniciado", nil)
  73. req := httptest.NewRequest("POST", "/api/agents/"+id+"/reinstall", nil)
  74. req.SetPathValue("slug", id)
  75. rec := httptest.NewRecorder()
  76. s.handleAgentReinstall(rec, req)
  77. if rec.Code != 200 {
  78. t.Fatalf("status = %d, body %s", rec.Code, rec.Body.String())
  79. }
  80. var body struct {
  81. Installed bool `json:"installed"`
  82. Output string `json:"output"`
  83. }
  84. if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
  85. t.Fatalf("json: %v", err)
  86. }
  87. if !body.Installed || !strings.Contains(body.Output, "agente iniciado") {
  88. t.Fatalf("respuesta sin la salida del script: %+v", body)
  89. }
  90. }
  91. func TestOutputTailKeepsTheEnd(t *testing.T) {
  92. if got := outputTail(" hola\n", 10); got != "hola" {
  93. t.Fatalf("recorte corto = %q", got)
  94. }
  95. long := strings.Repeat("a", 100) + "FIN"
  96. got := outputTail(long, 3)
  97. if got != "…FIN" {
  98. t.Fatalf("recorte largo = %q", got)
  99. }
  100. }