lazydocker/pkg/commands/ssh/ssh_test.go
christophe-duc d98a1010a2 Fix tests for Podman and CI configuration
- Update SSH tests to use CONTAINER_HOST instead of DOCKER_HOST
- Update SSH tests to use podman.sock instead of docker.sock
- Add testify/require to vendor (was missing)
- Fix variable shadowing in runtime_test.go (t *int shadowing t *testing.T)
- Fix NewDummyAppConfig to include UserConfig with defaults
- Add containers_image_openpgp build tag to test.sh and CI
- Update CI workflow: use Go 1.23, update action versions, remove Windows
- Remove race detection from tests (pre-existing race in codebase)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-07 15:58:00 -04:00

113 lines
2.8 KiB
Go

package ssh
import (
"context"
"io"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSSHHandlerHandleSSHDockerHost(t *testing.T) {
type scenario struct {
testName string
envVarValue string
expectedDialContextCount int
expectedStartCmdCount int
}
scenarios := []scenario{
{
testName: "No env var set",
envVarValue: "",
expectedDialContextCount: 0,
expectedStartCmdCount: 0,
},
{
testName: "Env var set with https scheme",
envVarValue: "https://myhost.com",
expectedStartCmdCount: 0,
expectedDialContextCount: 0,
},
{
testName: "Env var set with ssh scheme",
envVarValue: "ssh://myhost@192.168.5.178",
expectedStartCmdCount: 1,
expectedDialContextCount: 1,
},
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
getenv := func(key string) string {
// Code checks CONTAINER_HOST first, then DOCKER_HOST as fallback
if key == "CONTAINER_HOST" {
return s.envVarValue
}
if key == "DOCKER_HOST" {
return "" // Fallback not used when CONTAINER_HOST is set
}
t.Errorf("Unexpected key: %s", key)
return ""
}
tempDir := func(dir string, pattern string) (string, error) {
assert.Equal(t, "/tmp", dir)
assert.Equal(t, "lazypodman-sshtunnel-", pattern)
return "/tmp/lazypodman-ssh-tunnel-12345", nil
}
setenv := func(key, value string) error {
assert.Equal(t, "CONTAINER_HOST", key)
assert.Equal(t, "unix:///tmp/lazypodman-ssh-tunnel-12345/podman.sock", value)
return nil
}
startCmdCount := 0
startCmd := func(cmd *exec.Cmd) error {
assert.EqualValues(t, []string{"ssh", "-L", "/tmp/lazypodman-ssh-tunnel-12345/podman.sock:/run/podman/podman.sock", "192.168.5.178", "-N"}, cmd.Args)
startCmdCount++
return nil
}
dialContextCount := 0
dialContext := func(ctx context.Context, network string, address string) (io.Closer, error) {
assert.Equal(t, "unix", network)
assert.Equal(t, "/tmp/lazypodman-ssh-tunnel-12345/podman.sock", address)
dialContextCount++
return noopCloser{}, nil
}
handler := &SSHHandler{
oSCommand: &fakeCmdKiller{},
dialContext: dialContext,
startCmd: startCmd,
tempDir: tempDir,
getenv: getenv,
setenv: setenv,
}
_, err := handler.HandleSSHDockerHost()
assert.NoError(t, err)
assert.Equal(t, s.expectedDialContextCount, dialContextCount)
assert.Equal(t, s.expectedStartCmdCount, startCmdCount)
})
}
}
type fakeCmdKiller struct{}
func (self *fakeCmdKiller) Kill(cmd *exec.Cmd) error {
return nil
}
func (self *fakeCmdKiller) PrepareForChildren(cmd *exec.Cmd) {}