feat: funciones SSH para infra — conn, check, exec, download, upload, tunnel

Conjunto completo de funciones SSH para operaciones remotas: conexión,
verificación de host, ejecución de comandos, transferencia de archivos
(upload/download) y gestión de túneles. Incluye tipo SSHConn y tests.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-02 22:03:41 +02:00
parent 2b123e0c87
commit 560cbf280e
19 changed files with 729 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
package infra
import (
"fmt"
"net"
"testing"
"time"
)
func TestSSHTunnelOpenClose(t *testing.T) {
conn := skipIfNoSSH(t)
t.Run("abre tunel y lo cierra", func(t *testing.T) {
// Usar puerto alto aleatorio para evitar conflictos
localPort := 19876
// Tunel a localhost:22 del remoto (el propio sshd)
pid, err := SSHTunnelOpen(conn, localPort, "localhost", 22)
if err != nil {
t.Fatalf("SSHTunnelOpen: %v", err)
}
if pid <= 0 {
t.Fatalf("PID invalido: %d", pid)
}
// Verificar que el puerto local esta escuchando
time.Sleep(300 * time.Millisecond)
c, err := net.DialTimeout("tcp", fmt.Sprintf("localhost:%d", localPort), 2*time.Second)
if err != nil {
// Limpiar antes de fallar
SSHTunnelClose(pid)
t.Fatalf("no se puede conectar al tunel en localhost:%d: %v", localPort, err)
}
c.Close()
// Cerrar tunel
err = SSHTunnelClose(pid)
if err != nil {
t.Errorf("SSHTunnelClose: %v", err)
}
// Verificar que el puerto ya no escucha
time.Sleep(300 * time.Millisecond)
c, err = net.DialTimeout("tcp", fmt.Sprintf("localhost:%d", localPort), 1*time.Second)
if err == nil {
c.Close()
t.Error("el tunel sigue abierto despues de SSHTunnelClose")
}
})
}