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"
|
2022-09-28 16:18:51 -04:00
|
|
|
"github.com/docker/docker/errdefs"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2022-09-28 16:18:51 -04:00
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2018-11-09 09:10:41 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRemove(t *testing.T) {
|
2022-02-25 07:07:16 -05:00
|
|
|
cli := makeFakeCli(t)
|
2024-05-31 04:31:16 -04:00
|
|
|
createTestContexts(t, cli, "current", "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")
|
2022-09-28 16:18:51 -04:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveNotAContext(t *testing.T) {
|
2022-02-25 07:07:16 -05:00
|
|
|
cli := makeFakeCli(t)
|
2024-05-31 04:31:16 -04:00
|
|
|
createTestContexts(t, cli, "current", "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)
|
2024-05-31 04:31:16 -04:00
|
|
|
createTestContexts(t, cli, "current", "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"})
|
2022-09-28 16:18:51 -04:00
|
|
|
assert.ErrorContains(t, err, `context "current" is in use, set -f flag to force remove`)
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
2024-05-31 04:31:16 -04:00
|
|
|
createTestContexts(t, cli, "current", "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)
|
2024-05-31 04:40:04 -04:00
|
|
|
createTestContext(t, cli, "other", nil)
|
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`)
|
|
|
|
}
|