init_selfheal_1857_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package reinstall_test
  2. import (
  3. "os"
  4. "os/exec"
  5. "path/filepath"
  6. "strings"
  7. "testing"
  8. "github.com/gnacho/netpulse/server-go/internal/reinstall"
  9. )
  10. // #1857: el init procd solo debe registrar la instancia cuando hay binario.
  11. // Si el self-heal no lo consigue, procd arrancaria una ruta inexistente,
  12. // reintentaria y se rendiria: el servicio queda como arrancado y el agente
  13. // muerto sin mas rastro que una linea en el log.
  14. func initHeredocBody(t *testing.T, script string) string {
  15. t.Helper()
  16. const marker = "<<'INITEOF'\n"
  17. start := strings.Index(script, marker)
  18. if start < 0 {
  19. t.Fatalf("el instalador no tiene heredoc INITEOF")
  20. }
  21. start += len(marker)
  22. end := strings.Index(script[start:], "\nINITEOF\n")
  23. if end < 0 {
  24. t.Fatalf("el heredoc INITEOF no esta cerrado")
  25. }
  26. return script[start : start+end+1]
  27. }
  28. func runInit(t *testing.T, body, bin string) string {
  29. t.Helper()
  30. dir := t.TempDir()
  31. path := filepath.Join(dir, "init.sh")
  32. script := `logger() { echo "LOG: $*"; }
  33. procd_open_instance() { echo "OPEN $*"; }
  34. procd_set_param() { :; }
  35. procd_close_instance() { echo "CLOSE"; }
  36. crontab() { return 1; }
  37. ` + body + `
  38. BIN="` + bin + `"
  39. ENV_FILE="` + filepath.Join(dir, "no-existe") + `"
  40. start_service
  41. `
  42. if err := os.WriteFile(path, []byte(script), 0o755); err != nil {
  43. t.Fatalf("no pude escribir el init de prueba: %v", err)
  44. }
  45. out, err := exec.Command("sh", path).CombinedOutput()
  46. if err != nil {
  47. t.Fatalf("el init fallo: %v\n%s", err, out)
  48. }
  49. return string(out)
  50. }
  51. func TestInitPassesSyntaxCheck(t *testing.T) {
  52. body := initHeredocBody(t, reinstall.ScriptWithTargets(
  53. "test-router", strings.Repeat("a1", 32), "https://192.168.1.226:8443", "sha256//AAAA",
  54. map[string]string{"arm64": "cafebabe"}, reinstall.WanTargets{},
  55. ))
  56. if err := exec.Command("sh", "-n", "-c", body).Run(); err != nil {
  57. t.Fatalf("el init generado no pasa sh -n: %v", err)
  58. }
  59. }
  60. func TestInitSkipsStartWhenTheBinaryIsMissing(t *testing.T) {
  61. out := runInit(t, initHeredocBody(t, reinstall.ScriptWithTargets(
  62. "test-router", strings.Repeat("a1", 32), "https://192.168.1.226:8443", "sha256//AAAA",
  63. map[string]string{"arm64": "cafebabe"}, reinstall.WanTargets{},
  64. )), filepath.Join(t.TempDir(), "netpulse-agent"))
  65. if strings.Contains(out, "OPEN") {
  66. t.Errorf("registro la instancia sin binario:\n%s", out)
  67. }
  68. if !strings.Contains(out, "self-heal: could not restore the binary, the service will not start") {
  69. t.Errorf("no dijo por que no arranca:\n%s", out)
  70. }
  71. }
  72. func TestInitStartsWhenTheBinaryIsThere(t *testing.T) {
  73. bin := filepath.Join(t.TempDir(), "netpulse-agent")
  74. if err := os.WriteFile(bin, []byte("#!/bin/sh\n"), 0o755); err != nil {
  75. t.Fatalf("no pude crear el binario de prueba: %v", err)
  76. }
  77. out := runInit(t, initHeredocBody(t, reinstall.ScriptWithTargets(
  78. "test-router", strings.Repeat("a1", 32), "https://192.168.1.226:8443", "sha256//AAAA",
  79. map[string]string{"arm64": "cafebabe"}, reinstall.WanTargets{},
  80. )), bin)
  81. if !strings.Contains(out, "OPEN netpulse-agent") {
  82. t.Errorf("no registro la instancia con el binario presente:\n%s", out)
  83. }
  84. }