From 0be5b31f27d8bf4e2eb878adf82dafd1c9d201b4 Mon Sep 17 00:00:00 2001 From: Morgan Bauer Date: Thu, 17 Mar 2016 12:26:13 -0700 Subject: [PATCH] use apiClient for listing networks in refresh Signed-off-by: Morgan Bauer --- api/handlers.go | 2 +- cluster/engine.go | 10 ++++++---- cluster/engine_test.go | 35 ++++++++++++++++++++++++++--------- cluster/network.go | 6 +++--- cluster/network_test.go | 30 +++++++++++++++--------------- cluster/swarm/cluster_test.go | 6 +++--- 6 files changed, 54 insertions(+), 35 deletions(-) diff --git a/api/handlers.go b/api/handlers.go index b6cb6c0505..a46ce01257 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -268,7 +268,7 @@ func getNetworks(c *context, w http.ResponseWriter, r *http.Request) { } } - out := []*dockerclient.NetworkResource{} + out := []*apitypes.NetworkResource{} networks := c.cluster.Networks().Filter(filters.Get("name"), filters.Get("id"), types) for _, network := range networks { tmp := (*network).NetworkResource diff --git a/cluster/engine.go b/cluster/engine.go index 4a712fbbd2..4c2eff790e 100644 --- a/cluster/engine.go +++ b/cluster/engine.go @@ -19,6 +19,7 @@ import ( "github.com/docker/docker/pkg/version" engineapi "github.com/docker/engine-api/client" "github.com/docker/engine-api/types" + "github.com/docker/engine-api/types/filters" engineapinop "github.com/docker/swarm/api/nopclient" "github.com/samalba/dockerclient" "github.com/samalba/dockerclient/nopclient" @@ -487,7 +488,7 @@ func (e *Engine) RemoveImage(name string, force bool) ([]types.ImageDelete, erro // RemoveNetwork removes a network from the engine. func (e *Engine) RemoveNetwork(network *Network) error { - err := e.apiClient.NetworkRemove(network.ID) + err := e.apiClient.NetworkRemove(context.TODO(), network.ID) e.CheckConnectionErr(err) if err != nil { return err @@ -518,7 +519,7 @@ func (e *Engine) AddNetwork(network *Network) { // RemoveVolume deletes a volume from the engine. func (e *Engine) RemoveVolume(name string) error { - err := e.apiClient.VolumeRemove(name) + err := e.apiClient.VolumeRemove(context.TODO(), name) e.CheckConnectionErr(err) if err != nil { return err @@ -551,7 +552,8 @@ func (e *Engine) RefreshImages() error { // RefreshNetworks refreshes the list of networks on the engine. func (e *Engine) RefreshNetworks() error { - networks, err := e.client.ListNetworks("") + netLsOpts := types.NetworkListOptions{filters.NewArgs()} + networks, err := e.apiClient.NetworkList(context.TODO(), netLsOpts) e.CheckConnectionErr(err) if err != nil { return err @@ -559,7 +561,7 @@ func (e *Engine) RefreshNetworks() error { e.Lock() e.networks = make(map[string]*Network) for _, network := range networks { - e.networks[network.ID] = &Network{NetworkResource: *network, Engine: e} + e.networks[network.ID] = &Network{NetworkResource: network, Engine: e} } e.Unlock() return nil diff --git a/cluster/engine_test.go b/cluster/engine_test.go index e4bea9c81a..0bc876e7a6 100644 --- a/cluster/engine_test.go +++ b/cluster/engine_test.go @@ -158,10 +158,12 @@ func TestEngineCpusMemory(t *testing.T) { apiClient := engineapimock.NewMockClient() apiClient.On("Info", mock.Anything).Return(mockInfo, nil) apiClient.On("ServerVersion", mock.Anything).Return(mockVersion, nil) + apiClient.On("NetworkList", mock.Anything, + mock.AnythingOfType("NetworkListOptions"), + ).Return([]types.NetworkResource{}, nil) client.On("ListContainers", true, false, "").Return([]dockerclient.Container{}, nil) client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil) client.On("ListVolumes", mock.Anything).Return([]*dockerclient.Volume{}, nil) - client.On("ListNetworks", mock.Anything).Return([]*dockerclient.NetworkResource{}, nil) client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return() assert.NoError(t, engine.ConnectWithClient(client, apiClient)) @@ -184,10 +186,12 @@ func TestEngineSpecs(t *testing.T) { apiClient := engineapimock.NewMockClient() apiClient.On("Info", mock.Anything).Return(mockInfo, nil) apiClient.On("ServerVersion", mock.Anything).Return(mockVersion, nil) + apiClient.On("NetworkList", mock.Anything, + mock.AnythingOfType("NetworkListOptions"), + ).Return([]types.NetworkResource{}, nil) client.On("ListContainers", true, false, "").Return([]dockerclient.Container{}, nil) client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil) client.On("ListVolumes", mock.Anything).Return([]*dockerclient.Volume{}, nil) - client.On("ListNetworks", mock.Anything).Return([]*dockerclient.NetworkResource{}, nil) client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return() assert.NoError(t, engine.ConnectWithClient(client, apiClient)) @@ -215,13 +219,16 @@ func TestEngineState(t *testing.T) { apiClient := engineapimock.NewMockClient() apiClient.On("Info", mock.Anything).Return(mockInfo, nil) apiClient.On("ServerVersion", mock.Anything).Return(mockVersion, nil) + apiClient.On("NetworkList", mock.Anything, + mock.AnythingOfType("NetworkListOptions"), + ).Return([]types.NetworkResource{}, nil) + client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return() // The client will return one container at first, then a second one will appear. client.On("ListContainers", true, false, "").Return([]dockerclient.Container{{Id: "one"}}, nil).Once() client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil).Once() client.On("ListVolumes", mock.Anything).Return([]*dockerclient.Volume{}, nil) - client.On("ListNetworks", mock.Anything).Return([]*dockerclient.NetworkResource{}, nil) client.On("InspectContainer", "one").Return(&dockerclient.ContainerInfo{Config: &dockerclient.ContainerConfig{CpuShares: 100}}, nil).Once() client.On("ListContainers", true, false, fmt.Sprintf("{%q:[%q]}", "id", "two")).Return([]dockerclient.Container{{Id: "two"}}, nil).Once() client.On("InspectContainer", "two").Return(&dockerclient.ContainerInfo{Config: &dockerclient.ContainerConfig{CpuShares: 100}}, nil).Once() @@ -267,11 +274,14 @@ func TestCreateContainer(t *testing.T) { engine.setState(stateUnhealthy) apiClient.On("Info", mock.Anything).Return(mockInfo, nil) apiClient.On("ServerVersion", mock.Anything).Return(mockVersion, nil) + apiClient.On("NetworkList", mock.Anything, + mock.AnythingOfType("NetworkListOptions"), + ).Return([]types.NetworkResource{}, nil) + client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return() client.On("ListContainers", true, false, "").Return([]dockerclient.Container{}, nil).Once() client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil).Once() client.On("ListVolumes", mock.Anything).Return([]*dockerclient.Volume{}, nil) - client.On("ListNetworks", mock.Anything).Return([]*dockerclient.NetworkResource{}, nil) assert.NoError(t, engine.ConnectWithClient(client, apiClient)) assert.True(t, engine.isConnected()) @@ -288,7 +298,6 @@ func TestCreateContainer(t *testing.T) { client.On("ListContainers", true, false, fmt.Sprintf(`{"id":[%q]}`, id)).Return([]dockerclient.Container{{Id: id}}, nil).Once() client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil).Once() client.On("ListVolumes", mock.Anything).Return([]*dockerclient.Volume{}, nil) - client.On("ListNetworks", mock.Anything).Return([]*dockerclient.NetworkResource{}, nil) client.On("InspectContainer", id).Return(&dockerclient.ContainerInfo{Config: &config.ContainerConfig}, nil).Once() container, err := engine.Create(config, name, false, auth) assert.Nil(t, err) @@ -313,7 +322,6 @@ func TestCreateContainer(t *testing.T) { client.On("ListContainers", true, false, fmt.Sprintf(`{"id":[%q]}`, id)).Return([]dockerclient.Container{{Id: id}}, nil).Once() client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil).Once() client.On("ListVolumes", mock.Anything).Return([]*dockerclient.Volume{}, nil) - client.On("ListNetworks", mock.Anything).Return([]*dockerclient.NetworkResource{}, nil) client.On("InspectContainer", id).Return(&dockerclient.ContainerInfo{Config: &config.ContainerConfig}, nil).Once() container, err = engine.Create(config, name, true, auth) assert.Nil(t, err) @@ -373,10 +381,13 @@ func TestUsedCpus(t *testing.T) { apiClient.On("Info", mock.Anything).Return(mockInfo, nil).Once() apiClient.On("ServerVersion", mock.Anything).Return(mockVersion, nil) + apiClient.On("NetworkList", mock.Anything, + mock.AnythingOfType("NetworkListOptions"), + ).Return([]types.NetworkResource{}, nil) + client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return() client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil).Once() client.On("ListVolumes", mock.Anything).Return([]*dockerclient.Volume{}, nil) - client.On("ListNetworks", mock.Anything).Return([]*dockerclient.NetworkResource{}, nil) client.On("ListContainers", true, false, "").Return([]dockerclient.Container{{Id: "test"}}, nil).Once() client.On("InspectContainer", "test").Return(&dockerclient.ContainerInfo{Config: &dockerclient.ContainerConfig{CpuShares: cpuShares}}, nil).Once() @@ -407,9 +418,12 @@ func TestContainerRemovedDuringRefresh(t *testing.T) { apiClient.On("Info", mock.Anything).Return(mockInfo, nil) apiClient.On("ServerVersion", mock.Anything).Return(mockVersion, nil) + apiClient.On("NetworkList", mock.Anything, + mock.AnythingOfType("NetworkListOptions"), + ).Return([]types.NetworkResource{}, nil) + client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil) client.On("ListVolumes", mock.Anything).Return([]*dockerclient.Volume{}, nil) - client.On("ListNetworks", mock.Anything).Return([]*dockerclient.NetworkResource{}, nil) client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return() client.On("ListContainers", true, false, "").Return([]dockerclient.Container{container1, container2}, nil) client.On("InspectContainer", "c1").Return(info1, errors.New("Not found")) @@ -435,6 +449,10 @@ func TestDisconnect(t *testing.T) { apiClient.On("Info", mock.Anything).Return(mockInfo, nil) apiClient.On("ServerVersion", mock.Anything).Return(mockVersion, nil) + apiClient.On("NetworkList", mock.Anything, + mock.AnythingOfType("NetworkListOptions"), + ).Return([]types.NetworkResource{}, nil) + client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return() client.On("StopAllMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return() @@ -442,7 +460,6 @@ func TestDisconnect(t *testing.T) { client.On("ListContainers", true, false, "").Return([]dockerclient.Container{{Id: "one"}}, nil).Once() client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil).Once() client.On("ListVolumes", mock.Anything).Return([]*dockerclient.Volume{}, nil) - client.On("ListNetworks", mock.Anything).Return([]*dockerclient.NetworkResource{}, nil) client.On("InspectContainer", "one").Return(&dockerclient.ContainerInfo{Config: &dockerclient.ContainerConfig{CpuShares: 100}}, nil).Once() client.On("ListContainers", true, false, fmt.Sprintf("{%q:[%q]}", "id", "two")).Return([]dockerclient.Container{{Id: "two"}}, nil).Once() client.On("InspectContainer", "two").Return(&dockerclient.ContainerInfo{Config: &dockerclient.ContainerConfig{CpuShares: 100}}, nil).Once() diff --git a/cluster/network.go b/cluster/network.go index addf5d0c90..b5e3787abc 100644 --- a/cluster/network.go +++ b/cluster/network.go @@ -4,12 +4,12 @@ import ( "strings" "github.com/docker/docker/pkg/stringid" - "github.com/samalba/dockerclient" + "github.com/docker/engine-api/types" ) // Network is exported type Network struct { - dockerclient.NetworkResource + types.NetworkResource Engine *Engine } @@ -98,7 +98,7 @@ func (network *Network) RemoveDuplicateEndpoints() *Network { // Make a copy of the network netCopy := *network // clean up existing endpoints - netCopy.Containers = make(map[string]dockerclient.EndpointResource) + netCopy.Containers = make(map[string]types.EndpointResource) // add the endpoint index from endpointMap for _, index := range endpointMap { netCopy.Containers[index] = network.Containers[index] diff --git a/cluster/network_test.go b/cluster/network_test.go index 9ce73f6861..079b56aff1 100644 --- a/cluster/network_test.go +++ b/cluster/network_test.go @@ -3,26 +3,26 @@ package cluster import ( "testing" - "github.com/samalba/dockerclient" + "github.com/docker/engine-api/types" "github.com/stretchr/testify/assert" ) func TestNetworksFilter(t *testing.T) { engine := &Engine{ID: "id"} networks := Networks{ - {dockerclient.NetworkResource{ + {types.NetworkResource{ ID: "ababababab", Name: "something", }, engine}, - {dockerclient.NetworkResource{ + {types.NetworkResource{ ID: "aaaaaaaaaa1", Name: "network_name", }, engine}, - {dockerclient.NetworkResource{ + {types.NetworkResource{ ID: "bbbbbbbbbb", Name: "somethingelse", }, engine}, - {dockerclient.NetworkResource{ + {types.NetworkResource{ ID: "aaaaaaaaa2", Name: "foo", }, engine}, @@ -39,31 +39,31 @@ func TestNetworkUniq(t *testing.T) { engine1 := &Engine{ID: "id1"} engine2 := &Engine{ID: "id2"} networks := Networks{ - {dockerclient.NetworkResource{ + {types.NetworkResource{ ID: "global", Name: "global", - Containers: map[string]dockerclient.EndpointResource{ + Containers: map[string]types.EndpointResource{ "c1": {}, }, }, engine1}, - {dockerclient.NetworkResource{ + {types.NetworkResource{ ID: "global", Name: "global", - Containers: map[string]dockerclient.EndpointResource{ + Containers: map[string]types.EndpointResource{ "c2": {}, }, }, engine2}, - {dockerclient.NetworkResource{ + {types.NetworkResource{ ID: "local1", Name: "local", - Containers: map[string]dockerclient.EndpointResource{ + Containers: map[string]types.EndpointResource{ "c3": {}, }, }, engine1}, - {dockerclient.NetworkResource{ + {types.NetworkResource{ ID: "local2", Name: "local", - Containers: map[string]dockerclient.EndpointResource{ + Containers: map[string]types.EndpointResource{ "c4": {}, }, }, engine2}, @@ -84,10 +84,10 @@ func TestNetworkUniq(t *testing.T) { func TestRemoveDuplicateEndpoints(t *testing.T) { engine1 := &Engine{ID: "id1"} network := Network{ - dockerclient.NetworkResource{ + types.NetworkResource{ ID: "global", Name: "voteappbase_voteapp", - Containers: map[string]dockerclient.EndpointResource{ + Containers: map[string]types.EndpointResource{ "028771f7f6a54c486d441ecfc92aad68e0836a1f0a5a0c227c514f14848e2b54": { Name: "voteappbase_worker_1", EndpointID: "49f621862a0659f462870a6cd15874da44592e399f41da2a3019d81b7427315b", diff --git a/cluster/swarm/cluster_test.go b/cluster/swarm/cluster_test.go index 8e65e35b62..c90705310a 100644 --- a/cluster/swarm/cluster_test.go +++ b/cluster/swarm/cluster_test.go @@ -137,11 +137,11 @@ func TestImportImage(t *testing.T) { apiClient := engineapimock.NewMockClient() apiClient.On("Info", mock.Anything).Return(mockInfo, nil) apiClient.On("ServerVersion", mock.Anything).Return(mockVersion, nil) + apiClient.On("NetworkList", mock.Anything).Return([]types.NetworkResource{}, nil) client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return() client.On("ListContainers", true, false, "").Return([]dockerclient.Container{}, nil).Once() client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil) client.On("ListVolumes", mock.Anything).Return([]*dockerclient.Volume{}, nil) - client.On("ListNetworks", mock.Anything).Return([]*dockerclient.NetworkResource{}, nil) // connect client engine.ConnectWithClient(client, apiClient) @@ -188,11 +188,11 @@ func TestLoadImage(t *testing.T) { apiClient := engineapimock.NewMockClient() apiClient.On("Info", mock.Anything).Return(mockInfo, nil) apiClient.On("ServerVersion", mock.Anything).Return(mockVersion, nil) + apiClient.On("NetworkList", mock.Anything).Return([]types.NetworkResource{}, nil) client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return() client.On("ListContainers", true, false, "").Return([]dockerclient.Container{}, nil).Once() client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil) client.On("ListVolumes", mock.Anything).Return([]*dockerclient.Volume{}, nil) - client.On("ListNetworks", mock.Anything).Return([]*dockerclient.NetworkResource{}, nil) // connect client engine.ConnectWithClient(client, apiClient) @@ -242,11 +242,11 @@ func TestTagImage(t *testing.T) { apiClient := engineapimock.NewMockClient() apiClient.On("Info", mock.Anything).Return(mockInfo, nil) apiClient.On("ServerVersion", mock.Anything).Return(mockVersion, nil) + apiClient.On("NetworkList", mock.Anything).Return([]types.NetworkResource{}, nil) client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return() client.On("ListContainers", true, false, "").Return([]dockerclient.Container{}, nil).Once() client.On("ListImages", mock.Anything).Return(images, nil) client.On("ListVolumes", mock.Anything).Return([]*dockerclient.Volume{}, nil) - client.On("ListNetworks", mock.Anything).Return([]*dockerclient.NetworkResource{}, nil) // connect client engine.ConnectWithClient(client, apiClient)