Browse Source

Fix a bug where calling login, context inspect did not do anything if not on default context. These command will shell out to Moby cli regardless of current context

Guillaume Tardif 5 years ago
parent
commit
fe57e4c159
2 changed files with 32 additions and 13 deletions
  1. 17 13
      cli/mobycli/exec.go
  2. 15 0
      tests/e2e/e2e_test.go

+ 17 - 13
cli/mobycli/exec.go

@@ -16,7 +16,7 @@ import (
 // ComDockerCli name of the classic cli binary
 const ComDockerCli = "com.docker.cli"
 
-// Exec delegates to com.docker.cli
+// Exec delegates to com.docker.cli if on moby context
 func Exec(ctx context.Context) {
 	currentContext := apicontext.CurrentContext(ctx)
 	s := store.ContextStore(ctx)
@@ -25,24 +25,28 @@ func Exec(ctx context.Context) {
 	// Only run original docker command if the current context is not
 	// ours.
 	if err != nil {
-		cmd := exec.CommandContext(ctx, ComDockerCli, os.Args[1:]...)
-		cmd.Stdin = os.Stdin
-		cmd.Stdout = os.Stdout
-		cmd.Stderr = os.Stderr
-		if err := cmd.Run(); err != nil {
-			if exiterr, ok := err.(*exec.ExitError); ok {
-				os.Exit(exiterr.ExitCode())
-			}
-			fmt.Fprintln(os.Stderr, err)
-			os.Exit(1)
+		shellOut(ctx)
+	}
+}
+
+func shellOut(ctx context.Context) {
+	cmd := exec.CommandContext(ctx, ComDockerCli, os.Args[1:]...)
+	cmd.Stdin = os.Stdin
+	cmd.Stdout = os.Stdout
+	cmd.Stderr = os.Stderr
+	if err := cmd.Run(); err != nil {
+		if exiterr, ok := err.(*exec.ExitError); ok {
+			os.Exit(exiterr.ExitCode())
 		}
-		os.Exit(0)
+		fmt.Fprintln(os.Stderr, err)
+		os.Exit(1)
 	}
+	os.Exit(0)
 }
 
 // ExecCmd delegates the cli command to com.docker.cli. The error is never returned (process will exit with docker classic exit code), the return type is to make it easier to use with cobra commands
 func ExecCmd(command *cobra.Command) error {
-	Exec(command.Context())
+	shellOut(command.Context())
 	return nil
 }
 

+ 15 - 0
tests/e2e/e2e_test.go

@@ -84,6 +84,13 @@ func (s *E2eSuite) TestInspectContextNoArgs() {
 	Expect(output).To(ContainSubstring(`"Name": "default"`))
 }
 
+func (s *E2eSuite) TestInspectContextRegardlessCurrentContext() {
+	s.NewDockerCommand("context", "create", "local", "localCtx").ExecOrDie()
+	s.NewDockerCommand("context", "use", "localCtx").ExecOrDie()
+	output := s.NewDockerCommand("context", "inspect").ExecOrDie()
+	Expect(output).To(ContainSubstring(`"Name": "localCtx"`))
+}
+
 func (s *E2eSuite) TestContextCreateParseErrorDoesNotDelegateToLegacy() {
 	It("should dispay new cli error when parsing context create flags", func() {
 		_, err := s.NewDockerCommand("context", "create", "aci", "--subscription-id", "titi").Exec()
@@ -113,6 +120,14 @@ func (s *E2eSuite) TestClassicLoginWithparameters() {
 	Expect(err).NotTo(BeNil())
 }
 
+func (s *E2eSuite) TestClassicLoginRegardlessCurrentContext() {
+	s.NewDockerCommand("context", "create", "local", "localCtx").ExecOrDie()
+	s.NewDockerCommand("context", "use", "localCtx").ExecOrDie()
+	output, err := s.NewDockerCommand("login", "-u", "nouser", "-p", "wrongpasword").Exec()
+	Expect(output).To(ContainSubstring("Get https://registry-1.docker.io/v2/: unauthorized: incorrect username or password"))
+	Expect(err).NotTo(BeNil())
+}
+
 func (s *E2eSuite) TestClassicLogin() {
 	output, err := s.NewDockerCommand("login", "someregistry.docker.io").Exec()
 	Expect(output).To(ContainSubstring("Cannot perform an interactive login from a non TTY device"))