testing

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2019 License: MIT Imports: 19 Imported by: 4

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 MustConvertFromBytes added in v0.4.4

func MustConvertFromBytes(bb []byte, target interface{}) interface{}

func MustJsonMarshal added in v0.4.4

func MustJsonMarshal(val interface{}) []byte

func MustProtoMarshal added in v0.4.1

func MustProtoMarshal(pb proto.Message) []byte

MustProtoMarshal marshals proto.Message, panics if error

func MustProtoTimestamp added in v0.4.1

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

MustProtoTimestamp, creates proto.Timestamp, panics if error

func MustProtoUnmarshal added in v0.4.3

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 ChannelMockStubs added in v0.4.4

type ChannelMockStubs map[string]*MockStub

type ChannelsMockStubs added in v0.4.4

type ChannelsMockStubs map[string]ChannelMockStubs

type CreatorTransformer

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

type EventSubscription added in v0.4.4

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

func (*EventSubscription) Close added in v0.4.4

func (es *EventSubscription) Close() error

func (*EventSubscription) Errors added in v0.4.4

func (es *EventSubscription) Errors() chan error

func (*EventSubscription) Events added in v0.4.4

func (es *EventSubscription) Events() chan *peer.ChaincodeEvent

type Identities added in v0.4.4

type Identities map[string]*Identity

func IdentitiesFromFiles added in v0.4.4

func IdentitiesFromFiles(mspID string, files map[string]string, getContent identity.GetContent) (Identities, error)

ActorsFromPemFile returns map of CertIdentity, loaded from PEM files

func IdentitiesFromPem added in v0.4.4

func IdentitiesFromPem(mspID string, certPEMs map[string][]byte) (Identities, error)

ActorsFromPem returns CertIdentity (MSP ID and X.509 cert) converted PEM content

type Identity added in v0.4.4

type Identity struct {
	MspId       string
	Certificate *x509.Certificate
}

implements msp.SigningIdentity

func NewIdentity added in v0.4.4

func NewIdentity(mspId string, cert *x509.Certificate) *Identity

func (*Identity) Anonymous added in v0.4.4

func (i *Identity) Anonymous() bool

func (*Identity) ExpiresAt added in v0.4.4

func (i *Identity) ExpiresAt() time.Time

ExpiresAt returns date of certificate expiration

func (*Identity) GetID added in v0.4.4

func (i *Identity) GetID() string

func (*Identity) GetIdentifier added in v0.4.4

func (i *Identity) GetIdentifier() *msp.IdentityIdentifier

func (*Identity) GetMSPIdentifier added in v0.4.4

func (i *Identity) GetMSPIdentifier() string

GetMSPIdentifier returns current MspID of identity

func (*Identity) GetOrganizationalUnits added in v0.4.4

func (i *Identity) GetOrganizationalUnits() []*msp.OUIdentifier

func (*Identity) GetPEM added in v0.4.4

func (i *Identity) GetPEM() []byte

GetPEM certificate encoded to PEM

func (*Identity) GetPublicVersion added in v0.4.4

func (i *Identity) GetPublicVersion() msp.Identity

func (*Identity) GetSubject added in v0.4.4

func (i *Identity) GetSubject() string

func (*Identity) SatisfiesPrincipal added in v0.4.4

func (i *Identity) SatisfiesPrincipal(principal *msppb.MSPPrincipal) error

func (*Identity) Serialize added in v0.4.4

func (i *Identity) Serialize() ([]byte, error)

func (*Identity) Sign added in v0.4.4

func (i *Identity) Sign(msg []byte) ([]byte, error)

func (*Identity) Validate added in v0.4.4

func (i *Identity) Validate() error

func (*Identity) Verify added in v0.4.4

func (i *Identity) Verify(msg []byte, sig []byte) error

type MockInvoker added in v0.4.4

type MockInvoker struct {
	// channel name -> chaincode name
	ChannelCC ChannelsMockStubs
	// contains filtered or unexported fields
}

func NewInvoker added in v0.4.4

func NewInvoker() *MockInvoker

NewInvoker implements hlf-sdk-go

func (*MockInvoker) Chaincode added in v0.4.4

func (mi *MockInvoker) Chaincode(channel string, chaincode string) (*MockStub, error)

func (*MockInvoker) Invoke added in v0.4.4

func (mi *MockInvoker) Invoke(ctx context.Context, from msp.SigningIdentity, channel string, chaincode string, fn string, args [][]byte, transArgs api.TransArgs) (*peer.Response, api.ChaincodeTx, error)

func (*MockInvoker) Query added in v0.4.4

func (mi *MockInvoker) Query(ctx context.Context, from msp.SigningIdentity, channel string, chaincode string, fn string, args [][]byte, transArgs api.TransArgs) (*peer.Response, error)

func (*MockInvoker) Subscribe added in v0.4.4

func (mi *MockInvoker) Subscribe(ctx context.Context, from msp.SigningIdentity, channel, chaincode string) (api.EventCCSubscription, error)

func (*MockInvoker) WithChannel added in v0.4.4

func (mi *MockInvoker) WithChannel(channel string, mockStubs ...*MockStub) *MockInvoker

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 chaincode imitation

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 added in v0.3.0

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 added in v0.3.0

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 added in v0.4.1

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 added in v0.4.1

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 added in v0.3.0

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