Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle error properly during port forwarding initialization. #2550

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Handle error properly during port forwarding initialization.
  • Loading branch information
cedric-appdirect committed Jun 4, 2024
commit 64157a5cf9bcd675cbb79cb60f10b4fd4032f5bc
21 changes: 14 additions & 7 deletions port_forwarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testcontainers

import (
"context"
"errors"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -231,20 +232,22 @@ func (sshdC *sshdContainer) exposeHostPort(ctx context.Context, ports ...int) er
go pw.Forward(ctx)
}

var err error

// continue when all port forwarders have created the connection
for _, pfw := range sshdC.portForwarders {
<-pfw.connectionCreated
err = errors.Join(err, <-pfw.connectionCreated)
}

return nil
return err
}

type PortForwarder struct {
sshDAddr string
sshConfig *ssh.ClientConfig
remotePort int
localPort int
connectionCreated chan struct{} // used to signal that the connection has been created, so the caller can proceed
connectionCreated chan error // used to signal that the connection has been created, so the caller can proceed
terminateChan chan struct{} // used to signal that the connection has been terminated
}

Expand All @@ -254,7 +257,7 @@ func NewPortForwarder(sshDAddr string, sshConfig *ssh.ClientConfig, remotePort,
sshConfig: sshConfig,
remotePort: remotePort,
localPort: localPort,
connectionCreated: make(chan struct{}),
connectionCreated: make(chan error),
terminateChan: make(chan struct{}),
}
}
Expand All @@ -267,18 +270,22 @@ func (pf *PortForwarder) Close(ctx context.Context) {
func (pf *PortForwarder) Forward(ctx context.Context) error {
client, err := ssh.Dial("tcp", pf.sshDAddr, pf.sshConfig)
if err != nil {
return fmt.Errorf("error dialing ssh server: %w", err)
err = fmt.Errorf("error dialing ssh server: %w", err)
pf.connectionCreated <- err
return err
}
defer client.Close()

listener, err := client.Listen("tcp", fmt.Sprintf("localhost:%d", pf.remotePort))
if err != nil {
return fmt.Errorf("error listening on remote port: %w", err)
err = fmt.Errorf("error listening on remote port: %w", err)
pf.connectionCreated <- err
return err
}
defer listener.Close()

// signal that the connection has been created
pf.connectionCreated <- struct{}{}
pf.connectionCreated <- nil

// check if the context or the terminateChan has been closed
select {
Expand Down