scan_default_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package reinstall_test
  2. // scan_default_test.go - #1558: the env the reinstall writes must default
  3. // NETPULSE_SCAN_INTERVAL to 0 and still keep a value the unit already had.
  4. // These run the generated block with sh instead of matching strings: the
  5. // value depends on what the remote env file holds.
  6. import (
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "strings"
  11. "testing"
  12. )
  13. // envBlockForTest extracts the env rewrite block from the reinstall script,
  14. // the one shared with EnvScript.
  15. func envBlockForTest(t *testing.T) string {
  16. t.Helper()
  17. s := scriptForTest()
  18. start := strings.Index(s, `USER_VARS=""`)
  19. if start < 0 {
  20. t.Fatal("no encuentro el bloque de env en el script")
  21. }
  22. end := strings.Index(s[start:], `chmod 600 "$ENV_FILE"`)
  23. if end < 0 {
  24. t.Fatal("el bloque de env no termina con chmod")
  25. }
  26. return s[start : start+end+len(`chmod 600 "$ENV_FILE"`)]
  27. }
  28. func runEnvBlock(t *testing.T, prev string) string {
  29. t.Helper()
  30. dir := t.TempDir()
  31. envPath := filepath.Join(dir, "netpulse-agent.env")
  32. if prev != "" {
  33. if err := os.WriteFile(envPath, []byte(prev), 0o600); err != nil {
  34. t.Fatal(err)
  35. }
  36. }
  37. script := "ENV_FILE=" + envPath + "\n" +
  38. "SERVER=http://192.168.1.226:3000\n" +
  39. "SLUG=test-router\n" +
  40. "TOKEN=" + strings.Repeat("a1", 32) + "\n" +
  41. "SERVER_FP=\n" +
  42. envBlockForTest(t) + "\n"
  43. if out, err := exec.Command("sh", "-c", script).CombinedOutput(); err != nil {
  44. t.Fatalf("el bloque de env falla: %v\n%s", err, out)
  45. }
  46. data, err := os.ReadFile(envPath)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. return string(data)
  51. }
  52. // TestEnvDefaultsScanOff: sin SCAN_INTERVAL previo, el env sale con 0 activo.
  53. func TestEnvDefaultsScanOff(t *testing.T) {
  54. env := runEnvBlock(t, "")
  55. if !strings.Contains(env, "\nNETPULSE_SCAN_INTERVAL=0\n") {
  56. t.Fatalf("falta el default 0 activo:\n%s", env)
  57. }
  58. // Comentado ya no sirve: el agente no lo leería.
  59. if strings.Contains(env, "# NETPULSE_SCAN_INTERVAL=30m") {
  60. t.Fatalf("el default viejo sigue comentado:\n%s", env)
  61. }
  62. }
  63. // TestEnvKeepsScanOverride: un valor manual sobrevive al reinstall y no se
  64. // duplica (#851).
  65. func TestEnvKeepsScanOverride(t *testing.T) {
  66. env := runEnvBlock(t, "NETPULSE_SCAN_INTERVAL=45m\nNETPULSE_HEARTBEAT_FILE=/tmp/hb\n")
  67. if !strings.Contains(env, "\nNETPULSE_SCAN_INTERVAL=45m\n") {
  68. t.Fatalf("override perdido:\n%s", env)
  69. }
  70. if got := strings.Count(env, "NETPULSE_SCAN_INTERVAL="); got != 1 {
  71. t.Fatalf("SCAN_INTERVAL duplicado (%d):\n%s", got, env)
  72. }
  73. if !strings.Contains(env, "\nNETPULSE_HEARTBEAT_FILE=/tmp/hb\n") {
  74. t.Fatalf("var de usuario perdida:\n%s", env)
  75. }
  76. }