github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/context/remove_test.go (about) 1 package context 2 3 import ( 4 "path/filepath" 5 "testing" 6 7 "github.com/docker/cli/cli/config" 8 "github.com/docker/cli/cli/config/configfile" 9 "github.com/docker/docker/errdefs" 10 "gotest.tools/v3/assert" 11 is "gotest.tools/v3/assert/cmp" 12 ) 13 14 func TestRemove(t *testing.T) { 15 cli := makeFakeCli(t) 16 createTestContext(t, cli, "current") 17 createTestContext(t, cli, "other") 18 assert.NilError(t, RunRemove(cli, RemoveOptions{}, []string{"other"})) 19 _, err := cli.ContextStore().GetMetadata("current") 20 assert.NilError(t, err) 21 _, err = cli.ContextStore().GetMetadata("other") 22 assert.Check(t, is.ErrorType(err, errdefs.IsNotFound)) 23 } 24 25 func TestRemoveNotAContext(t *testing.T) { 26 cli := makeFakeCli(t) 27 createTestContext(t, cli, "current") 28 createTestContext(t, cli, "other") 29 err := RunRemove(cli, RemoveOptions{}, []string{"not-a-context"}) 30 assert.ErrorContains(t, err, `context "not-a-context" does not exist`) 31 32 err = RunRemove(cli, RemoveOptions{Force: true}, []string{"not-a-context"}) 33 assert.NilError(t, err) 34 } 35 36 func TestRemoveCurrent(t *testing.T) { 37 cli := makeFakeCli(t) 38 createTestContext(t, cli, "current") 39 createTestContext(t, cli, "other") 40 cli.SetCurrentContext("current") 41 err := RunRemove(cli, RemoveOptions{}, []string{"current"}) 42 assert.ErrorContains(t, err, `context "current" is in use, set -f flag to force remove`) 43 } 44 45 func TestRemoveCurrentForce(t *testing.T) { 46 configDir := t.TempDir() 47 configFilePath := filepath.Join(configDir, "config.json") 48 testCfg := configfile.New(configFilePath) 49 testCfg.CurrentContext = "current" 50 assert.NilError(t, testCfg.Save()) 51 52 cli := makeFakeCli(t, withCliConfig(testCfg)) 53 createTestContext(t, cli, "current") 54 createTestContext(t, cli, "other") 55 cli.SetCurrentContext("current") 56 assert.NilError(t, RunRemove(cli, RemoveOptions{Force: true}, []string{"current"})) 57 reloadedConfig, err := config.Load(configDir) 58 assert.NilError(t, err) 59 assert.Equal(t, "", reloadedConfig.CurrentContext) 60 } 61 62 func TestRemoveDefault(t *testing.T) { 63 cli := makeFakeCli(t) 64 createTestContext(t, cli, "other") 65 cli.SetCurrentContext("current") 66 err := RunRemove(cli, RemoveOptions{}, []string{"default"}) 67 assert.ErrorContains(t, err, `default: context "default" cannot be removed`) 68 }