testutil

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: BSD-2-Clause Imports: 21 Imported by: 0

Documentation

Overview

Package testutil provides test helpers including iterator comparisons (iter.Seq and iter.Seq2) and channel draining utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Drain

func Drain[T any](tb TB, channel <-chan T, opts ...Option)

Drain reads values from the channel until it is closed, expecting each read to complete within a short timeout.

func GenerateIntegrityKVs

func GenerateIntegrityKVs(prefix, name string, value []byte) []kv.KeyValue

GenerateIntegrityKVs generates integrity-formatted kv.KeyValue entries for the given name and value. Useful for building mock responses in tests.

func NewRawGenerator

func NewRawGenerator(prefix string) integrity.Generator[[]byte]

NewRawGenerator creates an integrity.Generator[[]byte] with no hashers or signers. It generates namer-formatted keys for test data.

func NewRawTyped

func NewRawTyped(strg storage.Storage, prefix string) *integrity.Typed[[]byte]

NewRawTyped creates an *integrity.Typed[[]byte] with no hashers or signers, using a raw passthrough marshaller for byte handling.

func NewRawValidator

func NewRawValidator(prefix string) integrity.Validator[[]byte]

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

func TestIterSeq2Compare[K any, V any](t TB, expected []TestIterSeq2Pair[K, V], it iter.Seq2[K, V])

TestIterSeq2Compare compares the output of an iter.Seq2 with an expected slice of key-value pairs.

func TestIterSeq2Empty

func TestIterSeq2Empty[K any, V any](t TB, it iter.Seq2[K, V])

TestIterSeq2Empty asserts that an iter.Seq2 produces no elements.

func TestIterSeq2Len

func TestIterSeq2Len[K any, V any](t TB, expected int, it iter.Seq2[K, V])

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

func TestIterSeqCompare[V any](t TB, expected []V, it iter.Seq[V])

TestIterSeqCompare compares the output of an iter.Seq with an expected slice.

func TestIterSeqEmpty

func TestIterSeqEmpty[V any](t TB, it iter.Seq[V])

TestIterSeqEmpty asserts that an iter.Seq produces no elements.

func TestIterSeqLen

func TestIterSeqLen[V any](t TB, expected int, it iter.Seq[V])

TestIterSeqLen asserts that an iter.Seq produces exactly the expected number of 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) Watch

func (m *MockStorage) Watch(_ context.Context, _ []byte, _ ...watch.Option) <-chan watch.Event

Watch 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

func WithTimeout(timeout time.Duration) Option

WithTimeout returns an Option that sets a custom timeout for draining the channel.

type TB

type TB interface {
	Helper()
	Errorf(format string, args ...any)
}

TB is a minimal interface for testing.TB.

type TestIterSeq2Pair

type TestIterSeq2Pair[K any, V any] struct {
	Key   K
	Value V
}

TestIterSeq2Pair holds a key-value pair from an iter.Seq2 iterator.

Jump to

Keyboard shortcuts

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