瀏覽代碼

Allow running commands with HOME=“”.
Do not try to create ~/.docker before using CONFIG_DIR. HOME=“” will result in trying to use a relative .docker folder as default config folder, and if we cannot create the context store for any reason, try delegate to Moby CLI.

Signed-off-by: Guillaume Tardif <[email protected]>

Guillaume Tardif 5 年之前
父節點
當前提交
9c6aead797
共有 6 個文件被更改,包括 29 次插入35 次删除
  1. 2 2
      cli/main.go
  2. 3 28
      context/store/store.go
  3. 1 1
      context/store/store_test.go
  4. 1 1
      server/interceptor.go
  5. 21 0
      tests/e2e/e2e_test.go
  6. 1 3
      tests/framework/unit.go

+ 2 - 2
cli/main.go

@@ -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

+ 3 - 28
context/store/store.go

@@ -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)

+ 1 - 1
context/store/store_test.go

@@ -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

+ 1 - 1
server/interceptor.go

@@ -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
 	}

+ 21 - 0
tests/e2e/e2e_test.go

@@ -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" {

+ 1 - 3
tests/framework/unit.go

@@ -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))