diff --git a/internal/actions/actions_suite_test.go b/internal/actions/actions_suite_test.go index 5bfbcdd..3a51bfa 100644 --- a/internal/actions/actions_suite_test.go +++ b/internal/actions/actions_suite_test.go @@ -8,8 +8,8 @@ import ( "github.com/containrrr/watchtower/pkg/container" "github.com/containrrr/watchtower/pkg/container/mocks" - cli "github.com/docker/docker/client" "github.com/docker/docker/api/types" + cli "github.com/docker/docker/client" . "github.com/containrrr/watchtower/internal/actions/mocks" . "github.com/onsi/ginkgo" @@ -35,7 +35,7 @@ var _ = Describe("the actions package", func() { removeVolumes := false client = CreateMockClient( - &TestData {}, + &TestData{}, dockerClient, pullImages, removeVolumes, @@ -143,4 +143,4 @@ func createMockContainer(id string, name string, image string, created time.Time }, } return *container.NewContainer(&content, nil) -} \ No newline at end of file +} diff --git a/internal/actions/mocks/client.go b/internal/actions/mocks/client.go index 2145484..33c196d 100644 --- a/internal/actions/mocks/client.go +++ b/internal/actions/mocks/client.go @@ -20,8 +20,8 @@ type MockClient struct { // TestData is the data used to perform the test type TestData struct { TriedToRemoveImageCount int - NameOfContainerToKeep string - Containers []container.Container + NameOfContainerToKeep string + Containers []container.Container } // TriedToRemoveImage is a test helper function to check whether RemoveImageByID has been called @@ -31,7 +31,7 @@ func (testdata *TestData) TriedToRemoveImage() bool { // CreateMockClient creates a mock watchtower Client for usage in tests func CreateMockClient(data *TestData, api cli.CommonAPIClient, pullImages bool, removeVolumes bool) MockClient { - return MockClient { + return MockClient{ data, api, pullImages, @@ -56,6 +56,7 @@ func (client MockClient) StopContainer(c container.Container, d time.Duration) e func (client MockClient) StartContainer(c container.Container) (string, error) { return "", nil } + // RenameContainer is a mock method func (client MockClient) RenameContainer(c container.Container, s string) error { return nil @@ -81,4 +82,3 @@ func (client MockClient) ExecuteCommand(containerID string, command string, time func (client MockClient) IsContainerStale(c container.Container) (bool, error) { return true, nil } - diff --git a/internal/actions/update.go b/internal/actions/update.go index 1694c59..89aa245 100644 --- a/internal/actions/update.go +++ b/internal/actions/update.go @@ -80,7 +80,7 @@ func stopStaleContainer(container container.Container, client container.Client, return } } - + if err := client.StopContainer(container, params.Timeout); err != nil { log.Error(err) } diff --git a/internal/actions/update_test.go b/internal/actions/update_test.go index 3c9befe..62945dc 100644 --- a/internal/actions/update_test.go +++ b/internal/actions/update_test.go @@ -13,7 +13,6 @@ import ( . "github.com/onsi/gomega" ) - var _ = Describe("the update action", func() { var dockerClient cli.CommonAPIClient var client MockClient @@ -25,7 +24,6 @@ var _ = Describe("the update action", func() { cli.WithHTTPClient(server.Client())) }) - When("watchtower has been instructed to clean up", func() { BeforeEach(func() { pullImages := false @@ -60,7 +58,7 @@ var _ = Describe("the update action", func() { When("there are multiple containers using the same image", func() { It("should only try to remove the image once", func() { - err := actions.Update(client, types.UpdateParams{ Cleanup: true }) + err := actions.Update(client, types.UpdateParams{Cleanup: true}) Expect(err).NotTo(HaveOccurred()) Expect(client.TestData.TriedToRemoveImageCount).To(Equal(1)) }) @@ -76,7 +74,7 @@ var _ = Describe("the update action", func() { time.Now(), ), ) - err := actions.Update(client, types.UpdateParams{ Cleanup: true }) + err := actions.Update(client, types.UpdateParams{Cleanup: true}) Expect(err).NotTo(HaveOccurred()) Expect(client.TestData.TriedToRemoveImageCount).To(Equal(2)) }) diff --git a/pkg/api/api.go b/pkg/api/api.go index f0e4b4e..a34b61c 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -1,15 +1,16 @@ package api import ( - "os" - "net/http" "errors" - log "github.com/sirupsen/logrus" "io" + "net/http" + "os" + + log "github.com/sirupsen/logrus" ) var ( - lock chan bool + lock chan bool ) func init() { @@ -17,16 +18,17 @@ func init() { lock <- true } +// SetupHTTPUpdates configures the endopint needed for triggering updates via http func SetupHTTPUpdates(apiToken string, updateFunction func()) error { if apiToken == "" { - return errors.New("API token is empty or has not been set. Not starting API.") + return errors.New("api token is empty or has not been set. not starting api") } - + log.Println("Watchtower HTTP API started.") - http.HandleFunc("/v1/update", func(w http.ResponseWriter, r *http.Request){ + http.HandleFunc("/v1/update", func(w http.ResponseWriter, r *http.Request) { log.Info("Updates triggered by HTTP API request.") - + _, err := io.Copy(os.Stdout, r.Body) if err != nil { log.Println(err) @@ -39,23 +41,23 @@ func SetupHTTPUpdates(apiToken string, updateFunction func()) error { } log.Println("Valid token found. Attempting to update.") - + select { - case chanValue := <- lock: - defer func() { lock <- chanValue }() - updateFunction() - default: - log.Debug("Skipped. Another update already running.") + case chanValue := <-lock: + defer func() { lock <- chanValue }() + updateFunction() + default: + log.Debug("Skipped. Another update already running.") } - }) - + return nil } +// WaitForHTTPUpdates starts the http server and listens for requests. func WaitForHTTPUpdates() error { log.Fatal(http.ListenAndServe(":8080", nil)) os.Exit(0) return nil -} \ No newline at end of file +} diff --git a/pkg/lifecycle/lifecycle.go b/pkg/lifecycle/lifecycle.go index 9311355..2d2d50c 100644 --- a/pkg/lifecycle/lifecycle.go +++ b/pkg/lifecycle/lifecycle.go @@ -88,4 +88,3 @@ func ExecutePostUpdateCommand(client container.Client, newContainerID string) { log.Error(err) } } - diff --git a/pkg/notifications/notifier.go b/pkg/notifications/notifier.go index fc6a310..6595b22 100644 --- a/pkg/notifications/notifier.go +++ b/pkg/notifications/notifier.go @@ -29,7 +29,7 @@ func NewNotifier(c *cobra.Command) *Notifier { // Parse types and create notifiers. types, err := f.GetStringSlice("notifications") if err != nil { - log.WithField("could not read notifications argument", log.Fields{ "Error": err }).Fatal() + log.WithField("could not read notifications argument", log.Fields{"Error": err}).Fatal() } for _, t := range types { var tn ty.Notifier