2018-11-09 09:10:41 -05:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/config"
|
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
|
|
|
"github.com/docker/cli/cli/context/store"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2018-11-09 09:10:41 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRemove(t *testing.T) {
|
2022-02-25 07:07:16 -05:00
|
|
|
cli := makeFakeCli(t)
|
2022-02-24 06:59:15 -05:00
|
|
|
createTestContext(t, cli, "current")
|
|
|
|
createTestContext(t, cli, "other")
|
2019-01-21 03:37:20 -05:00
|
|
|
assert.NilError(t, RunRemove(cli, RemoveOptions{}, []string{"other"}))
|
2019-04-18 09:12:30 -04:00
|
|
|
_, err := cli.ContextStore().GetMetadata("current")
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.NilError(t, err)
|
2019-04-18 09:12:30 -04:00
|
|
|
_, err = cli.ContextStore().GetMetadata("other")
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.Check(t, store.IsErrContextDoesNotExist(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveNotAContext(t *testing.T) {
|
2022-02-25 07:07:16 -05:00
|
|
|
cli := makeFakeCli(t)
|
2022-02-24 06:59:15 -05:00
|
|
|
createTestContext(t, cli, "current")
|
|
|
|
createTestContext(t, cli, "other")
|
2019-01-21 03:37:20 -05:00
|
|
|
err := RunRemove(cli, RemoveOptions{}, []string{"not-a-context"})
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.ErrorContains(t, err, `context "not-a-context" does not exist`)
|
2022-09-28 20:01:09 -04:00
|
|
|
|
|
|
|
err = RunRemove(cli, RemoveOptions{Force: true}, []string{"not-a-context"})
|
|
|
|
assert.NilError(t, err)
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveCurrent(t *testing.T) {
|
2022-02-25 07:07:16 -05:00
|
|
|
cli := makeFakeCli(t)
|
2022-02-24 06:59:15 -05:00
|
|
|
createTestContext(t, cli, "current")
|
|
|
|
createTestContext(t, cli, "other")
|
2018-11-09 09:10:41 -05:00
|
|
|
cli.SetCurrentContext("current")
|
2019-01-21 03:37:20 -05:00
|
|
|
err := RunRemove(cli, RemoveOptions{}, []string{"current"})
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.ErrorContains(t, err, "current: context is in use, set -f flag to force remove")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveCurrentForce(t *testing.T) {
|
2022-02-25 07:07:16 -05:00
|
|
|
configDir := t.TempDir()
|
2018-11-09 09:10:41 -05:00
|
|
|
configFilePath := filepath.Join(configDir, "config.json")
|
|
|
|
testCfg := configfile.New(configFilePath)
|
|
|
|
testCfg.CurrentContext = "current"
|
|
|
|
assert.NilError(t, testCfg.Save())
|
|
|
|
|
2022-02-25 07:07:16 -05:00
|
|
|
cli := makeFakeCli(t, withCliConfig(testCfg))
|
2022-02-24 06:59:15 -05:00
|
|
|
createTestContext(t, cli, "current")
|
|
|
|
createTestContext(t, cli, "other")
|
2018-11-09 09:10:41 -05:00
|
|
|
cli.SetCurrentContext("current")
|
2019-01-21 03:37:20 -05:00
|
|
|
assert.NilError(t, RunRemove(cli, RemoveOptions{Force: true}, []string{"current"}))
|
2018-11-09 09:10:41 -05:00
|
|
|
reloadedConfig, err := config.Load(configDir)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, "", reloadedConfig.CurrentContext)
|
|
|
|
}
|
2019-03-06 09:01:12 -05:00
|
|
|
|
|
|
|
func TestRemoveDefault(t *testing.T) {
|
2022-02-25 07:07:16 -05:00
|
|
|
cli := makeFakeCli(t)
|
2022-02-24 06:59:15 -05:00
|
|
|
createTestContext(t, cli, "other")
|
2019-03-06 09:01:12 -05:00
|
|
|
cli.SetCurrentContext("current")
|
|
|
|
err := RunRemove(cli, RemoveOptions{}, []string{"default"})
|
|
|
|
assert.ErrorContains(t, err, `default: context "default" cannot be removed`)
|
|
|
|
}
|