From fe57e4c1596193068e9815aecb1b16aed13a3298 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Wed, 17 Jun 2020 22:52:49 +0200 Subject: [PATCH] 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 --- cli/mobycli/exec.go | 32 ++++++++++++++++++-------------- tests/e2e/e2e_test.go | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/cli/mobycli/exec.go b/cli/mobycli/exec.go index ce5eed5a..1fd52fa7 100644 --- a/cli/mobycli/exec.go +++ b/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) - } - os.Exit(0) + 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()) + } + 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 } diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index 2e56bf72..0c1412c3 100644 --- a/tests/e2e/e2e_test.go +++ b/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"))