Skip to content

Commit 0df0534

Browse files
KevinFairise2pull[bot]
authored andcommitted
Implement automatic reconnection for SSH session (#22178)
Implement automatic reconnection for SSH session
1 parent 6d28d33 commit 0df0534

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

test/new-e2e/pkg/components/remotehost.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"io/fs"
1111
"os"
12+
"strings"
1213
"time"
1314

1415
"github.com/DataDog/datadog-agent/test/new-e2e/pkg/e2e"
@@ -27,7 +28,7 @@ import (
2728
const (
2829
// Waiting for only 10s as we expect remote to be ready when provisioning
2930
sshRetryInterval = 2 * time.Second
30-
sshMaxRetries = 5
31+
sshMaxRetries = 20
3132
)
3233

3334
// RemoteHost represents a remote host
@@ -43,21 +44,33 @@ var _ e2e.Initializable = &RemoteHost{}
4344
// Init is called by e2e test Suite after the component is provisioned.
4445
func (h *RemoteHost) Init(ctx e2e.Context) error {
4546
h.context = ctx
46-
return h.ReconnectSSH()
47+
return h.reconnectSSH()
4748
}
4849

4950
// Execute executes a command and returns an error if any.
5051
func (h *RemoteHost) Execute(command string, options ...ExecuteOption) (string, error) {
52+
var err error
53+
var output string
54+
5155
params, err := optional.MakeParams(options...)
5256
if err != nil {
5357
return "", err
5458
}
59+
5560
cmd := h.buildEnvVariables(command, params.EnvVariables)
61+
output, err = clients.ExecuteCommand(h.client, cmd)
5662

57-
output, err := clients.ExecuteCommand(h.client, cmd)
63+
if err != nil && strings.Contains(err.Error(), "failed to create session:") {
64+
err = h.reconnectSSH()
65+
if err != nil {
66+
return "", err
67+
}
68+
output, err = clients.ExecuteCommand(h.client, cmd)
69+
}
5870
if err != nil {
5971
return "", fmt.Errorf("%v: %v", output, err)
6072
}
73+
6174
return output, nil
6275
}
6376

@@ -130,9 +143,9 @@ func (h *RemoteHost) RemoveAll(path string) error {
130143
return clients.RemoveAll(h.client, path)
131144
}
132145

133-
// ReconnectSSH recreate the SSH connection to the VM. Should be used only after VM reboot to restore the SSH connection.
146+
// reconnectSSH recreate the SSH connection to the VM. Should be used only after VM reboot to restore the SSH connection.
134147
// Returns an error if the VM is not reachable after retries.
135-
func (h *RemoteHost) ReconnectSSH() error {
148+
func (h *RemoteHost) reconnectSSH() error {
136149
h.context.T().Logf("connecting to remote VM at %s@%s", h.Username, h.Address)
137150

138151
if h.client != nil {

test/new-e2e/pkg/utils/clients/ssh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func getSSHClient(user, host string, privateKey, privateKeyPassphrase []byte) (*
8989
func ExecuteCommand(client *ssh.Client, command string) (string, error) {
9090
session, err := client.NewSession()
9191
if err != nil {
92-
return "", err
92+
return "", fmt.Errorf("failed to create session: %v", err)
9393
}
9494

9595
stdout, err := session.CombinedOutput(command)

0 commit comments

Comments
 (0)