From 29cd8591ee94319a1cf436607ed91025f846b79c Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 31 May 2024 10:22:39 +0200 Subject: [PATCH] cli/command/context: list: add test for JSON output Signed-off-by: Sebastiaan van Stijn (cherry picked from commit 40a1da8b426807e016ff08b8252f908d60c8f566) Signed-off-by: Sebastiaan van Stijn --- cli/command/context/list_test.go | 28 +++++++++++++++++++ .../context/testdata/list-json-name.golden | 5 ++++ cli/command/context/testdata/list-json.golden | 5 ++++ 3 files changed, 38 insertions(+) create mode 100644 cli/command/context/testdata/list-json-name.golden create mode 100644 cli/command/context/testdata/list-json.golden diff --git a/cli/command/context/list_test.go b/cli/command/context/list_test.go index 9370bf6d2d..ea7dd1e7c9 100644 --- a/cli/command/context/list_test.go +++ b/cli/command/context/list_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/docker/cli/cli/command" + "github.com/docker/cli/cli/command/formatter" "gotest.tools/v3/assert" "gotest.tools/v3/golden" ) @@ -37,6 +38,33 @@ func TestList(t *testing.T) { golden.Assert(t, cli.OutBuffer().String(), "list.golden") } +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") + }) +} + func TestListQuiet(t *testing.T) { cli := makeFakeCli(t) createTestContexts(t, cli, "current", "other") diff --git a/cli/command/context/testdata/list-json-name.golden b/cli/command/context/testdata/list-json-name.golden new file mode 100644 index 0000000000..fbf2dd6c33 --- /dev/null +++ b/cli/command/context/testdata/list-json-name.golden @@ -0,0 +1,5 @@ +"context1" +"context2" +"context3" +"current" +"default" diff --git a/cli/command/context/testdata/list-json.golden b/cli/command/context/testdata/list-json.golden new file mode 100644 index 0000000000..6be551b6de --- /dev/null +++ b/cli/command/context/testdata/list-json.golden @@ -0,0 +1,5 @@ +{"Current":false,"Description":"description of context1","DockerEndpoint":"https://someswarmserver.example.com","Error":"","Name":"context1"} +{"Current":false,"Description":"description of context2","DockerEndpoint":"https://someswarmserver.example.com","Error":"","Name":"context2"} +{"Current":false,"Description":"description of context3","DockerEndpoint":"https://someswarmserver.example.com","Error":"","Name":"context3"} +{"Current":true,"Description":"description of current","DockerEndpoint":"https://someswarmserver.example.com","Error":"","Name":"current"} +{"Current":false,"Description":"Current DOCKER_HOST based configuration","DockerEndpoint":"unix:///var/run/docker.sock","Error":"","Name":"default"}