vmstestutil

package
v0.0.0-...-c547705 Latest Latest
Warning

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

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

README

Package cloudeng.io/vms/vmstestutil

import cloudeng.io/vms/vmstestutil

Functions

Func TestInstanceCloneStartStopDelete
func TestInstanceCloneStartStopDelete(t cicd.TestingT, cfg InstanceTestConfig)

TestInstanceCloneStartStopDelete verifies the standard Clone -> Start -> Stop -> Delete state transitions.

Func TestInstanceDeleteFromSuspended
func TestInstanceDeleteFromSuspended(t cicd.TestingT, cfg InstanceTestConfig)

TestInstanceDeleteFromSuspended verifies that an instance can be deleted directly from the Suspended state.

Func TestInstanceExec
func TestInstanceExec(t cicd.TestingT, cfg InstanceTestConfig)

TestInstanceExec verifies that a command can be executed inside a running VM.

Func TestInstanceLifecycle
func TestInstanceLifecycle(t cicd.TestingT, cfg InstanceTestConfig)

TestInstanceLifecycle is a detailed lifecycle test that walks a VM through its state machine. Initial → Clone → Stopped → Start → Running → Stop → Stopped → Stop (idempotent) → Start → Running → [Suspend → Suspended → Suspend (idempotent) → Start → Running →] Stop → Stopped → Delete → Deleted

Func TestInstanceStateErrors
func TestInstanceStateErrors(t cicd.TestingT, cfg InstanceTestConfig)
Func TestInstanceSuspendResume
func TestInstanceSuspendResume(t cicd.TestingT, cfg InstanceTestConfig)

TestInstanceSuspendResume verifies the Suspend and Resume (Start) transitions for suspendable VMs.

Func TestPoolAcquireExecRelease
func TestPoolAcquireExecRelease(t cicd.TestingT, cfg PoolTestConfig)

TestPoolAcquireExecRelease verifies the full acquire → exec → release → replenish cycle: releasing a VM triggers replenishment so the pool can serve another Acquire.

Func TestPoolClose
func TestPoolClose(t cicd.TestingT, cfg PoolTestConfig)

TestPoolClose verifies that Close prevents further Acquire calls.

Func TestPoolConcurrentAcquire
func TestPoolConcurrentAcquire(t cicd.TestingT, cfg PoolTestConfig)

TestPoolConcurrentAcquire verifies that poolSize goroutines can each acquire a VM concurrently without error, and that the pool replenishes after all are released.

Func TestPoolContextCancellation
func TestPoolContextCancellation(t cicd.TestingT, cfg PoolTestConfig)

TestPoolContextCancellation verifies that Acquire returns context.Canceled when the pool is empty and the context is cancelled.

Types

Type ExecCall
type ExecCall struct {
	Cmd  string
	Args []string
}

ExecCall records a single invocation of Mock.Exec.

Type InstanceTestConfig
type InstanceTestConfig struct {
	// Constructor creates a new uninitialized vms.Instance for each test.
	Constructor vmspool.Constructor

	// Timeout caps individual operations. Defaults to 30 s.
	Timeout time.Duration

	// ExecCmd is a command that should succeed inside a running VM. If empty,
	// the Exec subtest is skipped.
	ExecCmd    string
	ExecArgs   []string
	ExecStdout string // Expected output from the exec.
	ExecStderr string // Expected stderr output from the exec.

	// RequireUnderlyingState is an optional helper for tests that need to verify
	// the underlying state of the instance, e.g. by querying a cloud provider API.
	// The function is expected to wait for the instance to reach a stable state
	RequireUnderlyingState func(
		ctx context.Context, inst vms.Instance, msg string, final vms.State, intermediate ...vms.State) error
}

InstanceTestConfig configures the test suite for an implementation of vms.Instance.

Type Mock
type Mock struct {

	// CloneBlock, if non-nil, causes Clone to block until the channel is
	// closed or the context is cancelled. Used by tests to pause a VM
	// mid-creation so the test can manipulate pool state before proceeding.
	CloneBlock chan struct{}

	CloneErr   error
	StartErr   error
	StopRunErr error
	StopErr    error
	StopState  *vms.State
	SuspendErr error
	DeleteErr  error
	ExecErr    error
	// contains filtered or unexported fields
}

Mock represents a mock virtual machine instance for testing.

Functions
func NewMock(id string) *Mock

NewMock creates a new Mock VM instance.

Methods
func (m *Mock) Clone(ctx context.Context) error
func (m *Mock) Delete(_ context.Context) error
func (m *Mock) Exec(_ context.Context, _, _ io.Writer, cmd string, args ...string) error
func (m *Mock) ExecCalls() []ExecCall

ExecCalls returns all recorded Exec invocations.

func (m *Mock) ID() string
func (m *Mock) Properties(_ context.Context) (vms.Properties, error)
func (m *Mock) SetProperties(props vms.Properties)
func (m *Mock) SetState(state vms.State)
func (m *Mock) SetSuspendable(suspendable bool)
func (m *Mock) Start(ctx context.Context, _, _ io.Writer) error
func (m *Mock) State(_ context.Context) vms.State
func (m *Mock) Stop(_ context.Context, _ time.Duration) (error, error)
func (m *Mock) Suspend(_ context.Context) error
func (m *Mock) Suspendable() bool
Type MockFactory
type MockFactory struct {
	// contains filtered or unexported fields
}

MockFactory creates and tracks Mock instances for pool and integration tests. Use Inject to pre-supply configured mocks; otherwise MockFactory.New creates plain NewMock instances on demand.

Functions
func NewMockFactory(suspendable bool) *MockFactory

NewMockFactory returns an empty MockFactory.

Methods
func (f *MockFactory) Inject(m *Mock)

Inject queues m to be returned by the next New call instead of a freshly allocated Mock. Useful for injecting pre-configured error states.

func (f *MockFactory) Mocks() []*Mock

Mocks returns a snapshot of all Mock instances produced so far.

func (f *MockFactory) New() vms.Instance
Type PoolTestConfig
type PoolTestConfig struct {
	// Constructor creates new VM instances. Required.
	Constructor vmspool.Constructor

	// PoolSize is the default pool size used across all tests. Defaults to 2.
	// Some subtests intentionally use a size-1 pool for deterministic behavior.
	PoolSize int

	// ExecCmd is a command that should succeed inside an acquired VM. If empty
	// the Exec subtest is skipped.
	ExecCmd          string
	ExecArgs         []string
	ExecStdoutOutput string // Expected output from the exec.
	ExecStderrOutput string // Expected stderr output from the exec.

	StdoutRWC func(string) io.Writer // Optional factory
	StderrRWC func(string) io.Writer // Optional factory for stderr RWC used by Exec; defaults to bytes.Buffer-based implementation.

	// Timeout caps individual pool operations. Defaults to 30 s.
	Timeout time.Duration

	// StagingBehaviour determines the pool's staging behaviour. Defaults to
	// StagingBehaviourRunning.
	StagingBehaviour vmspool.StagingBehaviour
}

PoolTestConfig configures the pool integration test suite run by RunPoolTests.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TestInstanceCloneStartStopDelete

func TestInstanceCloneStartStopDelete(t cicd.TestingT, cfg InstanceTestConfig)

TestInstanceCloneStartStopDelete verifies the standard Clone -> Start -> Stop -> Delete state transitions.

func TestInstanceDeleteFromSuspended

func TestInstanceDeleteFromSuspended(t cicd.TestingT, cfg InstanceTestConfig)

TestInstanceDeleteFromSuspended verifies that an instance can be deleted directly from the Suspended state.

func TestInstanceExec

func TestInstanceExec(t cicd.TestingT, cfg InstanceTestConfig)

TestInstanceExec verifies that a command can be executed inside a running VM.

func TestInstanceLifecycle

func TestInstanceLifecycle(t cicd.TestingT, cfg InstanceTestConfig)

TestInstanceLifecycle is a detailed lifecycle test that walks a VM through its state machine. Initial → Clone → Stopped → Start → Running → Stop → Stopped → Stop (idempotent) → Start → Running → [Suspend → Suspended → Suspend (idempotent) → Start → Running →] Stop → Stopped → Delete → Deleted

func TestInstanceStateErrors

func TestInstanceStateErrors(t cicd.TestingT, cfg InstanceTestConfig)

func TestInstanceSuspendResume

func TestInstanceSuspendResume(t cicd.TestingT, cfg InstanceTestConfig)

TestInstanceSuspendResume verifies the Suspend and Resume (Start) transitions for suspendable VMs.

func TestPoolAcquireExecRelease

func TestPoolAcquireExecRelease(t cicd.TestingT, cfg PoolTestConfig)

TestPoolAcquireExecRelease verifies the full acquire → exec → release → replenish cycle: releasing a VM triggers replenishment so the pool can serve another Acquire.

func TestPoolClose

func TestPoolClose(t cicd.TestingT, cfg PoolTestConfig)

TestPoolClose verifies that Close prevents further Acquire calls.

func TestPoolConcurrentAcquire

func TestPoolConcurrentAcquire(t cicd.TestingT, cfg PoolTestConfig)

TestPoolConcurrentAcquire verifies that poolSize goroutines can each acquire a VM concurrently without error, and that the pool replenishes after all are released.

func TestPoolContextCancellation

func TestPoolContextCancellation(t cicd.TestingT, cfg PoolTestConfig)

TestPoolContextCancellation verifies that Acquire returns context.Canceled when the pool is empty and the context is cancelled.

Types

type ExecCall

type ExecCall struct {
	Cmd  string
	Args []string
}

ExecCall records a single invocation of Mock.Exec.

type InstanceTestConfig

type InstanceTestConfig struct {
	// Constructor creates a new uninitialized vms.Instance for each test.
	Constructor vmspool.Constructor

	// Timeout caps individual operations. Defaults to 30 s.
	Timeout time.Duration

	// ExecCmd is a command that should succeed inside a running VM. If empty,
	// the Exec subtest is skipped.
	ExecCmd    string
	ExecArgs   []string
	ExecStdout string // Expected output from the exec.
	ExecStderr string // Expected stderr output from the exec.

	// RequireUnderlyingState is an optional helper for tests that need to verify
	// the underlying state of the instance, e.g. by querying a cloud provider API.
	// The function is expected to wait for the instance to reach a stable state
	RequireUnderlyingState func(
		ctx context.Context, inst vms.Instance, msg string, final vms.State, intermediate ...vms.State) error
}

InstanceTestConfig configures the test suite for an implementation of vms.Instance.

type Mock

type Mock struct {

	// CloneBlock, if non-nil, causes Clone to block until the channel is
	// closed or the context is cancelled. Used by tests to pause a VM
	// mid-creation so the test can manipulate pool state before proceeding.
	CloneBlock chan struct{}

	CloneErr   error
	StartErr   error
	StopRunErr error
	StopErr    error
	StopState  *vms.State
	SuspendErr error
	DeleteErr  error
	ExecErr    error
	// contains filtered or unexported fields
}

Mock represents a mock virtual machine instance for testing.

func NewMock

func NewMock(id string) *Mock

NewMock creates a new Mock VM instance.

func (*Mock) Clone

func (m *Mock) Clone(ctx context.Context) error

func (*Mock) Delete

func (m *Mock) Delete(_ context.Context) error

func (*Mock) Exec

func (m *Mock) Exec(_ context.Context, _, _ io.Writer, cmd string, args ...string) error

func (*Mock) ExecCalls

func (m *Mock) ExecCalls() []ExecCall

ExecCalls returns all recorded Exec invocations.

func (*Mock) ID

func (m *Mock) ID() string

func (*Mock) Properties

func (m *Mock) Properties(_ context.Context) (vms.Properties, error)

func (*Mock) SetProperties

func (m *Mock) SetProperties(props vms.Properties)

func (*Mock) SetState

func (m *Mock) SetState(state vms.State)

func (*Mock) SetSuspendable

func (m *Mock) SetSuspendable(suspendable bool)

func (*Mock) Start

func (m *Mock) Start(ctx context.Context, _, _ io.Writer) error

func (*Mock) State

func (m *Mock) State(_ context.Context) vms.State

func (*Mock) Stop

func (m *Mock) Stop(_ context.Context, _ time.Duration) (error, error)

func (*Mock) Suspend

func (m *Mock) Suspend(_ context.Context) error

func (*Mock) Suspendable

func (m *Mock) Suspendable() bool

type MockFactory

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

MockFactory creates and tracks Mock instances for pool and integration tests. Use Inject to pre-supply configured mocks; otherwise MockFactory.New creates plain NewMock instances on demand.

func NewMockFactory

func NewMockFactory(suspendable bool) *MockFactory

NewMockFactory returns an empty MockFactory.

func (*MockFactory) Inject

func (f *MockFactory) Inject(m *Mock)

Inject queues m to be returned by the next New call instead of a freshly allocated Mock. Useful for injecting pre-configured error states.

func (*MockFactory) Mocks

func (f *MockFactory) Mocks() []*Mock

Mocks returns a snapshot of all Mock instances produced so far.

func (*MockFactory) New

func (f *MockFactory) New() vms.Instance

type PoolTestConfig

type PoolTestConfig struct {
	// Constructor creates new VM instances. Required.
	Constructor vmspool.Constructor

	// PoolSize is the default pool size used across all tests. Defaults to 2.
	// Some subtests intentionally use a size-1 pool for deterministic behavior.
	PoolSize int

	// ExecCmd is a command that should succeed inside an acquired VM. If empty
	// the Exec subtest is skipped.
	ExecCmd          string
	ExecArgs         []string
	ExecStdoutOutput string // Expected output from the exec.
	ExecStderrOutput string // Expected stderr output from the exec.

	StdoutRWC func(string) io.Writer // Optional factory
	StderrRWC func(string) io.Writer // Optional factory for stderr RWC used by Exec; defaults to bytes.Buffer-based implementation.

	// Timeout caps individual pool operations. Defaults to 30 s.
	Timeout time.Duration

	// StagingBehaviour determines the pool's staging behaviour. Defaults to
	// StagingBehaviourRunning.
	StagingBehaviour vmspool.StagingBehaviour
}

PoolTestConfig configures the pool integration test suite run by RunPoolTests.

Jump to

Keyboard shortcuts

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