From 021bf39d76d694cd9c6bcc73bbb0e57331729bb0 Mon Sep 17 00:00:00 2001 From: Arash Deshmeh Date: Wed, 1 Aug 2018 17:16:28 -0400 Subject: [PATCH] refactor network list unit tests to table-driven style Signed-off-by: Arash Deshmeh --- cli/command/network/list_test.go | 84 +++++++++++-------- .../network/testdata/network-list.golden | 2 +- 2 files changed, 50 insertions(+), 36 deletions(-) diff --git a/cli/command/network/list_test.go b/cli/command/network/list_test.go index 27062e4519..2f76e517d6 100644 --- a/cli/command/network/list_test.go +++ b/cli/command/network/list_test.go @@ -3,7 +3,6 @@ package network import ( "context" "io/ioutil" - "strings" "testing" "github.com/docker/cli/internal/test" @@ -41,40 +40,55 @@ func TestNetworkListErrors(t *testing.T) { } } -func TestNetworkListWithFlags(t *testing.T) { - expectedOpts := types.NetworkListOptions{ - Filters: filters.NewArgs(filters.Arg("image.name", "ubuntu")), +func TestNetworkList(t *testing.T) { + testCases := []struct { + doc string + networkListFunc func(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error) + flags map[string]string + golden string + }{ + { + doc: "network list with flags", + flags: map[string]string{ + "filter": "image.name=ubuntu", + }, + golden: "network-list.golden", + networkListFunc: func(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error) { + expectedOpts := types.NetworkListOptions{ + Filters: filters.NewArgs(filters.Arg("image.name", "ubuntu")), + } + assert.Check(t, is.DeepEqual(expectedOpts, options, cmp.AllowUnexported(filters.Args{}))) + + return []types.NetworkResource{*NetworkResource(NetworkResourceID("123454321"), + NetworkResourceName("network_1"), + NetworkResourceDriver("09.7.01"), + NetworkResourceScope("global"))}, nil + }, + }, + { + doc: "network list sort order", + flags: map[string]string{ + "format": "{{ .Name }}", + }, + golden: "network-list-sort.golden", + networkListFunc: func(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error) { + return []types.NetworkResource{ + *NetworkResource(NetworkResourceName("network-2-foo")), + *NetworkResource(NetworkResourceName("network-1-foo")), + *NetworkResource(NetworkResourceName("network-10-foo"))}, nil + }, + }, } - cli := test.NewFakeCli(&fakeClient{ - networkListFunc: func(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error) { - assert.Check(t, is.DeepEqual(expectedOpts, options, cmp.AllowUnexported(filters.Args{}))) - return []types.NetworkResource{*NetworkResource(NetworkResourceID("123454321"), - NetworkResourceName("network_1"), - NetworkResourceDriver("09.7.01"), - NetworkResourceScope("global"))}, nil - }, - }) - cmd := newListCommand(cli) - - cmd.Flags().Set("filter", "image.name=ubuntu") - assert.NilError(t, cmd.Execute()) - golden.Assert(t, strings.TrimSpace(cli.OutBuffer().String()), "network-list.golden") -} - -func TestNetworkListSort(t *testing.T) { - cli := test.NewFakeCli(&fakeClient{ - networkListFunc: func(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error) { - return []types.NetworkResource{ - *NetworkResource(NetworkResourceName("network-2-foo")), - *NetworkResource(NetworkResourceName("network-1-foo")), - *NetworkResource(NetworkResourceName("network-10-foo")), - }, nil - }, - }) - cmd := newListCommand(cli) - - cmd.Flags().Set("format", "{{ .Name }}") - assert.NilError(t, cmd.Execute()) - golden.Assert(t, cli.OutBuffer().String(), "network-list-sort.golden") + for _, tc := range testCases { + t.Run(tc.doc, func(t *testing.T) { + cli := test.NewFakeCli(&fakeClient{networkListFunc: tc.networkListFunc}) + cmd := newListCommand(cli) + for key, value := range tc.flags { + cmd.Flags().Set(key, value) + } + assert.NilError(t, cmd.Execute()) + golden.Assert(t, cli.OutBuffer().String(), tc.golden) + }) + } } diff --git a/cli/command/network/testdata/network-list.golden b/cli/command/network/testdata/network-list.golden index e776510903..61d982e096 100644 --- a/cli/command/network/testdata/network-list.golden +++ b/cli/command/network/testdata/network-list.golden @@ -1,2 +1,2 @@ NETWORK ID NAME DRIVER SCOPE -123454321 network_1 09.7.01 global \ No newline at end of file +123454321 network_1 09.7.01 global