Skip to content
Open
Show file tree
Hide file tree
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
19 changes: 13 additions & 6 deletions docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ func (c *DockerClient) Info(ctx context.Context, options client.InfoOptions) (cl
if err != nil {
return res, fmt.Errorf("failed to retrieve docker info: %w", err)
}
dockerInfo = res
dockerInfoSet = true

infoMessage := `%v - Connected to docker:
Server Version: %v
Expand All @@ -67,28 +65,37 @@ func (c *DockerClient) Info(ctx context.Context, options client.InfoOptions) (cl
Test ProcessID: %s
`
infoLabels := ""
if len(dockerInfo.Info.Labels) > 0 {
if len(res.Info.Labels) > 0 {
infoLabels = `
Labels:`
var infoLabelsSb72 strings.Builder
for _, lb := range dockerInfo.Info.Labels {
for _, lb := range res.Info.Labels {
infoLabelsSb72.WriteString("\n " + lb)
}
infoLabels += infoLabelsSb72.String()
}

host, err := core.ExtractDockerHost(ctx)
if err != nil {
return dockerInfo, err
return res, err
}
socket, err := core.ExtractDockerSocket(ctx)
if err != nil {
return res, err
}

// Cache the info only once the host and the socket are resolved, so a failure is retried.
dockerInfo = res
dockerInfoSet = true

log.Printf(infoMessage, packagePath,
dockerInfo.Info.ServerVersion,
c.ClientVersion(),
dockerInfo.Info.OperatingSystem, dockerInfo.Info.MemTotal/1024/1024,
infoLabels,
internal.Version,
host,
core.MustExtractDockerSocket(ctx),
socket,
c.config.SessionID,
bootstrap.ProcessID(),
)
Expand Down
89 changes: 58 additions & 31 deletions internal/core/docker_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ var (
ErrTestcontainersHostNotSetInProperties = errors.New("tc.host not set in ~/.testcontainers.properties")
)

// The discovery caches only keep successful results, so a failed or timed out
// discovery is retried by the next call.
var (
dockerHostCache string
dockerHostErrCache error
dockerHostOnce sync.Once
dockerHostCache string
dockerHostLock sync.Mutex
)

var (
dockerSocketPathCache string
dockerSocketPathOnce sync.Once
dockerSocketPathLock sync.Mutex
)

// deprecated
Expand Down Expand Up @@ -98,14 +99,22 @@ func ExtractDockerHost(ctx context.Context) (string, error) {
if err != nil {
return "", err
}
dockerHostOnce.Do(func() {
if runtime.DockerHost != "" {
dockerHostCache = runtime.DockerHost
return
dockerHostLock.Lock()
defer dockerHostLock.Unlock()
if dockerHostCache != "" {
return dockerHostCache, nil
}

host := runtime.DockerHost
if host == "" {
host, err = extractDockerHost(ctx)
if err != nil {
return "", err
}
dockerHostCache, dockerHostErrCache = extractDockerHost(ctx)
})
return dockerHostCache, dockerHostErrCache
}
dockerHostCache = host

return host, nil
}

// MustExtractDockerSocket Extracts the docker socket from the different alternatives, removing the socket schema and
Expand All @@ -122,19 +131,37 @@ func ExtractDockerHost(ctx context.Context) (string, error) {
//
// It panics if a Docker client cannot be created, or the Docker host cannot be discovered.
func MustExtractDockerSocket(ctx context.Context) string {
runtime, err := Runtime(ctx)
socket, err := ExtractDockerSocket(ctx)
if err != nil {
panic(err)
}
dockerSocketPathOnce.Do(func() {
if runtime.DockerSocketPath != "" {
dockerSocketPathCache = runtime.DockerSocketPath
return
return socket
}

// ExtractDockerSocket is like MustExtractDockerSocket, but it returns an error instead of
// panicking. Only successful results are cached, so a failed discovery is retried by the next call.
func ExtractDockerSocket(ctx context.Context) (string, error) {
runtime, err := Runtime(ctx)
if err != nil {
return "", err
}

dockerSocketPathLock.Lock()
defer dockerSocketPathLock.Unlock()
if dockerSocketPathCache != "" {
return dockerSocketPathCache, nil
}

socket := runtime.DockerSocketPath
if socket == "" {
socket, err = extractDockerSocket(ctx)
if err != nil {
return "", err
}
dockerSocketPathCache = extractDockerSocket(ctx)
})
}
dockerSocketPathCache = socket

return dockerSocketPathCache
return socket, nil
}

// extractDockerHost Extracts the docker host from the different alternatives, without caching the result.
Expand Down Expand Up @@ -177,11 +204,11 @@ func extractDockerHost(ctx context.Context) (string, error) {
// extractDockerSocket Extracts the docker socket from the different alternatives, without caching the result.
// It will internally use the default Docker client, calling the internal method extractDockerSocketFromClient with it.
// This internal method is handy for testing purposes.
// It panics if a Docker client cannot be created, or the Docker host is not discovered.
func extractDockerSocket(ctx context.Context) string {
// It returns an error if a Docker client cannot be created, or the Docker host is not discovered.
func extractDockerSocket(ctx context.Context) (string, error) {
cli, err := NewClient(ctx)
if err != nil {
panic(err) // a Docker client is required to get the Docker info
return "", err // a Docker client is required to get the Docker info
}
defer cli.Close()

Expand All @@ -191,8 +218,8 @@ func extractDockerSocket(ctx context.Context) string {
// extractDockerSocketFromClient Extracts the docker socket from the different alternatives, without caching the result,
// and receiving an instance of the Docker API client interface.
// This internal method is handy for testing purposes, passing a mock type simulating the desired behaviour.
// It panics if the Docker Info call errors, or the Docker host is not discovered.
func extractDockerSocketFromClient(ctx context.Context, cli client.APIClient) string {
// It returns an error if the Docker Info call errors, or the Docker host is not discovered.
func extractDockerSocketFromClient(ctx context.Context, cli client.APIClient) (string, error) {
// check that the socket is not a tcp or unix socket
checkDockerSocketFn := func(socket string) string {
// this use case will cover the case when the docker host is a tcp socket
Expand All @@ -209,34 +236,34 @@ func extractDockerSocketFromClient(ctx context.Context, cli client.APIClient) st

tcHost, err := testcontainersHostFromProperties(ctx)
if err == nil {
return checkDockerSocketFn(tcHost)
return checkDockerSocketFn(tcHost), nil
}

testcontainersDockerSocket, err := dockerSocketOverridePath()
if err == nil {
return checkDockerSocketFn(testcontainersDockerSocket)
return checkDockerSocketFn(testcontainersDockerSocket), nil
}

info, err := cli.Info(ctx, client.InfoOptions{})
if err != nil {
panic(err) // Docker Info is required to get the Operating System
return "", err // Docker Info is required to get the Operating System
}

// Because Docker Desktop runs in a VM, we need to use the default docker path for rootless docker
if info.Info.OperatingSystem == "Docker Desktop" {
if IsWindows() {
return WindowsDockerSocketPath
return WindowsDockerSocketPath, nil
}

return DockerSocketPath
return DockerSocketPath, nil
}

dockerHost, err := extractDockerHost(ctx)
if err != nil {
panic(err) // Docker host is required to get the Docker socket
return "", err // Docker host is required to get the Docker socket
}

return checkDockerSocketFn(dockerHost)
return checkDockerSocketFn(dockerHost), nil
}

// isHostNotSet returns true if the error is related to the Docker host
Expand Down
74 changes: 59 additions & 15 deletions internal/core/docker_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,41 @@ func (m mockCli) Info(context.Context, client.InfoOptions) (client.SystemInfoRes
}, nil
}

func TestExtractDockerHostDoesNotCacheFailures(t *testing.T) {
isolateRuntimeRegistry(t)
setupTestcontainersProperties(t, "")
t.Setenv("DOCKER_HOST", "tcp://127.0.0.1:12345")

mockCallbackCheck(t, testCallbackCheckError)
_, err := ExtractDockerHost(context.Background())
require.Error(t, err)

// the daemon recovered: the failure must not be served from the cache
mockCallbackCheck(t, testCallbackCheckPassing)
host, err := ExtractDockerHost(context.Background())
require.NoError(t, err)
require.Equal(t, "tcp://127.0.0.1:12345", host)
}

func TestExtractDockerSocketReturnsErrorWithoutCaching(t *testing.T) {
isolateRuntimeRegistry(t)
setupTestcontainersProperties(t, "")
t.Cleanup(resetSocketOverrideFn)
os.Unsetenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE")
mockCallbackCheck(t, testCallbackCheckPassing)

// nothing listens on the port, so the Docker info call fails
t.Setenv("DOCKER_HOST", "tcp://127.0.0.1:1")
_, err := ExtractDockerSocket(context.Background())
require.Error(t, err)

// the failure must not be served from the cache
t.Setenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", "/path/to/docker.sock")
socket, err := ExtractDockerSocket(context.Background())
require.NoError(t, err)
require.Equal(t, "/path/to/docker.sock", socket)
}

func TestExtractDockerSocketFromClient(t *testing.T) {
setupDockerHostNotFound(t)

Expand All @@ -346,7 +381,8 @@ func TestExtractDockerSocketFromClient(t *testing.T) {

setupTestcontainersProperties(t, content)

socket := extractDockerSocketFromClient(context.Background(), mockCli{OS: "foo"})
socket, err := extractDockerSocketFromClient(context.Background(), mockCli{OS: "foo"})
require.NoError(t, err)
require.Equal(t, DockerSocketPath, socket)
})

Expand All @@ -358,7 +394,8 @@ func TestExtractDockerSocketFromClient(t *testing.T) {
t.Cleanup(resetSocketOverrideFn)
t.Setenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", "/path/to/docker.sock")

socket := extractDockerSocketFromClient(context.Background(), mockCli{OS: "foo"})
socket, err := extractDockerSocketFromClient(context.Background(), mockCli{OS: "foo"})
require.NoError(t, err)
require.Equal(t, DockerSocketPath, socket)
})

Expand All @@ -368,7 +405,8 @@ func TestExtractDockerSocketFromClient(t *testing.T) {
t.Cleanup(resetSocketOverrideFn)

t.Setenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", "/path/to/docker.sock")
host := extractDockerSocketFromClient(context.Background(), mockCli{OS: "foo"})
host, err := extractDockerSocketFromClient(context.Background(), mockCli{OS: "foo"})
require.NoError(t, err)

require.Equal(t, "/path/to/docker.sock", host)
})
Expand All @@ -379,11 +417,13 @@ func TestExtractDockerSocketFromClient(t *testing.T) {
t.Cleanup(resetSocketOverrideFn)

t.Setenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", DockerSocketSchema+"/path/to/docker.sock")
host := extractDockerSocketFromClient(context.Background(), mockCli{OS: "foo"})
host, err := extractDockerSocketFromClient(context.Background(), mockCli{OS: "foo"})
require.NoError(t, err)
require.Equal(t, "/path/to/docker.sock", host)

t.Setenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", testRemoteHost)
host = extractDockerSocketFromClient(context.Background(), mockCli{OS: "foo"})
host, err = extractDockerSocketFromClient(context.Background(), mockCli{OS: "foo"})
require.NoError(t, err)
require.Equal(t, DockerSocketPath, host)
})

Expand All @@ -401,7 +441,8 @@ func TestExtractDockerSocketFromClient(t *testing.T) {
os.Unsetenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE")
t.Setenv("DOCKER_HOST", DockerSocketSchema+"/this/is/a/sample.sock")

socket := extractDockerSocketFromClient(ctx, mockCli{OS: "Docker Desktop"})
socket, err := extractDockerSocketFromClient(ctx, mockCli{OS: "Docker Desktop"})
require.NoError(t, err)

require.Equal(t, DockerSocketPath, socket)
})
Expand All @@ -416,7 +457,8 @@ func TestExtractDockerSocketFromClient(t *testing.T) {
os.Unsetenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE")
t.Setenv("DOCKER_HOST", DockerSocketSchema+"/this/is/a/sample.sock")

socket := extractDockerSocketFromClient(ctx, mockCli{OS: "Docker Desktop"})
socket, err := extractDockerSocketFromClient(ctx, mockCli{OS: "Docker Desktop"})
require.NoError(t, err)

require.Equal(t, WindowsDockerSocketPath, socket)
})
Expand All @@ -430,7 +472,8 @@ func TestExtractDockerSocketFromClient(t *testing.T) {
os.Unsetenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE")
t.Setenv("DOCKER_HOST", DockerSocketSchema+"/this/is/a/sample.sock")

socket := extractDockerSocketFromClient(ctx, mockCli{OS: "Ubuntu"})
socket, err := extractDockerSocketFromClient(ctx, mockCli{OS: "Ubuntu"})
require.NoError(t, err)

require.Equal(t, "/this/is/a/sample.sock", socket)
})
Expand All @@ -444,11 +487,13 @@ func TestExtractDockerSocketFromClient(t *testing.T) {
os.Unsetenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE")

t.Setenv("DOCKER_HOST", DockerSocketSchema+"/this/is/a/sample.sock")
socket := extractDockerSocketFromClient(ctx, mockCli{OS: "Ubuntu"})
socket, err := extractDockerSocketFromClient(ctx, mockCli{OS: "Ubuntu"})
require.NoError(t, err)
require.Equal(t, "/this/is/a/sample.sock", socket)

t.Setenv("DOCKER_HOST", testRemoteHost)
socket = extractDockerSocketFromClient(ctx, mockCli{OS: "Ubuntu"})
socket, err = extractDockerSocketFromClient(ctx, mockCli{OS: "Ubuntu"})
require.NoError(t, err)
require.Equal(t, DockerSocketPath, socket)
})

Expand All @@ -463,7 +508,8 @@ func TestExtractDockerSocketFromClient(t *testing.T) {
os.Unsetenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE")
os.Unsetenv("DOCKER_HOST")

socket := extractDockerSocketFromClient(ctx, mockCli{OS: "Ubuntu"})
socket, err := extractDockerSocketFromClient(ctx, mockCli{OS: "Ubuntu"})
require.NoError(t, err)

require.Equal(t, "/this/is/a/sample.sock", socket)
})
Expand All @@ -480,10 +526,8 @@ func TestExtractDockerSocketFromClient(t *testing.T) {

mockCallbackCheck(t, testCallbackCheckError)

require.Panics(t, func() {
// no need to check for the returned socket, as it must panic
_ = extractDockerSocketFromClient(ctx, mockCli{OS: "Ubuntu"})
})
_, err := extractDockerSocketFromClient(ctx, mockCli{OS: "Ubuntu"})
require.Error(t, err)
})
}

Expand Down
4 changes: 1 addition & 3 deletions internal/core/runtime_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,7 @@ func isolateRuntimeRegistry(t *testing.T) {
previous := runtimeProviders
runtimeProviders = newRuntimeRegistry()
reset := func() {
dockerHostOnce = sync.Once{}
dockerHostCache, dockerHostErrCache = "", nil
dockerSocketPathOnce = sync.Once{}
dockerHostCache = ""
dockerSocketPathCache = ""
config.Reset()
}
Expand Down