robust_1853_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package reinstall_test
  2. import (
  3. "os/exec"
  4. "strings"
  5. "testing"
  6. "github.com/gnacho/netpulse/server-go/internal/reinstall"
  7. )
  8. // #1853: un reinstall que falla no debe dejar el agente parado. El servicio se
  9. // para solo cuando el binario ya esta descargado y verificado, y si el
  10. // reemplazo falla se vuelve a arrancar el anterior.
  11. func TestScriptStopsOnlyOnceItCanInstall(t *testing.T) {
  12. s := reinstall.ScriptWithTargets(
  13. "test-router", strings.Repeat("a1", 32), "https://192.168.1.226:3000",
  14. "sha256//AAAA", map[string]string{"arm64": "cafebabe"},
  15. reinstall.WanTargets{},
  16. )
  17. download := strings.Index(s, "/binary?arch=$GOARCH")
  18. digest := strings.Index(s, "sha256 mismatch")
  19. stop := strings.Index(s, `"$INIT" stop`)
  20. replace := strings.Index(s, `mv -f /tmp/netpulse-agent.new "$BIN"`)
  21. if download < 0 || digest < 0 || stop < 0 || replace < 0 {
  22. t.Fatalf("falta un paso: download=%d digest=%d stop=%d replace=%d", download, digest, stop, replace)
  23. }
  24. if !(download < digest && digest < stop && stop < replace) {
  25. t.Errorf("orden incorrecto: download=%d digest=%d stop=%d replace=%d", download, digest, stop, replace)
  26. }
  27. if !strings.Contains(s, "restarting the previous agent") {
  28. t.Error("el fallo al reemplazar el binario debe avisar del rollback")
  29. }
  30. if !strings.Contains(s, `"$INIT" start`) {
  31. t.Error("el fallo al reemplazar el binario debe volver a arrancar el agente")
  32. }
  33. if !strings.Contains(s, "exit 23") {
  34. t.Error("falta el codigo de salida del reemplazo fallido")
  35. }
  36. // Los diagnosticos tienen que ir a stderr: el servidor solo captura stderr.
  37. for _, want := range []string{
  38. `sha256 mismatch (want $SHA256, got $GOT)" >&2`,
  39. `could not detect the endianness of $ARCH" >&2`,
  40. `unsupported arch: $ARCH" >&2`,
  41. } {
  42. if !strings.Contains(s, want) {
  43. t.Errorf("mensaje sin stderr: %s", want)
  44. }
  45. }
  46. if err := exec.Command("sh", "-n", "-c", s).Run(); err != nil {
  47. t.Fatalf("script invalido: %v", err)
  48. }
  49. }