2017-07-20 07:59:00 -04:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2022-02-25 07:12:19 -05:00
|
|
|
"io"
|
2017-07-20 07:59:00 -04:00
|
|
|
"testing"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-07-20 07:59:00 -04:00
|
|
|
"github.com/docker/docker/api/types/network"
|
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-07-20 07:59:00 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNetworkConnectErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
networkConnectFunc func(ctx context.Context, networkID, container string, config *network.EndpointSettings) error
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
2017-08-12 12:25:38 -04:00
|
|
|
expectedError: "requires exactly 2 arguments",
|
2017-07-20 07:59:00 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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)
|
2022-02-25 07:12:19 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-07-20 07:59:00 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkConnectWithFlags(t *testing.T) {
|
|
|
|
expectedOpts := []network.IPAMConfig{
|
|
|
|
{
|
2018-04-10 09:55:00 -04:00
|
|
|
Subnet: "192.168.4.0/24",
|
|
|
|
IPRange: "192.168.4.0/24",
|
|
|
|
Gateway: "192.168.4.1/24",
|
|
|
|
AuxAddress: map[string]string{},
|
2017-07-20 07:59:00 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
networkConnectFunc: func(ctx context.Context, networkID, container string, config *network.EndpointSettings) error {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(expectedOpts, config.IPAMConfig), "not expected driver error")
|
2017-07-20 07:59:00 -04:00
|
|
|
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")
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-07-20 07:59:00 -04:00
|
|
|
}
|