Session report collection and report templates (#981)

* 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
This commit is contained in:
nils måsén 2021-06-27 09:05:01 +02:00 committed by GitHub
parent d0ecc23d72
commit e3dd8d688a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 853 additions and 598 deletions

View file

@ -41,12 +41,12 @@ 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")
}
@ -54,28 +54,28 @@ func (client MockClient) StopContainer(c container.Container, d time.Duration) e
}
// 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
@ -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
}

View file

@ -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"
)
@ -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{}{},
@ -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),
},
@ -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{},
},
},
@ -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)
}

View file

@ -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()
}