fake

package
v1.0.39 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package fake is an in-memory implementation of podman.Client used by tests. It models pods, secrets, and volumes as maps keyed by host ID.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CopyCall

type CopyCall struct {
	Host      string
	Container string
	DestDir   string
	Name      string
	Content   []byte
}

CopyCall records one CopyToContainer invocation for assertions.

type ExecCall

type ExecCall struct {
	Host      string
	Container string
	Cmd       []string
}

ExecCall records one ContainerExec invocation for assertions.

type Fake

type Fake struct {

	// SecretData stores the raw bytes passed to SecretCreate, keyed by
	// (hostID, name). Tests can decode them to assert the wrapped K8s Secret
	// structure (metadata.name, data keys).
	SecretData map[string]map[string][]byte // hostID -> name -> raw value bytes

	// Optional hooks for tests that want to inject errors.
	PlayKubeErr error
	// PlayKubePodStatus overrides the status assigned to pods created by
	// PlayKube. Empty means "Running". Lets a test force a played pod to stay
	// un-healthy so a verify-poll times out.
	PlayKubePodStatus string
	// PlayKubeContainerStatus overrides the status of containers created by
	// PlayKube. Empty means "Running". Lets a test make a pod report Running
	// while a container is not, exercising the migrate container-level verify.
	PlayKubeContainerStatus string
	// PlayKubeContainerHealth sets the Health status of containers created by
	// PlayKube (default "" = no healthcheck declared). Lets a test drive the
	// migrate readiness gate.
	PlayKubeContainerHealth string
	// ExportErr, if non-nil, makes VolumeExport fail immediately.
	ExportErr error
	// ImportErr, if non-nil, makes VolumeImport fail immediately (without
	// reading the supplied reader) — models a destination that rejects the import.
	ImportErr error
	// ImportTransform, if non-nil, rewrites the bytes VolumeImport stores on the
	// destination — lets a test simulate a lossy/corrupting copy so the source
	// and dest manifests diverge.
	ImportTransform func(host, name string, in []byte) []byte
	// ExportReader, if non-nil, overrides VolumeExport's reader. Lets a test
	// supply a stream that errors mid-transfer.
	ExportReader func(host, name string) io.ReadCloser
	// Prune hooks. PruneReports maps a scope ("images","containers","buildcache",
	// "volumes") to the report ImagePrune/etc. should return; absent → empty report.
	// PruneErr maps a scope to an error to return. PruneCalls records every call.
	PruneReports map[string]podman.PruneReport
	PruneErr     map[string]error
	PruneCalls   []PruneCall
	// Unknown lists hosts that Knows should report as not registered; nil means
	// every host is known. Lets a test exercise the scheduler's unknown-host skip.
	Unknown map[string]bool
	// SetHostsCalls records every host list passed to SetHosts, in order.
	SetHostsCalls [][]config.Host
	// VersionStr overrides the version Version reports; empty means "fake-1.0".
	VersionStr string

	// PullErr, if non-nil, makes ImagePull return this error for matching refs.
	// Key is image ref; the empty key matches any ref.
	PullErr map[string]error
	// PullCalls records every (host, image) pair passed to ImagePull, along
	// with the deadline (if any) on the context ImagePull was actually called
	// with. Deadline/HasDeadline let a test assert how tightly the caller
	// bounded the pull (e.g. applyImagePullTimeout in internal/instance)
	// independent of any package-level ImagePull timeout override.
	PullCalls []struct {
		Host, Image string
		Deadline    time.Time
		HasDeadline bool
	}
	// PlayCalls records every PlayKube invocation (host, replace, networks).
	PlayCalls []PlayCall
	// PodListErr, if non-nil, makes PodList return this error.
	PodListErr error

	// LogLines, if set, are emitted in order by ContainerLogs before the
	// channel closes. Lets tests exercise the streaming response paths.
	LogLines []podman.LogLine
	// ContainerLogsErr, if non-nil, makes ContainerLogs return this error
	// instead of a channel.
	ContainerLogsErr error
	// UsedHostPortsErr, if non-nil, makes UsedHostPorts return this error.
	UsedHostPortsErr error
	// HostBoundPortsErr, if non-nil, makes HostBoundPorts return this error.
	HostBoundPortsErr error

	// PodInspectErr, if non-nil, makes PodInspect return this error (use a
	// non-ErrNotFound error to exercise the unexpected-backend-error paths).
	PodInspectErr error
	// VolumeInspectErr, if non-nil, makes VolumeInspect return this error (use a
	// non-ErrNotFound error to exercise the transient-failure path).
	VolumeInspectErr error
	// SecretCreateErr, if non-nil, makes SecretCreate return this error
	// instead of recording the secret.
	SecretCreateErr error
	// LifecycleErr, if non-nil, makes PodStart/PodStop/PodRestart fail while
	// leaving the pod in place, to exercise action-failure paths.
	LifecycleErr error
	// HostInfoVal is returned by HostInfo when HostInfoErr is nil.
	HostInfoVal podman.HostInfo
	// HostInfoErr, if non-nil, makes HostInfo return this error.
	HostInfoErr error
	// HostInfoCalls counts HostInfo invocations (lets a test assert probe throttling).
	HostInfoCalls int

	// HostUptimeVal is returned by HostUptime when HostUptimeErr is nil.
	HostUptimeVal time.Duration
	// HostUptimeOK is HostUptime's second return value (false = uptime unknown).
	HostUptimeOK bool
	// HostUptimeErr, if non-nil, makes HostUptime return this error.
	HostUptimeErr error
	// HostUptimeCalls counts HostUptime invocations.
	HostUptimeCalls int

	// ContainerStatsVal is returned by ContainerStats, keyed by host ID.
	ContainerStatsVal map[string][]podman.ContainerStats
	// ContainerStatsErr, if non-nil, makes ContainerStats return this error.
	ContainerStatsErr error
	// ContainerStatsCalls counts ContainerStats invocations.
	ContainerStatsCalls int

	// VolumeUsageVal is returned by VolumeUsage, keyed by host ID then volume name.
	VolumeUsageVal map[string]map[string]int64
	// VolumeUsageErr, if non-nil, makes VolumeUsage return this error.
	VolumeUsageErr error
	// VolumeUsageCalls counts VolumeUsage invocations.
	VolumeUsageCalls int

	// NetworkEnsureCalls records, per host, the network names ensured.
	NetworkEnsureCalls map[string][]string
	// NetworkEnsureErr, if non-nil, makes NetworkEnsure fail.
	NetworkEnsureErr error

	// ExecFunc, if set, produces the ContainerExec result for tests. Default
	// (nil) returns ExitCode 0, empty output.
	ExecFunc func(host, container string, cmd []string) (podman.ExecResult, error)
	// ExecCalls records every ContainerExec invocation.
	ExecCalls []ExecCall

	// CopyCalls records every CopyToContainer invocation.
	CopyCalls []CopyCall
	// CopyErr, if non-nil, makes CopyToContainer fail.
	CopyErr error
	// contains filtered or unexported fields
}

Fake is a thread-safe in-memory podman.Client.

func New

func New() *Fake

New returns a fresh fake.

func (*Fake) AddHostBoundPort added in v1.0.36

func (f *Fake) AddHostBoundPort(host, protocol string, port int)

AddHostBoundPort registers port as bound on host for protocol ("tcp" or "udp"), for HostBoundPorts to report back — e.g. modeling vedanta's native charon holding udp/500 exclusively, outside podman's own visibility.

func (*Fake) AddPod

func (f *Fake) AddPod(host string, p podman.Pod)

AddPod seeds a pod on a host (with whatever container ports it carries), so a test can occupy host ports or pre-place an instance. Test-only.

func (*Fake) AddVolume

func (f *Fake) AddVolume(host string, v podman.Volume)

AddVolume seeds a volume on a host so VolumeInspect resolves it. Test-only.

func (*Fake) BuildCachePrune

func (f *Fake) BuildCachePrune(_ context.Context, host string) (podman.PruneReport, error)

func (*Fake) ContainerExec

func (f *Fake) ContainerExec(_ context.Context, host, container string, cmd []string) (podman.ExecResult, error)

func (*Fake) ContainerLogs

func (f *Fake) ContainerLogs(_ context.Context, _, _ string, _ podman.LogOptions) (<-chan podman.LogLine, error)

func (*Fake) ContainerPrune

func (f *Fake) ContainerPrune(_ context.Context, host string) (podman.PruneReport, error)

func (*Fake) ContainerStats added in v1.0.28

func (f *Fake) ContainerStats(_ context.Context, h string) ([]podman.ContainerStats, error)

func (*Fake) CopyToContainer

func (f *Fake) CopyToContainer(_ context.Context, host, container, destDir, name string, content []byte) error

func (*Fake) FailPodListFor added in v1.0.17

func (f *Fake) FailPodListFor(tmplID string, err error)

FailPodListFor makes PodList return err whenever it is called with a "podman-api/template" filter equal to tmplID. Test-only.

func (*Fake) HostBoundPorts added in v1.0.36

func (f *Fake) HostBoundPorts(_ context.Context, h, protocol string) ([]int, error)

HostBoundPorts returns the ports a test has registered via AddHostBoundPort for (h, protocol) — standing in for a real /proc/net/<protocol> read. Models a plain host-level bind podman itself has no visibility into (a native systemd service, e.g.), distinct from UsedHostPorts' podman-managed container ports above.

func (*Fake) HostInfo

func (f *Fake) HostInfo(_ context.Context, _ string) (podman.HostInfo, error)

func (*Fake) HostUptime added in v1.0.36

func (f *Fake) HostUptime(_ context.Context, _ string) (time.Duration, bool, error)

func (*Fake) ImagePrune

func (f *Fake) ImagePrune(_ context.Context, host string, all bool) (podman.PruneReport, error)

func (*Fake) ImagePull

func (f *Fake) ImagePull(ctx context.Context, host, ref string) error

func (*Fake) Knows

func (f *Fake) Knows(id string) bool

Knows reports a host as registered unless it's listed in Unknown.

func (*Fake) NetworkEnsure

func (f *Fake) NetworkEnsure(_ context.Context, host, name string) error

func (*Fake) Ping

func (f *Fake) Ping(_ context.Context, _ string) error

func (*Fake) PlayKube

func (f *Fake) PlayKube(_ context.Context, hostID, raw string, replace bool, networks ...string) error

func (*Fake) PodInspect

func (f *Fake) PodInspect(_ context.Context, h, name string) (podman.Pod, error)

func (*Fake) PodList

func (f *Fake) PodList(_ context.Context, h string, filters map[string]string) ([]podman.Pod, error)

func (*Fake) PodListCallCount added in v1.0.17

func (f *Fake) PodListCallCount() int

PodListCallCount returns the number of PodList invocations so far.

func (*Fake) PodRemove

func (f *Fake) PodRemove(_ context.Context, h, name string, _ bool) error

func (*Fake) PodRestart

func (f *Fake) PodRestart(_ context.Context, h, name string) error

func (*Fake) PodStart

func (f *Fake) PodStart(_ context.Context, h, name string) error

func (*Fake) PodStop

func (f *Fake) PodStop(_ context.Context, h, name string) error

func (*Fake) SecretCreate

func (f *Fake) SecretCreate(_ context.Context, h, name string, value []byte) error

func (*Fake) SecretInspect

func (f *Fake) SecretInspect(_ context.Context, h, name string) (podman.Secret, error)

func (*Fake) SecretList

func (f *Fake) SecretList(_ context.Context, h string) ([]podman.Secret, error)

func (*Fake) SecretRemove

func (f *Fake) SecretRemove(_ context.Context, h, name string) error

func (*Fake) SetHosts

func (f *Fake) SetHosts(hosts []config.Host)

SetHosts does not change behaviour — the fake has no persistent host map and services any host ID — but it RECORDS the call in SetHostsCalls. Behaviour being a no-op is exactly why a caller that forgets to propagate a host-list change to the real client (podman.Real keeps its own map) is invisible to every fake-backed test; the recorder makes that observable.

func (*Fake) SetVolumeData

func (f *Fake) SetVolumeData(host, name string, data []byte)

SetVolumeData seeds a volume and its contents on a host. Test-only.

func (*Fake) UsedHostPorts

func (f *Fake) UsedHostPorts(_ context.Context, h string) ([]podman.PortMapping, error)

func (*Fake) Version

func (f *Fake) Version(_ context.Context, _ string) (string, error)

func (*Fake) VolumeCreate

func (f *Fake) VolumeCreate(_ context.Context, h, name string) error

func (*Fake) VolumeData

func (f *Fake) VolumeData(host, name string) []byte

VolumeData returns the stored contents of a volume (nil if none). Test-only.

func (*Fake) VolumeExport

func (f *Fake) VolumeExport(_ context.Context, h, name string) (io.ReadCloser, error)

func (*Fake) VolumeImport

func (f *Fake) VolumeImport(_ context.Context, h, name string, r io.Reader) error

func (*Fake) VolumeInspect

func (f *Fake) VolumeInspect(_ context.Context, h, name string) (podman.Volume, error)

func (*Fake) VolumePrune

func (f *Fake) VolumePrune(_ context.Context, host string, filters map[string][]string) (podman.PruneReport, error)

func (*Fake) VolumeRemove

func (f *Fake) VolumeRemove(_ context.Context, h, name string, _ bool) error

func (*Fake) VolumeUsage added in v1.0.28

func (f *Fake) VolumeUsage(_ context.Context, h string) (map[string]int64, error)

func (*Fake) WaitForPodCompletion added in v1.0.35

func (f *Fake) WaitForPodCompletion(_ context.Context, h, name string, _ time.Duration) (int, error)

WaitForPodCompletion computes its result from the containers already recorded on the pod: every container must have Exited=true to succeed, returning the first non-zero ExitCode found (else 0). Any container still not Exited is treated as never finishing within the fake's synchronous model, so it returns podman.ErrWaitTimeout immediately regardless of the requested timeout — tests drive this by seeding pod/container state via AddPod rather than by waiting on a clock.

type PlayCall

type PlayCall struct {
	Host     string
	Replace  bool
	Networks []string
	YAML     string
}

PlayCall records one PlayKube invocation for assertions.

type PruneCall

type PruneCall struct {
	Host    string
	Scope   string              // "images" | "containers" | "buildcache" | "volumes"
	All     bool                // ImagePrune only
	Filters map[string][]string // VolumePrune only
}

PruneCall records one prune invocation for assertions.

Jump to

Keyboard shortcuts

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