cli/command/context: add test-utility to create multiple contexts

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 54291dd47a)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2024-05-31 10:31:16 +02:00
parent b72abbb6f0
commit 1e52a86d42
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
3 changed files with 15 additions and 15 deletions

View File

@ -8,6 +8,13 @@ import (
"gotest.tools/v3/golden" "gotest.tools/v3/golden"
) )
func createTestContexts(t *testing.T, cli command.Cli, name ...string) {
t.Helper()
for _, n := range name {
createTestContext(t, cli, n)
}
}
func createTestContext(t *testing.T, cli command.Cli, name string) { func createTestContext(t *testing.T, cli command.Cli, name string) {
t.Helper() t.Helper()
@ -21,9 +28,7 @@ func createTestContext(t *testing.T, cli command.Cli, name string) {
func TestList(t *testing.T) { func TestList(t *testing.T) {
cli := makeFakeCli(t) cli := makeFakeCli(t)
createTestContext(t, cli, "current") createTestContexts(t, cli, "current", "other", "unset")
createTestContext(t, cli, "other")
createTestContext(t, cli, "unset")
cli.SetCurrentContext("current") cli.SetCurrentContext("current")
cli.OutBuffer().Reset() cli.OutBuffer().Reset()
assert.NilError(t, runList(cli, &listOptions{})) assert.NilError(t, runList(cli, &listOptions{}))
@ -32,8 +37,7 @@ func TestList(t *testing.T) {
func TestListQuiet(t *testing.T) { func TestListQuiet(t *testing.T) {
cli := makeFakeCli(t) cli := makeFakeCli(t)
createTestContext(t, cli, "current") createTestContexts(t, cli, "current", "other")
createTestContext(t, cli, "other")
cli.SetCurrentContext("current") cli.SetCurrentContext("current")
cli.OutBuffer().Reset() cli.OutBuffer().Reset()
assert.NilError(t, runList(cli, &listOptions{quiet: true})) assert.NilError(t, runList(cli, &listOptions{quiet: true}))

View File

@ -13,8 +13,7 @@ import (
func TestRemove(t *testing.T) { func TestRemove(t *testing.T) {
cli := makeFakeCli(t) cli := makeFakeCli(t)
createTestContext(t, cli, "current") createTestContexts(t, cli, "current", "other")
createTestContext(t, cli, "other")
assert.NilError(t, RunRemove(cli, RemoveOptions{}, []string{"other"})) assert.NilError(t, RunRemove(cli, RemoveOptions{}, []string{"other"}))
_, err := cli.ContextStore().GetMetadata("current") _, err := cli.ContextStore().GetMetadata("current")
assert.NilError(t, err) assert.NilError(t, err)
@ -24,8 +23,7 @@ func TestRemove(t *testing.T) {
func TestRemoveNotAContext(t *testing.T) { func TestRemoveNotAContext(t *testing.T) {
cli := makeFakeCli(t) cli := makeFakeCli(t)
createTestContext(t, cli, "current") createTestContexts(t, cli, "current", "other")
createTestContext(t, cli, "other")
err := RunRemove(cli, RemoveOptions{}, []string{"not-a-context"}) err := RunRemove(cli, RemoveOptions{}, []string{"not-a-context"})
assert.ErrorContains(t, err, `context "not-a-context" does not exist`) assert.ErrorContains(t, err, `context "not-a-context" does not exist`)
@ -35,8 +33,7 @@ func TestRemoveNotAContext(t *testing.T) {
func TestRemoveCurrent(t *testing.T) { func TestRemoveCurrent(t *testing.T) {
cli := makeFakeCli(t) cli := makeFakeCli(t)
createTestContext(t, cli, "current") createTestContexts(t, cli, "current", "other")
createTestContext(t, cli, "other")
cli.SetCurrentContext("current") cli.SetCurrentContext("current")
err := RunRemove(cli, RemoveOptions{}, []string{"current"}) err := RunRemove(cli, RemoveOptions{}, []string{"current"})
assert.ErrorContains(t, err, `context "current" is in use, set -f flag to force remove`) assert.ErrorContains(t, err, `context "current" is in use, set -f flag to force remove`)
@ -50,8 +47,7 @@ func TestRemoveCurrentForce(t *testing.T) {
assert.NilError(t, testCfg.Save()) assert.NilError(t, testCfg.Save())
cli := makeFakeCli(t, withCliConfig(testCfg)) cli := makeFakeCli(t, withCliConfig(testCfg))
createTestContext(t, cli, "current") createTestContexts(t, cli, "current", "other")
createTestContext(t, cli, "other")
cli.SetCurrentContext("current") cli.SetCurrentContext("current")
assert.NilError(t, RunRemove(cli, RemoveOptions{Force: true}, []string{"current"})) assert.NilError(t, RunRemove(cli, RemoveOptions{Force: true}, []string{"current"}))
reloadedConfig, err := config.Load(configDir) reloadedConfig, err := config.Load(configDir)

View File

@ -6,7 +6,7 @@ import (
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/context/docker" "github.com/docker/cli/cli/context/docker"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp" is "gotest.tools/v3/assert/cmp"
) )
func TestUpdateDescriptionOnly(t *testing.T) { func TestUpdateDescriptionOnly(t *testing.T) {
@ -46,7 +46,7 @@ func TestUpdateDockerOnly(t *testing.T) {
dc, err := command.GetDockerContext(c) dc, err := command.GetDockerContext(c)
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, dc.Description, "description of test") assert.Equal(t, dc.Description, "description of test")
assert.Check(t, cmp.Contains(c.Endpoints, docker.DockerEndpoint)) assert.Check(t, is.Contains(c.Endpoints, docker.DockerEndpoint))
assert.Equal(t, c.Endpoints[docker.DockerEndpoint].(docker.EndpointMeta).Host, "tcp://some-host") assert.Equal(t, c.Endpoints[docker.DockerEndpoint].(docker.EndpointMeta).Host, "tcp://some-host")
} }