Merge pull request #627 from docker/fix_no_home

Fix context store when no home defined
This commit is contained in:
Guillaume Tardif 2020-09-17 11:40:40 +02:00 committed by GitHub
commit dae9ab7bb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 35 deletions

View File

@ -169,9 +169,9 @@ func main() {
currentContext := determineCurrentContext(opts.Context, configDir)
s, err := store.New(store.WithRoot(configDir))
s, err := store.New(configDir)
if err != nil {
fatal(errors.Wrap(err, "unable to create context store"))
mobycli.Exec()
}
ctype := store.DefaultContextType

View File

@ -62,7 +62,6 @@ const (
const (
dockerEndpointKey = "docker"
configDir = ".docker"
contextsDir = "contexts"
metadataDir = "meta"
metaFile = "meta.json"
@ -111,34 +110,10 @@ type store struct {
root string
}
// Opt is a functional option for the store
type Opt func(*store)
// WithRoot sets a new root to the store
func WithRoot(root string) Opt {
return func(s *store) {
s.root = root
}
}
// New returns a configured context store with $HOME/.docker as root
func New(opts ...Opt) (Store, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
root := filepath.Join(home, configDir)
if err := createDirIfNotExist(root); err != nil {
return nil, err
}
// New returns a configured context store with specified root dir (eg. $HOME/.docker) as root
func New(rootDir string) (Store, error) {
s := &store{
root: root,
}
for _, opt := range opts {
opt(s)
root: rootDir,
}
m := filepath.Join(s.root, contextsDir, metadataDir)

View File

@ -36,7 +36,7 @@ func testStore(t *testing.T) Store {
_ = os.RemoveAll(d)
})
s, err := New(WithRoot(d))
s, err := New(d)
assert.NilError(t, err)
return s

View File

@ -117,7 +117,7 @@ func configureContext(ctx context.Context, currentContext string, method string)
}
}
s, err := store.New(store.WithRoot(configDir))
s, err := store.New(configDir)
if err != nil {
return nil, err
}

View File

@ -293,6 +293,27 @@ func TestLegacy(t *testing.T) {
})
})
t.Run("run without HOME defined", func(t *testing.T) {
cmd := c.NewDockerCmd("ps")
cmd.Env = []string{"PATH=" + c.BinDir}
res := icmd.RunCmd(cmd)
res.Assert(t, icmd.Expected{
ExitCode: 0,
Out: "CONTAINER ID",
})
assert.Equal(t, res.Stderr(), "")
})
t.Run("run without write access to context store", func(t *testing.T) {
cmd := c.NewDockerCmd("ps")
cmd.Env = []string{"PATH=" + c.BinDir, "HOME=/doesnotexist/"}
res := icmd.RunCmd(cmd)
res.Assert(t, icmd.Expected{
ExitCode: 0,
Out: "CONTAINER ID",
})
})
t.Run("host flag", func(t *testing.T) {
stderr := "Cannot connect to the Docker daemon at tcp://localhost:123"
if runtime.GOOS == "windows" {

View File

@ -48,9 +48,7 @@ func NewTestCLI(t *testing.T) *TestCLI {
_ = os.RemoveAll(dir)
})
s, err := store.New(
store.WithRoot(dir),
)
s, err := store.New(dir)
assert.Check(t, cmp.Nil(err))
err = s.Create("example", "example", "", store.ContextMetadata{})
assert.Check(t, cmp.Nil(err))