From c6c4832a78138890cfd52543f3658a8d5ee62fd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 21 Aug 2024 12:57:25 +0200 Subject: [PATCH] chore: add test --- internal/core/docker_host_test.go | 32 ++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/internal/core/docker_host_test.go b/internal/core/docker_host_test.go index 23d8e1e1fa..cc0b36bc38 100644 --- a/internal/core/docker_host_test.go +++ b/internal/core/docker_host_test.go @@ -2,12 +2,14 @@ package core import ( "context" + "fmt" "os" "path/filepath" "testing" "github.com/docker/docker/api/types/system" "github.com/docker/docker/client" + "github.com/docker/docker/errdefs" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -271,12 +273,17 @@ func TestExtractDockerHost(t *testing.T) { // different operating systems. type mockCli struct { client.APIClient - OS string + OS string + notReachable bool } // Info returns a mock implementation of types.Info, which is handy for detecting the operating system, // which is used to determine the default docker socket path. func (m mockCli) Info(ctx context.Context) (system.Info, error) { + if m.notReachable { + return system.Info{}, errdefs.Unavailable(fmt.Errorf("Docker is not reachable")) + } + return system.Info{ OperatingSystem: m.OS, }, nil @@ -411,6 +418,29 @@ func TestExtractDockerSocketFromClient(t *testing.T) { assert.Equal(t, "/this/is/a/sample.sock", socket) }) + + t.Run("Unix Docker Socket is not reachable, so Info panics", func(t *testing.T) { + defer func() { + if r := recover(); r == nil { + t.Errorf("The code did not panic") + } + }() + + content := "docker.host=" + DockerSocketSchema + "/this/is/a/sample.sock" + setupTestcontainersProperties(t, content) + setupDockerSocketNotFound(t) + + t.Cleanup(resetSocketOverrideFn) + + ctx := context.Background() + os.Unsetenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE") + os.Unsetenv("DOCKER_HOST") + + // force a non + socket := extractDockerSocketFromClient(ctx, mockCli{notReachable: true}) + + assert.Equal(t, "/this/is/a/sample.sock", socket) + }) } func TestInAContainer(t *testing.T) {