-
-
Notifications
You must be signed in to change notification settings - Fork 512
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: extract docker host calculation to an internal package (#796)
- Loading branch information
1 parent
898a14d
commit 9fb8074
Showing
5 changed files
with
97 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package testcontainersdocker | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"os" | ||
) | ||
|
||
type dockerHostContext string | ||
|
||
var DockerHostContextKey = dockerHostContext("docker_host") | ||
|
||
// Extracts the docker host from the context, or returns the default value | ||
func ExtractDockerHost(ctx context.Context) (dockerHostPath string) { | ||
if dockerHostPath = os.Getenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE"); dockerHostPath != "" { | ||
return dockerHostPath | ||
} | ||
|
||
dockerHostPath = "/var/run/docker.sock" | ||
|
||
var hostRawURL string | ||
if h, ok := ctx.Value(DockerHostContextKey).(string); !ok || h == "" { | ||
return dockerHostPath | ||
} else { | ||
hostRawURL = h | ||
} | ||
var hostURL *url.URL | ||
if u, err := url.Parse(hostRawURL); err != nil { | ||
return dockerHostPath | ||
} else { | ||
hostURL = u | ||
} | ||
|
||
switch hostURL.Scheme { | ||
case "unix": | ||
return hostURL.Path | ||
default: | ||
return dockerHostPath | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package testcontainersdocker | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_ExtractDockerHost(t *testing.T) { | ||
t.Run("Docker Host as environment variable", func(t *testing.T) { | ||
t.Setenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", "/path/to/docker.sock") | ||
host := ExtractDockerHost(context.Background()) | ||
|
||
assert.Equal(t, "/path/to/docker.sock", host) | ||
}) | ||
|
||
t.Run("Default Docker Host", func(t *testing.T) { | ||
host := ExtractDockerHost(context.Background()) | ||
|
||
assert.Equal(t, "/var/run/docker.sock", host) | ||
}) | ||
|
||
t.Run("Malformed Docker Host is passed in context", func(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
host := ExtractDockerHost(context.WithValue(ctx, DockerHostContextKey, "path-to-docker-sock")) | ||
|
||
assert.Equal(t, "/var/run/docker.sock", host) | ||
}) | ||
|
||
t.Run("Malformed Schema Docker Host is passed in context", func(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
host := ExtractDockerHost(context.WithValue(ctx, DockerHostContextKey, "http://path to docker sock")) | ||
|
||
assert.Equal(t, "/var/run/docker.sock", host) | ||
}) | ||
|
||
t.Run("Unix Docker Host is passed in context", func(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
host := ExtractDockerHost(context.WithValue(ctx, DockerHostContextKey, "unix:///this/is/a/sample.sock")) | ||
|
||
assert.Equal(t, "/this/is/a/sample.sock", host) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters