Skip to content

Commit

Permalink
tests: add test to check for keepalive from Agent
Browse files Browse the repository at this point in the history
  • Loading branch information
henrybarreto authored and gustavosbarreto committed Jan 2, 2025
1 parent 308a2ae commit 8cfddbb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions tests/environment/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func New(t *testing.T) *DockerComposeConfigurator {
envs["SHELLHUB_HTTP_PORT"] = GetFreePort(t)
envs["SHELLHUB_SSH_PORT"] = GetFreePort(t)
envs["SHELLHUB_NETWORK"] = "shellhub_network_" + uuid.Generate()
envs["SHELLHUB_LOG_LEVEL"] = "trace"

return &DockerComposeConfigurator{
envs: envs,
Expand Down
50 changes: 46 additions & 4 deletions tests/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net"
"os"
"strconv"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -49,10 +50,12 @@ func NewAgentContainerWithIdentity(identity string) NewAgentContainerOption {

func NewAgentContainer(ctx context.Context, port string, opts ...NewAgentContainerOption) (testcontainers.Container, error) {
envs := map[string]string{
"SHELLHUB_SERVER_ADDRESS": fmt.Sprintf("http://localhost:%s", port),
"SHELLHUB_TENANT_ID": "00000000-0000-4000-0000-000000000000",
"SHELLHUB_PRIVATE_KEY": "/tmp/shellhub.key",
"SHELLHUB_LOG_FORMAT": "json",
"SHELLHUB_SERVER_ADDRESS": fmt.Sprintf("http://localhost:%s", port),
"SHELLHUB_TENANT_ID": "00000000-0000-4000-0000-000000000000",
"SHELLHUB_PRIVATE_KEY": "/tmp/shellhub.key",
"SHELLHUB_LOG_FORMAT": "json",
"SHELLHUB_KEEPALIVE_INTERVAL": "1",
"SHELLHUB_LOG_LEVEL": "trace",
}

for _, opt := range opts {
Expand Down Expand Up @@ -273,6 +276,45 @@ func TestSSH(t *testing.T) {
require.Error(t, err)
},
},
{
name: "connection keepalive when session is requested",
run: func(t *testing.T, environment *Environment, device *models.Device) {
config := &ssh.ClientConfig{
User: fmt.Sprintf("%s@%s.%s", ShellHubAgentUsername, ShellHubNamespaceName, device.Name),
Auth: []ssh.AuthMethod{
ssh.Password(ShellHubAgentPassword),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(), //nolint:gosec
}

var globalConn ssh.Conn

require.EventuallyWithT(t, func(tt *assert.CollectT) {
var err error

dialed, err := net.DialTimeout("tcp", fmt.Sprintf("localhost:%s", environment.services.Env("SHELLHUB_SSH_PORT")), config.Timeout)
assert.NoError(tt, err)

conn, _, _, err := ssh.NewClientConn(dialed, fmt.Sprintf("localhost:%s", environment.services.Env("SHELLHUB_SSH_PORT")), config)
assert.NoError(tt, err)

globalConn = conn
}, 30*time.Second, 1*time.Second)

ch, reqs, err := globalConn.OpenChannel("session", nil)
assert.NoError(t, err)

ok, err := ch.SendRequest("shell", true, nil)
assert.True(t, ok)
assert.NoError(t, err)

req := <-reqs
assert.True(t, strings.HasPrefix(req.Type, "keepalive"))

ch.Close()
globalConn.Close()
},
},
{
name: "connection SHELL with Pty",
run: func(t *testing.T, environment *Environment, device *models.Device) {
Expand Down

0 comments on commit 8cfddbb

Please sign in to comment.