command: print appropriate warning messages on 'context list'/'contex…#3668
command: print appropriate warning messages on 'context list'/'contex…#3668thaJeztah merged 2 commits intodocker:masterfrom
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3668 +/- ##
==========================================
+ Coverage 59.01% 59.03% +0.02%
==========================================
Files 289 289
Lines 24623 24626 +3
==========================================
+ Hits 14532 14539 +7
+ Misses 9218 9215 -3
+ Partials 873 872 -1 |
cli/command/context/list.go
Outdated
| return err | ||
| } | ||
| if os.Getenv(client.EnvOverrideHost) != "" { | ||
| if _, present := os.LookupEnv(client.EnvOverrideHost); present { |
There was a problem hiding this comment.
I haven't looked closely at the other places where this is checked, but wondering if instead of checking if the env-var is found, we should just take the "simple" approach and treat "empty" and "unset" as equivalent everywhere.
When scripting, it's not uncommon to have a variable set to an empty string (to "reset it"), and I think it would be ok for us to ignore in that case.
49f4db9 to
2e494c1
Compare
|
@thaJeztah I updated the PR so that it treats DOCKER_HOST="" and DOCKER_HOST unset the same way. |
b622717 to
c5d33b9
Compare
cli/command/cli.go
Outdated
| if os.Getenv(client.EnvOverrideHost) != "" { | ||
| return DefaultContextName, nil | ||
| } | ||
| if ctxName, ok := os.LookupEnv("DOCKER_CONTEXT"); ok { |
There was a problem hiding this comment.
should we also do the same thing with DOCKER_CONTEXT, for consistency?
There was a problem hiding this comment.
Whoops, missed your comment; yes, I think we should do the same (Or at least, I don't see a good reason to consider an empty env-var to be used 🤔)
cli/command/context/use.go
Outdated
| host := os.Getenv(client.EnvOverrideHost) | ||
| isIneffective := host != "" && configValue != command.DefaultContextName | ||
| if isIneffective { |
There was a problem hiding this comment.
Note to self: need to have another look at this logic 🤔
There was a problem hiding this comment.
Ah! Looks like this one needs to check either configValue != "" or name != command.DefaultContextName, because a couple of lines up, configValue is set to an empty string when using default;
cli/cli/command/context/use.go
Lines 34 to 37 in a445d97
print appropriate warning messages on 'context list'/'context use' Signed-off-by: Nick Santos <nick.santos@docker.com> Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Nick Santos <nick.santos@docker.com>
thaJeztah
left a comment
There was a problem hiding this comment.
Thanks; I found some minor issues, and one issue with the check; we can probably also squash the first two commits (as the second one is touching-up the first one).
I was writing along while reviewing, so let me push to your branch to get this going.
cli/command/context/use.go
Outdated
| host := os.Getenv(client.EnvOverrideHost) | ||
| isIneffective := host != "" && configValue != command.DefaultContextName | ||
| if isIneffective { |
There was a problem hiding this comment.
Ah! Looks like this one needs to check either configValue != "" or name != command.DefaultContextName, because a couple of lines up, configValue is set to an empty string when using default;
cli/cli/command/context/use.go
Lines 34 to 37 in a445d97
cli/command/context/use_test.go
Outdated
| assert.Assert(t, | ||
| strings.Contains( | ||
| cli.ErrBuffer().String(), | ||
| `Warning: DOCKER_HOST environment variable overrides the active context.`)) | ||
| } |
There was a problem hiding this comment.
Related to the above; we could probably add a test here then that verifies that the warning is not triggered for default.
cli/command/context/use_test.go
Outdated
| err = newUseCommand(cli).RunE(nil, []string{"test"}) | ||
| assert.NilError(t, err) | ||
| assert.Assert(t, !strings.Contains(out.String(), "Warning")) | ||
| assert.Assert(t, strings.Contains(out.String(), "Current context is now \"test\"")) |
There was a problem hiding this comment.
nit; could probably use ` as quotes make it slightly more readable
For the string comparing, we can use gotest.tools/v3/assert/cmp, which prints a nice diff if things aren't equal.
cli/command/context/use_test.go
Outdated
| configDir := t.TempDir() | ||
| config.SetDir(configDir) | ||
|
|
||
| socketPath := fmt.Sprintf("unix://%s", filepath.Join(configDir, "docker.sock")) |
There was a problem hiding this comment.
nit; for simple cases, we generally prefer to just concatenate;
socketPath := "unix://" + filepath.Join(configDir, "docker.sock")dd6198e to
50893d7
Compare
thaJeztah
left a comment
There was a problem hiding this comment.
CI is happy; let's get this one in!
LGTM
|
woooo, thanks!! |
- What I did
print appropriate warning messages on 'docker context list' and 'docker context use'
- How I did it
Makes sure that the use/list implementations use the same logic for checking env
variables as the code that processes the override.
- How to verify it
see attached issues
docker context usefails silently when DOCKER_HOST set to empty string #3667- Description for the changelog
print appropriate warning messages on 'docker context list' and 'docker context use'
- A picture of a cute animal (not mandatory but encouraged)
