reinstall_script_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // reinstall_script_test.go — contrato de reinstallScript (#463): instalación
  2. // completa del agente (binario verificado, init self-heal, watchdog, cron).
  3. package httpapi
  4. import (
  5. "strings"
  6. "testing"
  7. )
  8. func reinstallScriptForTest(t *testing.T) string {
  9. t.Helper()
  10. return reinstallScript(
  11. "test-router",
  12. strings.Repeat("a1", 32), // 64 hex como un token real
  13. "http://192.168.1.226:3000",
  14. map[string]string{
  15. "arm64": "cafebabe",
  16. "arm": "deadbeef",
  17. "amd64": "abcd1234",
  18. },
  19. )
  20. }
  21. func TestReinstallScriptConfig(t *testing.T) {
  22. s := reinstallScriptForTest(t)
  23. for _, want := range []string{
  24. `SERVER="http://192.168.1.226:3000"`,
  25. `SLUG="test-router"`,
  26. `NETPULSE_SERVER=$SERVER`,
  27. `NETPULSE_SLUG=$SLUG`,
  28. `NETPULSE_TOKEN=$TOKEN`,
  29. "chmod 600 \"$ENV_FILE\"",
  30. } {
  31. if !strings.Contains(s, want) {
  32. t.Errorf("script sin %q", want)
  33. }
  34. }
  35. }
  36. func TestReinstallScriptArchAndDigests(t *testing.T) {
  37. s := reinstallScriptForTest(t)
  38. // Mapeo uname → goarch con alias arm completos.
  39. for _, want := range []string{
  40. "aarch64|arm64) GOARCH=arm64; SHA256=\"cafebabe\"",
  41. "armv7l|armv7|armhf|arm) GOARCH=arm; SHA256=\"deadbeef\"",
  42. "x86_64|amd64) GOARCH=amd64; SHA256=\"abcd1234\"",
  43. `/api/agents/$SLUG/binary?arch=$GOARCH`,
  44. } {
  45. if !strings.Contains(s, want) {
  46. t.Errorf("script sin %q", want)
  47. }
  48. }
  49. // El armv7 legacy (que el endpoint normaliza a arm) ya no se pide.
  50. if strings.Contains(s, "arch=armv7\"") {
  51. t.Error("el script no debe pedir arch=armv7 (normalizeArch espera arm)")
  52. }
  53. // Verificación sha256 presente y aborta con exit 21 si no coincide.
  54. if !strings.Contains(s, `GOT=$(sha256sum /tmp/netpulse-agent.new | awk '{print $1}')`) {
  55. t.Error("sin verificación sha256sum")
  56. }
  57. if !strings.Contains(s, "exit 21") {
  58. t.Error("sin código de salida 21 para sha256 mismatch")
  59. }
  60. }
  61. func TestReinstallScriptSelfHealInit(t *testing.T) {
  62. s := reinstallScriptForTest(t)
  63. for _, want := range []string{
  64. "selfheal_binary()",
  65. `url="${NETPULSE_SERVER%/}/api/agents/${NETPULSE_SLUG}/binary?arch=${ARCH}"`,
  66. "logger -t netpulse-agent \"self-heal: binario restaurado\"",
  67. "selfheal_binary || logger -t netpulse-agent \"self-heal: no se pudo restaurar el binario\"",
  68. } {
  69. if !strings.Contains(s, want) {
  70. t.Errorf("init sin self-heal: falta %q", want)
  71. }
  72. }
  73. // El self-heal debe conocer los tres archs (mismo case que el exterior).
  74. if strings.Count(s, "armv7l|armv7|armhf|arm") != 2 {
  75. t.Errorf("case de arch incompleto en el script (apariciones: %d, esperadas 2)",
  76. strings.Count(s, "armv7l|armv7|armhf|arm"))
  77. }
  78. }
  79. func TestReinstallScriptWatchdogAndCron(t *testing.T) {
  80. s := reinstallScriptForTest(t)
  81. for _, want := range []string{
  82. "pidof netpulse-agent", // pidof, nunca pgrep -x (BusyBox)
  83. `echo '*/2 * * * * /usr/sbin/netpulse-watchdog'`,
  84. "/etc/init.d/cron restart",
  85. } {
  86. if !strings.Contains(s, want) {
  87. t.Errorf("watchdog/cron incompleto: falta %q", want)
  88. }
  89. }
  90. if strings.Contains(s, "pgrep -x") {
  91. t.Error("el watchdog no debe usar pgrep -x (bug BusyBox)")
  92. }
  93. // Idempotencia del cron: reemplaza la línea previa en vez de duplicarla.
  94. if !strings.Contains(s, "grep -v netpulse-watchdog") {
  95. t.Error("el cron no es idempotente")
  96. }
  97. }
  98. func TestReinstallScriptFinishesWithStart(t *testing.T) {
  99. s := reinstallScriptForTest(t)
  100. if !strings.HasSuffix(strings.TrimSpace(s), "\"$INIT\" enable\n\"$INIT\" restart") {
  101. t.Error("el script debe terminar con enable + restart")
  102. }
  103. }
  104. func TestReinstallScriptEmptyDigestSkipsVerify(t *testing.T) {
  105. s := reinstallScript(
  106. "r", strings.Repeat("b2", 32), "http://s:3000",
  107. map[string]string{"arm64": "", "arm": "", "amd64": ""},
  108. )
  109. // Sin digests: el case asigna SHA256 vacío y el guard salta la verificación.
  110. if !strings.Contains(s, `GOARCH=arm64; SHA256=""`) {
  111. t.Error("sin digest, SHA256 debe quedar vacío")
  112. }
  113. if !strings.Contains(s, `if [ -n "$SHA256" ]; then`) {
  114. t.Error("la verificación debe estar protegida contra digest vacío")
  115. }
  116. }