diff --git a/internal/actions/actions_suite_test.go b/internal/actions/actions_suite_test.go index b633b6a..5bfbcdd 100644 --- a/internal/actions/actions_suite_test.go +++ b/internal/actions/actions_suite_test.go @@ -9,6 +9,7 @@ import ( "github.com/containrrr/watchtower/pkg/container/mocks" cli "github.com/docker/docker/client" + "github.com/docker/docker/api/types" . "github.com/containrrr/watchtower/internal/actions/mocks" . "github.com/onsi/ginkgo" @@ -142,52 +143,4 @@ func createMockContainer(id string, name string, image string, created time.Time }, } return *container.NewContainer(&content, nil) -} - -type mockClient struct { - TestData *TestData - api cli.CommonAPIClient - pullImages bool - removeVolumes bool -} - -type TestData struct { - TriedToRemoveImage bool - NameOfContainerToKeep string - Containers []container.Container -} - -func (client mockClient) ListContainers(f t.Filter) ([]container.Container, error) { - return client.TestData.Containers, nil -} - -func (client mockClient) StopContainer(c container.Container, d time.Duration) error { - if c.Name() == client.TestData.NameOfContainerToKeep { - return errors.New("tried to stop the instance we want to keep") - } - return nil -} -func (client mockClient) StartContainer(c container.Container) (string, error) { - panic("Not implemented") -} - -func (client mockClient) RenameContainer(c container.Container, s string) error { - panic("Not implemented") -} - -func (client mockClient) RemoveImage(c container.Container) error { - client.TestData.TriedToRemoveImage = true - return nil -} - -func (client mockClient) GetContainer(containerID string) (container.Container, error) { - return container.Container{}, nil -} - -func (client mockClient) ExecuteCommand(containerID string, command string, timeout int) error { - return nil -} - -func (client mockClient) IsContainerStale(c container.Container) (bool, error) { - panic("Not implemented") -} +} \ No newline at end of file diff --git a/internal/actions/mocks/client.go b/internal/actions/mocks/client.go index dad2506..2145484 100644 --- a/internal/actions/mocks/client.go +++ b/internal/actions/mocks/client.go @@ -73,7 +73,7 @@ func (client MockClient) GetContainer(containerID string) (container.Container, } // ExecuteCommand is a mock method -func (client MockClient) ExecuteCommand(containerID string, command string) error { +func (client MockClient) ExecuteCommand(containerID string, command string, timeout int) error { return nil } diff --git a/pkg/container/container.go b/pkg/container/container.go index 32e5a31..fb495fe 100644 --- a/pkg/container/container.go +++ b/pkg/container/container.go @@ -121,8 +121,8 @@ func (c Container) IsWatchtower() bool { // PreUpdateTimeout checks whether a container has a specific timeout set // for how long the pre-update command is allowed to run. This value is expressed -// either as an integer, in minutes, or as "off" which will allow the command/script -// to run indefinitely. Users should be cautious with the off option, as that +// either as an integer, in minutes, or as 0 which will allow the command/script +// to run indefinitely. Users should be cautious with the 0 option, as that // could result in watchtower waiting forever. func (c Container) PreUpdateTimeout() int { var minutes int diff --git a/pkg/lifecycle/lifecycle.go b/pkg/lifecycle/lifecycle.go index 9823f9d..9311355 100644 --- a/pkg/lifecycle/lifecycle.go +++ b/pkg/lifecycle/lifecycle.go @@ -37,7 +37,7 @@ func ExecutePreCheckCommand(client container.Client, container container.Contain } log.Info("Executing pre-check command.") - if err := client.ExecuteCommand(container.ID(), command); err != nil { + if err := client.ExecuteCommand(container.ID(), command, 1); err != nil { log.Error(err) } } @@ -51,24 +51,22 @@ func ExecutePostCheckCommand(client container.Client, container container.Contai } log.Info("Executing post-check command.") - if err := client.ExecuteCommand(container.ID(), command); err != nil { + if err := client.ExecuteCommand(container.ID(), command, 1); err != nil { log.Error(err) } } // ExecutePreUpdateCommand tries to run the pre-update lifecycle hook for a single container. -func ExecutePreUpdateCommand(client container.Client, container container.Container) { - +func ExecutePreUpdateCommand(client container.Client, container container.Container) error { + timeout := container.PreUpdateTimeout() command := container.GetLifecyclePreUpdateCommand() if len(command) == 0 { log.Debug("No pre-update command supplied. Skipping") - return + return nil } log.Info("Executing pre-update command.") - if err := client.ExecuteCommand(container.ID(), command); err != nil { - log.Error(err) - } + return client.ExecuteCommand(container.ID(), command, timeout) } // ExecutePostUpdateCommand tries to run the post-update lifecycle hook for a single container. @@ -86,7 +84,7 @@ func ExecutePostUpdateCommand(client container.Client, newContainerID string) { } log.Info("Executing post-update command.") - if err := client.ExecuteCommand(newContainerID, command); err != nil { + if err := client.ExecuteCommand(newContainerID, command, 1); err != nil { log.Error(err) } }