From 20eedb0348809bbffd64424e1d9f3ce15e1e1050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 13 Jun 2023 11:31:02 +0200 Subject: [PATCH] fix: return proper docker socket for remote Docker hosts (#1281) * fix: return proper docker socket for remote Docker hosts * chore: simplify resetting the config in tests * docs: document that tc.host will return default docker socket path --- docs/features/configuration.md | 3 ++- internal/testcontainersdocker/docker_host.go | 5 ++++ .../testcontainersdocker/docker_host_test.go | 23 ++++++++----------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/docs/features/configuration.md b/docs/features/configuration.md index aaa8d1c998..e40c14af4c 100644 --- a/docs/features/configuration.md +++ b/docs/features/configuration.md @@ -80,7 +80,8 @@ _Testcontainers for Go_ will attempt to detect the Docker socket path and config However, sometimes customization is required. _Testcontainers for Go_ will respect the following order: -1. Read the **tc.host** property in the `~/.testcontainers.properties` file. E.g. `tc.host=tcp://my.docker.host:1234` +1. Read the **tc.host** property in the `~/.testcontainers.properties` file. E.g. `tc.host=tcp://my.docker.host:1234`. If this property is set, the returned Docker socket path +will be the default Docker socket path: `/var/run/docker.sock`. 2. Read the **TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE** environment variable. Path to Docker's socket. Used by Ryuk, Docker Compose, and a few other containers that need to perform Docker actions. diff --git a/internal/testcontainersdocker/docker_host.go b/internal/testcontainersdocker/docker_host.go index 6b9827f733..6013595964 100644 --- a/internal/testcontainersdocker/docker_host.go +++ b/internal/testcontainersdocker/docker_host.go @@ -135,6 +135,11 @@ func extractDockerSocket(ctx context.Context) string { func extractDockerSocketFromClient(ctx context.Context, cli client.APIClient) string { tcHost, err := testcontainersHostFromProperties(ctx) if err == nil { + // this use case will cover the case when the docker host is a tcp socket + if strings.HasPrefix(tcHost, "tcp://") { + return "/var/run/docker.sock" + } + return tcHost } diff --git a/internal/testcontainersdocker/docker_host_test.go b/internal/testcontainersdocker/docker_host_test.go index 9da3eff303..dbbe2cf9b5 100644 --- a/internal/testcontainersdocker/docker_host_test.go +++ b/internal/testcontainersdocker/docker_host_test.go @@ -58,7 +58,6 @@ func TestExtractDockerHost(t *testing.T) { tmpHost := "tcp://127.0.0.1:12345" content := "tc.host=" + tmpHost - config.Reset() setupTestcontainersProperties(t, content) host := extractDockerHost(context.Background()) @@ -127,9 +126,7 @@ func TestExtractDockerHost(t *testing.T) { tmpSocket := "tcp://127.0.0.1:12345" content := "tc.host=" + tmpSocket - config.Reset() setupTestcontainersProperties(t, content) - defer config.Reset() socket, err := testcontainersHostFromProperties(context.Background()) require.Nil(t, err) @@ -139,9 +136,7 @@ func TestExtractDockerHost(t *testing.T) { t.Run("Testcontainers host is not defined in properties", func(t *testing.T) { content := "ryuk.disabled=false" - config.Reset() setupTestcontainersProperties(t, content) - defer config.Reset() socket, err := testcontainersHostFromProperties(context.Background()) require.ErrorIs(t, err, ErrTestcontainersHostNotSetInProperties) @@ -228,7 +223,6 @@ func TestExtractDockerHost(t *testing.T) { tmpSocket := "/this/is/a/sample.sock" content := "docker.host=unix://" + tmpSocket - config.Reset() setupTestcontainersProperties(t, content) socket, err := dockerHostFromProperties(context.Background()) @@ -239,7 +233,6 @@ func TestExtractDockerHost(t *testing.T) { t.Run("Docker host is not defined in properties", func(t *testing.T) { content := "ryuk.disabled=false" - config.Reset() setupTestcontainersProperties(t, content) socket, err := dockerHostFromProperties(context.Background()) @@ -279,30 +272,28 @@ func TestExtractDockerSocketFromClient(t *testing.T) { tmpSocket := "tcp://127.0.0.1:12345" content := "tc.host=" + tmpSocket - config.Reset() setupTestcontainersProperties(t, content) - defer config.Reset() socket := extractDockerSocketFromClient(context.Background(), mockCli{OS: "foo"}) - assert.Equal(t, tmpSocket, socket) + assert.Equal(t, "/var/run/docker.sock", socket) }) t.Run("Docker socket from Testcontainers host takes precedence over TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", func(t *testing.T) { tmpSocket := "tcp://127.0.0.1:12345" content := "tc.host=" + tmpSocket - config.Reset() setupTestcontainersProperties(t, content) - t.Cleanup(config.Reset) t.Cleanup(resetSocketOverrideFn) t.Setenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", "/path/to/docker.sock") socket := extractDockerSocketFromClient(context.Background(), mockCli{OS: "foo"}) - assert.Equal(t, tmpSocket, socket) + assert.Equal(t, "/var/run/docker.sock", socket) }) t.Run("Docker Socket as Testcontainers environment variable", func(t *testing.T) { + setupTestcontainersProperties(t, "") + t.Cleanup(resetSocketOverrideFn) t.Setenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", "/path/to/docker.sock") @@ -312,6 +303,8 @@ func TestExtractDockerSocketFromClient(t *testing.T) { }) t.Run("Unix Docker Socket is passed as DOCKER_HOST variable (Docker Desktop)", func(t *testing.T) { + setupTestcontainersProperties(t, "") + t.Cleanup(resetSocketOverrideFn) ctx := context.Background() @@ -324,6 +317,8 @@ func TestExtractDockerSocketFromClient(t *testing.T) { }) t.Run("Unix Docker Socket is passed as DOCKER_HOST variable (Not Docker Desktop)", func(t *testing.T) { + setupTestcontainersProperties(t, "") + t.Cleanup(resetSocketOverrideFn) ctx := context.Background() @@ -421,6 +416,8 @@ func setupTestcontainersProperties(t *testing.T, content string) { config.Reset() }) + config.Reset() + tmpDir := t.TempDir() homeDir := filepath.Join(tmpDir, "home") err := createTmpDir(homeDir)