2018-11-09 09:10:41 -05:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/command"
|
2024-05-31 04:22:39 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
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 {
|
2024-05-31 04:40:04 -04:00
|
|
|
createTestContext(t, cli, n, nil)
|
2024-05-31 04:31:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-31 04:40:04 -04:00
|
|
|
func createTestContext(t *testing.T, cli command.Cli, name string, metaData map[string]any) {
|
2022-02-24 06:59:15 -05:00
|
|
|
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"},
|
2024-05-31 04:40:04 -04:00
|
|
|
|
|
|
|
metaData: metaData,
|
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")
|
|
|
|
}
|
|
|
|
|
2024-05-31 04:22:39 -04:00
|
|
|
func TestListJSON(t *testing.T) {
|
|
|
|
cli := makeFakeCli(t)
|
|
|
|
createTestContext(t, cli, "current", nil)
|
|
|
|
createTestContext(t, cli, "context1", map[string]any{"Type": "aci"})
|
|
|
|
createTestContext(t, cli, "context2", map[string]any{"Type": "ecs"})
|
|
|
|
createTestContext(t, cli, "context3", map[string]any{"Type": "moby"})
|
|
|
|
cli.SetCurrentContext("current")
|
|
|
|
|
|
|
|
t.Run("format={{json .}}", func(t *testing.T) {
|
|
|
|
cli.OutBuffer().Reset()
|
|
|
|
assert.NilError(t, runList(cli, &listOptions{format: formatter.JSONFormat}))
|
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "list-json.golden")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("format=json", func(t *testing.T) {
|
|
|
|
cli.OutBuffer().Reset()
|
|
|
|
assert.NilError(t, runList(cli, &listOptions{format: formatter.JSONFormatKey}))
|
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "list-json.golden")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("format={{ json .Name }}", func(t *testing.T) {
|
|
|
|
cli.OutBuffer().Reset()
|
|
|
|
assert.NilError(t, runList(cli, &listOptions{format: `{{ json .Name }}`}))
|
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "list-json-name.golden")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
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")
|
|
|
|
}
|