diff --git a/container/client.go b/container/client.go index d590ed3..3f634a9 100644 --- a/container/client.go +++ b/container/client.go @@ -14,14 +14,6 @@ const ( signalLabel = "com.centurylinklabs.watchtower.stop-signal" ) -var ( - pullImages bool -) - -func init() { - pullImages = true -} - type Filter func(Container) bool type Client interface { @@ -32,18 +24,19 @@ type Client interface { IsContainerStale(Container) (bool, error) } -func NewClient(dockerHost string) Client { +func NewClient(dockerHost string, pullImages bool) Client { docker, err := dockerclient.NewDockerClient(dockerHost, nil) if err != nil { log.Fatalf("Error instantiating Docker client: %s\n", err) } - return DockerClient{api: docker} + return DockerClient{api: docker, pullImages: pullImages} } type DockerClient struct { - api dockerclient.Client + api dockerclient.Client + pullImages bool } func (client DockerClient) ListContainers(fn Filter) ([]Container, error) { @@ -121,7 +114,7 @@ func (client DockerClient) IsContainerStale(c Container) (bool, error) { oldImageInfo := c.imageInfo imageName := containerInfo.Config.Image - if pullImages { + if client.pullImages { if !strings.Contains(imageName, ":") { imageName = fmt.Sprintf("%s:latest", imageName) } diff --git a/container/client_test.go b/container/client_test.go index f2e74a0..9928f8e 100644 --- a/container/client_test.go +++ b/container/client_test.go @@ -305,7 +305,7 @@ func TestIsContainerStale_NotStaleSuccess(t *testing.T) { api.On("PullImage", "bar:latest", mock.Anything).Return(nil) api.On("InspectImage", "bar:latest").Return(newImageInfo, nil) - client := DockerClient{api: api} + client := DockerClient{api: api, pullImages: true} stale, err := client.IsContainerStale(c) assert.NoError(t, err) @@ -327,7 +327,28 @@ func TestIsContainerStale_StaleSuccess(t *testing.T) { api.On("PullImage", "bar:1.0", mock.Anything).Return(nil) api.On("InspectImage", "bar:1.0").Return(newImageInfo, nil) - client := DockerClient{api: api} + client := DockerClient{api: api, pullImages: true} + stale, err := client.IsContainerStale(c) + + assert.NoError(t, err) + assert.True(t, stale) + api.AssertExpectations(t) +} + +func TestIsContainerStale_NoPullSuccess(t *testing.T) { + c := Container{ + containerInfo: &dockerclient.ContainerInfo{ + Name: "foo", + Config: &dockerclient.ContainerConfig{Image: "bar:1.0"}, + }, + imageInfo: &dockerclient.ImageInfo{Id: "abc123"}, + } + newImageInfo := &dockerclient.ImageInfo{Id: "xyz789"} + + api := mockclient.NewMockClient() + api.On("InspectImage", "bar:1.0").Return(newImageInfo, nil) + + client := DockerClient{api: api, pullImages: false} stale, err := client.IsContainerStale(c) assert.NoError(t, err) @@ -347,7 +368,7 @@ func TestIsContainerStale_PullImageError(t *testing.T) { api := mockclient.NewMockClient() api.On("PullImage", "bar:latest", mock.Anything).Return(errors.New("oops")) - client := DockerClient{api: api} + client := DockerClient{api: api, pullImages: true} _, err := client.IsContainerStale(c) assert.Error(t, err) @@ -369,7 +390,7 @@ func TestIsContainerStale_InspectImageError(t *testing.T) { api.On("PullImage", "bar:latest", mock.Anything).Return(nil) api.On("InspectImage", "bar:latest").Return(newImageInfo, errors.New("uh-oh")) - client := DockerClient{api: api} + client := DockerClient{api: api, pullImages: true} _, err := client.IsContainerStale(c) assert.Error(t, err) diff --git a/main.go b/main.go index 7b196b7..4d8a102 100644 --- a/main.go +++ b/main.go @@ -35,6 +35,10 @@ func main() { Value: 300, Usage: "poll interval (in seconds)", }, + cli.BoolFlag{ + Name: "no-pull", + Usage: "do not pull new images", + }, } handleSignals() @@ -76,5 +80,6 @@ func start(c *cli.Context) { func newContainerClient(c *cli.Context) container.Client { dockerHost := c.GlobalString("host") - return container.NewClient(dockerHost) + noPull := c.GlobalBool("no-pull") + return container.NewClient(dockerHost, !noPull) }