Skip to content

Commit

Permalink
chore: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Aug 21, 2024
1 parent fbadada commit c6c4832
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion internal/core/docker_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c6c4832

Please sign in to comment.