4
0

agent_install_script_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package httpapi_test
  2. import (
  3. "io"
  4. "net/http"
  5. "strings"
  6. "testing"
  7. "github.com/gnacho/netpulse/server-go/internal/agentinstall"
  8. )
  9. // #1751: el instalador se sirve desde el propio servidor, sin sesión ni token
  10. // (el script no lleva secretos: el host y el token van en los argumentos del
  11. // one-liner) y byte a byte igual al que se embebe desde el repo.
  12. func TestAgentInstallScriptServedFromServer(t *testing.T) {
  13. ts := makeAgentTestServer(t)
  14. res, err := http.Get(ts.URL + "/api/agents/install-agent.sh")
  15. if err != nil {
  16. t.Fatalf("GET install-agent.sh: %v", err)
  17. }
  18. defer res.Body.Close()
  19. if res.StatusCode != http.StatusOK {
  20. t.Fatalf("status = %d, quiero 200", res.StatusCode)
  21. }
  22. if ct := res.Header.Get("Content-Type"); !strings.Contains(ct, "shellscript") {
  23. t.Fatalf("content-type = %q, quiero text/x-shellscript", ct)
  24. }
  25. body, err := io.ReadAll(res.Body)
  26. if err != nil {
  27. t.Fatalf("leer body: %v", err)
  28. }
  29. if !strings.HasPrefix(string(body), "#!/bin/sh") {
  30. head := string(body)
  31. if len(head) > 40 {
  32. head = head[:40]
  33. }
  34. t.Fatalf("el cuerpo no parece el instalador: %q", head)
  35. }
  36. if string(body) != string(agentinstall.Script()) {
  37. t.Fatalf("el servidor debe servir el script embebido (%d bytes), recibidos %d",
  38. len(agentinstall.Script()), len(body))
  39. }
  40. }