2018-11-09 09:10:41 -05:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/command"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/golden"
|
2018-11-09 09:10:41 -05:00
|
|
|
)
|
|
|
|
|
2024-05-31 04:31:16 -04:00
|
|
|
func createTestContexts(t *testing.T, cli command.Cli, name ...string) {
|
|
|
|
t.Helper()
|
|
|
|
for _, n := range name {
|
|
|
|
createTestContext(t, cli, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-24 06:59:15 -05:00
|
|
|
func createTestContext(t *testing.T, cli command.Cli, name string) {
|
|
|
|
t.Helper()
|
2018-11-09 09:10:41 -05:00
|
|
|
|
2019-01-21 03:37:20 -05:00
|
|
|
err := RunCreate(cli, &CreateOptions{
|
2022-02-24 06:59:15 -05:00
|
|
|
Name: name,
|
|
|
|
Description: "description of " + name,
|
|
|
|
Docker: map[string]string{keyHost: "https://someswarmserver.example.com"},
|
2018-11-09 09:10:41 -05:00
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestList(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", "unset")
|
2018-11-09 09:10:41 -05:00
|
|
|
cli.SetCurrentContext("current")
|
|
|
|
cli.OutBuffer().Reset()
|
|
|
|
assert.NilError(t, runList(cli, &listOptions{}))
|
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "list.golden")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListQuiet(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")
|
|
|
|
cli.OutBuffer().Reset()
|
|
|
|
assert.NilError(t, runList(cli, &listOptions{quiet: true}))
|
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "quiet-list.golden")
|
|
|
|
}
|
2022-11-04 10:48:30 -04:00
|
|
|
|
|
|
|
func TestListError(t *testing.T) {
|
|
|
|
cli := makeFakeCli(t)
|
|
|
|
cli.SetCurrentContext("nosuchcontext")
|
|
|
|
cli.OutBuffer().Reset()
|
|
|
|
assert.NilError(t, runList(cli, &listOptions{}))
|
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "list-with-error.golden")
|
|
|
|
}
|