Documentation
¶
Overview ¶
Package testing provides test utilities for the provisioning state machine. The MockStateStore satisfies provisioning.StateStore with a thread-safe in-memory backing and configurable failure injection so consumer-side step implementations can be tested without a real PG.
Usage:
store := provtest.NewMockStateStore()
exec, err := provisioning.NewExecutor(store, mySteps, myLogger)
require.NoError(t, err)
_, err = store.Upsert(ctx, &provisioning.Job{ID: "j1", TenantID: "t1"})
require.NoError(t, err)
require.NoError(t, exec.Run(ctx, "j1"))
provtest.AssertJobReachedState(t, store, "j1", provisioning.StateReady)
Tests that want to verify the persisted state graph without exercising real PostgreSQL can use this mock everywhere a StateStore is required.
Index ¶
- func AssertJobLastError(t *testing.T, store provisioning.StateStore, jobID, want string)
- func AssertJobReachedState(t *testing.T, store provisioning.StateStore, jobID string, ...)
- func AssertTransitionPath(t *testing.T, m *MockStateStore, jobID string, want []provisioning.State)
- type MockStateStore
- func (m *MockStateStore) CreateTable(ctx context.Context) error
- func (m *MockStateStore) Get(ctx context.Context, jobID string) (*provisioning.Job, error)
- func (m *MockStateStore) Reset()
- func (m *MockStateStore) Transition(ctx context.Context, jobID string, from, to provisioning.State, ...) error
- func (m *MockStateStore) TransitionLog() []TransitionEvent
- func (m *MockStateStore) Upsert(ctx context.Context, job *provisioning.Job) (*provisioning.Job, error)
- func (m *MockStateStore) WithCreateTableError(err error) *MockStateStore
- func (m *MockStateStore) WithGetError(err error) *MockStateStore
- func (m *MockStateStore) WithTransitionError(err error) *MockStateStore
- func (m *MockStateStore) WithUpsertError(err error) *MockStateStore
- type TransitionEvent
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertJobLastError ¶
func AssertJobLastError(t *testing.T, store provisioning.StateStore, jobID, want string)
AssertJobLastError fails the test if the job's LastError does not equal want. Useful when verifying that a failed transition recorded the expected diagnostic message.
func AssertJobReachedState ¶
func AssertJobReachedState(t *testing.T, store provisioning.StateStore, jobID string, want provisioning.State)
AssertJobReachedState fails the test if the job named jobID is not at the expected state. Helpful for the typical assertion in consumer tests where the executor's terminal-state check is the contract under test.
func AssertTransitionPath ¶
func AssertTransitionPath(t *testing.T, m *MockStateStore, jobID string, want []provisioning.State)
AssertTransitionPath verifies the MockStateStore observed the expected sequence of transitions for jobID, in order. Other jobs' transitions are ignored — pass jobID="" to assert across all jobs.
Types ¶
type MockStateStore ¶
type MockStateStore struct {
// contains filtered or unexported fields
}
MockStateStore is a thread-safe StateStore implementation for unit tests. It mirrors the semantics of provisioning.MemoryStore (which is the production-side reference implementation) but adds configurable failure injection for testing error paths.
func NewMockStateStore ¶
func NewMockStateStore() *MockStateStore
NewMockStateStore returns a fresh MockStateStore.
func (*MockStateStore) CreateTable ¶
func (m *MockStateStore) CreateTable(ctx context.Context) error
CreateTable implements StateStore.
func (*MockStateStore) Get ¶
func (m *MockStateStore) Get(ctx context.Context, jobID string) (*provisioning.Job, error)
Get implements StateStore.
func (*MockStateStore) Reset ¶
func (m *MockStateStore) Reset()
Reset clears all configured errors and the transition log so a single MockStateStore can be reused across subtests.
func (*MockStateStore) Transition ¶
func (m *MockStateStore) Transition( ctx context.Context, jobID string, from, to provisioning.State, metadata map[string]string, lastError string, ) error
Transition implements StateStore.
func (*MockStateStore) TransitionLog ¶
func (m *MockStateStore) TransitionLog() []TransitionEvent
TransitionLog returns a snapshot of recorded successful transitions in insertion order. Useful for asserting the executor walked the expected path.
func (*MockStateStore) Upsert ¶
func (m *MockStateStore) Upsert(ctx context.Context, job *provisioning.Job) (*provisioning.Job, error)
Upsert implements StateStore.
func (*MockStateStore) WithCreateTableError ¶
func (m *MockStateStore) WithCreateTableError(err error) *MockStateStore
WithCreateTableError configures the mock to return err from CreateTable.
func (*MockStateStore) WithGetError ¶
func (m *MockStateStore) WithGetError(err error) *MockStateStore
WithGetError configures the mock to return err from every Get call.
func (*MockStateStore) WithTransitionError ¶
func (m *MockStateStore) WithTransitionError(err error) *MockStateStore
WithTransitionError configures the mock to return err from every Transition call. Useful for testing that the Executor propagates store errors (e.g. transient PG connectivity failures) up to its caller.
func (*MockStateStore) WithUpsertError ¶
func (m *MockStateStore) WithUpsertError(err error) *MockStateStore
WithUpsertError configures the mock to return err from every Upsert call.
type TransitionEvent ¶
type TransitionEvent struct {
JobID string
From provisioning.State
To provisioning.State
}
TransitionEvent records a single successful Transition call for assertion.