mocks

package
v0.0.0-...-5840427 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package mocks provides mock implementations for testing Watchtower components.

Package mocks provides mock implementations for testing Watchtower components.

Package mocks provides mock implementations for testing Watchtower components.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateContainerForProgress

func CreateContainerForProgress(
	index int,
	idPrefix int,
	nameFormat string,
) (types.Container, types.ImageID)

CreateContainerForProgress creates a container substitute for tracking session/update progress. It generates a unique ID and name based on the index and state prefix for testing progress reporting.

func CreateMockContainer

func CreateMockContainer(id, name, image string, created time.Time) types.Container

CreateMockContainer creates a container substitute valid for testing. It initializes a basic container with the specified ID, name, image, and creation time.

func CreateMockContainerWithConfig

func CreateMockContainerWithConfig(
	id string,
	name string,
	image string,
	running bool,
	restarting bool,
	created time.Time,
	config *dockerContainer.Config,
) types.Container

CreateMockContainerWithConfig creates a container substitute with custom configuration for testing. It allows specifying running/restarting states and config details for flexible test scenarios.

func CreateMockContainerWithDigest

func CreateMockContainerWithDigest(
	id string,
	name string,
	image string,
	created time.Time,
	digest string,
) types.Container

CreateMockContainerWithDigest creates a container with a specific digest for testing. It builds a container and overrides its image digest, useful for staleness tests.

func CreateMockContainerWithImageInfo

func CreateMockContainerWithImageInfo(
	id string,
	name string,
	image string,
	created time.Time,
	imageInfo dockerImage.InspectResponse,
) types.Container

CreateMockContainerWithImageInfo creates a container with custom image info for testing. It is intended for specific test cases requiring detailed image configuration.

func CreateMockContainerWithImageInfoP

func CreateMockContainerWithImageInfoP(
	id string,
	name string,
	image string,
	created time.Time,
	imageInfo *dockerImage.InspectResponse,
) types.Container

CreateMockContainerWithImageInfoP creates a container with a pointer to custom image info for testing. It uses a pointer to the image info struct, suitable for cases where the info is pre-allocated.

func CreateMockContainerWithLinks(
	id string,
	name string,
	image string,
	created time.Time,
	links []string,
	imageInfo *dockerImage.InspectResponse,
) types.Container

CreateMockContainerWithLinks creates a container with specified links for testing. It is useful for testing dependency-related behaviors in Watchtower.

func CreateMockImageInfo

func CreateMockImageInfo(mockImage string) *dockerImage.InspectResponse

CreateMockImageInfo returns a mock image info struct based on the passed image. It provides a minimal image representation for testing purposes.

func CreateMockProgressReport

func CreateMockProgressReport(states ...session.State) types.Report

CreateMockProgressReport generates a mock report from a given set of container states. It assigns each container a unique ID and name based on its state and index, simulating various update outcomes for testing session progress reporting.

Types

type MockClient

type MockClient struct {
	TestData *TestData

	Stopped map[string]bool // Track stopped containers by ID.
	// contains filtered or unexported fields
}

MockClient is a mock implementation of a Watchtower Client for testing purposes. It simulates container operations with configurable behavior defined by TestData.

func CreateMockClient

func CreateMockClient(data *TestData, pullImages, removeVolumes bool) MockClient

CreateMockClient constructs a new MockClient instance for testing. It initializes the client with provided test data, pull and volume removal flags, and an empty map for tracking stopped containers.

func CreateMockClientWithContext

func CreateMockClientWithContext(
	ctx context.Context,
	data *TestData,
	pullImages bool,
	removeVolumes bool,
) MockClient

CreateMockClientWithContext constructs a new MockClient instance with context for testing. It initializes the client with provided context, test data, pull and volume removal flags, and an empty map for tracking stopped containers.

func (MockClient) ExecuteCommand

func (client MockClient) ExecuteCommand(
	ctx context.Context,
	_ types.Container,
	command string,
	_ int,
	_ int,
	_ int,
) (bool, error)

ExecuteCommand simulates executing a command in a container for testing lifecycle hooks. It returns a SkipUpdate boolean indicating whether to skip the update and an error if the command fails. The method uses predefined command behaviors to mimic real execution outcomes.

func (MockClient) GetContainer

func (client MockClient) GetContainer(_ context.Context, containerID types.ContainerID) (types.Container, error)

GetContainer returns the container with the specified ID from TestData. It provides a mock response for testing container retrieval.

func (MockClient) GetCurrentWatchtowerContainer

func (client MockClient) GetCurrentWatchtowerContainer(_ context.Context, containerID types.ContainerID) (types.Container, error)

GetCurrentWatchtowerContainer returns the container with the specified ID from TestData with imageInfo set to nil. It provides a mock response for testing current Watchtower container retrieval.

func (MockClient) GetInfo

func (client MockClient) GetInfo(ctx context.Context) (map[string]any, error)

GetInfo returns mock system information for testing. It provides a basic map with mock Docker/Podman info.

func (MockClient) GetVersion

func (client MockClient) GetVersion() string

GetVersion returns a mock Docker API client version. It provides a static version string for testing purposes.

func (MockClient) IsContainerRunning

func (client MockClient) IsContainerRunning(c types.Container) bool

IsContainerRunning checks if a container is running based on the Stopped map. It returns true if the container's ID is not marked as stopped, false otherwise.

func (MockClient) IsContainerStale

func (client MockClient) IsContainerStale(
	ctx context.Context,
	container types.Container,
	_ types.UpdateParams,
) (bool, types.ImageID, error)

IsContainerStale determines if a container is stale based on TestData's Staleness map. It returns true if the container's name isn't explicitly marked as fresh, along with an empty ImageID and no error. If IsContainerStaleError is set, it returns that error instead.

func (MockClient) ListContainers

func (client MockClient) ListContainers(ctx context.Context, filter ...types.Filter) ([]types.Container, error)

ListContainers returns containers from TestData, optionally filtered.

func (MockClient) RemoveContainer

func (client MockClient) RemoveContainer(ctx context.Context, _ types.Container) error

RemoveContainer simulates removing a container. It returns nil to indicate success.

func (MockClient) RemoveImageByID

func (client MockClient) RemoveImageByID(ctx context.Context, imageID types.ImageID, _ string) error

RemoveImageByID increments the count of image removal attempts in TestData. It simulates image cleanup and returns configured error if set or if image ID is in FailedImageIDs, nil otherwise.

func (MockClient) RenameContainer

func (client MockClient) RenameContainer(ctx context.Context, _ types.Container, _ string) error

RenameContainer simulates renaming a container, incrementing the RenameContainerCount. It returns nil to indicate success without modifying any state.

func (MockClient) StartContainer

func (client MockClient) StartContainer(ctx context.Context, c types.Container) (types.ContainerID, error)

StartContainer simulates starting a container, returning the container's ID. It provides a minimal implementation for testing purposes. Returns the configured StartContainerError if set.

func (MockClient) StopAndRemoveContainer

func (client MockClient) StopAndRemoveContainer(ctx context.Context, c types.Container, timeout time.Duration) error

StopAndRemoveContainer simulates stopping and removing a container by calling StopContainer followed by RemoveContainer. It properly simulates the stop-and-remove operation sequence while respecting error conditions.

func (MockClient) StopContainer

func (client MockClient) StopContainer(ctx context.Context, c types.Container, _ time.Duration) error

StopContainer simulates stopping a container by marking it in the Stopped map. It records the container's ID as stopped, increments the StopContainerCount, and returns nil for simplicity.

func (MockClient) UpdateContainer

func (client MockClient) UpdateContainer(ctx context.Context, _ types.Container, _ dockerContainer.UpdateConfig) error

UpdateContainer simulates updating a container's configuration. It increments the UpdateContainerCount and returns the configured error if set.

func (MockClient) WaitForContainerHealthy

func (client MockClient) WaitForContainerHealthy(ctx context.Context, _ types.ContainerID, _ time.Duration) error

WaitForContainerHealthy simulates waiting for a container to become healthy. It increments the count and returns nil to indicate success.

func (MockClient) WarnOnHeadPullFailed

func (client MockClient) WarnOnHeadPullFailed(_ types.Container) bool

WarnOnHeadPullFailed always returns true for the mock client. It simulates a warning condition for HEAD pull failures in tests.

type TestData

type TestData struct {
	TriedToRemoveImageCount      atomic.Int32                          // Number of times RemoveImageByID was called.
	RenameContainerCount         atomic.Int32                          // Number of times RenameContainer was called.
	StopContainerCount           atomic.Int32                          // Number of times StopContainer was called.
	StartContainerCount          atomic.Int32                          // Number of times StartContainer was called.
	UpdateContainerCount         atomic.Int32                          // Number of times UpdateContainer was called.
	IsContainerStaleCount        atomic.Int32                          // Number of times IsContainerStale was called.
	WaitForContainerHealthyCount atomic.Int32                          // Number of times WaitForContainerHealthy was called.
	NameOfContainerToKeep        string                                // Name of the container to avoid stopping.
	Containers                   []types.Container                     // List of mock containers.
	ContainersByID               map[types.ContainerID]types.Container // Map of containers by ID.
	Staleness                    map[string]bool                       // Map of container names to staleness status.
	IsContainerStaleError        error                                 // Error to return from IsContainerStale (for testing).
	ListContainersError          error                                 // Error to return from ListContainers (for testing).
	StopContainerError           error                                 // Error to return from StopContainer (for testing).
	StartContainerError          error                                 // Error to return from StartContainer (for testing).
	UpdateContainerError         error                                 // Error to return from UpdateContainer (for testing).
	StopContainerFailCount       int                                   // Number of times StopContainer should fail before succeeding.
	RemoveImageError             error                                 // Error to return from RemoveImageByID (for testing).
	FailedImageIDs               []types.ImageID                       // List of image IDs that should fail removal.
	StopOrder                    []string                              // Order in which containers were stopped.
	StartOrder                   []string                              // Order in which containers were started.
	SimulatedLatency             time.Duration                         // Simulated latency for operations (default 0 for fast tests, set for context cancellation tests).
}

TestData holds configuration data for MockClient's test behavior. It defines container states, staleness, and mock operation results. Counter fields use atomic.Int32 for thread-safe concurrent access.

func (*TestData) TriedToRemoveImage

func (testdata *TestData) TriedToRemoveImage() bool

TriedToRemoveImage checks if RemoveImageByID has been invoked. It returns true if the count is greater than zero, aiding test assertions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL