Update config and checkpoint commands to use gotestyourself/golden

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-08-16 11:54:23 -04:00
parent 882992c6fc
commit f9007ad7db
11 changed files with 54 additions and 88 deletions

View File

@ -1,14 +1,13 @@
package checkpoint package checkpoint
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"testing" "testing"
"github.com/docker/cli/cli/internal/test" "github.com/docker/cli/cli/internal/test"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/testutil" "github.com/docker/docker/pkg/testutil"
"github.com/docker/docker/pkg/testutil/golden" "github.com/gotestyourself/gotestyourself/golden"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -37,9 +36,9 @@ func TestCheckpointListErrors(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
cli := test.NewFakeCliWithOutput(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
checkpointListFunc: tc.checkpointListFunc, checkpointListFunc: tc.checkpointListFunc,
}, &bytes.Buffer{}) })
cmd := newListCommand(cli) cmd := newListCommand(cli)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
@ -49,8 +48,7 @@ func TestCheckpointListErrors(t *testing.T) {
func TestCheckpointListWithOptions(t *testing.T) { func TestCheckpointListWithOptions(t *testing.T) {
var containerID, checkpointDir string var containerID, checkpointDir string
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cli := test.NewFakeCliWithOutput(&fakeClient{
checkpointListFunc: func(container string, options types.CheckpointListOptions) ([]types.Checkpoint, error) { checkpointListFunc: func(container string, options types.CheckpointListOptions) ([]types.Checkpoint, error) {
containerID = container containerID = container
checkpointDir = options.CheckpointDir checkpointDir = options.CheckpointDir
@ -58,14 +56,12 @@ func TestCheckpointListWithOptions(t *testing.T) {
{Name: "checkpoint-foo"}, {Name: "checkpoint-foo"},
}, nil }, nil
}, },
}, buf) })
cmd := newListCommand(cli) cmd := newListCommand(cli)
cmd.SetArgs([]string{"container-foo"}) cmd.SetArgs([]string{"container-foo"})
cmd.Flags().Set("checkpoint-dir", "/dir/foo") cmd.Flags().Set("checkpoint-dir", "/dir/foo")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
assert.Equal(t, "container-foo", containerID) assert.Equal(t, "container-foo", containerID)
assert.Equal(t, "/dir/foo", checkpointDir) assert.Equal(t, "/dir/foo", checkpointDir)
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), "checkpoint-list-with-options.golden")
expected := golden.Get(t, []byte(actual), "checkpoint-list-with-options.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }

View File

@ -1,7 +1,6 @@
package checkpoint package checkpoint
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"testing" "testing"
@ -36,9 +35,9 @@ func TestCheckpointRemoveErrors(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
cli := test.NewFakeCliWithOutput(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
checkpointDeleteFunc: tc.checkpointDeleteFunc, checkpointDeleteFunc: tc.checkpointDeleteFunc,
}, &bytes.Buffer{}) })
cmd := newRemoveCommand(cli) cmd := newRemoveCommand(cli)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
@ -48,14 +47,14 @@ func TestCheckpointRemoveErrors(t *testing.T) {
func TestCheckpointRemoveWithOptions(t *testing.T) { func TestCheckpointRemoveWithOptions(t *testing.T) {
var containerID, checkpointID, checkpointDir string var containerID, checkpointID, checkpointDir string
cli := test.NewFakeCliWithOutput(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
checkpointDeleteFunc: func(container string, options types.CheckpointDeleteOptions) error { checkpointDeleteFunc: func(container string, options types.CheckpointDeleteOptions) error {
containerID = container containerID = container
checkpointID = options.CheckpointID checkpointID = options.CheckpointID
checkpointDir = options.CheckpointDir checkpointDir = options.CheckpointDir
return nil return nil
}, },
}, &bytes.Buffer{}) })
cmd := newRemoveCommand(cli) cmd := newRemoveCommand(cli)
cmd.SetArgs([]string{"container-foo", "checkpoint-bar"}) cmd.SetArgs([]string{"container-foo", "checkpoint-bar"})
cmd.Flags().Set("checkpoint-dir", "/dir/foo") cmd.Flags().Set("checkpoint-dir", "/dir/foo")

View File

@ -11,7 +11,7 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/pkg/testutil" "github.com/docker/docker/pkg/testutil"
"github.com/docker/docker/pkg/testutil/golden" "github.com/gotestyourself/gotestyourself/golden"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -71,8 +71,7 @@ func TestConfigCreateWithName(t *testing.T) {
cmd := newConfigCreateCommand(cli) cmd := newConfigCreateCommand(cli)
cmd.SetArgs([]string{name, filepath.Join("testdata", configDataFile)}) cmd.SetArgs([]string{name, filepath.Join("testdata", configDataFile)})
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
expected := golden.Get(t, actual, configDataFile) golden.Assert(t, string(actual), configDataFile)
assert.Equal(t, string(expected), string(actual))
assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String())) assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
} }

View File

@ -1,7 +1,6 @@
package config package config
import ( import (
"bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"testing" "testing"
@ -13,7 +12,7 @@ import (
// Import builders to get the builder function as package function // Import builders to get the builder function as package function
. "github.com/docker/cli/cli/internal/test/builders" . "github.com/docker/cli/cli/internal/test/builders"
"github.com/docker/docker/pkg/testutil" "github.com/docker/docker/pkg/testutil"
"github.com/docker/docker/pkg/testutil/golden" "github.com/gotestyourself/gotestyourself/golden"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -53,11 +52,10 @@ func TestConfigInspectErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newConfigInspectCommand( cmd := newConfigInspectCommand(
test.NewFakeCliWithOutput(&fakeClient{ test.NewFakeCli(&fakeClient{
configInspectFunc: tc.configInspectFunc, configInspectFunc: tc.configInspectFunc,
}, buf), }),
) )
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
for key, value := range tc.flags { for key, value := range tc.flags {
@ -95,17 +93,11 @@ func TestConfigInspectWithoutFormat(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{configInspectFunc: tc.configInspectFunc})
cmd := newConfigInspectCommand( cmd := newConfigInspectCommand(cli)
test.NewFakeCliWithOutput(&fakeClient{
configInspectFunc: tc.configInspectFunc,
}, buf),
)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("config-inspect-without-format.%s.golden", tc.name))
expected := golden.Get(t, []byte(actual), fmt.Sprintf("config-inspect-without-format.%s.golden", tc.name))
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
} }
@ -135,18 +127,14 @@ func TestConfigInspectWithFormat(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cmd := newConfigInspectCommand( configInspectFunc: tc.configInspectFunc,
test.NewFakeCliWithOutput(&fakeClient{ })
configInspectFunc: tc.configInspectFunc, cmd := newConfigInspectCommand(cli)
}, buf),
)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.Flags().Set("format", tc.format) cmd.Flags().Set("format", tc.format)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("config-inspect-with-format.%s.golden", tc.name))
expected := golden.Get(t, []byte(actual), fmt.Sprintf("config-inspect-with-format.%s.golden", tc.name))
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
} }
@ -172,16 +160,14 @@ func TestConfigInspectPretty(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cmd := newConfigInspectCommand( configInspectFunc: tc.configInspectFunc,
test.NewFakeCliWithOutput(&fakeClient{ })
configInspectFunc: tc.configInspectFunc, cmd := newConfigInspectCommand(cli)
}, buf))
cmd.SetArgs([]string{"configID"}) cmd.SetArgs([]string{"configID"})
cmd.Flags().Set("pretty", "true") cmd.Flags().Set("pretty", "true")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("config-inspect-pretty.%s.golden", tc.name))
expected := golden.Get(t, []byte(actual), fmt.Sprintf("config-inspect-pretty.%s.golden", tc.name))
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
} }

View File

@ -13,7 +13,7 @@ import (
// Import builders to get the builder function as package function // Import builders to get the builder function as package function
. "github.com/docker/cli/cli/internal/test/builders" . "github.com/docker/cli/cli/internal/test/builders"
"github.com/docker/docker/pkg/testutil" "github.com/docker/docker/pkg/testutil"
"github.com/docker/docker/pkg/testutil/golden" "github.com/gotestyourself/gotestyourself/golden"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -68,9 +68,7 @@ func TestConfigList(t *testing.T) {
cmd := newConfigListCommand(cli) cmd := newConfigListCommand(cli)
cmd.SetOutput(cli.OutBuffer()) cmd.SetOutput(cli.OutBuffer())
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := cli.OutBuffer().String() golden.Assert(t, cli.OutBuffer().String(), "config-list.golden")
expected := golden.Get(t, []byte(actual), "config-list.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestConfigListWithQuietOption(t *testing.T) { func TestConfigListWithQuietOption(t *testing.T) {
@ -87,9 +85,7 @@ func TestConfigListWithQuietOption(t *testing.T) {
cmd := newConfigListCommand(cli) cmd := newConfigListCommand(cli)
cmd.Flags().Set("quiet", "true") cmd.Flags().Set("quiet", "true")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := cli.OutBuffer().String() golden.Assert(t, cli.OutBuffer().String(), "config-list-with-quiet-option.golden")
expected := golden.Get(t, []byte(actual), "config-list-with-quiet-option.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestConfigListWithConfigFormat(t *testing.T) { func TestConfigListWithConfigFormat(t *testing.T) {
@ -108,9 +104,7 @@ func TestConfigListWithConfigFormat(t *testing.T) {
}) })
cmd := newConfigListCommand(cli) cmd := newConfigListCommand(cli)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := cli.OutBuffer().String() golden.Assert(t, cli.OutBuffer().String(), "config-list-with-config-format.golden")
expected := golden.Get(t, []byte(actual), "config-list-with-config-format.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestConfigListWithFormat(t *testing.T) { func TestConfigListWithFormat(t *testing.T) {
@ -127,9 +121,7 @@ func TestConfigListWithFormat(t *testing.T) {
cmd := newConfigListCommand(cli) cmd := newConfigListCommand(cli)
cmd.Flags().Set("format", "{{ .Name }} {{ .Labels }}") cmd.Flags().Set("format", "{{ .Name }} {{ .Labels }}")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := cli.OutBuffer().String() golden.Assert(t, cli.OutBuffer().String(), "config-list-with-format.golden")
expected := golden.Get(t, []byte(actual), "config-list-with-format.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestConfigListWithFilter(t *testing.T) { func TestConfigListWithFilter(t *testing.T) {
@ -157,7 +149,5 @@ func TestConfigListWithFilter(t *testing.T) {
cmd.Flags().Set("filter", "name=foo") cmd.Flags().Set("filter", "name=foo")
cmd.Flags().Set("filter", "label=lbl1=Label-bar") cmd.Flags().Set("filter", "label=lbl1=Label-bar")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := cli.OutBuffer().String() golden.Assert(t, cli.OutBuffer().String(), "config-list-with-filter.golden")
expected := golden.Get(t, []byte(actual), "config-list-with-filter.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }

View File

@ -1,7 +1,6 @@
package config package config
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"strings" "strings"
"testing" "testing"
@ -31,11 +30,10 @@ func TestConfigRemoveErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newConfigRemoveCommand( cmd := newConfigRemoveCommand(
test.NewFakeCliWithOutput(&fakeClient{ test.NewFakeCli(&fakeClient{
configRemoveFunc: tc.configRemoveFunc, configRemoveFunc: tc.configRemoveFunc,
}, buf), }),
) )
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
@ -45,27 +43,25 @@ func TestConfigRemoveErrors(t *testing.T) {
func TestConfigRemoveWithName(t *testing.T) { func TestConfigRemoveWithName(t *testing.T) {
names := []string{"foo", "bar"} names := []string{"foo", "bar"}
buf := new(bytes.Buffer)
var removedConfigs []string var removedConfigs []string
cli := test.NewFakeCliWithOutput(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
configRemoveFunc: func(name string) error { configRemoveFunc: func(name string) error {
removedConfigs = append(removedConfigs, name) removedConfigs = append(removedConfigs, name)
return nil return nil
}, },
}, buf) })
cmd := newConfigRemoveCommand(cli) cmd := newConfigRemoveCommand(cli)
cmd.SetArgs(names) cmd.SetArgs(names)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
assert.Equal(t, names, strings.Split(strings.TrimSpace(buf.String()), "\n")) assert.Equal(t, names, strings.Split(strings.TrimSpace(cli.OutBuffer().String()), "\n"))
assert.Equal(t, names, removedConfigs) assert.Equal(t, names, removedConfigs)
} }
func TestConfigRemoveContinueAfterError(t *testing.T) { func TestConfigRemoveContinueAfterError(t *testing.T) {
names := []string{"foo", "bar"} names := []string{"foo", "bar"}
buf := new(bytes.Buffer)
var removedConfigs []string var removedConfigs []string
cli := test.NewFakeCliWithOutput(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
configRemoveFunc: func(name string) error { configRemoveFunc: func(name string) error {
removedConfigs = append(removedConfigs, name) removedConfigs = append(removedConfigs, name)
if name == "foo" { if name == "foo" {
@ -73,7 +69,7 @@ func TestConfigRemoveContinueAfterError(t *testing.T) {
} }
return nil return nil
}, },
}, buf) })
cmd := newConfigRemoveCommand(cli) cmd := newConfigRemoveCommand(cli)
cmd.SetArgs(names) cmd.SetArgs(names)

View File

@ -1,8 +1,8 @@
ID: configID ID: configID
Name: configName Name: configName
Labels: Labels:
- lbl1=value1 - lbl1=value1
Created at: 0001-01-01 00:00:00+0000 utc Created at: 0001-01-01 00:00:00 +0000 utc
Updated at: 0001-01-01 00:00:00+0000 utc Updated at: 0001-01-01 00:00:00 +0000 utc
Data: Data:
payload here payload here

View File

@ -1,7 +1,7 @@
[ [
{ {
"ID": "ID-foo", "ID": "ID-foo",
"Version": {}, "Version": {},
"CreatedAt": "0001-01-01T00:00:00Z", "CreatedAt": "0001-01-01T00:00:00Z",
"UpdatedAt": "0001-01-01T00:00:00Z", "UpdatedAt": "0001-01-01T00:00:00Z",
"Spec": { "Spec": {
@ -13,7 +13,7 @@
}, },
{ {
"ID": "ID-bar", "ID": "ID-bar",
"Version": {}, "Version": {},
"CreatedAt": "0001-01-01T00:00:00Z", "CreatedAt": "0001-01-01T00:00:00Z",
"UpdatedAt": "0001-01-01T00:00:00Z", "UpdatedAt": "0001-01-01T00:00:00Z",
"Spec": { "Spec": {

View File

@ -1,9 +1,9 @@
[ [
{ {
"ID": "ID-foo", "ID": "ID-foo",
"Version": {}, "Version": {},
"CreatedAt": "0001-01-01T00:00:00Z", "CreatedAt": "0001-01-01T00:00:00Z",
"UpdatedAt": "0001-01-01T00:00:00Z", "UpdatedAt": "0001-01-01T00:00:00Z",
"Spec": { "Spec": {
"Name": "foo", "Name": "foo",
"Labels": null "Labels": null