Skip to content

Commit

Permalink
Session report collection and report templates (#981)
Browse files Browse the repository at this point in the history
* wip: notification stats

* make report notifications optional

* linting/documentation fixes

* linting/documentation fixes

* merge types.Container and container.Interface

* smaller naming/format fixes

* use typed image/container IDs

* simplify notifier and update tests

* add missed doc comments

* lint fixes

* remove unused constructors

* rename old/new current/latest
  • Loading branch information
piksel authored Jun 27, 2021
1 parent d0ecc23 commit e3dd8d6
Show file tree
Hide file tree
Showing 32 changed files with 849 additions and 594 deletions.
13 changes: 7 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (
noRestart bool
monitorOnly bool
enableLabel bool
notifier *notifications.Notifier
notifier t.Notifier
timeout time.Duration
lifecycleHooks bool
rollingRestart bool
Expand Down Expand Up @@ -268,9 +268,9 @@ func writeStartupMessage(c *cobra.Command, sched time.Time, filtering string) {
}

notifs := "Using no notifications"
notifList := notifier.String()
if len(notifList) > 0 {
notifs = "Using notifications: " + notifList
notifierNames := notifier.GetNames()
if len(notifierNames) > 0 {
notifs = "Using notifications: " + strings.Join(notifierNames, ", ")
}

log.Info("Watchtower ", meta.Version, "\n", notifs, "\n", filtering, "\n", schedMessage)
Expand Down Expand Up @@ -338,11 +338,12 @@ func runUpdatesWithNotifications(filter t.Filter) *metrics.Metric {
LifecycleHooks: lifecycleHooks,
RollingRestart: rollingRestart,
}
metricResults, err := actions.Update(client, updateParams)
result, err := actions.Update(client, updateParams)
if err != nil {
log.Error(err)
}
notifier.SendNotification()
notifier.SendNotification(result)
metricResults := metrics.NewMetric(result)
log.Debugf("Session done: %v scanned, %v updated, %v failed",
metricResults.Scanned, metricResults.Updated, metricResults.Failed)
return metricResults
Expand Down
20 changes: 10 additions & 10 deletions internal/actions/mocks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,41 +41,41 @@ func CreateMockClient(data *TestData, api cli.CommonAPIClient, pullImages bool,
}

// ListContainers is a mock method returning the provided container testdata
func (client MockClient) ListContainers(f t.Filter) ([]container.Container, error) {
func (client MockClient) ListContainers(_ t.Filter) ([]container.Container, error) {
return client.TestData.Containers, nil
}

// StopContainer is a mock method
func (client MockClient) StopContainer(c container.Container, d time.Duration) error {
func (client MockClient) StopContainer(c container.Container, _ time.Duration) error {
if c.Name() == client.TestData.NameOfContainerToKeep {
return errors.New("tried to stop the instance we want to keep")
}
return nil
}

// StartContainer is a mock method
func (client MockClient) StartContainer(c container.Container) (string, error) {
func (client MockClient) StartContainer(_ container.Container) (t.ContainerID, error) {
return "", nil
}

// RenameContainer is a mock method
func (client MockClient) RenameContainer(c container.Container, s string) error {
func (client MockClient) RenameContainer(_ container.Container, _ string) error {
return nil
}

// RemoveImageByID increments the TriedToRemoveImageCount on being called
func (client MockClient) RemoveImageByID(id string) error {
func (client MockClient) RemoveImageByID(_ t.ImageID) error {
client.TestData.TriedToRemoveImageCount++
return nil
}

// GetContainer is a mock method
func (client MockClient) GetContainer(containerID string) (container.Container, error) {
func (client MockClient) GetContainer(_ t.ContainerID) (container.Container, error) {
return client.TestData.Containers[0], nil
}

// ExecuteCommand is a mock method
func (client MockClient) ExecuteCommand(containerID string, command string, timeout int) (SkipUpdate bool, err error) {
func (client MockClient) ExecuteCommand(_ t.ContainerID, command string, _ int) (SkipUpdate bool, err error) {
switch command {
case "/PreUpdateReturn0.sh":
return false, nil
Expand All @@ -89,11 +89,11 @@ func (client MockClient) ExecuteCommand(containerID string, command string, time
}

// IsContainerStale is always true for the mock client
func (client MockClient) IsContainerStale(c container.Container) (bool, error) {
return true, nil
func (client MockClient) IsContainerStale(_ container.Container) (bool, t.ImageID, error) {
return true, "", nil
}

// WarnOnHeadPullFailed is always true for the mock client
func (client MockClient) WarnOnHeadPullFailed(c container.Container) bool {
func (client MockClient) WarnOnHeadPullFailed(_ container.Container) bool {
return true
}
34 changes: 27 additions & 7 deletions internal/actions/mocks/container.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package mocks

import (
"fmt"
"github.com/containrrr/watchtower/pkg/container"
wt "github.com/containrrr/watchtower/pkg/types"
"github.com/docker/docker/api/types"
container2 "github.com/docker/docker/api/types/container"
dockerContainer "github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
"strconv"
"strings"
"time"
)

Expand All @@ -16,11 +20,11 @@ func CreateMockContainer(id string, name string, image string, created time.Time
Image: image,
Name: name,
Created: created.String(),
HostConfig: &container2.HostConfig{
HostConfig: &dockerContainer.HostConfig{
PortBindings: map[nat.Port][]nat.PortBinding{},
},
},
Config: &container2.Config{
Config: &dockerContainer.Config{
Image: image,
Labels: make(map[string]string),
ExposedPorts: map[nat.Port]struct{}{},
Expand All @@ -46,7 +50,7 @@ func CreateMockContainerWithImageInfo(id string, name string, image string, crea
Name: name,
Created: created.String(),
},
Config: &container2.Config{
Config: &dockerContainer.Config{
Image: image,
Labels: make(map[string]string),
},
Expand All @@ -65,18 +69,18 @@ func CreateMockContainerWithDigest(id string, name string, image string, created
}

// CreateMockContainerWithConfig creates a container substitute valid for testing
func CreateMockContainerWithConfig(id string, name string, image string, running bool, restarting bool, created time.Time, config *container2.Config) container.Container {
func CreateMockContainerWithConfig(id string, name string, image string, running bool, restarting bool, created time.Time, config *dockerContainer.Config) container.Container {
content := types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{
ID: id,
Image: image,
Name: name,
State: &types.ContainerState{
Running: running,
Running: running,
Restarting: restarting,
},
Created: created.String(),
HostConfig: &container2.HostConfig{
HostConfig: &dockerContainer.HostConfig{
PortBindings: map[nat.Port][]nat.PortBinding{},
},
},
Expand All @@ -89,3 +93,19 @@ func CreateMockContainerWithConfig(id string, name string, image string, running
},
)
}

// CreateContainerForProgress creates a container substitute for tracking session/update progress
func CreateContainerForProgress(index int, idPrefix int, nameFormat string) (container.Container, wt.ImageID) {
indexStr := strconv.Itoa(idPrefix + index)
mockID := indexStr + strings.Repeat("0", 61-len(indexStr))
contID := "c79" + mockID
contName := fmt.Sprintf(nameFormat, index+1)
oldImgID := "01d" + mockID
newImgID := "d0a" + mockID
imageName := fmt.Sprintf("mock/%s:latest", contName)
config := &dockerContainer.Config{
Image: imageName,
}
c := CreateMockContainerWithConfig(contID, contName, oldImgID, true, false, time.Now(), config)
return c, wt.ImageID(newImgID)
}
46 changes: 46 additions & 0 deletions internal/actions/mocks/progress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package mocks

import (
"errors"
"github.com/containrrr/watchtower/pkg/session"
wt "github.com/containrrr/watchtower/pkg/types"
)

// CreateMockProgressReport creates a mock report from a given set of container states
// All containers will be given a unique ID and name based on its state and index
func CreateMockProgressReport(states ...session.State) wt.Report {

stateNums := make(map[session.State]int)
progress := session.Progress{}
failed := make(map[wt.ContainerID]error)

for _, state := range states {
index := stateNums[state]

switch state {
case session.SkippedState:
c, _ := CreateContainerForProgress(index, 41, "skip%d")
progress.AddSkipped(c, errors.New("unpossible"))
break
case session.FreshState:
c, _ := CreateContainerForProgress(index, 31, "frsh%d")
progress.AddScanned(c, c.ImageID())
break
case session.UpdatedState:
c, newImage := CreateContainerForProgress(index, 11, "updt%d")
progress.AddScanned(c, newImage)
progress.MarkForUpdate(c.ID())
break
case session.FailedState:
c, newImage := CreateContainerForProgress(index, 21, "fail%d")
progress.AddScanned(c, newImage)
failed[c.ID()] = errors.New("accidentally the whole container")
}

stateNums[state] = index + 1
}
progress.UpdateFailed(failed)

return progress.Report()

}
Loading

0 comments on commit e3dd8d6

Please sign in to comment.