2017-08-01 05:06:47 -04:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
2017-12-21 16:27:57 -05:00
|
|
|
"testing"
|
2017-08-01 05:06:47 -04:00
|
|
|
|
|
|
|
"github.com/docker/cli/internal/test"
|
|
|
|
. "github.com/docker/cli/internal/test/builders"
|
|
|
|
"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"
|
2018-03-05 18:53:52 -05:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2017-08-01 05:06:47 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/golden"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2017-12-21 16:27:57 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-08-01 05:06:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkListWithFlags(t *testing.T) {
|
|
|
|
expectedOpts := types.NetworkListOptions{
|
2017-12-21 16:27:57 -05:00
|
|
|
Filters: filters.NewArgs(filters.Arg("image.name", "ubuntu")),
|
2017-08-01 05:06:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
networkListFunc: func(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error) {
|
2017-12-21 16:27:57 -05:00
|
|
|
assert.Check(t, is.DeepEqual(expectedOpts, options, cmp.AllowUnexported(filters.Args{})))
|
2017-08-01 05:06:47 -04:00
|
|
|
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")
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-01 05:06:47 -04:00
|
|
|
golden.Assert(t, strings.TrimSpace(cli.OutBuffer().String()), "network-list.golden")
|
|
|
|
}
|