From 7296abf39f84d731843b35a1be793e7295accc05 Mon Sep 17 00:00:00 2001 From: khaled souf Date: Thu, 20 Jul 2017 13:59:00 +0200 Subject: [PATCH] adding connect disconnect network tests Signed-off-by: khaled souf --- cli/command/network/client_test.go | 19 ++++++- cli/command/network/connect_test.go | 70 ++++++++++++++++++++++++++ cli/command/network/disconnect_test.go | 41 +++++++++++++++ 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 cli/command/network/connect_test.go create mode 100644 cli/command/network/disconnect_test.go diff --git a/cli/command/network/client_test.go b/cli/command/network/client_test.go index 1a5ee4ed81..722c72bd18 100644 --- a/cli/command/network/client_test.go +++ b/cli/command/network/client_test.go @@ -2,13 +2,16 @@ package network import ( "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/network" "github.com/docker/docker/client" "golang.org/x/net/context" ) type fakeClient struct { client.Client - networkCreateFunc func(ctx context.Context, name string, options types.NetworkCreate) (types.NetworkCreateResponse, error) + 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 } func (c *fakeClient) NetworkCreate(ctx context.Context, name string, options types.NetworkCreate) (types.NetworkCreateResponse, error) { @@ -17,3 +20,17 @@ func (c *fakeClient) NetworkCreate(ctx context.Context, name string, options typ } return types.NetworkCreateResponse{}, nil } + +func (c *fakeClient) NetworkConnect(ctx context.Context, networkID, container string, config *network.EndpointSettings) error { + if c.networkConnectFunc != nil { + return c.networkConnectFunc(ctx, networkID, container, config) + } + return nil +} + +func (c *fakeClient) NetworkDisconnect(ctx context.Context, networkID, container string, force bool) error { + if c.networkDisconnectFunc != nil { + return c.networkDisconnectFunc(ctx, networkID, container, force) + } + return nil +} diff --git a/cli/command/network/connect_test.go b/cli/command/network/connect_test.go new file mode 100644 index 0000000000..790ec25c21 --- /dev/null +++ b/cli/command/network/connect_test.go @@ -0,0 +1,70 @@ +package network + +import ( + "io/ioutil" + "testing" + + "github.com/docker/cli/cli/internal/test" + "github.com/docker/docker/api/types/network" + "github.com/docker/docker/pkg/testutil" + "github.com/pkg/errors" + "github.com/stretchr/testify/assert" + "golang.org/x/net/context" +) + +func TestNetworkConnectErrors(t *testing.T) { + testCases := []struct { + args []string + networkConnectFunc func(ctx context.Context, networkID, container string, config *network.EndpointSettings) error + expectedError string + }{ + { + expectedError: "requires exactly 2 argument(s)", + }, + { + args: []string{"toto", "titi"}, + networkConnectFunc: func(ctx context.Context, networkID, container string, config *network.EndpointSettings) error { + return errors.Errorf("error connecting network") + }, + expectedError: "error connecting network", + }, + } + + for _, tc := range testCases { + cmd := newConnectCommand( + test.NewFakeCli(&fakeClient{ + networkConnectFunc: tc.networkConnectFunc, + }), + ) + cmd.SetArgs(tc.args) + cmd.SetOutput(ioutil.Discard) + testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) + + } +} + +func TestNetworkConnectWithFlags(t *testing.T) { + expectedOpts := []network.IPAMConfig{ + { + "192.168.4.0/24", + "192.168.4.0/24", + "192.168.4.1/24", + map[string]string{}, + }, + } + cli := test.NewFakeCli(&fakeClient{ + networkConnectFunc: func(ctx context.Context, networkID, container string, config *network.EndpointSettings) error { + assert.Equal(t, expectedOpts, config.IPAMConfig, "not expected driver error") + return nil + }, + }) + args := []string{"banana"} + cmd := newCreateCommand(cli) + + cmd.SetArgs(args) + cmd.Flags().Set("driver", "foo") + cmd.Flags().Set("ip-range", "192.168.4.0/24") + cmd.Flags().Set("gateway", "192.168.4.1/24") + cmd.Flags().Set("subnet", "192.168.4.0/24") + assert.NoError(t, cmd.Execute()) +} diff --git a/cli/command/network/disconnect_test.go b/cli/command/network/disconnect_test.go new file mode 100644 index 0000000000..01cba2ff36 --- /dev/null +++ b/cli/command/network/disconnect_test.go @@ -0,0 +1,41 @@ +package network + +import ( + "io/ioutil" + "testing" + + "github.com/docker/cli/cli/internal/test" + "github.com/docker/docker/pkg/testutil" + "github.com/pkg/errors" + "golang.org/x/net/context" +) + +func TestNetworkDisconnectErrors(t *testing.T) { + testCases := []struct { + args []string + networkDisconnectFunc func(ctx context.Context, networkID, container string, force bool) error + expectedError string + }{ + { + expectedError: "requires exactly 2 argument(s)", + }, + { + args: []string{"toto", "titi"}, + networkDisconnectFunc: func(ctx context.Context, networkID, container string, force bool) error { + return errors.Errorf("error disconnecting network") + }, + expectedError: "error disconnecting network", + }, + } + + for _, tc := range testCases { + cmd := newDisconnectCommand( + test.NewFakeCli(&fakeClient{ + networkDisconnectFunc: tc.networkDisconnectFunc, + }), + ) + cmd.SetArgs(tc.args) + cmd.SetOutput(ioutil.Discard) + testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) + } +}