scan_default_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. // El bloque empieza en USER_VARS: warn() y say() viven ahora en el
  19. // preámbulo del script (#1894) y no forman parte de este fragmento.
  20. start := strings.Index(s, `USER_VARS=""`)
  21. if start < 0 {
  22. t.Fatal("no encuentro el bloque de env en el script")
  23. }
  24. marker := "warn \"could not write $ENV_FILE, keeping the previous one\"\nfi"
  25. end := strings.Index(s[start:], marker)
  26. if end < 0 {
  27. t.Fatal("el bloque de env no termina con el guard de write_env")
  28. }
  29. return s[start : start+end+len(marker)]
  30. }
  31. func runEnvBlock(t *testing.T, prev string) string {
  32. t.Helper()
  33. dir := t.TempDir()
  34. envPath := filepath.Join(dir, "netpulse-agent.env")
  35. if prev != "" {
  36. if err := os.WriteFile(envPath, []byte(prev), 0o600); err != nil {
  37. t.Fatal(err)
  38. }
  39. }
  40. script := "warn() { echo \"netpulse-agent: $*\" >&2; }\n" +
  41. "ENV_FILE=" + envPath + "\n" +
  42. "SERVER=http://192.168.1.226:3000\n" +
  43. "SLUG=test-router\n" +
  44. "TOKEN=" + strings.Repeat("a1", 32) + "\n" +
  45. "SERVER_FP=\n" +
  46. envBlockForTest(t) + "\n"
  47. if out, err := exec.Command("sh", "-c", script).CombinedOutput(); err != nil {
  48. t.Fatalf("el bloque de env falla: %v\n%s", err, out)
  49. }
  50. data, err := os.ReadFile(envPath)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. return string(data)
  55. }
  56. // TestEnvDefaultsScanOff: sin SCAN_INTERVAL previo, el env sale con 0 activo.
  57. func TestEnvDefaultsScanOff(t *testing.T) {
  58. env := runEnvBlock(t, "")
  59. if !strings.Contains(env, "\nNETPULSE_SCAN_INTERVAL=0\n") {
  60. t.Fatalf("falta el default 0 activo:\n%s", env)
  61. }
  62. // Comentado ya no sirve: el agente no lo leería.
  63. if strings.Contains(env, "# NETPULSE_SCAN_INTERVAL=30m") {
  64. t.Fatalf("el default viejo sigue comentado:\n%s", env)
  65. }
  66. }
  67. // TestEnvKeepsScanOverride: un valor manual sobrevive al reinstall y no se
  68. // duplica (#851).
  69. func TestEnvKeepsScanOverride(t *testing.T) {
  70. env := runEnvBlock(t, "NETPULSE_SCAN_INTERVAL=45m\nNETPULSE_HEARTBEAT_FILE=/tmp/hb\n")
  71. if !strings.Contains(env, "\nNETPULSE_SCAN_INTERVAL=45m\n") {
  72. t.Fatalf("override perdido:\n%s", env)
  73. }
  74. if got := strings.Count(env, "NETPULSE_SCAN_INTERVAL="); got != 1 {
  75. t.Fatalf("SCAN_INTERVAL duplicado (%d):\n%s", got, env)
  76. }
  77. if !strings.Contains(env, "\nNETPULSE_HEARTBEAT_FILE=/tmp/hb\n") {
  78. t.Fatalf("var de usuario perdida:\n%s", env)
  79. }
  80. }