Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check if the discovered docker socket responds #2741

Merged
merged 26 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
19fb55b
fix: check if the discovered docker socket responds
mdelapenya Aug 21, 2024
fbadada
fix: update tests
mdelapenya Aug 21, 2024
c6c4832
chore: add test
mdelapenya Aug 21, 2024
ff83aff
Revert "chore: add test"
mdelapenya Aug 21, 2024
270c2b6
Revert "fix: update tests"
mdelapenya Aug 21, 2024
cdb3825
Revert "fix: check if the discovered docker socket responds"
mdelapenya Aug 21, 2024
42a77dd
chore: support passing callback checks to the docker host/socket path…
mdelapenya Aug 21, 2024
64c4d2d
chore: convert var into function
mdelapenya Aug 22, 2024
e8eeff3
chore: mock callback check instead
mdelapenya Aug 22, 2024
da6d2b1
chore: simplify
mdelapenya Aug 22, 2024
8dd2979
chore: raise error when extracting the docker host
mdelapenya Aug 26, 2024
320ac54
Merge branch 'main' into docker-socket-info
mdelapenya Aug 27, 2024
94b661d
docs: document that the extract functions panics
mdelapenya Aug 28, 2024
d29de5e
chore: simplify error handler
mdelapenya Aug 28, 2024
c4546f6
chore: use require.Panics
mdelapenya Aug 28, 2024
b493c72
fix: remove duplicated case
mdelapenya Aug 28, 2024
5a234fe
fix: negotiate API version in the plain docker client call
mdelapenya Aug 28, 2024
38720a3
fix: defer closing the client earlier
mdelapenya Aug 28, 2024
1da780c
chore: better function name
mdelapenya Aug 28, 2024
2472045
chore: convert vars into functions
mdelapenya Aug 28, 2024
1f3c6d9
chore: no need to assert as panic should occur
mdelapenya Aug 28, 2024
311507e
chor: rename check function
mdelapenya Aug 28, 2024
50d424f
chore: pass ctx to new function
mdelapenya Aug 28, 2024
f030b5c
chore: more exhaustive error check in tests
mdelapenya Aug 28, 2024
8cbe4e6
docs: typo
mdelapenya Aug 28, 2024
9dfd4fb
fix: update usage
mdelapenya Aug 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions internal/core/docker_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ func DefaultGatewayIP() (string, error) {
return ip, nil
}

// defaultCallbackCheckFn Use a vanilla Docker client to check if the Docker host is reachable.
// It will avoid recursive calls to this function.
var defaultCallbackCheckFn = func(ctx context.Context, host string) error {
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithHost(host))
if err != nil {
return err
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
}
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved

_, err = cli.Info(ctx)
if err != nil {
return err
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
}
defer cli.Close()
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved

return nil
}

// ExtractDockerHost Extracts the docker host from the different alternatives, caching the result to avoid unnecessary
// calculations. Use this function to get the actual Docker host. This function does not consider Windows containers at the moment.
// The possible alternatives are:
Expand All @@ -66,10 +83,15 @@ func DefaultGatewayIP() (string, error) {
// 4. Docker host from the default docker socket path, without the unix schema.
// 5. Docker host from the "docker.host" property in the ~/.testcontainers.properties file.
// 6. Rootless docker socket path.
// 7. Else, the default Docker socket including schema will be returned.
// 7. Else, because the Docker host is not set, it panics.
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
func ExtractDockerHost(ctx context.Context) string {
dockerHostOnce.Do(func() {
dockerHostCache = extractDockerHost(ctx)
cache, err := extractDockerHost(ctx)
if err != nil {
panic(err)
}

dockerHostCache = cache
})

return dockerHostCache
Expand Down Expand Up @@ -98,7 +120,7 @@ func ExtractDockerSocket(ctx context.Context) string {

// extractDockerHost Extracts the docker host from the different alternatives, without caching the result.
// This internal method is handy for testing purposes.
func extractDockerHost(ctx context.Context) string {
func extractDockerHost(ctx context.Context) (string, error) {
dockerHostFns := []func(context.Context) (string, error){
testcontainersHostFromProperties,
dockerHostFromEnv,
Expand All @@ -116,14 +138,17 @@ func extractDockerHost(ctx context.Context) string {
continue
}

return dockerHost
if err = defaultCallbackCheckFn(ctx, dockerHost); err != nil {
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
continue
}

return dockerHost, nil
}

// We are not supporting Windows containers at the moment
return DockerSocketPathWithSchema
return "", outerErr
}

// extractDockerHost Extracts the docker socket from the different alternatives, without caching the result.
// extractDockerSocket Extracts the docker socket from the different alternatives, without caching the result.
// It will internally use the default Docker client, calling the internal method extractDockerSocketFromClient with it.
// This internal method is handy for testing purposes.
// If a Docker client cannot be created, the program will panic.
Expand Down Expand Up @@ -179,7 +204,10 @@ func extractDockerSocketFromClient(ctx context.Context, cli client.APIClient) st
return DockerSocketPath
}

dockerHost := extractDockerHost(ctx)
dockerHost, err := extractDockerHost(ctx)
if err != nil {
panic(err) // Docker host is required to get the Docker socket
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug: This results in ExtractDockerSocket panic which is used in other non internal functions including its namesake ExtractDockerSocket none of which mention they can panic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added comments to the functions, could you please check that? 🙏

}

return checkDockerSocketFn(dockerHost)
}
Expand Down
Loading
Loading