From ddc44d7abe8ce927e258e9ae14b29adea2468388 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Mon, 15 Jun 2020 15:52:44 +0200 Subject: [PATCH] Do not allow deleting curent context without -f flag --- cli/cmd/context/rm.go | 9 +++++++-- tests/e2e/e2e_test.go | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/cli/cmd/context/rm.go b/cli/cmd/context/rm.go index b6b3a8727..cded45124 100644 --- a/cli/cmd/context/rm.go +++ b/cli/cmd/context/rm.go @@ -29,10 +29,11 @@ package context import ( "context" + "errors" "fmt" "github.com/spf13/cobra" - + apicontext "github.com/docker/api/context" "github.com/docker/api/context/store" "github.com/docker/api/multierror" ) @@ -50,10 +51,14 @@ func removeCommand() *cobra.Command { } func runRemove(ctx context.Context, args []string) error { + currentContext := apicontext.CurrentContext(ctx) s := store.ContextStore(ctx) + var errs *multierror.Error for _, n := range args { - if err := s.Remove(n); err != nil { + if currentContext == n { + errs = multierror.Append(errs, errors.New("cannot delete current context")) + } else if err := s.Remove(n); err != nil { errs = multierror.Append(errs, err) } else { fmt.Println(n) diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index 2344e37b8..be69ee1c4 100644 --- a/tests/e2e/e2e_test.go +++ b/tests/e2e/e2e_test.go @@ -79,6 +79,13 @@ func (s *E2eSuite) TestContextCreateParseErrorDoesNotDelegateToLegacy() { }) } +func (s *E2eSuite) TestCannotRemoveCurrentContext() { + s.NewDockerCommand("context", "create", "test-context", "--from", "default").ExecOrDie() + s.NewDockerCommand("context", "use", "test-context").ExecOrDie() + _, err := s.NewDockerCommand("context", "rm", "test-context").Exec() + Expect(err.Error()).To(ContainSubstring("cannot delete current context")) +} + func (s *E2eSuite) TestClassicLoginWithparameters() { 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"))