Repository navigation
Fix(connectors): resolve Docker host from active context #550
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,112 @@ | ||
| package connectors | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "os/exec" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestConfigureDockerHost(t *testing.T) { | ||
| oldDockerHost := os.Getenv("DOCKER_HOST") | ||
| defer func() { | ||
| if oldDockerHost == "" { | ||
| os.Unsetenv("DOCKER_HOST") | ||
| } else { | ||
| os.Setenv("DOCKER_HOST", oldDockerHost) | ||
| } | ||
| }() | ||
|
|
||
| oldExecCommand := execCommand | ||
| defer func() { | ||
| execCommand = oldExecCommand | ||
| }() | ||
|
|
||
| t.Run("sets DOCKER_HOST from docker context", func(t *testing.T) { | ||
| os.Unsetenv("DOCKER_HOST") | ||
|
|
||
| execCommand = func(name string, args ...string) *exec.Cmd { | ||
| cmd := exec.Command( | ||
| os.Args[0], | ||
| "-test.run=TestHelperProcess", | ||
| "--", | ||
| "npipe:////./pipe/dockerDesktopLinuxEngine", | ||
| ) | ||
| cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1") | ||
| return cmd | ||
| } | ||
|
|
||
| require.NoError(t, ConfigureDockerHost()) | ||
| assert.Equal(t, "npipe:////./pipe/dockerDesktopLinuxEngine", os.Getenv("DOCKER_HOST")) | ||
| }) | ||
|
|
||
| t.Run("does not overwrite existing DOCKER_HOST", func(t *testing.T) { | ||
| require.NoError(t, os.Setenv("DOCKER_HOST", "existing-endpoint")) | ||
|
|
||
| require.NoError(t, ConfigureDockerHost()) | ||
| assert.Equal(t, "existing-endpoint", os.Getenv("DOCKER_HOST")) | ||
| }) | ||
|
|
||
| t.Run("returns error when docker context command fails", func(t *testing.T) { | ||
| os.Unsetenv("DOCKER_HOST") | ||
|
|
||
| execCommand = func(name string, args ...string) *exec.Cmd { | ||
| cmd := exec.Command( | ||
| os.Args[0], | ||
| "-test.run=TestHelperProcessFailure", | ||
| ) | ||
| cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1") | ||
| return cmd | ||
| } | ||
|
|
||
| err := ConfigureDockerHost() | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "resolving docker context host") | ||
| }) | ||
| } | ||
| func TestNewDockerClient(t *testing.T) { | ||
| oldDockerHost := os.Getenv("DOCKER_HOST") | ||
| defer func() { | ||
| if oldDockerHost == "" { | ||
| os.Unsetenv("DOCKER_HOST") | ||
| } else { | ||
| os.Setenv("DOCKER_HOST", oldDockerHost) | ||
| } | ||
| }() | ||
|
|
||
| os.Unsetenv("DOCKER_HOST") | ||
|
|
||
| dockerClient, err := NewDockerClient() | ||
| require.NoError(t, err) | ||
| require.NotNil(t, dockerClient) | ||
|
|
||
| defer dockerClient.cli.Close() | ||
|
|
||
| ctx := context.Background() | ||
| _, err = dockerClient.cli.Ping(ctx) | ||
| require.NoError(t, err) | ||
| } | ||
| func TestHelperProcess(t *testing.T) { | ||
| if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { | ||
| return | ||
| } | ||
|
|
||
| for i, arg := range os.Args { | ||
| if arg == "--" && i+1 < len(os.Args) { | ||
| os.Stdout.WriteString(os.Args[i+1]) | ||
| os.Exit(0) | ||
| } | ||
| } | ||
|
|
||
| os.Exit(1) | ||
| } | ||
| func TestHelperProcessFailure(t *testing.T) { | ||
| if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { | ||
| return | ||
| } | ||
|
|
||
| os.Exit(2) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SafeeImran Bro this solution is already implemented in #538 by @ish-g09. I intentionally put that on halt as we want to discover that wether this is possible or not without making any manual effort. So I cant approve these changes.