Skip to content

Commit

Permalink
fix: return proper docker socket for remote Docker hosts (#1281)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mdelapenya authored Jun 13, 2023
1 parent b9e1740 commit 20eedb0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
3 changes: 2 additions & 1 deletion docs/features/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions internal/testcontainersdocker/docker_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
23 changes: 10 additions & 13 deletions internal/testcontainersdocker/docker_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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")
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 20eedb0

Please sign in to comment.