/
mocks.go
88 lines (72 loc) · 2.51 KB
/
mocks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package action
import (
"fmt"
"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
fabApi "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/peer"
"github.com/pkg/errors"
"github.com/securekey/fabric-snaps/configurationsnap/cmd/configcli/cliconfig"
"github.com/spf13/pflag"
)
// MockInvoker allows mock implementation for the ExecuteTx and Query functions
type MockInvoker func(chaincodeID, fctn string, args [][]byte) ([]byte, error)
// MockAction provides a mock implementation of Action
type MockAction struct {
action
Invoker MockInvoker
Response []byte
}
// Initialize initializes the action
func (a *MockAction) Initialize() error {
if err := cliconfig.InitConfig(); err != nil {
return err
}
logging.SetLevel("", levelFromName(cliconfig.Config().LoggingLevel()))
channelID := cliconfig.Config().ChannelID()
if channelID == "" {
return errors.New("no channel ID specified")
}
return a.initTargetPeers()
}
// ChannelClient creates a new channel client
func (a *MockAction) ChannelClient() (*channel.Client, error) {
panic("not implemented")
}
// Query queries the given chaincode with the given function and args and returns a response
func (a *MockAction) Query(chaincodeID, fctn string, args [][]byte) ([]byte, error) {
return a.Invoker(chaincodeID, fctn, args)
}
// ExecuteTx executes a transaction on the given chaincode with the given function and args
func (a *MockAction) ExecuteTx(chaincodeID, fctn string, args [][]byte) error {
_, err := a.Invoker(chaincodeID, fctn, args)
return err
}
// InitGlobalFlags initializes the global command flags
func InitGlobalFlags(flags *pflag.FlagSet) {
cliconfig.InitLoggingLevel(flags)
cliconfig.InitClientConfigFile(flags)
cliconfig.InitChannelID(flags)
cliconfig.InitUserName(flags)
cliconfig.InitUserPassword(flags)
cliconfig.InitOrgID(flags)
cliconfig.InitMspID(flags)
cliconfig.InitKeyType(flags)
cliconfig.InitEphemeralFlag(flags)
cliconfig.InitSigAlg(flags)
cliconfig.InitTimeout(flags)
}
// NewMockPeer creates a mock peer
func NewMockPeer(url string, mspID string) fabApi.Peer {
config := mocks.NewMockEndpointConfig()
peer, err := peer.New(config, peer.WithURL(url), peer.WithMSPID(mspID))
if err != nil {
panic(fmt.Sprintf("Failed to create peer: %v)", err))
}
return peer
}