script_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // script_test.go — contrato de uninstall.Script (#624): desinstalación
  2. // completa del agente (detener/deshabilitar init, borrar binario, env,
  3. // watchdog y artefactos temporales).
  4. package uninstall_test
  5. import (
  6. "strings"
  7. "testing"
  8. "github.com/gnacho/netpulse/server-go/internal/uninstall"
  9. )
  10. func TestScriptRemovesAgentArtifacts(t *testing.T) {
  11. s := uninstall.Script()
  12. for _, want := range []string{
  13. "INIT=/etc/init.d/netpulse-agent",
  14. "BIN=/usr/sbin/netpulse-agent",
  15. "ENV_FILE=/etc/netpulse-agent.env",
  16. "WATCHDOG=/usr/sbin/netpulse-watchdog",
  17. `"$INIT" stop >/dev/null 2>&1 || true`,
  18. `"$INIT" disable >/dev/null 2>&1 || true`,
  19. `rm -f "$INIT"`,
  20. `killall netpulse-agent >/dev/null 2>&1 || true`,
  21. `rm -f "$BIN"`,
  22. `rm -f "$WATCHDOG"`,
  23. `rm -f "$ENV_FILE"`,
  24. `grep -v netpulse-watchdog`,
  25. `exit 0`,
  26. } {
  27. if !strings.Contains(s, want) {
  28. t.Errorf("script sin %q", want)
  29. }
  30. }
  31. }
  32. func TestScriptIdempotentNoFailWithoutAgent(t *testing.T) {
  33. // Si el agente no está instalado, cada comando es no-fatal (|| true,
  34. // -f guard, pidof guard) y el script acaba con exit 0.
  35. s := uninstall.Script()
  36. if !strings.Contains(s, `WATCHDOG=/usr/sbin/netpulse-watchdog`) {
  37. t.Fatal("script sin claves de rutas")
  38. }
  39. // No debe dejar referencias a la instalación (no descarga, no enable).
  40. for _, forbidden := range []string{"--binary", "curl", "wget", "procd_open_instance", "chmod 600 \"$ENV_FILE\""} {
  41. if strings.Contains(s, forbidden) {
  42. t.Errorf("script de uninstall no debe contener %q", forbidden)
  43. }
  44. }
  45. }