testing

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2019 License: MIT Imports: 13 Imported by: 0

README

Hyperledger Fabric chaincode kit (CCKit)

Chaincode testing tools

Mockstub

Deploying a Hyperledger Fabric blockchain network, chaincode installing and initializing is quite complicated to set up and a long procedure.

The time to re-install / upgrade the code of a smart contract can be reduced by using chaincode dev mode. Normally chaincodes are started and maintained by peer. In “dev” mode, chaincode is built and started by the user. This mode is useful during chaincode development phase for rapid code/build/run/debug cycle turnaround. However, the process of updating the code will still be slow.

The shim package contains a MockStub implementation that wraps calls to a chaincode, simulating its behavior in the HLF peer environment. MockStub don't need to start multiple docker containers with peer, world state, chaincodes and allows to get test results almost immediately. MockStub essentially replaces the SDK and peer enviroment and allow to test chaincode without actually starting your network. It implements almost every function the actual stub does, but in memory.

mockstub

To get started, we need to import the ‘matcher’ functionality from the Ginkgo testing package so we can use different comparison mechanisms like comparing response objects or status codes

Documentation

Index

Constants

View Source
const EventChannelBufferSize = 100

Variables

View Source
var (
	// ErrChaincodeNotExists occurs when attempting to invoke a nonexostent external chaincode
	ErrChaincodeNotExists = errors.New(`chaincode not exists`)
	// ErrUnknownFromArgsType  occurs when attempting to set unknown args in From func
	ErrUnknownFromArgsType = errors.New(`unknown args type to cckit.MockStub.From func`)
)

Functions

func MustProtoMarshal

func MustProtoMarshal(pb proto.Message) []byte

MustProtoMarshal marshals proto.Message, panics if error

func MustProtoTimestamp

func MustProtoTimestamp(t time.Time) *timestamp.Timestamp

MustProtoTimestamp, creates proto.Timestamp, panics if error

func MustProtoUnmarshal

func MustProtoUnmarshal(bb []byte, pm proto.Message) proto.Message

MustProtoUnmarshal unmarshals proto.Message, panics if error

func TransformCreator

func TransformCreator(txCreator ...interface{}) (mspID string, certPEM []byte, err error)

TransformCreator transforms arbitrary tx creator (pmsp.SerializedIdentity etc) to mspID string, certPEM []byte,

Types

type CreatorTransformer

type CreatorTransformer func(...interface{}) (mspID string, certPEM []byte, err error)

type MockStub

type MockStub struct {
	shim.MockStub

	ClearCreatorAfterInvoke bool

	InvokablesFull map[string]*MockStub // invokable this version of MockStub

	ChaincodeEvent *peer.ChaincodeEvent // event in last tx
	// contains filtered or unexported fields
}

MockStub replacement of shim.MockStub with creator mocking facilities

func NewMockStub

func NewMockStub(name string, cc shim.Chaincode) *MockStub

NewMockStub creates MockStub

func (*MockStub) ClearEvents

func (stub *MockStub) ClearEvents()

ClearEvents clears chaincode events channel -

func (*MockStub) EventSubscription

func (stub *MockStub) EventSubscription() chan *peer.ChaincodeEvent

func (*MockStub) From

func (stub *MockStub) From(txCreator ...interface{}) *MockStub

From mock tx creator

func (*MockStub) GetArgs

func (stub *MockStub) GetArgs() [][]byte

GetArgs mocked args

func (*MockStub) GetCreator

func (stub *MockStub) GetCreator() ([]byte, error)

GetCreator mocked

func (*MockStub) GetFunctionAndParameters

func (stub *MockStub) GetFunctionAndParameters() (function string, params []string)

GetFunctionAndParameters mocked

func (*MockStub) GetStringArgs

func (stub *MockStub) GetStringArgs() []string

GetStringArgs get mocked args as strings

func (*MockStub) GetTransient

func (stub *MockStub) GetTransient() (map[string][]byte, error)

func (*MockStub) Init

func (stub *MockStub) Init(iargs ...interface{}) peer.Response

Init func of chaincode - sugared version with autogenerated tx uuid

func (*MockStub) Invoke

func (stub *MockStub) Invoke(funcName string, iargs ...interface{}) peer.Response

Invoke sugared invoke function with autogenerated tx uuid

func (*MockStub) InvokeBytes

func (stub *MockStub) InvokeBytes(args ...[]byte) peer.Response

InvokeByte mock invoke with autogenerated tx uuid

func (*MockStub) InvokeChaincode

func (stub *MockStub) InvokeChaincode(chaincodeName string, args [][]byte, channel string) peer.Response

InvokeChaincode using another MockStub

func (*MockStub) MockCreator

func (stub *MockStub) MockCreator(mspID string, certPEM []byte)

MockCreator of tx

func (*MockStub) MockInit

func (stub *MockStub) MockInit(uuid string, args [][]byte) peer.Response

MockInit mocked init function

func (*MockStub) MockInvoke

func (stub *MockStub) MockInvoke(uuid string, args [][]byte) peer.Response

MockInvoke

func (*MockStub) MockPeerChaincode

func (stub *MockStub) MockPeerChaincode(invokableChaincodeName string, otherStub *MockStub)

MockPeerChaincode link to another MockStub

func (*MockStub) MockQuery

func (stub *MockStub) MockQuery(uuid string, args [][]byte) peer.Response

MockQuery

func (*MockStub) MockedPeerChancodes

func (stub *MockStub) MockedPeerChancodes() []string

MockedPeerChancodes returns names of mocked chaincodes, available for invoke from current stub

func (*MockStub) Query

func (stub *MockStub) Query(funcName string, iargs ...interface{}) peer.Response

func (*MockStub) QueryBytes

func (stub *MockStub) QueryBytes(args ...[]byte) peer.Response

func (*MockStub) RegisterCreatorTransformer

func (stub *MockStub) RegisterCreatorTransformer(creatorTransformer CreatorTransformer) *MockStub

RegisterCreatorTransformer that transforms creator data to MSP_ID and X.509 certificate

func (*MockStub) SetArgs

func (stub *MockStub) SetArgs(args [][]byte)

SetArgs set mocked args

func (*MockStub) SetEvent

func (stub *MockStub) SetEvent(name string, payload []byte) error

SetEvent sets chaincode event

func (*MockStub) WithTransient

func (stub *MockStub) WithTransient(transient map[string][]byte) *MockStub

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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