Skip to content

Commit

Permalink
break: do not return the Docker Host when retrieving a Docker client
Browse files Browse the repository at this point in the history
It comes in the TC config
  • Loading branch information
mdelapenya committed Mar 15, 2023
1 parent 9f27165 commit e754c24
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 18 deletions.
8 changes: 7 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ More on this: https://golang.testcontainers.org/features/garbage_collector/
// readConfig reads from testcontainers properties file, if it exists
// it is possible that certain values get overridden when set as environment variables
func readConfig() TestcontainersConfig {
config := TestcontainersConfig{}
config := TestcontainersConfig{
Host: "unix:///var/run/docker.sock",
}

applyEnvironmentConfiguration := func(config TestcontainersConfig) TestcontainersConfig {
if dockerHostEnv := os.Getenv("DOCKER_HOST"); dockerHostEnv != "" {
config.Host = dockerHostEnv
}

ryukDisabledEnv := os.Getenv("TESTCONTAINERS_RYUK_DISABLED")
if parseBool(ryukDisabledEnv) {
config.RyukDisabled = ryukDisabledEnv == "true"
Expand Down
13 changes: 13 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestReadConfig(t *testing.T) {

expected := TestcontainersConfig{
RyukDisabled: true,
Host: "unix:///var/run/docker.sock",
}

assert.Equal(t, expected, config)
Expand Down Expand Up @@ -60,6 +61,18 @@ func TestReadTCConfig(t *testing.T) {
assert.Empty(t, config, "TC props file should not exist")
})

t.Run("HOME does not contain TC props file - DOCKER_HOST env is set", func(t *testing.T) {
tmpDir := t.TempDir()
t.Setenv("HOME", tmpDir)
t.Setenv("DOCKER_HOST", "tcp://127.0.0.1:33293")

config := readConfig()
expected := TestcontainersConfig{}
expected.Host = "tcp://127.0.0.1:33293"

assert.Equal(t, expected, config)
})

t.Run("HOME does not contain TC props file - TESTCONTAINERS_ env is set", func(t *testing.T) {
tmpDir := t.TempDir()
t.Setenv("HOME", tmpDir)
Expand Down
14 changes: 5 additions & 9 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,10 +744,10 @@ func (p *DockerProvider) SetClient(c client.APIClient) {

var _ ContainerProvider = (*DockerProvider)(nil)

func NewDockerClient() (cli *client.Client, host string, err error) {
func NewDockerClient() (cli *client.Client, err error) {
tcConfig = ReadConfig()

host = tcConfig.Host
host := tcConfig.Host

opts := []client.Opt{client.FromEnv, client.WithAPIVersionNegotiation()}
if host != "" {
Expand All @@ -761,10 +761,6 @@ func NewDockerClient() (cli *client.Client, host string, err error) {

opts = append(opts, client.WithTLSClientConfig(cacertPath, certPath, keyPath))
}
} else if dockerHostEnv := os.Getenv("DOCKER_HOST"); dockerHostEnv != "" {
host = dockerHostEnv
} else {
host = "unix:///var/run/docker.sock"
}

opts = append(opts, client.WithHTTPHeaders(
Expand All @@ -775,20 +771,20 @@ func NewDockerClient() (cli *client.Client, host string, err error) {

cli, err = client.NewClientWithOpts(opts...)
if err != nil {
return nil, "", err
return nil, err
}

_, err = cli.Ping(context.TODO())
if err != nil {
// fallback to environment
cli, err = testcontainersdocker.NewClient(context.Background())
if err != nil {
return nil, "", err
return nil, err
}
}
defer cli.Close()

return cli, host, nil
return cli, nil
}

// BuildImage will build and image from context and Dockerfile, then return the tag
Expand Down
6 changes: 3 additions & 3 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1393,7 +1393,7 @@ func TestContainerCreationWithBindAndVolume(t *testing.T) {
ctx, cnl := context.WithTimeout(context.Background(), 30*time.Second)
defer cnl()
// Create a Docker client.
dockerCli, _, err := NewDockerClient()
dockerCli, err := NewDockerClient()
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1547,7 +1547,7 @@ func TestContainerCustomPlatformImage(t *testing.T) {
require.NoError(t, err)
terminateContainerOnEnd(t, ctx, c)

dockerCli, _, err := NewDockerClient()
dockerCli, err := NewDockerClient()
require.NoError(t, err)

ctr, err := dockerCli.ContainerInspect(ctx, c.GetContainerID())
Expand Down Expand Up @@ -1584,7 +1584,7 @@ func TestContainerWithCustomHostname(t *testing.T) {
}

func readHostname(tb testing.TB, containerId string) string {
containerClient, _, err := NewDockerClient()
containerClient, err := NewDockerClient()
if err != nil {
tb.Fatalf("Failed to create Docker client: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion modules/compose/compose_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func withEnv(env map[string]string) func(*cli.ProjectOptions) error {
}

func makeClient(*command.DockerCli) (client.APIClient, error) {
dockerClient, _, err := testcontainers.NewDockerClient()
dockerClient, err := testcontainers.NewDockerClient()
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions modules/compose/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ func TestLocalDockerComposeWithVolume(t *testing.T) {
}

func assertVolumeDoesNotExist(tb testing.TB, volumeName string) {
containerClient, _, err := testcontainers.NewDockerClient()
containerClient, err := testcontainers.NewDockerClient()
if err != nil {
tb.Fatalf("Failed to get provider: %v", err)
}
Expand All @@ -470,7 +470,7 @@ func assertContainerEnvironmentVariables(
present map[string]string,
absent map[string]string,
) {
containerClient, _, err := testcontainers.NewDockerClient()
containerClient, err := testcontainers.NewDockerClient()
if err != nil {
tb.Fatalf("Failed to get provider: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func NewDockerProvider(provOpts ...DockerProviderOption) (*DockerProvider, error
provOpts[idx].ApplyDockerTo(o)
}

c, host, err := NewDockerClient()
c, err := NewDockerClient()
if err != nil {
return nil, err
}
Expand All @@ -137,7 +137,7 @@ func NewDockerProvider(provOpts ...DockerProviderOption) (*DockerProvider, error

p := &DockerProvider{
DockerProviderOptions: o,
host: host,
host: tcConfig.Host,
client: c,
config: tcConfig,
}
Expand Down

0 comments on commit e754c24

Please sign in to comment.