mirror of https://github.com/docker/cli.git
adding network list test
Signed-off-by: khaled souf <khaled.souf@gmail.com>
This commit is contained in:
parent
7e52344cd2
commit
15d92a9e03
|
@ -12,6 +12,7 @@ type fakeClient struct {
|
|||
networkCreateFunc func(ctx context.Context, name string, options types.NetworkCreate) (types.NetworkCreateResponse, error)
|
||||
networkConnectFunc func(ctx context.Context, networkID, container string, config *network.EndpointSettings) error
|
||||
networkDisconnectFunc func(ctx context.Context, networkID, container string, force bool) error
|
||||
networkListFunc func(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error)
|
||||
}
|
||||
|
||||
func (c *fakeClient) NetworkCreate(ctx context.Context, name string, options types.NetworkCreate) (types.NetworkCreateResponse, error) {
|
||||
|
@ -34,3 +35,10 @@ func (c *fakeClient) NetworkDisconnect(ctx context.Context, networkID, container
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *fakeClient) NetworkList(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error) {
|
||||
if c.networkListFunc != nil {
|
||||
return c.networkListFunc(ctx, options)
|
||||
}
|
||||
return []types.NetworkResource{}, nil
|
||||
}
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
package network
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"io/ioutil"
|
||||
|
||||
"strings"
|
||||
|
||||
"github.com/docker/cli/internal/test"
|
||||
. "github.com/docker/cli/internal/test/builders"
|
||||
"github.com/docker/cli/internal/test/testutil"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/gotestyourself/gotestyourself/golden"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"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)
|
||||
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestNetworkListWithFlags(t *testing.T) {
|
||||
|
||||
filterArgs := filters.NewArgs()
|
||||
filterArgs.Add("image.name", "ubuntu")
|
||||
|
||||
expectedOpts := types.NetworkListOptions{
|
||||
Filters: filterArgs,
|
||||
}
|
||||
|
||||
cli := test.NewFakeCli(&fakeClient{
|
||||
networkListFunc: func(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error) {
|
||||
assert.Equal(t, expectedOpts, options, "not expected options error")
|
||||
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.NoError(t, cmd.Execute())
|
||||
golden.Assert(t, strings.TrimSpace(cli.OutBuffer().String()), "network-list.golden")
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
NETWORK ID NAME DRIVER SCOPE
|
||||
123454321 network_1 09.7.01 global
|
|
@ -0,0 +1,45 @@
|
|||
package builders
|
||||
|
||||
import (
|
||||
"github.com/docker/docker/api/types"
|
||||
)
|
||||
|
||||
// NetworkResource creates a network resource with default values.
|
||||
// Any number of networkResource function builder can be pass to modify the existing value.
|
||||
// feel free to add another builder func if you need to override another value
|
||||
func NetworkResource(builders ...func(resource *types.NetworkResource)) *types.NetworkResource {
|
||||
resource := &types.NetworkResource{}
|
||||
|
||||
for _, builder := range builders {
|
||||
builder(resource)
|
||||
}
|
||||
return resource
|
||||
}
|
||||
|
||||
// NetworkResourceName sets the name of the resource network
|
||||
func NetworkResourceName(name string) func(networkResource *types.NetworkResource) {
|
||||
return func(networkResource *types.NetworkResource) {
|
||||
networkResource.Name = name
|
||||
}
|
||||
}
|
||||
|
||||
// NetworkResourceID sets the ID of the resource network
|
||||
func NetworkResourceID(id string) func(networkResource *types.NetworkResource) {
|
||||
return func(networkResource *types.NetworkResource) {
|
||||
networkResource.ID = id
|
||||
}
|
||||
}
|
||||
|
||||
// NetworkResourceDriver sets the driver of the resource network
|
||||
func NetworkResourceDriver(name string) func(networkResource *types.NetworkResource) {
|
||||
return func(networkResource *types.NetworkResource) {
|
||||
networkResource.Driver = name
|
||||
}
|
||||
}
|
||||
|
||||
// NetworkResourceScope sets the Scope of the resource network
|
||||
func NetworkResourceScope(scope string) func(networkResource *types.NetworkResource) {
|
||||
return func(networkResource *types.NetworkResource) {
|
||||
networkResource.Scope = scope
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue