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

cli/config: prevent warning if HOME is not set #2934

Merged
merged 1 commit into from
Jan 19, 2021

Conversation

thaJeztah
Copy link
Member

@thaJeztah thaJeztah commented Jan 18, 2021

fixes moby/moby#41890 WARNING: Error loading config file: .dockercfg: $HOME is not defined

commit c2626a8 (#2101) replaced the use of github.com/docker/docker/pkg/homedir with Golang's os.UserHomeDir().

This change was partially reverted in 7a279af (#2111) to account for situations where $HOME is not set.

In situations where no configuration file is present in ~/.config/, the CLI falls back to looking for the (deprecated) ~/.dockercfg configuration file, which was still using os.UserHomeDir(), which produces an error/warning if $HOME is not set.

This patch introduces a helper function and a global variable to get the user's home-directory. The global variable is used to prevent repeatedly looking up the user's information (which, depending on the setup can be a costly operation).

- Description for the changelog

- Fix "`WARNING: Error loading config file: .dockercfg: $HOME is not defined`"

- A picture of a cute animal (not mandatory but encouraged)

@thaJeztah thaJeztah changed the title cli/config: re-use home-directory lookup cli/config: prevent warning if HOME is not set Jan 18, 2021
Copy link
Contributor

@silvin-lubecki silvin-lubecki left a comment

Choose a reason for hiding this comment

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

LGTM

@silvin-lubecki
Copy link
Contributor

@thaJeztah some tests are failing:

=== Failed
=== FAIL: cli/config TestOldInvalidsAuth (0.00s)
    config_test.go:129: assertion failed: expected an error, got nil

=== FAIL: cli/config TestOldValidAuth (0.00s)
    config_test.go:150: assertion failed:  (ac.Username string) != joejoe (string)

=== FAIL: cli/config TestOldJSONInvalid (0.00s)
    config_test.go:182: Expected an error got : &{map[] map[]         map[] /tmp/config-test967963783/config.json       [] map[]   <nil>  [] map[] map[]}, <nil>

=== FAIL: cli/config TestOldJSON (0.00s)
    config_test.go:202: assertion failed:  (ac.Username string) != joejoe (string)

@thaJeztah
Copy link
Member Author

some tests are failing:

Hmmm... ah, I suspect those go wrong because it will only fetch the home-directory once, and the test is updating the path 🤔. Will have to have a look how to solve that test

commit c2626a8 replaced the use of
github.com/docker/docker/pkg/homedir with Golang's os.UserHomeDir().

This change was partially reverted in 7a279af
to account for situations where `$HOME` is not set.

In  situations where no configuration file is present in `~/.config/`, the CLI
falls back to looking for the (deprecated) `~/.dockercfg` configuration file,
which was still using `os.UserHomeDir()`, which produces an error/warning if
`$HOME` is not set.

This patch introduces a helper function and a global variable to get the user's
home-directory. The global variable is used to prevent repeatedly looking up
the user's information (which, depending on the setup can be a costly operation).

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah
Copy link
Member Author

Fixed the tests, but it's a bit ugly; not sure if there's really a cleaner way though

@codecov-io
Copy link

Codecov Report

Merging #2934 (c85a37d) into master (9a3fdc1) will increase coverage by 0.01%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #2934      +/-   ##
==========================================
+ Coverage   57.04%   57.05%   +0.01%     
==========================================
  Files         297      297              
  Lines       18655    18656       +1     
==========================================
+ Hits        10641    10644       +3     
+ Misses       7154     7153       -1     
+ Partials      860      859       -1     

)

// resetHomeDir is used in testing to resets the "homeDir" package variable to
// force re-lookup of the home directory between tests.
func resetHomeDir() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can move this function in the tests files? I mean, tests are in the same package, so they have access to the homeDir variable.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I was on the fence what would be better; I put it in the non-test file to make it more visible (I really don't like having to do this 😞)

@@ -115,6 +115,7 @@ password`: "Invalid Auth config file",
email`: "Invalid auth configuration file",
}

resetHomeDir()
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit on the fence between calling this function for each test as first step, to make sure the path is clean for the test, OR defer it at first step, to make sure everything will be cleared for other tests. 🤔

func TestOldInvalidsAuth(t *testing.T) {
    defer resetHomeDir()
    ...
}
func TestOldInvalidsAuth(t *testing.T) {
   resetHomeDir()
    ...
}

Copy link
Member Author

Choose a reason for hiding this comment

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

It's tricky; any test that uses the homeDir() will initialise the package variable; my current approach was to put it in those tests that expect the variable to be re-resolved (because they patch the HOME variable for the test); changing the HOME variable is something that would only happen in tests (under normal circumstances it would not modify after the cli binary is started, so it would be safe to store it in the variable.

Copy link
Contributor

@silvin-lubecki silvin-lubecki left a comment

Choose a reason for hiding this comment

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

Ok let's merge it this way then 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WARNING: Error loading config file: .dockercfg: $HOME is not defined
3 participants