Do not allow deleting curent context without -f flag

This commit is contained in:
Guillaume Tardif 2020-06-15 15:52:44 +02:00
parent 3566c720e3
commit ddc44d7abe
2 changed files with 14 additions and 2 deletions

View File

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

View File

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