Help us shape the future of Go! Take the 2026 Go Developer Survey

testutil

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 33 Imported by: 0

Documentation

Overview

Package testutil provides shared test infrastructure for SynapS3 packages. It is only compiled and linked in test binaries.

Index

Constants

View Source
const (
	TaskPollInterval  = 25 * time.Millisecond
	TaskLeaseDuration = 5 * time.Second
)

Business-flow tests leave enough lease time for transactional settlement under load.

Variables

View Source
var (
	DefaultPDPVerifierAddress = common.HexToAddress("0x00000000000000000000000000000000000000c3")
	DefaultPaymentsAddress    = common.HexToAddress("0x00000000000000000000000000000000000000d4")
	DefaultUSDFCAddress       = common.HexToAddress("0x00000000000000000000000000000000000000e5")
)
View Source
var DefaultContextIdentity = storage.ContextIdentity{
	Payer:        common.HexToAddress("0x00000000000000000000000000000000000000a1"),
	ChainID:      314159,
	RecordKeeper: common.HexToAddress("0x00000000000000000000000000000000000000b2"),
}

DefaultContextIdentity is the signing identity mock targets report unless a test sets ContextIdentityValue.

Functions

func AddQueryHook

func AddQueryHook(db *bun.DB, hook bun.QueryHook)

AddQueryHook attaches hook to db in place. Tests use it to observe or pause the queries of components that already hold db; bun's WithQueryHook returns a copy those components never see.

func CommitExtraData

func CommitExtraData(nonce uint64) []byte

CommitExtraData encodes add-pieces extra data signed with nonce, the shape a storage commit reads its nonce from.

func CommitExtraDataHex

func CommitExtraDataHex(nonce uint64) string

CommitExtraDataHex is CommitExtraData as the lowercase hex a copy stores.

func CommitStorageCopy

func CommitStorageCopy(t *testing.T, db bun.IDB, _ *repository.Repositories, input CommitCopyInput)

CommitStorageCopy commits one copy the way a confirmed registration leaves it: a confirmed single-piece request is recorded first, and the copy then names it. A committed copy without a confirmed request is refused by the schema, so tests cannot shortcut this.

func DefaultWalletExecutionIdentity

func DefaultWalletExecutionIdentity() walletoperation.ExecutionIdentity

func FailWrites

func FailWrites(tb testing.TB, db bun.IDB, table, event, when, message string) func()

FailWrites makes matching row writes to table fail with message, to inject a database failure at one statement. event is a trigger event such as "INSERT" or "UPDATE OF status"; when, if not empty, is a condition over NEW and OLD. The returned function removes the fault.

func JSONEqual

func JSONEqual(a, b []byte) bool

JSONEqual reports whether a and b hold the same JSON value; two absent values are equal. PostgreSQL stores jsonb normalized, so a value read back need not repeat the bytes written.

func NewTestDB

func NewTestDB(t *testing.T) *bun.DB

NewTestDB clones this process's migrated template into a private database. Its connections close and the database is dropped when the test completes.

func NewTestRepos

func NewTestRepos(t *testing.T) *repository.Repositories

NewTestRepos creates a Repositories instance backed by NewTestDB.

func OpenBucketReplicaSlots

func OpenBucketReplicaSlots(tb testing.TB, db bun.IDB, bucketID int64, copies int)

OpenBucketReplicaSlots inserts the replica slots a bucket seeded outside repository.Buckets.Create would otherwise be missing. Data sets, copies, replacements, cleanups and observability rows all reference these, so a test that seeds a bucket directly must open its slots too.

func SeedBucket

func SeedBucket(t *testing.T, db *bun.DB, name string) *model.Bucket

SeedBucket inserts an active bucket and returns it.

func SeedBucketWithCopies

func SeedBucketWithCopies(t *testing.T, db *bun.DB, name string, status model.BucketStatus, copies int) *model.Bucket

SeedBucketWithCopies inserts a bucket whose durability policy is the given replica target, along with the replica slots that target opens.

func SeedBucketWithStatus

func SeedBucketWithStatus(t *testing.T, db *bun.DB, name string, status model.BucketStatus) *model.Bucket

SeedBucketWithStatus inserts a bucket with the specified status.

func StorageChecksum

func StorageChecksum(identity string) string

StorageChecksum turns a readable fixture identity into the canonical lowercase SHA-256 representation required by storage_contents.

Types

type CommitCopyInput

type CommitCopyInput struct {
	StorageCopyID int64
	ContentID     int64
	CopyIndex     int
	PieceCID      string
	PieceID       *idtypes.OnChainID
	RetrievalURL  string
	TransactionID string
}

CommitCopyInput names a copy to commit and the evidence it ends with.

type MockCache

type MockCache struct {
	ListUploadIDsFunc            func(context.Context) ([]string, error)
	WalkContentFilesFunc         func(context.Context, bool, func(string, int64) error) error
	QuarantineFunc               func(context.Context, string, int64) error
	DeleteQuarantinedFunc        func(context.Context, string, int64) error
	PutFunc                      func(ctx context.Context, bucket, key string, r io.Reader, size int64) (*cache.ObjectInfo, error)
	PutStagedFunc                func(ctx context.Context, bucket, key string, r io.Reader, size int64) (*cache.StagedObject, error)
	GetFunc                      func(ctx context.Context, bucket, key string) (io.ReadCloser, *cache.ObjectInfo, error)
	DeleteFunc                   func(ctx context.Context, bucket, key string) error
	ExistsFunc                   func(ctx context.Context, bucket, key string) bool
	UsedBytesFunc                func() int64
	CapacitySnapshotFunc         func() cache.CapacitySnapshot
	ConsumeRefusedWriteBytesFunc func() int64
	CreateBucketDirFunc          func(ctx context.Context, bucket string) error
	DeleteBucketDirFunc          func(ctx context.Context, bucket string) error
	PutPartStagedFunc            func(ctx context.Context, uploadID string, partNumber int, r io.Reader, size int64) (*cache.StagedObject, error)
	AssemblePartsFunc            func(ctx context.Context, bucket, key, uploadID string, partNumbers []int) (*cache.StagedObject, []string, error)
	DeleteUploadFunc             func(ctx context.Context, uploadID string) error
}

MockCache is a configurable test double for cache.Cache. Use for fault injection tests; for happy-path tests prefer real cache.NewFilesystem.

func (*MockCache) AssemblePartsStaged

func (m *MockCache) AssemblePartsStaged(ctx context.Context, bucket, key, uploadID string, partNumbers []int) (*cache.StagedObject, []string, error)

func (*MockCache) CapacitySnapshot

func (m *MockCache) CapacitySnapshot() cache.CapacitySnapshot

func (*MockCache) ConsumeRefusedWriteBytes

func (m *MockCache) ConsumeRefusedWriteBytes() int64

func (*MockCache) CreateBucketDir

func (m *MockCache) CreateBucketDir(ctx context.Context, bucket string) error

func (*MockCache) Delete

func (m *MockCache) Delete(ctx context.Context, bucket, key string) error

func (*MockCache) DeleteBucketDir

func (m *MockCache) DeleteBucketDir(ctx context.Context, bucket string) error

func (*MockCache) DeleteQuarantined added in v0.1.1

func (m *MockCache) DeleteQuarantined(ctx context.Context, bucket string, contentID int64) error

func (*MockCache) DeleteUpload

func (m *MockCache) DeleteUpload(ctx context.Context, uploadID string) error

func (*MockCache) Exists

func (m *MockCache) Exists(ctx context.Context, bucket, key string) bool

func (*MockCache) Get

func (m *MockCache) Get(ctx context.Context, bucket, key string) (io.ReadCloser, *cache.ObjectInfo, error)

func (*MockCache) ListUploadIDs added in v0.1.1

func (m *MockCache) ListUploadIDs(ctx context.Context) ([]string, error)

func (*MockCache) Put

func (m *MockCache) Put(ctx context.Context, bucket, key string, r io.Reader, size int64) (*cache.ObjectInfo, error)

func (*MockCache) PutPartStaged added in v0.1.1

func (m *MockCache) PutPartStaged(ctx context.Context, uploadID string, partNumber int, r io.Reader, size int64) (*cache.StagedObject, error)

func (*MockCache) PutStaged

func (m *MockCache) PutStaged(ctx context.Context, bucket, key string, r io.Reader, size int64) (*cache.StagedObject, error)

func (*MockCache) Quarantine added in v0.1.1

func (m *MockCache) Quarantine(ctx context.Context, bucket string, contentID int64) error

func (*MockCache) UsedBytes

func (m *MockCache) UsedBytes() int64

func (*MockCache) WalkContentFiles added in v0.1.1

func (m *MockCache) WalkContentFiles(ctx context.Context, quarantined bool, visit func(string, int64) error) error

type MockChainEpochReader

type MockChainEpochReader struct {
	CurrentEpochFunc func(ctx context.Context) (int64, error)
}

MockChainEpochReader is a configurable test double for synapse.ChainEpochReader. An unconfigured reader reports epoch zero, which keeps a retirement gate waiting rather than letting it pass by accident.

func (*MockChainEpochReader) CurrentEpoch

func (m *MockChainEpochReader) CurrentEpoch(ctx context.Context) (int64, error)

type MockCommitNonces

type MockCommitNonces struct {

	// Err fails every read while set.
	Err error
	// contains filtered or unexported fields
}

MockCommitNonces is chain nonce state a test sets directly.

func (*MockCommitNonces) ClientNonce

func (*MockCommitNonces) Consume

func (m *MockCommitNonces) Consume(nonce uint64, dataSetID, pieceID sdktypes.BigInt, pieceCID cid.Cid)

Consume records that the request signed with nonce added pieceCID to the data set as pieceID.

func (*MockCommitNonces) ConsumeExtraData

func (m *MockCommitNonces) ConsumeExtraData(extraData []byte, dataSetID, pieceID sdktypes.BigInt, pieceCID cid.Cid)

ConsumeExtraData is Consume for the nonce signed into extraData.

func (*MockCommitNonces) ConsumeRequest

func (m *MockCommitNonces) ConsumeRequest(extraData []byte, dataSetID, firstPieceID sdktypes.BigInt, pieceCIDs []cid.Cid)

ConsumeRequest records that the request signed into extraData added pieceCIDs, in order, to the data set starting at firstPieceID.

func (*MockCommitNonces) PieceCIDAt

func (m *MockCommitNonces) PieceCIDAt(_ context.Context, dataSetID, pieceID sdktypes.BigInt) (cid.Cid, error)

func (*MockCommitNonces) Reads

func (m *MockCommitNonces) Reads() int

Reads counts the nonce reads made so far.

type MockServiceTerminator

type MockServiceTerminator struct {
	TerminateServiceFunc func(ctx context.Context, dataSetID sdktypes.BigInt) (*synapse.TerminationResult, error)
	// VerifyServicePayerFunc defaults to accepting every data set.
	VerifyServicePayerFunc func(ctx context.Context, dataSetID sdktypes.BigInt) error
	// ContextIdentityValue overrides DefaultContextIdentity.
	ContextIdentityValue storage.ContextIdentity
}

MockServiceTerminator is a configurable test double for synapse.ServiceTerminator.

func (*MockServiceTerminator) ContextIdentity

func (m *MockServiceTerminator) ContextIdentity() storage.ContextIdentity

ContextIdentity returns ContextIdentityValue, or DefaultContextIdentity when no test sets it.

func (*MockServiceTerminator) PDPVerifierAddress

func (m *MockServiceTerminator) PDPVerifierAddress() common.Address

func (*MockServiceTerminator) TerminateService

func (*MockServiceTerminator) VerifyServicePayer

func (m *MockServiceTerminator) VerifyServicePayer(ctx context.Context, dataSetID sdktypes.BigInt) error

VerifyServicePayer calls VerifyServicePayerFunc, accepting every data set when no test sets it.

type MockStorageClient

type MockStorageClient struct {
	ContextIdentityValue    storage.ContextIdentity
	PDPVerifierAddressValue common.Address
	UploadFunc              func(ctx context.Context, r io.Reader, opts *storage.UploadOptions) (*storage.UploadResult, error)
	DownloadFunc            func(ctx context.Context, pieceCID cid.Cid, opts *storage.DownloadOptions) (io.ReadCloser, error)
	PrepareUploadFunc       func(ctx context.Context, dataSize uint64, targets []synapse.StorageTarget) (*sdkcosts.MultiContextCosts, error)
	SelectUploadTargetsFunc func(ctx context.Context, opts storage.SelectUploadContextsOptions) ([]synapse.StorageTarget, error)
	OpenProviderTargetFunc  func(ctx context.Context, providerID sdktypes.BigInt, opts storage.NewProviderContextOptions) (synapse.ProviderTarget, error)
	OpenDataSetTargetFunc   func(ctx context.Context, dataSetID sdktypes.BigInt, opts storage.NewDataSetContextOptions) (synapse.DataSetTarget, error)
	OpenTargetFunc          func(ctx context.Context, opts *OpenTargetOptions) (synapse.StorageTarget, error)
	FindMatchingDataSetFunc func(ctx context.Context, providerID sdktypes.BigInt, metadata map[string]string, withCDN bool) (*storage.DataSetRef, error)
	OpenCleanupContextFunc  func(ctx context.Context, dataSetID sdktypes.BigInt, opts storage.NewDataSetContextOptions) (synapse.CleanupContext, error)
	DeletionStateFunc       func(ctx context.Context, dataSetID, pieceID sdktypes.BigInt) (synapse.CleanupPieceState, error)
	DeletionStatesFunc      func(ctx context.Context, dataSetID sdktypes.BigInt, pieceIDs []sdktypes.BigInt) (synapse.CleanupPiecesState, error)
	ProviderPiecesKnownFunc func(ctx context.Context, serviceURL string, dataSetID sdktypes.BigInt, pieceIDs []sdktypes.BigInt) (synapse.ProviderPiecesState, error)
}

MockStorageClient is a configurable test double for synapse.StorageClient.

func (*MockStorageClient) ContextIdentity

func (m *MockStorageClient) ContextIdentity() storage.ContextIdentity

func (*MockStorageClient) DeletionStates added in v0.1.1

func (m *MockStorageClient) DeletionStates(ctx context.Context, dataSetID sdktypes.BigInt, pieceIDs []sdktypes.BigInt) (synapse.CleanupPiecesState, error)

func (*MockStorageClient) Download

func (m *MockStorageClient) Download(ctx context.Context, pieceCID cid.Cid, opts *storage.DownloadOptions) (io.ReadCloser, error)

func (*MockStorageClient) FindMatchingDataSet

func (m *MockStorageClient) FindMatchingDataSet(ctx context.Context, providerID sdktypes.BigInt, metadata map[string]string, withCDN bool) (*storage.DataSetRef, error)

func (*MockStorageClient) OpenCleanupContext

func (*MockStorageClient) OpenDataSetTarget

func (*MockStorageClient) OpenProviderTarget

func (*MockStorageClient) PDPVerifierAddress

func (m *MockStorageClient) PDPVerifierAddress() common.Address

func (*MockStorageClient) PrepareUpload

func (m *MockStorageClient) PrepareUpload(ctx context.Context, dataSize uint64, targets []synapse.StorageTarget) (*sdkcosts.MultiContextCosts, error)

func (*MockStorageClient) ProviderPiecesKnown added in v0.1.1

func (m *MockStorageClient) ProviderPiecesKnown(ctx context.Context, serviceURL string, dataSetID sdktypes.BigInt, pieceIDs []sdktypes.BigInt) (synapse.ProviderPiecesState, error)

func (*MockStorageClient) SelectUploadTargets

func (*MockStorageClient) Upload

type MockStorageTarget

type MockStorageTarget struct {
	ProviderIDValue      sdktypes.BigInt
	DataSetIDValue       *sdktypes.BigInt
	ClientDataSetIDValue sdktypes.BigInt
	ServiceURLValue      string
	WithCDNValue         bool
	CreateDataSetFunc    func(context.Context, *storage.CreateDataSetOptions) (*storage.CreateDataSetResult, error)
	WaitDataSetFunc      func(context.Context, string, sdktypes.BigInt) (*storage.CreateDataSetResult, error)
	// ContextIdentityValue overrides DefaultContextIdentity.
	ContextIdentityValue      storage.ContextIdentity
	FindDataSetByClientIDFunc func(context.Context, sdktypes.BigInt) (storage.DataSetRef, bool, error)
	CreateStoreSessionFunc    func(context.Context) (string, error)
	UploadStoreSessionFunc    func(context.Context, string, string, io.Reader, *storage.StoreOptions) (*storage.StoreResult, error)
	FinalizeStoreSessionFunc  func(context.Context, string, string, cid.Cid) error
	PresignForCommitFunc      func(context.Context, []storage.PieceInput) ([]byte, error)
	SubmitPullFunc            func(context.Context, storage.PullRequest) (*storage.PullResult, error)
	SubmitCommitFunc          func(context.Context, storage.CommitRequest) (*storage.CommitSubmission, error)
	GetCommitStatusFunc       func(context.Context, string) (*storage.CommitStatus, error)
	PieceStatusFunc           func(context.Context, cid.Cid) (*storage.PieceStatus, error)
	// CheckWritableFunc defaults to a writable data set.
	CheckWritableFunc func(context.Context) error
}

MockStorageTarget is a minimal immutable storage target for tests that only need provider metadata for upload preparation.

func NewMockDataSetTarget

func NewMockDataSetTarget(providerID, dataSetID sdktypes.BigInt, withCDN *bool) *MockStorageTarget

func NewMockProviderTarget

func NewMockProviderTarget(providerID sdktypes.BigInt, opts storage.NewProviderContextOptions) *MockStorageTarget

func (*MockStorageTarget) CDNEnabled

func (m *MockStorageTarget) CDNEnabled() bool

func (*MockStorageTarget) CheckWritable

func (m *MockStorageTarget) CheckWritable(ctx context.Context) error

func (*MockStorageTarget) ClientDataSetID

func (m *MockStorageTarget) ClientDataSetID() sdktypes.BigInt

func (*MockStorageTarget) ContextIdentity

func (m *MockStorageTarget) ContextIdentity() storage.ContextIdentity

func (*MockStorageTarget) CreateDataSet

func (*MockStorageTarget) CreateStoreSession

func (m *MockStorageTarget) CreateStoreSession(ctx context.Context) (string, error)

func (*MockStorageTarget) DataSetRef

func (m *MockStorageTarget) DataSetRef() (storage.DataSetRef, bool)

func (*MockStorageTarget) FinalizeStoreSession

func (m *MockStorageTarget) FinalizeStoreSession(ctx context.Context, serviceURL, sessionID string, pieceCID cid.Cid) error

func (*MockStorageTarget) FindDataSetByClientDataSetID

func (m *MockStorageTarget) FindDataSetByClientDataSetID(ctx context.Context, clientDataSetID sdktypes.BigInt) (storage.DataSetRef, bool, error)

func (*MockStorageTarget) GetCommitStatus

func (m *MockStorageTarget) GetCommitStatus(ctx context.Context, statusURL string) (*storage.CommitStatus, error)

func (*MockStorageTarget) GetProviderInfo

func (m *MockStorageTarget) GetProviderInfo() storage.Provider

func (*MockStorageTarget) PieceStatus

func (m *MockStorageTarget) PieceStatus(ctx context.Context, pieceCID cid.Cid) (*storage.PieceStatus, error)

func (*MockStorageTarget) PieceURL

func (m *MockStorageTarget) PieceURL(piece cid.Cid) string

func (*MockStorageTarget) PresignForCommit

func (m *MockStorageTarget) PresignForCommit(ctx context.Context, pieces []storage.PieceInput) ([]byte, error)

func (*MockStorageTarget) ProviderID

func (m *MockStorageTarget) ProviderID() sdktypes.BigInt

func (*MockStorageTarget) ServiceURL

func (m *MockStorageTarget) ServiceURL() string

func (*MockStorageTarget) SubmitCommit

func (*MockStorageTarget) SubmitPull

func (m *MockStorageTarget) SubmitPull(ctx context.Context, request storage.PullRequest) (*storage.PullResult, error)

func (*MockStorageTarget) UploadStoreSession

func (m *MockStorageTarget) UploadStoreSession(ctx context.Context, serviceURL, sessionID string, reader io.Reader, opts *storage.StoreOptions) (*storage.StoreResult, error)

func (*MockStorageTarget) WaitForDataSetCreated

func (m *MockStorageTarget) WaitForDataSetCreated(ctx context.Context, statusURL string, clientDataSetID sdktypes.BigInt) (*storage.CreateDataSetResult, error)

type MockWalletQuerier

type MockWalletQuerier struct {
	GetWalletInfoFunc func(ctx context.Context) (*synapse.WalletInfo, error)
}

MockWalletQuerier is a configurable test double for synapse.WalletQuerier.

func (*MockWalletQuerier) GetWalletInfo

func (m *MockWalletQuerier) GetWalletInfo(ctx context.Context) (*synapse.WalletInfo, error)

func (*MockWalletQuerier) WalletIdentity

type OpenTargetOptions

type OpenTargetOptions struct {
	ProviderID      *sdktypes.BigInt
	DataSetID       *sdktypes.BigInt
	DataSetMetadata map[string]string
	WithCDN         *bool
}

OpenTargetOptions lets worker tests configure one callback for both immutable provider and data-set target opens.

Jump to

Keyboard shortcuts

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