Documentation
¶
Overview ¶
Package testutil provides test helpers including iterator comparisons (iter.Seq and iter.Seq2) and channel draining utilities.
Index ¶
- func Drain[T any](tb TB, channel <-chan T, opts ...Option)
- func GenerateIntegrityKVs(prefix, name string, value []byte) []kv.KeyValue
- func NewRawGenerator(prefix string) integrity.Generator[[]byte]
- func NewRawTyped(strg storage.Storage, prefix string) *integrity.Typed[[]byte]
- func NewRawValidator(prefix string) integrity.Validator[[]byte]
- func PutIntegrity(mock *MockStorage, prefix, name string, value []byte)
- func TestIterSeq2Compare[K any, V any](t TB, expected []TestIterSeq2Pair[K, V], it iter.Seq2[K, V])
- func TestIterSeq2Empty[K any, V any](t TB, it iter.Seq2[K, V])
- func TestIterSeq2Len[K any, V any](t TB, expected int, it iter.Seq2[K, V])
- func TestIterSeq2UnorderedCompare[K any, V any](t TB, expected []TestIterSeq2Pair[K, V], it iter.Seq2[K, V])
- func TestIterSeqCompare[V any](t TB, expected []V, it iter.Seq[V])
- func TestIterSeqEmpty[V any](t TB, it iter.Seq[V])
- func TestIterSeqLen[V any](t TB, expected int, it iter.Seq[V])
- type EtcdTestCluster
- type MockStorage
- func (m *MockStorage) CloseWatchChannels()
- func (m *MockStorage) Put(key, value []byte) *MockStorage
- func (m *MockStorage) PutKV(entry kv.KeyValue) *MockStorage
- func (m *MockStorage) Range(_ context.Context, _ ...storage.RangeOption) ([]kv.KeyValue, error)
- func (m *MockStorage) SendWatchEvent(event watch.Event)
- func (m *MockStorage) Tx(_ context.Context) tx.Tx
- func (m *MockStorage) Watch(_ context.Context, _ []byte, _ ...watch.Option) <-chan watch.Event
- func (m *MockStorage) WithTxError(err error) *MockStorage
- type Option
- type TB
- type TestIterSeq2Pair
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Drain ¶
Drain reads values from the channel until it is closed, expecting each read to complete within a short timeout.
func GenerateIntegrityKVs ¶
GenerateIntegrityKVs generates integrity-formatted kv.KeyValue entries for the given name and value. Useful for building mock responses in tests.
func NewRawGenerator ¶
NewRawGenerator creates an integrity.Generator[[]byte] with no hashers or signers. It generates namer-formatted keys for test data.
func NewRawTyped ¶
NewRawTyped creates an *integrity.Typed[[]byte] with no hashers or signers, using a raw passthrough marshaller for byte handling.
func NewRawValidator ¶
NewRawValidator creates an integrity.Validator[[]byte] with no hashers or signers. It validates integrity keys but performs no hash/signature checks.
func PutIntegrity ¶
func PutIntegrity(mock *MockStorage, prefix, name string, value []byte)
PutIntegrity stores a named value in the mock storage using the integrity generator. This ensures the correct namer-formatted keys are used, matching what the collector's validator will expect. ModRevision is auto-incremented by the mock.
func TestIterSeq2Compare ¶
TestIterSeq2Compare compares the output of an iter.Seq2 with an expected slice of key-value pairs.
func TestIterSeq2Empty ¶
TestIterSeq2Empty asserts that an iter.Seq2 produces no elements.
func TestIterSeq2Len ¶
TestIterSeq2Len asserts that an iter.Seq2 produces exactly the expected number of key-value pairs.
func TestIterSeq2UnorderedCompare ¶
func TestIterSeq2UnorderedCompare[K any, V any](t TB, expected []TestIterSeq2Pair[K, V], it iter.Seq2[K, V])
TestIterSeq2UnorderedCompare compares the output of an iter.Seq2 with an expected slice of key-value pairs unordered.
func TestIterSeqCompare ¶
TestIterSeqCompare compares the output of an iter.Seq with an expected slice.
func TestIterSeqEmpty ¶
TestIterSeqEmpty asserts that an iter.Seq produces no elements.
Types ¶
type EtcdTestCluster ¶
type EtcdTestCluster struct {
Storage storage.Storage
Client *etcdclient.Client
}
EtcdTestCluster holds the resources for an embedded etcd test cluster.
func NewEtcdTestCluster ¶
func NewEtcdTestCluster(t *testing.T) *EtcdTestCluster
NewEtcdTestCluster starts an embedded single-node etcd cluster and returns a storage.Storage backed by it along with a raw etcd client for direct access. The cluster is terminated and the client is closed when the test finishes. Tests using this helper are skipped in short mode.
type MockStorage ¶
type MockStorage struct {
// contains filtered or unexported fields
}
MockStorage is an in-memory implementation of storage.Storage for testing. It supports Get (with prefix matching), Put, and Delete operations, and optionally injects errors.
func NewMockStorage ¶
func NewMockStorage() *MockStorage
NewMockStorage creates a new MockStorage instance.
func (*MockStorage) CloseWatchChannels ¶
func (m *MockStorage) CloseWatchChannels()
CloseWatchChannels closes all registered watch channels.
func (*MockStorage) Put ¶
func (m *MockStorage) Put(key, value []byte) *MockStorage
Put stores a key-value pair in the mock. It assigns an auto-incremented ModRevision and returns the mock for chaining.
func (*MockStorage) PutKV ¶
func (m *MockStorage) PutKV(entry kv.KeyValue) *MockStorage
PutKV stores a kv.KeyValue directly (preserving its ModRevision).
func (*MockStorage) Range ¶
func (m *MockStorage) Range(_ context.Context, _ ...storage.RangeOption) ([]kv.KeyValue, error)
Range implements storage.Storage.
func (*MockStorage) SendWatchEvent ¶
func (m *MockStorage) SendWatchEvent(event watch.Event)
SendWatchEvent sends an event to all registered watch channels.
func (*MockStorage) Tx ¶
func (m *MockStorage) Tx(_ context.Context) tx.Tx
Tx implements storage.Storage.
func (*MockStorage) WithTxError ¶
func (m *MockStorage) WithTxError(err error) *MockStorage
WithTxError configures the mock to return an error on Tx.Commit.
type Option ¶
type Option func(*options)
Option configures Drain behavior.
func WithTimeout ¶
WithTimeout returns an Option that sets a custom timeout for draining the channel.
type TestIterSeq2Pair ¶
TestIterSeq2Pair holds a key-value pair from an iter.Seq2 iterator.