mirror of https://github.com/docker/cli.git
refactor network list unit tests to table-driven style
Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
This commit is contained in:
parent
4f388ffca3
commit
021bf39d76
|
@ -3,7 +3,6 @@ package network
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/docker/cli/internal/test"
|
"github.com/docker/cli/internal/test"
|
||||||
|
@ -41,40 +40,55 @@ func TestNetworkListErrors(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNetworkListWithFlags(t *testing.T) {
|
func TestNetworkList(t *testing.T) {
|
||||||
expectedOpts := types.NetworkListOptions{
|
testCases := []struct {
|
||||||
Filters: filters.NewArgs(filters.Arg("image.name", "ubuntu")),
|
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{
|
for _, tc := range testCases {
|
||||||
networkListFunc: func(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error) {
|
t.Run(tc.doc, func(t *testing.T) {
|
||||||
assert.Check(t, is.DeepEqual(expectedOpts, options, cmp.AllowUnexported(filters.Args{})))
|
cli := test.NewFakeCli(&fakeClient{networkListFunc: tc.networkListFunc})
|
||||||
return []types.NetworkResource{*NetworkResource(NetworkResourceID("123454321"),
|
cmd := newListCommand(cli)
|
||||||
NetworkResourceName("network_1"),
|
for key, value := range tc.flags {
|
||||||
NetworkResourceDriver("09.7.01"),
|
cmd.Flags().Set(key, value)
|
||||||
NetworkResourceScope("global"))}, nil
|
}
|
||||||
},
|
assert.NilError(t, cmd.Execute())
|
||||||
})
|
golden.Assert(t, cli.OutBuffer().String(), tc.golden)
|
||||||
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")
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
NETWORK ID NAME DRIVER SCOPE
|
NETWORK ID NAME DRIVER SCOPE
|
||||||
123454321 network_1 09.7.01 global
|
123454321 network_1 09.7.01 global
|
||||||
|
|
Loading…
Reference in New Issue