Skip to content

Commit

Permalink
use apiClient for removing images
Browse files Browse the repository at this point in the history
 - remove unused argument
 - unit test of new client

Signed-off-by: Morgan Bauer <[email protected]>
  • Loading branch information
MHBauer committed Mar 29, 2016
1 parent 0b50dbd commit 1e22226
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Cluster interface {
Image(IDOrName string) *Image

// Remove images from the cluster
RemoveImages(name string, force bool) ([]*dockerclient.ImageDelete, error)
RemoveImages(name string, force bool) ([]types.ImageDelete, error)

// Return all containers
Containers() Containers
Expand Down
8 changes: 4 additions & 4 deletions cluster/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,12 +477,12 @@ func (e *Engine) updateSpecs() error {
}

// RemoveImage deletes an image from the engine.
func (e *Engine) RemoveImage(image *Image, name string, force bool) ([]*dockerclient.ImageDelete, error) {
array, err := e.client.RemoveImage(name, force)
func (e *Engine) RemoveImage(name string, force bool) ([]types.ImageDelete, error) {
rmOpts := types.ImageRemoveOptions{name, force, true}
dels, err := e.apiClient.ImageRemove(context.TODO(), rmOpts)
e.CheckConnectionErr(err)
e.RefreshImages()
return array, err

return dels, err
}

// RemoveNetwork removes a network from the engine.
Expand Down
29 changes: 26 additions & 3 deletions cluster/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"testing"
"time"

"golang.org/x/net/context"

engineapi "github.com/docker/engine-api/client"
"github.com/docker/engine-api/types"
engineapimock "github.com/docker/swarm/api/mockclient"
Expand Down Expand Up @@ -373,9 +375,9 @@ func TestUsedCpus(t *testing.T) {
apiClient.On("ServerVersion", mock.Anything).Return(mockVersion, nil)
client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()
client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil).Once()
client.On("ListContainers", true, false, "").Return([]dockerclient.Container{{Id: "test"}}, 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()

engine.ConnectWithClient(client, apiClient)
Expand Down Expand Up @@ -406,10 +408,10 @@ func TestContainerRemovedDuringRefresh(t *testing.T) {
apiClient.On("Info", mock.Anything).Return(mockInfo, nil)
apiClient.On("ServerVersion", mock.Anything).Return(mockVersion, nil)
client.On("ListImages", mock.Anything).Return([]*dockerclient.Image{}, nil)
client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()
client.On("ListContainers", true, false, "").Return([]dockerclient.Container{container1, container2}, 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"))
client.On("InspectContainer", "c2").Return(info2, nil)

Expand Down Expand Up @@ -460,3 +462,24 @@ func TestDisconnect(t *testing.T) {
// Double disconnect shouldn't cause panic
engine.Disconnect()
}

func TestRemoveImage(t *testing.T) {
engine := NewEngine("test", 0, engOpts)

imageName := "test-image"
dIs := []types.ImageDelete{{Deleted: imageName}}

apiClient := engineapimock.NewMockClient()
apiClient.On("ImageRemove", context.TODO(),
mock.AnythingOfType("ImageRemoveOptions")).Return(dIs, nil)
engine.apiClient = apiClient

deletedImages, err := engine.RemoveImage("test-image", true)
if err != nil {
t.Errorf("encountered an unexpected error")
}
if deletedImages[0].Deleted != imageName {
t.Errorf("didn't get the image we removed")
}
apiClient.Mock.AssertExpectations(t)
}
4 changes: 2 additions & 2 deletions cluster/mesos/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func (c *Cluster) Image(IDOrName string) *cluster.Image {
}

// RemoveImages removes images from the cluster
func (c *Cluster) RemoveImages(name string, force bool) ([]*dockerclient.ImageDelete, error) {
func (c *Cluster) RemoveImages(name string, force bool) ([]types.ImageDelete, error) {
return nil, errNotSupported
}

Expand Down Expand Up @@ -358,7 +358,7 @@ func (c *Cluster) Container(IDOrName string) *cluster.Container {
}

// RemoveImage removes an image from the cluster
func (c *Cluster) RemoveImage(image *cluster.Image) ([]*dockerclient.ImageDelete, error) {
func (c *Cluster) RemoveImage(image *cluster.Image) ([]types.ImageDelete, error) {
return nil, errNotSupported
}

Expand Down
6 changes: 3 additions & 3 deletions cluster/swarm/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,17 +419,17 @@ func (c *Cluster) Image(IDOrName string) *cluster.Image {
}

// RemoveImages removes all the images that match `name` from the cluster
func (c *Cluster) RemoveImages(name string, force bool) ([]*dockerclient.ImageDelete, error) {
func (c *Cluster) RemoveImages(name string, force bool) ([]types.ImageDelete, error) {
c.Lock()
defer c.Unlock()

out := []*dockerclient.ImageDelete{}
out := []types.ImageDelete{}
errs := []string{}
var err error
for _, e := range c.engines {
for _, image := range e.Images() {
if image.Match(name, true) {
content, err := image.Engine.RemoveImage(image, name, force)
content, err := image.Engine.RemoveImage(name, force)
if err != nil {
errs = append(errs, fmt.Sprintf("%s: %s", image.Engine.Name, err.Error()))
continue
Expand Down

0 comments on commit 1e22226

Please sign in to comment.