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) {
|
2024-04-08 04:07:48 -04:00
|
|
|
expectedConfig := &network.EndpointSettings{
|
|
|
|
IPAMConfig: &network.EndpointIPAMConfig{
|
|
|
|
IPv4Address: "192.168.4.1",
|
|
|
|
IPv6Address: "fdef:f401:8da0:1234::5678",
|
|
|
|
LinkLocalIPs: []string{"169.254.42.42"},
|
|
|
|
},
|
|
|
|
Links: []string{"otherctr"},
|
|
|
|
Aliases: []string{"poor-yorick"},
|
|
|
|
DriverOpts: map[string]string{
|
|
|
|
"adriveropt": "anoptval",
|
2017-07-20 07:59:00 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
networkConnectFunc: func(ctx context.Context, networkID, container string, config *network.EndpointSettings) error {
|
2024-04-08 04:07:48 -04:00
|
|
|
assert.Check(t, is.DeepEqual(expectedConfig, config))
|
2017-07-20 07:59:00 -04:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
2024-04-08 04:07:48 -04:00
|
|
|
args := []string{"mynet", "myctr"}
|
|
|
|
cmd := newConnectCommand(cli)
|
2017-07-20 07:59:00 -04:00
|
|
|
|
|
|
|
cmd.SetArgs(args)
|
2024-04-08 04:07:48 -04:00
|
|
|
for _, opt := range []struct{ name, value string }{
|
|
|
|
{"alias", "poor-yorick"},
|
|
|
|
{"driver-opt", "adriveropt=anoptval"},
|
|
|
|
{"ip", "192.168.4.1"},
|
|
|
|
{"ip6", "fdef:f401:8da0:1234::5678"},
|
|
|
|
{"link", "otherctr"},
|
|
|
|
{"link-local-ip", "169.254.42.42"},
|
|
|
|
} {
|
|
|
|
err := cmd.Flags().Set(opt.name, opt.value)
|
|
|
|
assert.Check(t, err)
|
|
|
|
}
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-07-20 07:59:00 -04:00
|
|
|
}
|