apk_pkg_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package reinstall_test
  2. import (
  3. "os/exec"
  4. "strings"
  5. "testing"
  6. "github.com/gnacho/netpulse/server-go/internal/reinstall"
  7. )
  8. // #1476: OpenWrt 25.12 trae apk y no opkg, asi que el paquete del servidor
  9. // tiene que instalarse tambien por ahi. El .apk lo firma el CI y el servidor
  10. // solo lo sirve con format=apk; si falta, el script cae al camino de ficheros
  11. // sueltos como antes.
  12. func TestScriptInstallsPackageWithAPK(t *testing.T) {
  13. s := reinstall.ScriptWithTargets(
  14. "test-router",
  15. strings.Repeat("a1", 32),
  16. "https://192.168.1.226:3000",
  17. "sha256//AAAA",
  18. map[string]string{"arm64": "cafebabe"},
  19. reinstall.WanTargets{},
  20. )
  21. for _, want := range []string{
  22. `elif command -v apk >/dev/null 2>&1; then`,
  23. `pkg_fetch "$PKG_URL&format=apk" /tmp/netpulse-agent.apk`,
  24. `apk add --allow-untrusted /tmp/netpulse-agent.apk`,
  25. `rm -f /tmp/netpulse-agent.apk`,
  26. } {
  27. if !strings.Contains(s, want) {
  28. t.Errorf("el script no contiene %q", want)
  29. }
  30. }
  31. // El bloque opkg sigue existiendo y va ANTES que el de apk: en un router
  32. // con ambos gestores manda el .ipk, que el servidor compone al vuelo y no
  33. // depende de que el operador haya copiado el asset.
  34. opkg := strings.Index(s, `command -v opkg`)
  35. apk := strings.Index(s, `elif command -v apk`)
  36. if opkg < 0 || apk < 0 {
  37. t.Fatalf("faltan los bloques de gestor de paquetes (opkg=%d apk=%d)", opkg, apk)
  38. }
  39. if opkg > apk {
  40. t.Error("el bloque opkg debe ir antes que el de apk")
  41. }
  42. if out, err := exec.Command("sh", "-n", "-c", s).CombinedOutput(); err != nil {
  43. t.Fatalf("script invalido para sh -n: %v\n%s", err, out)
  44. }
  45. }
  46. // La funcion pkg_fetch centraliza la descarga autenticada con curl o wget, de
  47. // modo que ambos formatos comparten el mismo manejo de TLS y cabeceras.
  48. func TestScriptPackageFetchIsShared(t *testing.T) {
  49. s := reinstall.ScriptWithTargets(
  50. "test-router",
  51. strings.Repeat("a1", 32),
  52. "https://192.168.1.226:3000",
  53. "",
  54. map[string]string{"arm64": "cafebabe"},
  55. reinstall.WanTargets{},
  56. )
  57. if !strings.Contains(s, `PKG_URL="$SERVER/api/agents/$SLUG/package?arch=$GOARCH"`) {
  58. t.Error("falta la base URL del paquete")
  59. }
  60. if strings.Count(s, `pkg_fetch "$PKG_URL&format=`) != 2 {
  61. t.Error("los dos formatos deben descargarse por pkg_fetch")
  62. }
  63. if !strings.Contains(s, `-H "Authorization: Bearer $TOKEN"`) {
  64. t.Error("la descarga debe autenticarse con el token del agente")
  65. }
  66. }