Turn DockerClient into dockerClient

No need to export this particular struct since we already have a public
Client interface available and a NewClient function which can be used to
instantiate the concrete struct.
pull/1/head
Brian DeHamer 9 years ago
parent dd80aa4a0d
commit 058d6e5507

@ -31,15 +31,15 @@ func NewClient(dockerHost string, tlsConfig *tls.Config, pullImages bool) Client
log.Fatalf("Error instantiating Docker client: %s", err) log.Fatalf("Error instantiating Docker client: %s", err)
} }
return DockerClient{api: docker, pullImages: pullImages} return dockerClient{api: docker, pullImages: pullImages}
} }
type DockerClient struct { type dockerClient struct {
api dockerclient.Client api dockerclient.Client
pullImages bool pullImages bool
} }
func (client DockerClient) ListContainers(fn Filter) ([]Container, error) { func (client dockerClient) ListContainers(fn Filter) ([]Container, error) {
cs := []Container{} cs := []Container{}
log.Debug("Retrieving running containers") log.Debug("Retrieving running containers")
@ -73,7 +73,7 @@ func (client DockerClient) ListContainers(fn Filter) ([]Container, error) {
return cs, nil return cs, nil
} }
func (client DockerClient) StopContainer(c Container, timeout time.Duration) error { func (client dockerClient) StopContainer(c Container, timeout time.Duration) error {
signal := c.StopSignal() signal := c.StopSignal()
if signal == "" { if signal == "" {
signal = defaultStopSignal signal = defaultStopSignal
@ -102,7 +102,7 @@ func (client DockerClient) StopContainer(c Container, timeout time.Duration) err
return nil return nil
} }
func (client DockerClient) StartContainer(c Container) error { func (client dockerClient) StartContainer(c Container) error {
config := c.runtimeConfig() config := c.runtimeConfig()
hostConfig := c.hostConfig() hostConfig := c.hostConfig()
name := c.Name() name := c.Name()
@ -119,12 +119,12 @@ func (client DockerClient) StartContainer(c Container) error {
return client.api.StartContainer(newContainerID, hostConfig) return client.api.StartContainer(newContainerID, hostConfig)
} }
func (client DockerClient) RenameContainer(c Container, newName string) error { func (client dockerClient) RenameContainer(c Container, newName string) error {
log.Debugf("Renaming container %s (%s) to %s", c.Name(), c.ID(), newName) log.Debugf("Renaming container %s (%s) to %s", c.Name(), c.ID(), newName)
return client.api.RenameContainer(c.ID(), newName) return client.api.RenameContainer(c.ID(), newName)
} }
func (client DockerClient) IsContainerStale(c Container) (bool, error) { func (client dockerClient) IsContainerStale(c Container) (bool, error) {
oldImageInfo := c.imageInfo oldImageInfo := c.imageInfo
imageName := c.ImageName() imageName := c.ImageName()
@ -148,14 +148,14 @@ func (client DockerClient) IsContainerStale(c Container) (bool, error) {
return false, nil return false, nil
} }
func (client DockerClient) RemoveImage(c Container) error { func (client dockerClient) RemoveImage(c Container) error {
imageID := c.ImageID() imageID := c.ImageID()
log.Infof("Removing image %s", imageID) log.Infof("Removing image %s", imageID)
_, err := client.api.RemoveImage(imageID) _, err := client.api.RemoveImage(imageID)
return err return err
} }
func (client DockerClient) waitForStop(c Container, waitTime time.Duration) error { func (client dockerClient) waitForStop(c Container, waitTime time.Duration) error {
timeout := time.After(waitTime) timeout := time.After(waitTime)
for { for {

@ -22,7 +22,7 @@ func TestListContainers_Success(t *testing.T) {
api.On("InspectContainer", "foo").Return(ci, nil) api.On("InspectContainer", "foo").Return(ci, nil)
api.On("InspectImage", "abc123").Return(ii, nil) api.On("InspectImage", "abc123").Return(ii, nil)
client := DockerClient{api: api} client := dockerClient{api: api}
cs, err := client.ListContainers(allContainers) cs, err := client.ListContainers(allContainers)
assert.NoError(t, err) assert.NoError(t, err)
@ -40,7 +40,7 @@ func TestListContainers_Filter(t *testing.T) {
api.On("InspectContainer", "foo").Return(ci, nil) api.On("InspectContainer", "foo").Return(ci, nil)
api.On("InspectImage", "abc123").Return(ii, nil) api.On("InspectImage", "abc123").Return(ii, nil)
client := DockerClient{api: api} client := dockerClient{api: api}
cs, err := client.ListContainers(noContainers) cs, err := client.ListContainers(noContainers)
assert.NoError(t, err) assert.NoError(t, err)
@ -52,7 +52,7 @@ func TestListContainers_ListError(t *testing.T) {
api := mockclient.NewMockClient() api := mockclient.NewMockClient()
api.On("ListContainers", false, false, "").Return([]dockerclient.Container{}, errors.New("oops")) api.On("ListContainers", false, false, "").Return([]dockerclient.Container{}, errors.New("oops"))
client := DockerClient{api: api} client := dockerClient{api: api}
_, err := client.ListContainers(allContainers) _, err := client.ListContainers(allContainers)
assert.Error(t, err) assert.Error(t, err)
@ -65,7 +65,7 @@ func TestListContainers_InspectContainerError(t *testing.T) {
api.On("ListContainers", false, false, "").Return([]dockerclient.Container{{Id: "foo", Names: []string{"bar"}}}, nil) api.On("ListContainers", false, false, "").Return([]dockerclient.Container{{Id: "foo", Names: []string{"bar"}}}, nil)
api.On("InspectContainer", "foo").Return(&dockerclient.ContainerInfo{}, errors.New("uh-oh")) api.On("InspectContainer", "foo").Return(&dockerclient.ContainerInfo{}, errors.New("uh-oh"))
client := DockerClient{api: api} client := dockerClient{api: api}
_, err := client.ListContainers(allContainers) _, err := client.ListContainers(allContainers)
assert.Error(t, err) assert.Error(t, err)
@ -81,7 +81,7 @@ func TestListContainers_InspectImageError(t *testing.T) {
api.On("InspectContainer", "foo").Return(ci, nil) api.On("InspectContainer", "foo").Return(ci, nil)
api.On("InspectImage", "abc123").Return(ii, errors.New("whoops")) api.On("InspectImage", "abc123").Return(ii, errors.New("whoops"))
client := DockerClient{api: api} client := dockerClient{api: api}
_, err := client.ListContainers(allContainers) _, err := client.ListContainers(allContainers)
assert.Error(t, err) assert.Error(t, err)
@ -110,7 +110,7 @@ func TestStopContainer_DefaultSuccess(t *testing.T) {
api.On("RemoveContainer", "abc123", true, false).Return(nil) api.On("RemoveContainer", "abc123", true, false).Return(nil)
api.On("InspectContainer", "abc123").Return(&dockerclient.ContainerInfo{}, errors.New("Not Found")) api.On("InspectContainer", "abc123").Return(&dockerclient.ContainerInfo{}, errors.New("Not Found"))
client := DockerClient{api: api} client := dockerClient{api: api}
err := client.StopContainer(c, time.Second) err := client.StopContainer(c, time.Second)
assert.NoError(t, err) assert.NoError(t, err)
@ -139,7 +139,7 @@ func TestStopContainer_CustomSignalSuccess(t *testing.T) {
api.On("RemoveContainer", "abc123", true, false).Return(nil) api.On("RemoveContainer", "abc123", true, false).Return(nil)
api.On("InspectContainer", "abc123").Return(&dockerclient.ContainerInfo{}, errors.New("Not Found")) api.On("InspectContainer", "abc123").Return(&dockerclient.ContainerInfo{}, errors.New("Not Found"))
client := DockerClient{api: api} client := dockerClient{api: api}
err := client.StopContainer(c, time.Second) err := client.StopContainer(c, time.Second)
assert.NoError(t, err) assert.NoError(t, err)
@ -158,7 +158,7 @@ func TestStopContainer_KillContainerError(t *testing.T) {
api := mockclient.NewMockClient() api := mockclient.NewMockClient()
api.On("KillContainer", "abc123", "SIGTERM").Return(errors.New("oops")) api.On("KillContainer", "abc123", "SIGTERM").Return(errors.New("oops"))
client := DockerClient{api: api} client := dockerClient{api: api}
err := client.StopContainer(c, time.Second) err := client.StopContainer(c, time.Second)
assert.Error(t, err) assert.Error(t, err)
@ -180,7 +180,7 @@ func TestStopContainer_RemoveContainerError(t *testing.T) {
api.On("InspectContainer", "abc123").Return(&dockerclient.ContainerInfo{}, errors.New("dangit")) api.On("InspectContainer", "abc123").Return(&dockerclient.ContainerInfo{}, errors.New("dangit"))
api.On("RemoveContainer", "abc123", true, false).Return(errors.New("whoops")) api.On("RemoveContainer", "abc123", true, false).Return(errors.New("whoops"))
client := DockerClient{api: api} client := dockerClient{api: api}
err := client.StopContainer(c, time.Second) err := client.StopContainer(c, time.Second)
assert.Error(t, err) assert.Error(t, err)
@ -204,7 +204,7 @@ func TestStartContainer_Success(t *testing.T) {
api.On("CreateContainer", mock.AnythingOfType("*dockerclient.ContainerConfig"), "foo").Return("def789", nil) api.On("CreateContainer", mock.AnythingOfType("*dockerclient.ContainerConfig"), "foo").Return("def789", nil)
api.On("StartContainer", "def789", mock.AnythingOfType("*dockerclient.HostConfig")).Return(nil) api.On("StartContainer", "def789", mock.AnythingOfType("*dockerclient.HostConfig")).Return(nil)
client := DockerClient{api: api} client := dockerClient{api: api}
err := client.StartContainer(c) err := client.StartContainer(c)
assert.NoError(t, err) assert.NoError(t, err)
@ -226,7 +226,7 @@ func TestStartContainer_CreateContainerError(t *testing.T) {
api := mockclient.NewMockClient() api := mockclient.NewMockClient()
api.On("CreateContainer", mock.Anything, "foo").Return("", errors.New("oops")) api.On("CreateContainer", mock.Anything, "foo").Return("", errors.New("oops"))
client := DockerClient{api: api} client := dockerClient{api: api}
err := client.StartContainer(c) err := client.StartContainer(c)
assert.Error(t, err) assert.Error(t, err)
@ -250,7 +250,7 @@ func TestStartContainer_StartContainerError(t *testing.T) {
api.On("CreateContainer", mock.Anything, "foo").Return("def789", nil) api.On("CreateContainer", mock.Anything, "foo").Return("def789", nil)
api.On("StartContainer", "def789", mock.Anything).Return(errors.New("whoops")) api.On("StartContainer", "def789", mock.Anything).Return(errors.New("whoops"))
client := DockerClient{api: api} client := dockerClient{api: api}
err := client.StartContainer(c) err := client.StartContainer(c)
assert.Error(t, err) assert.Error(t, err)
@ -268,7 +268,7 @@ func TestRenameContainer_Success(t *testing.T) {
api := mockclient.NewMockClient() api := mockclient.NewMockClient()
api.On("RenameContainer", "abc123", "foo").Return(nil) api.On("RenameContainer", "abc123", "foo").Return(nil)
client := DockerClient{api: api} client := dockerClient{api: api}
err := client.RenameContainer(c, "foo") err := client.RenameContainer(c, "foo")
assert.NoError(t, err) assert.NoError(t, err)
@ -285,7 +285,7 @@ func TestRenameContainer_Error(t *testing.T) {
api := mockclient.NewMockClient() api := mockclient.NewMockClient()
api.On("RenameContainer", "abc123", "foo").Return(errors.New("oops")) api.On("RenameContainer", "abc123", "foo").Return(errors.New("oops"))
client := DockerClient{api: api} client := dockerClient{api: api}
err := client.RenameContainer(c, "foo") err := client.RenameContainer(c, "foo")
assert.Error(t, err) assert.Error(t, err)
@ -307,7 +307,7 @@ func TestIsContainerStale_NotStaleSuccess(t *testing.T) {
api.On("PullImage", "bar:latest", mock.Anything).Return(nil) api.On("PullImage", "bar:latest", mock.Anything).Return(nil)
api.On("InspectImage", "bar:latest").Return(newImageInfo, nil) api.On("InspectImage", "bar:latest").Return(newImageInfo, nil)
client := DockerClient{api: api, pullImages: true} client := dockerClient{api: api, pullImages: true}
stale, err := client.IsContainerStale(c) stale, err := client.IsContainerStale(c)
assert.NoError(t, err) assert.NoError(t, err)
@ -329,7 +329,7 @@ func TestIsContainerStale_StaleSuccess(t *testing.T) {
api.On("PullImage", "bar:1.0", mock.Anything).Return(nil) api.On("PullImage", "bar:1.0", mock.Anything).Return(nil)
api.On("InspectImage", "bar:1.0").Return(newImageInfo, nil) api.On("InspectImage", "bar:1.0").Return(newImageInfo, nil)
client := DockerClient{api: api, pullImages: true} client := dockerClient{api: api, pullImages: true}
stale, err := client.IsContainerStale(c) stale, err := client.IsContainerStale(c)
assert.NoError(t, err) assert.NoError(t, err)
@ -350,7 +350,7 @@ func TestIsContainerStale_NoPullSuccess(t *testing.T) {
api := mockclient.NewMockClient() api := mockclient.NewMockClient()
api.On("InspectImage", "bar:1.0").Return(newImageInfo, nil) api.On("InspectImage", "bar:1.0").Return(newImageInfo, nil)
client := DockerClient{api: api, pullImages: false} client := dockerClient{api: api, pullImages: false}
stale, err := client.IsContainerStale(c) stale, err := client.IsContainerStale(c)
assert.NoError(t, err) assert.NoError(t, err)
@ -370,7 +370,7 @@ func TestIsContainerStale_PullImageError(t *testing.T) {
api := mockclient.NewMockClient() api := mockclient.NewMockClient()
api.On("PullImage", "bar:latest", mock.Anything).Return(errors.New("oops")) api.On("PullImage", "bar:latest", mock.Anything).Return(errors.New("oops"))
client := DockerClient{api: api, pullImages: true} client := dockerClient{api: api, pullImages: true}
_, err := client.IsContainerStale(c) _, err := client.IsContainerStale(c)
assert.Error(t, err) assert.Error(t, err)
@ -392,7 +392,7 @@ func TestIsContainerStale_InspectImageError(t *testing.T) {
api.On("PullImage", "bar:latest", mock.Anything).Return(nil) api.On("PullImage", "bar:latest", mock.Anything).Return(nil)
api.On("InspectImage", "bar:latest").Return(newImageInfo, errors.New("uh-oh")) api.On("InspectImage", "bar:latest").Return(newImageInfo, errors.New("uh-oh"))
client := DockerClient{api: api, pullImages: true} client := dockerClient{api: api, pullImages: true}
_, err := client.IsContainerStale(c) _, err := client.IsContainerStale(c)
assert.Error(t, err) assert.Error(t, err)
@ -410,7 +410,7 @@ func TestRemoveImage_Success(t *testing.T) {
api := mockclient.NewMockClient() api := mockclient.NewMockClient()
api.On("RemoveImage", "abc123").Return([]*dockerclient.ImageDelete{}, nil) api.On("RemoveImage", "abc123").Return([]*dockerclient.ImageDelete{}, nil)
client := DockerClient{api: api} client := dockerClient{api: api}
err := client.RemoveImage(c) err := client.RemoveImage(c)
assert.NoError(t, err) assert.NoError(t, err)
@ -427,7 +427,7 @@ func TestRemoveImage_Error(t *testing.T) {
api := mockclient.NewMockClient() api := mockclient.NewMockClient()
api.On("RemoveImage", "abc123").Return([]*dockerclient.ImageDelete{}, errors.New("oops")) api.On("RemoveImage", "abc123").Return([]*dockerclient.ImageDelete{}, errors.New("oops"))
client := DockerClient{api: api} client := dockerClient{api: api}
err := client.RemoveImage(c) err := client.RemoveImage(c)
assert.Error(t, err) assert.Error(t, err)

Loading…
Cancel
Save