4
0

agentinstall_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package agentinstall
  2. import (
  3. "bytes"
  4. "os"
  5. "testing"
  6. )
  7. // repoScript: la copia canónica vive en la raíz del repositorio. El test se
  8. // ejecuta con el directorio del paquete como CWD, así que sube cuatro niveles
  9. // (agentinstall -> internal -> server-go -> raíz).
  10. const repoScript = "../../../install-agent.sh"
  11. // TestScriptMatchesRepoCopy falla cuando el instalador embebido se separa del
  12. // que se publica en el repositorio: el servidor serviría un script viejo y la
  13. // línea de instalación dejaría de coincidir con lo que dicen los docs (#1751).
  14. func TestScriptMatchesRepoCopy(t *testing.T) {
  15. want, err := os.ReadFile(repoScript)
  16. if err != nil {
  17. t.Fatalf("read %s: %v", repoScript, err)
  18. }
  19. // #1812: la comparación ignora los finales de línea. Un checkout con
  20. // autocrlf deja CRLF en las dos copias y Script() los normaliza, así que
  21. // comparar en crudo rompería en Windows sin que las copias se hayan
  22. // separado de verdad.
  23. if !bytes.Equal(normalizeLF(want), Script()) {
  24. t.Fatalf("install-agent.sh embebido y el del repositorio difieren: copia de nuevo el fichero a internal/agentinstall/install-agent.sh")
  25. }
  26. if len(want) == 0 {
  27. t.Fatal("install-agent.sh vacío")
  28. }
  29. }
  30. func TestScriptIsShell(t *testing.T) {
  31. if !bytes.HasPrefix(Script(), []byte("#!/bin/sh")) {
  32. t.Fatalf("el instalador embebido no empieza por #!/bin/sh")
  33. }
  34. }
  35. func TestDigestIsStable(t *testing.T) {
  36. d := Digest()
  37. if len(d) != 64 {
  38. t.Fatalf("digest = %q, se esperaba sha256 hex", d)
  39. }
  40. if d != Digest() {
  41. t.Fatal("Digest no es estable entre llamadas")
  42. }
  43. }
  44. // #1812: el instalador servido nunca lleva CRLF, sea cual sea el checkout con
  45. // el que se compiló el servidor.
  46. func TestScriptServesLF(t *testing.T) {
  47. if bytes.Contains(Script(), []byte("\r\n")) {
  48. t.Fatal("el instalador embebido conserva CRLF: busybox ash lo rechaza")
  49. }
  50. }
  51. // La normalización no cambia un fichero que ya está en LF.
  52. func TestNormalizeLFIsIdempotent(t *testing.T) {
  53. lf := []byte("#!/bin/sh\necho ok\n")
  54. if got := normalizeLF(lf); !bytes.Equal(got, lf) {
  55. t.Fatalf("normalizeLF alteró un fichero LF: %q", got)
  56. }
  57. crlf := []byte("#!/bin/sh\r\necho ok\r\n")
  58. if got := normalizeLF(crlf); bytes.Contains(got, []byte("\r\n")) {
  59. t.Fatalf("normalizeLF dejó CRLF: %q", got)
  60. }
  61. }