Skip to content

Commit

Permalink
use apiClient for listing networks in refresh
Browse files Browse the repository at this point in the history
Signed-off-by: Morgan Bauer <[email protected]>
  • Loading branch information
MHBauer committed Mar 29, 2016
1 parent 08a475d commit 0be5b31
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 35 deletions.
2 changes: 1 addition & 1 deletion api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions cluster/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -551,15 +552,16 @@ 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
}
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
Expand Down
35 changes: 26 additions & 9 deletions cluster/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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())
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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"))
Expand All @@ -435,14 +449,17 @@ 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()

// 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()
Expand Down
6 changes: 3 additions & 3 deletions cluster/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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]
Expand Down
30 changes: 15 additions & 15 deletions cluster/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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},
Expand All @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions cluster/swarm/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0be5b31

Please sign in to comment.