fake

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 16 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	DefaultRegion         = "us-east"
	DefaultClusterID      = 12345
	DefaultClusterName    = "test-cluster"
	DefaultClusterTier    = linodego.LKEVersionStandard
	DefaultClusterVersion = "1.31"
)

Variables

This section is empty.

Functions

func InstanceID

func InstanceID() int

func MakeInstanceOfferings

func MakeInstanceOfferings(types []linodego.LinodeType) []linodego.RegionAvailability

func MakeInstances

func MakeInstances() []linodego.LinodeType

func NewLinodeAPIError added in v0.1.8

func NewLinodeAPIError(statusCode int, reasons ...linodego.APIErrorReason) *linodego.Error

NewLinodeAPIError returns a fake Linode API error with structured response details.

func NewPlanUnavailableError added in v0.1.8

func NewPlanUnavailableError(instanceType string) *linodego.Error

NewPlanUnavailableError returns a structured error for an unavailable LKE plan.

func PlanUnavailableReason added in v0.1.8

func PlanUnavailableReason(instanceType string) linodego.APIErrorReason

PlanUnavailableReason returns the API error reason for an unavailable LKE plan.

func ProviderID

func ProviderID(id int) string

func RandomProviderID

func RandomProviderID() string

Types

type AtomicError

type AtomicError struct {
	// contains filtered or unexported fields
}

func (*AtomicError) Get

func (e *AtomicError) Get() error

Get is equivalent to the error being called, so we increase number of calls in this function

func (*AtomicError) IsNil

func (e *AtomicError) IsNil() bool

func (*AtomicError) Reset

func (e *AtomicError) Reset()

func (*AtomicError) Set

func (e *AtomicError) Set(err error, opts ...AtomicErrorOption)

type AtomicErrorOption

type AtomicErrorOption func(atomicError *AtomicError)

func MaxCalls

func MaxCalls(maxCalls int) AtomicErrorOption

type AtomicPtr

type AtomicPtr[T any] struct {
	// contains filtered or unexported fields
}

AtomicPtr is intended for use in mocks to easily expose variables for use in testing. It makes setting and retrieving the values race free by wrapping the pointer itself in a mutex. There is no Get() method, but instead a Clone() method deep copies the object being stored by serializing/de-serializing it from JSON. This pattern shouldn't be followed anywhere else but is an easy way to eliminate races in our tests.

func (*AtomicPtr[T]) Clone

func (a *AtomicPtr[T]) Clone() *T

func (*AtomicPtr[T]) IsNil

func (a *AtomicPtr[T]) IsNil() bool

func (*AtomicPtr[T]) Reset

func (a *AtomicPtr[T]) Reset()

func (*AtomicPtr[T]) Set

func (a *AtomicPtr[T]) Set(v *T)

type AtomicPtrSlice

type AtomicPtrSlice[T any] struct {
	// contains filtered or unexported fields
}

AtomicPtrSlice exposes a slice of a pointer type in a race-free manner. The interface is just enough to replace the set.Set usage in our previous tests.

func (*AtomicPtrSlice[T]) Add

func (a *AtomicPtrSlice[T]) Add(input *T)

func (*AtomicPtrSlice[T]) At

func (a *AtomicPtrSlice[T]) At(index int) *T

func (*AtomicPtrSlice[T]) ForEach

func (a *AtomicPtrSlice[T]) ForEach(fn func(*T))

func (*AtomicPtrSlice[T]) Len

func (a *AtomicPtrSlice[T]) Len() int

func (*AtomicPtrSlice[T]) Pop

func (a *AtomicPtrSlice[T]) Pop() *T

func (*AtomicPtrSlice[T]) Reset

func (a *AtomicPtrSlice[T]) Reset()

type CapacityPool

type CapacityPool struct {
	InstanceType string
	Region       string
}

type LinodeAPIBehavior

type LinodeAPIBehavior struct {
	ListTypesOutput               AtomicPtr[[]linodego.LinodeType]
	GetRegionAvailabilityOutput   AtomicPtr[[]linodego.RegionAvailability]
	GetTypeBehavior               MockedFunction[string, *linodego.LinodeType]
	GetRegionAvailabilityBehavior MockedFunction[string, []linodego.RegionAvailability]
	CreateInstanceBehavior        MockedFunction[linodego.InstanceCreateOptions, *linodego.Instance]
	GetInstanceBehavior           MockedFunction[int, *linodego.Instance]
	DeleteInstanceBehavior        MockedFunction[int, error]
	ListInstancesBehavior         MockedFunction[linodego.ListOptions, []linodego.Instance]
	CreateTagsBehavior            MockedFunction[linodego.TagCreateOptions, linodego.Tag]
	ListTypesBehavior             MockedFunction[linodego.ListOptions, []linodego.LinodeType]
	Instances                     sync.Map
	InsufficientCapacityPools     atomic.Slice[CapacityPool]
	// NodePool storage and behaviors
	NodePools                 sync.Map // key: "clusterID-poolID", value: *linodego.LKENodePool
	CreateLKENodePoolBehavior MockedFunction[struct {
		ClusterID int
		Opts      linodego.LKENodePoolCreateOptions
	}, *linodego.LKENodePool]
	GetLKEClusterBehavior    MockedFunction[int, *linodego.LKECluster]
	ListLKENodePoolsBehavior MockedFunction[int, []linodego.LKENodePool]
	GetLKENodePoolBehavior   MockedFunction[struct {
		ClusterName int
		PoolID      int
	}, *linodego.LKENodePool]
	UpdateLKENodePoolBehavior MockedFunction[struct {
		ClusterID int
		PoolID    int
		Opts      linodego.LKENodePoolUpdateOptions
	}, *linodego.LKENodePool]
	DeleteLKENodePoolBehavior MockedFunction[struct {
		ClusterName int
		PoolID      int
	}, error]
	DeleteLKENodePoolNodeBehavior MockedFunction[struct {
		ClusterID int
		NodeID    string
	}, error]
	UpdateInstanceBehavior MockedFunction[struct {
		LinodeID int
		Opts     linodego.InstanceUpdateOptions
	}, *linodego.Instance]
}

LinodeAPIBehavior must be reset between tests otherwise tests will pollute each other.

type LinodeClient

type LinodeClient struct {
	LinodeAPIBehavior
	ClusterTier    linodego.LKEVersionTier
	ClusterVersion string
	// AfterListLKENodePools runs after the fake has captured the list result.
	AfterListLKENodePools func(context.Context)
	// AfterListInstances runs after the fake has captured the filtered list result.
	AfterListInstances func(context.Context, *linodego.ListOptions)
}

func NewLinodeClient

func NewLinodeClient() *LinodeClient

func (*LinodeClient) CreateInstance

func (*LinodeClient) CreateLKENodePool

func (l *LinodeClient) CreateLKENodePool(_ context.Context, clusterID int, opts linodego.LKENodePoolCreateOptions) (*linodego.LKENodePool, error)

func (*LinodeClient) CreateTag

func (*LinodeClient) DeleteInstance

func (l *LinodeClient) DeleteInstance(_ context.Context, linodeID int) error

func (*LinodeClient) DeleteLKENodePool

func (l *LinodeClient) DeleteLKENodePool(_ context.Context, clusterID, poolID int) error

func (*LinodeClient) DeleteLKENodePoolNode

func (l *LinodeClient) DeleteLKENodePoolNode(_ context.Context, clusterID int, nodeID string) error

func (*LinodeClient) GetInstance

func (l *LinodeClient) GetInstance(_ context.Context, linodeID int) (*linodego.Instance, error)

func (*LinodeClient) GetLKECluster added in v0.1.5

func (l *LinodeClient) GetLKECluster(_ context.Context, clusterID int) (*linodego.LKECluster, error)

func (*LinodeClient) GetLKENodePool

func (l *LinodeClient) GetLKENodePool(_ context.Context, clusterID, poolID int) (*linodego.LKENodePool, error)

func (*LinodeClient) GetRegionAvailability added in v0.1.4

func (l *LinodeClient) GetRegionAvailability(_ context.Context, regionID string) ([]linodego.RegionAvailability, error)

func (*LinodeClient) GetType

func (l *LinodeClient) GetType(_ context.Context, typeID string) (*linodego.LinodeType, error)

func (*LinodeClient) ListInstances

func (l *LinodeClient) ListInstances(ctx context.Context, opts *linodego.ListOptions) ([]linodego.Instance, error)

func (*LinodeClient) ListLKEClusters

func (l *LinodeClient) ListLKEClusters(_ context.Context, _ *linodego.ListOptions) ([]linodego.LKECluster, error)

func (*LinodeClient) ListLKENodePools

func (l *LinodeClient) ListLKENodePools(ctx context.Context, clusterID int, opts *linodego.ListOptions) ([]linodego.LKENodePool, error)

func (*LinodeClient) ListTypes

func (*LinodeClient) Reset

func (l *LinodeClient) Reset()

func (*LinodeClient) UpdateInstance

func (l *LinodeClient) UpdateInstance(_ context.Context, linodeID int, opts linodego.InstanceUpdateOptions) (*linodego.Instance, error)

func (*LinodeClient) UpdateLKENodePool

func (l *LinodeClient) UpdateLKENodePool(_ context.Context, clusterID, poolID int, opts linodego.LKENodePoolUpdateOptions) (*linodego.LKENodePool, error)

type MockedFunction

type MockedFunction[I any, O any] struct {
	Output          AtomicPtr[O] // Output to return on call to this function
	MultiOut        AtomicPtrSlice[O]
	CalledWithInput AtomicPtrSlice[I] // Slice used to keep track of passed input to this function
	Error           AtomicError       // Error to return a certain number of times defined by custom error options
	// contains filtered or unexported fields
}

func (*MockedFunction[I, O]) Calls

func (m *MockedFunction[I, O]) Calls() int

func (*MockedFunction[I, O]) FailedCalls

func (m *MockedFunction[I, O]) FailedCalls() int

func (*MockedFunction[I, O]) Invoke

func (m *MockedFunction[I, O]) Invoke(input *I, defaultTransformer func(*I) (*O, error)) (*O, error)

func (*MockedFunction[I, O]) Reset

func (m *MockedFunction[I, O]) Reset()

Reset must be called between tests otherwise tests will pollute each other.

func (*MockedFunction[I, O]) SuccessfulCalls

func (m *MockedFunction[I, O]) SuccessfulCalls() int

Jump to

Keyboard shortcuts

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