mock

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package mock provides in-memory hal.HAL implementations for use in tests and simulation environments.

All fields are exported so tests can set initial state and inspect results directly. Methods are safe for concurrent use only if the test sets fields before calling [NewHAL] and does not mutate them afterwards; for race-free concurrent tests, protect shared state with a mutex in a custom sub-struct.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chassis

type Chassis struct {
	On              bool
	Intruded        bool
	ColdResets      int
	WarmResets      int
	PowerCycles     int
	LastIdentifySec uint8
	BootFlags       *types.BootOptionParam_BootFlags
	BootInfoAck     *types.BootOptionParam_BootInfoAcknowledge

	// Hook allows tests to inject custom behaviour.
	SetPowerHook func(on bool) error
	// contains filtered or unexported fields
}

Chassis is the mock hal.ChassisHAL.

func (*Chassis) ColdReset

func (c *Chassis) ColdReset(_ context.Context) error

func (*Chassis) GetBootFlags

GetBootFlags returns the last stored boot flags, or hal.ErrNotSupported when none have been set.

func (*Chassis) GetBootInfoAcknowledge

func (c *Chassis) GetBootInfoAcknowledge(_ context.Context) (*types.BootOptionParam_BootInfoAcknowledge, error)

GetBootInfoAcknowledge returns the last stored acknowledge data, or a default value when none have been stored.

func (*Chassis) Identify

func (c *Chassis) Identify(_ context.Context, seconds uint8) error

func (*Chassis) IntrusionState

func (c *Chassis) IntrusionState(_ context.Context) (bool, error)

func (*Chassis) PowerCycle

func (c *Chassis) PowerCycle(_ context.Context) error

PowerCycle performs a power cycle. The mock counts calls independently so tests can assert that Chassis Control action 0x02 dispatched here rather than to ColdReset. Real noop HALs that lack a distinct power-cycle operation may delegate to ColdReset; the mock keeps the counters separate for clarity.

func (*Chassis) PowerState

func (c *Chassis) PowerState(_ context.Context) (bool, error)

func (*Chassis) SetBootFlags

func (c *Chassis) SetBootFlags(_ context.Context, flags *types.BootOptionParam_BootFlags) error

SetBootFlags stores the full boot flags structure so tests and the reference server can round-trip Set/Get System Boot Options.

func (*Chassis) SetBootInfoAcknowledge

func (c *Chassis) SetBootInfoAcknowledge(_ context.Context, ack *types.BootOptionParam_BootInfoAcknowledge) error

SetBootInfoAcknowledge stores the boot info acknowledge data.

func (*Chassis) SetPower

func (c *Chassis) SetPower(_ context.Context, on bool) error

func (*Chassis) WarmReset

func (c *Chassis) WarmReset(_ context.Context) error

type Console added in v0.9.1

type Console struct {

	// Conn is returned by Open when OpenHook is nil. Nil means "no console
	// hardware": Open then returns hal.ErrNotSupported.
	Conn hal.ConsoleConn

	// OpenHook allows tests to inject custom behaviour (e.g. fail the open).
	OpenHook func(ctx context.Context) (hal.ConsoleConn, error)

	// OpenCount records how many activations attached to the console.
	OpenCount int
	// contains filtered or unexported fields
}

Console is the mock hal.ConsoleHAL.

func (*Console) Open added in v0.9.1

func (c *Console) Open(ctx context.Context) (hal.ConsoleConn, error)

type FakeConsoleConn added in v0.9.1

type FakeConsoleConn struct {

	// RX holds console output waiting to be drained by ReadAvailable.
	RX []byte
	// TX records everything written toward the system console.
	TX []byte

	Closed   bool
	Breaks   int
	WriteErr error
	ReadErr  error

	// WriteLimit, when > 0, caps how many bytes a single Write accepts,
	// forcing the partial accepts (and NACKs) a flow-controlled serial
	// controller produces.
	WriteLimit int
	// contains filtered or unexported fields
}

FakeConsoleConn is an in-memory hal.ConsoleConn for tests.

func (*FakeConsoleConn) Close added in v0.9.1

func (f *FakeConsoleConn) Close() error

func (*FakeConsoleConn) FeedRX added in v0.9.1

func (f *FakeConsoleConn) FeedRX(p []byte)

FeedRX appends bytes to the pending console output, as if the system had written them to its serial port. Safe for concurrent use with a running SOL session, unlike direct RX mutation.

func (*FakeConsoleConn) IsClosed added in v0.9.1

func (f *FakeConsoleConn) IsClosed() bool

IsClosed reports whether Close was called. Safe for concurrent use with a running SOL session, unlike direct Closed reads.

func (*FakeConsoleConn) ReadAvailable added in v0.9.1

func (f *FakeConsoleConn) ReadAvailable(p []byte) (int, error)

func (*FakeConsoleConn) SendBreak added in v0.9.1

func (f *FakeConsoleConn) SendBreak(_ context.Context) error

func (*FakeConsoleConn) SetReadErr added in v0.9.1

func (f *FakeConsoleConn) SetReadErr(err error)

SetReadErr arms a read failure for the next ReadAvailable, safe for concurrent use with a running SOL session (unlike direct ReadErr writes).

func (*FakeConsoleConn) SetWriteErr added in v0.9.1

func (f *FakeConsoleConn) SetWriteErr(err error)

SetWriteErr arms a write failure for the next Write, safe for concurrent use with a running SOL session (unlike direct WriteErr writes).

func (*FakeConsoleConn) TXString added in v0.9.1

func (f *FakeConsoleConn) TXString() string

TXString returns everything written toward the system console so far.

func (*FakeConsoleConn) Write added in v0.9.1

func (f *FakeConsoleConn) Write(p []byte) (int, error)

type GPIO

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

GPIO is the mock hal.GPIOHAL.

func (*GPIO) Get

func (g *GPIO) Get(_ context.Context, pin string) (bool, error)

func (*GPIO) Set

func (g *GPIO) Set(_ context.Context, pin string, high bool) error

func (*GPIO) Watch

func (g *GPIO) Watch(_ context.Context, pin string, cb func(bool)) (func(), error)

type HAL

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

HAL is a fully in-memory hal.HAL.

func New

func New() *HAL

New returns a HAL with all sub-interfaces initialised. Each sub-type is also exported individually for targeted embedding.

The console sub-interface is left unset: most simulated targets have no redirectable serial port, so HAL.Console returns nil until HAL.SetConsole installs one.

func (*HAL) Chassis

func (h *HAL) Chassis() hal.ChassisHAL

func (*HAL) Close

func (h *HAL) Close() error

func (*HAL) Console added in v0.9.1

func (h *HAL) Console() hal.ConsoleHAL

Console returns the console installed by HAL.SetConsole, or nil when the simulated target has no redirectable serial port.

func (*HAL) GPIO

func (h *HAL) GPIO() hal.GPIOHAL

func (*HAL) I2C

func (h *HAL) I2C() hal.I2CHAL

func (*HAL) Network

func (h *HAL) Network() hal.NetworkHAL

func (*HAL) Sensors

func (h *HAL) Sensors() hal.SensorHAL

func (*HAL) SetConsole added in v0.9.1

func (h *HAL) SetConsole(c hal.ConsoleHAL)

SetConsole installs the console sub-interface returned by HAL.Console. Tests typically pass *Console; production wiring may pass any hal.ConsoleHAL implementation.

func (*HAL) Storage

func (h *HAL) Storage() hal.StorageHAL

type I2C

type I2C struct {
	ReadFunc func(bus int, addr, reg uint8, length int) ([]byte, error)
	Writes   []I2CWrite
	// contains filtered or unexported fields
}

I2C is the mock hal.I2CHAL. Reads return data from ReadFunc if set, otherwise ErrNotSupported.

func (*I2C) Read

func (i *I2C) Read(_ context.Context, bus int, addr, reg uint8, length int) ([]byte, error)

func (*I2C) Write

func (i *I2C) Write(_ context.Context, bus int, addr, reg uint8, data []byte) error

type I2CWrite

type I2CWrite struct {
	Bus  int
	Addr uint8
	Reg  uint8
	Data []byte
}

I2CWrite records a single I2C write for test assertions.

type Network

type Network struct {
	Cfg hal.IPConfig
	// contains filtered or unexported fields
}

Network is the mock hal.NetworkHAL.

func (*Network) GetConfig

func (n *Network) GetConfig(_ context.Context) (*hal.IPConfig, error)

func (*Network) SetConfig

func (n *Network) SetConfig(_ context.Context, cfg *hal.IPConfig) error

type Sensors

type Sensors struct {
	Values map[uint8]uint8
	Descs  []hal.SensorDescriptor
	// contains filtered or unexported fields
}

Sensors is the mock hal.SensorHAL.

func (*Sensors) List

func (s *Sensors) List(_ context.Context) ([]hal.SensorDescriptor, error)

func (*Sensors) ReadRaw

func (s *Sensors) ReadRaw(_ context.Context, id uint8) (uint8, error)

type Storage

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

Storage is the mock hal.StorageHAL.

func (*Storage) FRU

func (s *Storage) FRU() hal.FRUStore

func (*Storage) SDR

func (s *Storage) SDR() hal.SDRStore

Jump to

Keyboard shortcuts

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