testhelpers

package
v1.15.2 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: Apache-2.0, MIT Imports: 44 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// DefaultDaemonCmdTimeout is the default timeout for executing commands.
	DefaultDaemonCmdTimeout = 1 * time.Minute
)

Variables

View Source
var EmptyMessagesCID cid.Cid

EmptyMessagesCID is the cid of an empty collection of messages.

View Source
var EmptyReceiptsCID cid.Cid

EmptyReceiptsCID is the cid of an empty collection of receipts.

View Source
var EmptyTxMetaCID cid.Cid

EmptyTxMetaCID is the cid of a TxMeta wrapping empty cids

Functions

func AssertCidsEqual added in v1.2.0

func AssertCidsEqual(t *testing.T, m cid.Cid, n cid.Cid)

AssertCidsEqual asserts that two CIDS are identical.

func AssertHaveSameCid added in v1.2.0

func AssertHaveSameCid(t *testing.T, m HasCid, n HasCid)

AssertHaveSameCid asserts that two values have identical CIDs.

func AutoSealInterval

func AutoSealInterval(autoSealInterval string) func(*TestDaemon)

AutoSealInterval specifies an interval for automatically sealing

func CidFromString added in v1.2.0

func CidFromString(t *testing.T, input string) cid.Cid

CidFromString generates Cid from string input

func CmdTimeout

func CmdTimeout(t time.Duration) func(*TestDaemon)

CmdTimeout allows setting the `cmdTimeout` config option on the daemon.

func ContainerDir

func ContainerDir(dir string) func(*TestDaemon)

ContainerDir sets the `containerDir` path for the daemon.

func DefaultAddress

func DefaultAddress(defaultAddr address.Address) func(*TestDaemon)

DefaultAddress specifies a key file for this daemon to add to their wallet during init

func EmptyReceipts added in v1.2.0

func EmptyReceipts(n int) []*types.MessageReceipt

EmptyReceipts returns a slice of n empty receipts.

func GenesisFile

func GenesisFile(a string) func(*TestDaemon)

GenesisFile allows setting the `genesisFile` config option on the daemon.

func GenesisFilePath

func GenesisFilePath() string

GenesisFilePath returns the path to the test genesis

func GetFilecoinBinary

func GetFilecoinBinary() (string, error)

GetFilecoinBinary returns the path where the filecoin binary will be if it has been built

func GetFreePort

func GetFreePort() (int, error)

GetFreePort gets a free port from the kernel Credit: https://github.com/phayes/freeport

func GetGitRoot added in v1.2.3

func GetGitRoot() string

GetGitRoot return the project root joined with any path fragments

func InitArgs

func InitArgs(a ...string) func(*TestDaemon)

InitArgs allows setting additional arguments to repo initialization

func IsRelay

func IsRelay(td *TestDaemon)

IsRelay starts the daemon with the --is-relay option.

func KeyFile

func KeyFile(kf string) func(*TestDaemon)

KeyFile specifies a key file for this daemon to add to their wallet during init

func MustGenerateBLSKeyInfo added in v1.2.0

func MustGenerateBLSKeyInfo(n int, seed byte) []key.KeyInfo

MustGenerateBLSKeyInfo produces n distinct BLS keyinfos.

func MustGenerateKeyInfo added in v1.2.0

func MustGenerateKeyInfo(n int, seed byte) []key.KeyInfo

MustGenerateKeyInfo generates `n` distinct keyinfos using seed `seed`. The result is deterministic (for stable tests), don't use this for real keys!

func MustGenerateMixedKeyInfo added in v1.2.0

func MustGenerateMixedKeyInfo(m int, n int) []key.KeyInfo

MustGenerateMixedKeyInfo produces m bls keys and n secp keys. BLS and Secp will be interleaved. The keys will be valid, but not deterministic.

func MustGetFilecoinBinary

func MustGetFilecoinBinary() string

MustGetFilecoinBinary returns the path where the filecoin binary will be if it has been built and panics otherwise.

func NewCidForTestGetter added in v1.2.0

func NewCidForTestGetter() func() cid.Cid

NewCidForTestGetter returns a closure that returns a Cid unique to that invocation. The Cid is unique wrt the closure returned, not globally. You can use this function in tests.

func NewForTestGetter added in v1.2.0

func NewForTestGetter() func() address.Address

NewForTestGetter returns a closure that returns an address unique to that invocation. The address is unique wrt the closure returned, not globally.

func NewMessage added in v1.2.0

func NewMessage(from, to address.Address, nonce uint64, value abi.TokenAmount, method abi.MethodNum, params []byte) *types.Message

NewMessage creates a new message.

func NewMessageForTestGetter added in v1.2.0

func NewMessageForTestGetter() func() *types.Message

NewMessageForTestGetter returns a closure that returns a message unique to that invocation. The message is unique wrt the closure returned, not globally. You can use this function in tests instead of manually creating messages -- it both reduces duplication and gives us exactly one place to create valid messages for tests if messages require validation in the future.

func NewMeteredMessage added in v1.2.0

func NewMeteredMessage(from, to address.Address, nonce uint64, value abi.TokenAmount, method abi.MethodNum, params []byte, gasFeeCap, gasPremium abi.TokenAmount, limit int64) *types.Message

NewMeteredMessage adds gas price and gas limit to the message

func NewMsgs added in v1.2.0

func NewMsgs(n int) []*types.Message

NewMsgs returns n messages. The messages returned are unique to this invocation but are not unique globally (ie, a second call to NewMsgs will return the same set of messages).

func NewMsgsWithAddrs added in v1.2.0

func NewMsgsWithAddrs(n int, a []address.Address) []*types.Message

NewMsgsWithAddrs returns a slice of `n` messages who's `From` field's are pulled from `a`. This method should be used when the addresses returned are to be signed at a later point.

func NewSignedMessage added in v1.2.0

func NewSignedMessage(ctx context.Context, msg types.Message, s types.Signer) (*types.SignedMessage, error)

NewSignedMessage accepts a message `msg` and a signer `s`. NewSignedMessage returns a `SignedMessage` containing a signature derived from the serialized `msg` and `msg.From` NOTE: this method can only sign message with From being a public-key type address, not an ID address. We should deprecate this and move to more explicit signing via an address resolver.

func NewSignedMessageForTestGetter added in v1.2.0

func NewSignedMessageForTestGetter(ms MockSigner) func(uint64) *types.SignedMessage

NewSignedMessageForTestGetter returns a closure that returns a SignedMessage unique to that invocation. The message is unique wrt the closure returned, not globally. You can use this function in tests instead of manually creating messages -- it both reduces duplication and gives us exactly one place to create valid messages for tests if messages require validation in the future. TODO support chosing from address

func NewSignedMsgs added in v1.2.0

func NewSignedMsgs(n uint, ms MockSigner) []*types.SignedMessage

NewSignedMsgs returns n signed messages. The messages returned are unique to this invocation but are not unique globally (ie, a second call to NewSignedMsgs will return the same set of messages).

func RandPeerID

func RandPeerID() (peer.ID, error)

RandPeerID is a libp2p random peer ID generator. These peer.ID generators were copied from libp2p/go-testutil. We didn't bring in the whole repo as a dependency because we only need this small bit. However if we find ourselves using more and more pieces we should just take a dependency on it.

func RequireIDAddress added in v1.2.0

func RequireIDAddress(t *testing.T, i int) address.Address

func RequireIntPeerID

func RequireIntPeerID(t *testing.T, i int64) peer.ID

RequireIntPeerID takes in an integer and creates a unique peer id for it.

func RequireNewTipSet added in v1.2.0

func RequireNewTipSet(t *testing.T, blks ...*types.BlockHeader) *types.TipSet

RequireNewTipSet instantiates and returns a new tipset of the given blocks and requires that the setup validation succeed.

func RequireRandomPeerID

func RequireRandomPeerID(t *testing.T) peer.ID

RequireRandomPeerID returns a new libp2p peer ID or panics.

func RequireTipset

func RequireTipset(t *testing.T) *types.TipSet

RequireTipset is a helper that constructs a tipset

func RequireTipsetWithHeight

func RequireTipsetWithHeight(t *testing.T, height abi.ChainEpoch) *types.TipSet

func Root added in v1.2.3

func Root(paths ...string) string

Root return the project root joined with any path fragments

func RunSuccessFirstLine

func RunSuccessFirstLine(td *TestDaemon, args ...string) string

RunSuccessFirstLine executes the given command, asserts success and returns the first line of stdout.

func RunSuccessLines

func RunSuccessLines(td *TestDaemon, args ...string) []string

RunSuccessLines executes the given command, asserts success and returns an array of lines of the stdout.

func SignMsgs added in v1.2.0

func SignMsgs(ms MockSigner, msgs []*types.Message) ([]*types.SignedMessage, error)

SignMsgs returns a slice of signed messages where the original messages are `msgs`, if signing one of the `msgs` fails an error is returned

func WaitForIt

func WaitForIt(count int, delay time.Duration, cb func() (bool, error)) error

WaitForIt waits until the given callback returns true.

func WaitTimeout

func WaitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool

WaitTimeout waits for the waitgroup for the specified max timeout. Returns true if waiting timed out.

Types

type CmdOutput

type CmdOutput struct {
	// Args is the command and argument sequence executed.
	Args []string
	// contains filtered or unexported fields
}

CmdOutput collects the output from a CLI command issued to a process.

func ReadOutput

func ReadOutput(tb testing.TB, args []string, stdout io.Reader, stderr io.Reader) *CmdOutput

ReadOutput reads the `stdout` and `stderr` streams completely and returns a new Output object.

func (*CmdOutput) AssertFail

func (o *CmdOutput) AssertFail(err string) *CmdOutput

AssertFail asserts that the output represents a failed execution, with the error matching the passed in error.

func (*CmdOutput) AssertSuccess

func (o *CmdOutput) AssertSuccess() *CmdOutput

AssertSuccess asserts that the output represents a successful execution.

func (*CmdOutput) ReadStderr

func (o *CmdOutput) ReadStderr() string

ReadStderr returns a string representation of the stderr output.

func (*CmdOutput) ReadStdout

func (o *CmdOutput) ReadStdout() string

ReadStdout returns a string representation of the stdout output.

func (*CmdOutput) ReadStdoutTrimNewlines

func (o *CmdOutput) ReadStdoutTrimNewlines() string

ReadStdoutTrimNewlines returns a string representation of stdout, with trailing line breaks removed.

func (*CmdOutput) SetInvocationError

func (o *CmdOutput) SetInvocationError(executionErr error)

SetInvocationError sets the error for an unsuccessful invocation. May not be called if a status code has been set (probably indicating a usage error).

func (*CmdOutput) SetStatus

func (o *CmdOutput) SetStatus(status int)

SetStatus sets the status code for a successful invocation. May not be called if a status code has been set (probably indicating a usage error).

func (*CmdOutput) Status

func (o *CmdOutput) Status() (int, error)

Status returns the status code and any error value from execution. The status code and any output streams are valid only if error is nil.

func (*CmdOutput) Stderr

func (o *CmdOutput) Stderr() []byte

Stderr returns the raw bytes from stderr.

func (*CmdOutput) Stdout

func (o *CmdOutput) Stdout() []byte

Stdout returns the raw bytes from stdout.

type FakeBlockValidator

type FakeBlockValidator struct{}

FakeBlockValidator passes everything as valid

func NewFakeBlockValidator

func NewFakeBlockValidator() *FakeBlockValidator

NewFakeBlockValidator createas a FakeBlockValidator that passes everything as valid.

func (*FakeBlockValidator) ValidateHeaderSemantic

func (fbv *FakeBlockValidator) ValidateHeaderSemantic(ctx context.Context, child *types.BlockHeader, parents types.TipSet) error

ValidateHeaderSemantic does nothing.

func (*FakeBlockValidator) ValidateMessagesSyntax

func (fbv *FakeBlockValidator) ValidateMessagesSyntax(ctx context.Context, messages []*types.SignedMessage) error

ValidateMessagesSyntax does nothing

func (*FakeBlockValidator) ValidateReceiptsSyntax

func (fbv *FakeBlockValidator) ValidateReceiptsSyntax(ctx context.Context, receipts []types.MessageReceipt) error

ValidateReceiptsSyntax does nothing

func (*FakeBlockValidator) ValidateSyntax

func (fbv *FakeBlockValidator) ValidateSyntax(ctx context.Context, blk *types.BlockHeader) error

ValidateSyntax does nothing.

func (*FakeBlockValidator) ValidateUnsignedMessagesSyntax

func (fbv *FakeBlockValidator) ValidateUnsignedMessagesSyntax(ctx context.Context, messages []*types.Message) error

ValidateUnsignedMessagesSyntax does nothing

type HasCid added in v1.2.0

type HasCid interface {
	Cid() cid.Cid
}

HasCid allows two values with CIDs to be compared.

type MessageMaker added in v1.2.0

type MessageMaker struct {
	DefaultGasFeeCap  types.BigInt
	DefaultGasPremium types.BigInt
	DefaultGasUnits   int64
	// contains filtered or unexported fields
}

MessageMaker creates unique, signed messages for use in tests.

func NewMessageMaker added in v1.2.0

func NewMessageMaker(t *testing.T, keys []key.KeyInfo) *MessageMaker

NewMessageMaker creates a new message maker with a set of signing keys.

func (*MessageMaker) Addresses added in v1.2.0

func (mm *MessageMaker) Addresses() []address.Address

Addresses returns the addresses for which this maker can sign messages.

func (*MessageMaker) NewSignedMessage added in v1.2.0

func (mm *MessageMaker) NewSignedMessage(from address.Address, nonce uint64) *types.SignedMessage

NewSignedMessage creates a new signed message.

func (*MessageMaker) NewUnsignedMessage added in v1.2.0

func (mm *MessageMaker) NewUnsignedMessage(from address.Address, nonce uint64) *types.Message

NewUnsignedMessage creates a new message.

func (*MessageMaker) Signer added in v1.2.0

func (mm *MessageMaker) Signer() *MockSigner

Signer returns the signer with which this maker signs messages.

type MockMessagePoolValidator

type MockMessagePoolValidator struct {
	Valid bool
}

MockMessagePoolValidator is a mock validator

func NewMockMessagePoolValidator

func NewMockMessagePoolValidator() *MockMessagePoolValidator

NewMockMessagePoolValidator creates a MockMessagePoolValidator

func (*MockMessagePoolValidator) ValidateSignedMessageSyntax

func (v *MockMessagePoolValidator) ValidateSignedMessageSyntax(ctx context.Context, msg *types.SignedMessage) error

Validate returns true if the mock validator is set to validate the message

type MockSigner added in v1.2.0

type MockSigner struct {
	AddrKeyInfo map[address.Address]key.KeyInfo
	Addresses   []address.Address
	PubKeys     [][]byte
}

MockSigner implements the Signer interface

func NewMockSigner added in v1.2.0

func NewMockSigner(kis []key.KeyInfo) MockSigner

NewMockSigner returns a new mock signer, capable of signing data with keys (addresses derived from) in keyinfo

func NewMockSignersAndKeyInfo added in v1.2.0

func NewMockSignersAndKeyInfo(numSigners int) (MockSigner, []key.KeyInfo)

NewMockSignersAndKeyInfo is a convenience function to generate a mock signers with some keys.

func (MockSigner) GetAddressForPubKey added in v1.2.0

func (ms MockSigner) GetAddressForPubKey(pk []byte) (address.Address, error)

GetAddressForPubKey looks up a KeyInfo address associated with a given PublicKeyForSecpSecretKey for a MockSigner

func (MockSigner) HasAddress added in v1.2.0

func (ms MockSigner) HasAddress(_ context.Context, addr address.Address) (bool, error)

HasAddress returns whether the signer can sign with this address

func (MockSigner) SignBytes added in v1.2.0

func (ms MockSigner) SignBytes(_ context.Context, data []byte, addr address.Address) (*crypto.Signature, error)

SignBytes cryptographically signs `data` using the `addr`.

type ReceiptMaker added in v1.2.0

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

ReceiptMaker generates unique receipts

func NewReceiptMaker added in v1.2.0

func NewReceiptMaker() *ReceiptMaker

NewReceiptMaker creates a new receipt maker

func (*ReceiptMaker) NewReceipt added in v1.2.0

func (rm *ReceiptMaker) NewReceipt() types.MessageReceipt

NewReceipt creates a new distinct receipt.

type StubBlockValidator

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

StubBlockValidator is a mockable block validator.

func NewStubBlockValidator

func NewStubBlockValidator() *StubBlockValidator

NewStubBlockValidator creates a StubBlockValidator that allows errors to configured for blocks passed to the Validate* methods.

func (*StubBlockValidator) StubSyntaxValidationForBlock

func (mbv *StubBlockValidator) StubSyntaxValidationForBlock(blk *types.BlockHeader, err error)

StubSyntaxValidationForBlock stubs an error when the ValidateSyntax is called on the with the given block.

func (*StubBlockValidator) ValidateBlockMsg added in v0.9.1

func (mbv *StubBlockValidator) ValidateBlockMsg(ctx context.Context, blk *types.BlockMsg) pubsub.ValidationResult

ValidateSyntax return nil or error for stubbed block `blk`.

type TestDaemon

type TestDaemon struct {
	Stdin  io.Writer
	Stdout io.Reader
	Stderr io.Reader
	// contains filtered or unexported fields
}

TestDaemon is used to manage a Filecoin daemon instance for testing purposes.

func NewDaemon

func NewDaemon(t *testing.T, options ...func(*TestDaemon)) *TestDaemon

NewDaemon creates a new `TestDaemon`, using the passed in configuration options.

func (*TestDaemon) CmdAddr

func (td *TestDaemon) CmdAddr() (ma.Multiaddr, error)

CmdAddr returns the command address of the test daemon (if it is running).

func (*TestDaemon) CmdToken added in v0.9.7

func (td *TestDaemon) CmdToken() (string, error)

CmdToken returns the command token of the test daemon (if it is running).

func (*TestDaemon) Config

func (td *TestDaemon) Config() *config.Config

Config is a helper to read out the config of the daemon.

func (*TestDaemon) ConnectSuccess

func (td *TestDaemon) ConnectSuccess(remote *TestDaemon) *CmdOutput

ConnectSuccess connects the daemon to another daemon, asserting that the operation was successful.

func (*TestDaemon) CreateAddress

func (td *TestDaemon) CreateAddress() string

CreateAddress adds a new address to the daemons wallet and returns it. equivalent to:

`venus address new`

func (*TestDaemon) CreateStorageMinerAddr

func (td *TestDaemon) CreateStorageMinerAddr(peer *TestDaemon, fromAddr address.Address) address.Address

CreateStorageMinerAddr issues a new message to the network, mines the message and returns the address of the new miner equivalent to:

`venus miner create --from $TEST_ACCOUNT 20`

func (*TestDaemon) GetAddresses

func (td *TestDaemon) GetAddresses() []string

GetAddresses returns all of the addresses of the daemon.

func (*TestDaemon) GetChainHead

func (td *TestDaemon) GetChainHead() []types.BlockHeader

GetChainHead returns the blocks in the head tipset from `td`

func (*TestDaemon) GetDefaultAddress

func (td *TestDaemon) GetDefaultAddress() string

GetDefaultAddress returns the default sender address for this daemon.

func (*TestDaemon) GetID

func (td *TestDaemon) GetID() string

GetID returns the id of the daemon.

func (*TestDaemon) GetMinerAddress

func (td *TestDaemon) GetMinerAddress() address.Address

GetMinerAddress returns the miner address for this daemon.

func (*TestDaemon) MakeMoney

func (td *TestDaemon) MakeMoney(rewards int, peers ...*TestDaemon)

MakeMoney mines a block and ensures that the block has been propagated to all peers.

func (*TestDaemon) MineAndPropagate

func (td *TestDaemon) MineAndPropagate(wait time.Duration, peers ...*TestDaemon)

MineAndPropagate mines a block and ensure the block has propagated to all `peers` by comparing the current head block of `td` with the head block of each peer in `peers`

func (*TestDaemon) MinerSetPrice

func (td *TestDaemon) MinerSetPrice(minerAddr address.Address, fromAddr address.Address, price string, expiry string) cid.Cid

MinerSetPrice creates an ask for a CURRENTLY MINING test daemon and waits for it to appears on chain. It returns the cid of the AddAsk message so other daemons can `message wait` for it.

func (*TestDaemon) MustHaveChainHeadBy

func (td *TestDaemon) MustHaveChainHeadBy(wait time.Duration, peers []*TestDaemon)

MustHaveChainHeadBy ensures all `peers` have the same chain head as `td`, by duration `wait`

func (*TestDaemon) MustUnmarshalChain

func (td *TestDaemon) MustUnmarshalChain(input string) [][]types.BlockHeader

MustUnmarshalChain unmarshals the chain from `input` into a slice of blocks

func (*TestDaemon) ReadStderr

func (td *TestDaemon) ReadStderr() string

ReadStderr returns a string representation of the stderr of the daemon.

func (*TestDaemon) ReadStdout

func (td *TestDaemon) ReadStdout() string

ReadStdout returns a string representation of the stdout of the daemon.

func (*TestDaemon) RepoDir

func (td *TestDaemon) RepoDir() string

RepoDir returns the repo directory of the test daemon.

func (*TestDaemon) Restart

func (td *TestDaemon) Restart() *TestDaemon

Restart restarts the daemon

func (*TestDaemon) Run

func (td *TestDaemon) Run(args ...string) *CmdOutput

Run executes the given command against the test daemon.

func (*TestDaemon) RunFail

func (td *TestDaemon) RunFail(err string, args ...string) *CmdOutput

RunFail is like Run, but asserts that the command exited with an error matching the passed in error.

func (*TestDaemon) RunSuccess

func (td *TestDaemon) RunSuccess(args ...string) *CmdOutput

RunSuccess is like Run, but asserts that the command exited successfully.

func (*TestDaemon) RunWithStdin

func (td *TestDaemon) RunWithStdin(stdin io.Reader, args ...string) *CmdOutput

RunWithStdin executes the given command against the test daemon, allowing to control stdin of the process.

func (*TestDaemon) Shutdown

func (td *TestDaemon) Shutdown()

Shutdown stops the daemon and deletes the repository.

func (*TestDaemon) ShutdownEasy

func (td *TestDaemon) ShutdownEasy()

ShutdownEasy stops the daemon using `SIGINT`.

func (*TestDaemon) ShutdownSuccess

func (td *TestDaemon) ShutdownSuccess()

ShutdownSuccess stops the daemon, asserting that it exited successfully.

func (*TestDaemon) Start

func (td *TestDaemon) Start() *TestDaemon

Start starts up the daemon.

func (*TestDaemon) Stop

func (td *TestDaemon) Stop() *TestDaemon

Stop stops the daemon

func (*TestDaemon) UpdatePeerID

func (td *TestDaemon) UpdatePeerID()

UpdatePeerID updates a currently mining miner's peer ID

func (*TestDaemon) WaitForAPI

func (td *TestDaemon) WaitForAPI() error

WaitForAPI polls if the API on the daemon is available, and blocks until it is.

func (*TestDaemon) WaitForMessageRequireSuccess

func (td *TestDaemon) WaitForMessageRequireSuccess(msgCid cid.Cid) *types.MessageReceipt

WaitForMessageRequireSuccess accepts a message cid and blocks until a message with matching cid is included in a block. The receipt is then inspected to ensure that the corresponding message receipt had a 0 exit code.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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