Skip to content

Commit da1f481

Browse files
knqyf263mdelapenya
andauthored
fix: preserve unix socket schema in testcontainers host from properties (#3213)
This fix addresses an issue where unix socket URLs like 'unix:///var/run/docker.sock' would have their schema stripped when parsed through testcontainersHostFromProperties, causing Docker client initialization to fail. The solution validates the URL format but preserves the original URL with schema for Docker client compatibility, while maintaining backward compatibility for other use cases. - Fix testcontainersHostFromProperties to preserve unix socket schema - Add comprehensive test coverage for unix socket handling - Ensure TCP hosts continue to work as expected Co-authored-by: Manuel de la Peña <[email protected]>
1 parent 906c1aa commit da1f481

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

internal/core/docker_host.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,14 @@ func testcontainersHostFromProperties(_ context.Context) (string, error) {
298298
cfg := config.Read()
299299
testcontainersHost := cfg.TestcontainersHost
300300
if testcontainersHost != "" {
301-
parsed, err := parseURL(testcontainersHost)
301+
// Validate the URL format
302+
_, err := parseURL(testcontainersHost)
302303
if err != nil {
303304
return "", err
304305
}
305306

306-
return parsed, nil
307+
// Return the original URL to preserve schema for Docker client
308+
return testcontainersHost, nil
307309
}
308310

309311
return "", ErrTestcontainersHostNotSetInProperties

internal/core/docker_host_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,26 @@ func TestExtractDockerHost(t *testing.T) {
177177
t.Cleanup(resetSocketOverrideFn)
178178

179179
t.Run("Testcontainers host is defined in properties", func(t *testing.T) {
180-
content := "tc.host=" + testRemoteHost
180+
t.Run("TCP host", func(t *testing.T) {
181+
content := "tc.host=" + testRemoteHost
181182

182-
setupTestcontainersProperties(t, content)
183+
setupTestcontainersProperties(t, content)
183184

184-
socket, err := testcontainersHostFromProperties(context.Background())
185-
require.NoError(t, err)
186-
require.Equal(t, testRemoteHost, socket)
185+
socket, err := testcontainersHostFromProperties(context.Background())
186+
require.NoError(t, err)
187+
require.Equal(t, testRemoteHost, socket)
188+
})
189+
190+
t.Run("Unix socket host preserves schema", func(t *testing.T) {
191+
unixSocket := "unix:///var/run/docker.sock"
192+
content := "tc.host=" + unixSocket
193+
194+
setupTestcontainersProperties(t, content)
195+
196+
socket, err := testcontainersHostFromProperties(context.Background())
197+
require.NoError(t, err)
198+
require.Equal(t, unixSocket, socket)
199+
})
187200
})
188201

189202
t.Run("Testcontainers host is not defined in properties", func(t *testing.T) {

0 commit comments

Comments
 (0)