2017-08-01 05:06:47 -04:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2022-02-25 07:12:19 -05:00
|
|
|
"io"
|
2017-12-21 16:27:57 -05:00
|
|
|
"testing"
|
2017-08-01 05:06:47 -04:00
|
|
|
|
|
|
|
"github.com/docker/cli/internal/test"
|
2023-10-23 08:51:01 -04:00
|
|
|
"github.com/docker/cli/internal/test/builders"
|
2017-08-01 05:06:47 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
2017-12-21 16:27:57 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2017-08-01 05:06:47 -04:00
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/golden"
|
2017-08-01 05:06:47 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNetworkListErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
networkListFunc func(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error)
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
networkListFunc: func(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error) {
|
|
|
|
return []types.NetworkResource{}, errors.Errorf("error creating network")
|
|
|
|
},
|
|
|
|
expectedError: "error creating network",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newListCommand(
|
|
|
|
test.NewFakeCli(&fakeClient{
|
|
|
|
networkListFunc: tc.networkListFunc,
|
|
|
|
}),
|
|
|
|
)
|
2022-02-25 07:12:19 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2017-12-21 16:27:57 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-08-01 05:06:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 17:16:28 -04:00
|
|
|
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{})))
|
2017-08-01 05:06:47 -04:00
|
|
|
|
2023-10-23 08:51:01 -04:00
|
|
|
return []types.NetworkResource{*builders.NetworkResource(builders.NetworkResourceID("123454321"),
|
|
|
|
builders.NetworkResourceName("network_1"),
|
|
|
|
builders.NetworkResourceDriver("09.7.01"),
|
|
|
|
builders.NetworkResourceScope("global"))}, nil
|
2018-08-01 17:16:28 -04:00
|
|
|
},
|
2017-08-01 05:06:47 -04:00
|
|
|
},
|
2018-08-01 17:16:28 -04:00
|
|
|
{
|
|
|
|
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{
|
2023-10-23 08:51:01 -04:00
|
|
|
*builders.NetworkResource(builders.NetworkResourceName("network-2-foo")),
|
|
|
|
*builders.NetworkResource(builders.NetworkResourceName("network-1-foo")),
|
|
|
|
*builders.NetworkResource(builders.NetworkResourceName("network-10-foo")),
|
2022-09-29 11:21:51 -04:00
|
|
|
}, nil
|
2018-08-01 17:16:28 -04:00
|
|
|
},
|
2018-08-01 14:40:41 -04:00
|
|
|
},
|
2018-08-01 17:16:28 -04:00
|
|
|
}
|
2018-08-01 14:40:41 -04:00
|
|
|
|
2018-08-01 17:16:28 -04:00
|
|
|
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 {
|
2023-10-23 08:51:01 -04:00
|
|
|
assert.Check(t, cmd.Flags().Set(key, value))
|
2018-08-01 17:16:28 -04:00
|
|
|
}
|
|
|
|
assert.NilError(t, cmd.Execute())
|
|
|
|
golden.Assert(t, cli.OutBuffer().String(), tc.golden)
|
|
|
|
})
|
|
|
|
}
|
2018-08-01 14:40:41 -04:00
|
|
|
}
|