Skip to content

Commit fd03063

Browse files
Anil Ambatiwlahti
Anil Ambati
authored andcommitted
[FAB-3927] Add tests for invoke,query,instantiate cmds
Add tests for invoke, query and instantiate peer chaincode commands. Change-Id: If9aa1fe85e027ef9d5ec7101395fe11a1c7db33d Signed-off-by: Anil Ambati <[email protected]>
1 parent 010dcf6 commit fd03063

11 files changed

+588
-61
lines changed

peer/chaincode/common.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ func chaincodeInvokeOrQuery(cmd *cobra.Command, args []string, invoke bool, cf *
101101
return err
102102
}
103103

104-
proposalResp, err := ChaincodeInvokeOrQuery(spec, chainID, invoke, cf.Signer, cf.EndorserClient, cf.BroadcastClient)
104+
proposalResp, err := ChaincodeInvokeOrQuery(spec, chainID, invoke,
105+
cf.Signer, cf.EndorserClient, cf.BroadcastClient)
105106
if err != nil {
106107
return err
107108
}
@@ -137,7 +138,8 @@ func checkChaincodeCmdParams(cmd *cobra.Command) error {
137138
return fmt.Errorf("Must supply value for %s name parameter.", chainFuncName)
138139
}
139140

140-
if cmd.Name() == instantiate_cmdname || cmd.Name() == install_cmdname || cmd.Name() == upgrade_cmdname || cmd.Name() == package_cmdname {
141+
if cmd.Name() == instantiate_cmdname || cmd.Name() == install_cmdname ||
142+
cmd.Name() == upgrade_cmdname || cmd.Name() == package_cmdname {
141143
if chaincodeVersion == common.UndefinedParamValue {
142144
return fmt.Errorf("Chaincode version is not provided for %s", cmd.Name())
143145
}
@@ -222,21 +224,21 @@ func InitCmdFactory(isEndorserRequired, isOrdererRequired bool) (*ChaincodeCmdFa
222224
var err error
223225
var endorserClient pb.EndorserClient
224226
if isEndorserRequired {
225-
endorserClient, err = common.GetEndorserClient()
227+
endorserClient, err = common.GetEndorserClientFnc()
226228
if err != nil {
227229
return nil, fmt.Errorf("Error getting endorser client %s: %s", chainFuncName, err)
228230
}
229231
}
230232

231-
signer, err := common.GetDefaultSigner()
233+
signer, err := common.GetDefaultSignerFnc()
232234
if err != nil {
233235
return nil, fmt.Errorf("Error getting default signer: %s", err)
234236
}
235237

236238
var broadcastClient common.BroadcastClient
237239
if isOrdererRequired {
238240
if len(orderingEndpoint) == 0 {
239-
orderingEndpoints, err := common.GetOrdererEndpointOfChain(chainID, signer, endorserClient)
241+
orderingEndpoints, err := common.GetOrdererEndpointOfChainFnc(chainID, signer, endorserClient)
240242
if err != nil {
241243
return nil, fmt.Errorf("Error getting (%s) orderer endpoint: %s", chainID, err)
242244
}
@@ -247,7 +249,7 @@ func InitCmdFactory(isEndorserRequired, isOrdererRequired bool) (*ChaincodeCmdFa
247249
orderingEndpoint = orderingEndpoints[0]
248250
}
249251

250-
broadcastClient, err = common.GetBroadcastClient(orderingEndpoint, tls, caFile)
252+
broadcastClient, err = common.GetBroadcastClientFnc(orderingEndpoint, tls, caFile)
251253

252254
if err != nil {
253255
return nil, fmt.Errorf("Error getting broadcast client: %s", err)
@@ -269,7 +271,8 @@ func InitCmdFactory(isEndorserRequired, isOrdererRequired bool) (*ChaincodeCmdFa
269271
//
270272
// NOTE - Query will likely go away as all interactions with the endorser are
271273
// Proposal and ProposalResponses
272-
func ChaincodeInvokeOrQuery(spec *pb.ChaincodeSpec, cID string, invoke bool, signer msp.SigningIdentity, endorserClient pb.EndorserClient, bc common.BroadcastClient) (*pb.ProposalResponse, error) {
274+
func ChaincodeInvokeOrQuery(spec *pb.ChaincodeSpec, cID string, invoke bool,
275+
signer msp.SigningIdentity, endorserClient pb.EndorserClient, bc common.BroadcastClient) (*pb.ProposalResponse, error) {
273276
// Build the ChaincodeInvocationSpec message
274277
invocation := &pb.ChaincodeInvocationSpec{ChaincodeSpec: spec}
275278
if customIDGenAlg != common.UndefinedParamValue {

peer/chaincode/instantiate_test.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package chaincode
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestInstantiateCmd(t *testing.T) {
26+
InitMSP()
27+
28+
mockCF, err := getMockChaincodeCmdFactory()
29+
assert.NoError(t, err, "Error getting mock chaincode command factory")
30+
31+
// basic function tests
32+
var tests = []struct {
33+
args []string
34+
errorExpected bool
35+
errMsg string
36+
}{
37+
{
38+
args: []string{"-n", "example02", "-p", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02",
39+
"-v", "anotherversion", "-c", "{\"Args\": [\"init\",\"a\",\"100\",\"b\",\"200\"]}"},
40+
errorExpected: false,
41+
errMsg: "Run chaincode instantiate cmd error",
42+
},
43+
{
44+
args: []string{},
45+
errorExpected: true,
46+
errMsg: "Expected error executing instantiate command without required options",
47+
},
48+
{
49+
args: []string{"-n", "example02", "-p", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02",
50+
"-c", "{\"Args\": [\"init\",\"a\",\"100\",\"b\",\"200\"]}"},
51+
errorExpected: true,
52+
errMsg: "Expected error executing instantiate command without the -v option",
53+
},
54+
{
55+
args: []string{"-p", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02",
56+
"-v", "anotherversion", "-c", "{\"Args\": [\"init\",\"a\",\"100\",\"b\",\"200\"]}"},
57+
errorExpected: true,
58+
errMsg: "Expected error executing instantiate command without the -n option",
59+
},
60+
{
61+
args: []string{"-n", "example02", "-p", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02",
62+
"-v", "anotherversion"},
63+
errorExpected: true,
64+
errMsg: "Expected error executing instantiate command without the -c option",
65+
},
66+
}
67+
for _, test := range tests {
68+
cmd := instantiateCmd(mockCF)
69+
AddFlags(cmd)
70+
cmd.SetArgs(test.args)
71+
err = cmd.Execute()
72+
checkError(t, err, test.errorExpected, test.errMsg)
73+
}
74+
}
75+
76+
func checkError(t *testing.T, err error, expectedError bool, msg string) {
77+
if expectedError {
78+
assert.Error(t, err, msg)
79+
} else {
80+
assert.NoError(t, err, msg)
81+
}
82+
}

peer/chaincode/invoke_test.go

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package chaincode
18+
19+
import (
20+
"errors"
21+
"testing"
22+
23+
"github.com/hyperledger/fabric/msp"
24+
"github.com/hyperledger/fabric/peer/common"
25+
pb "github.com/hyperledger/fabric/protos/peer"
26+
"github.com/stretchr/testify/assert"
27+
)
28+
29+
func TestInvokeCmd(t *testing.T) {
30+
InitMSP()
31+
mockCF, err := getMockChaincodeCmdFactory()
32+
assert.NoError(t, err, "Error getting mock chaincode command factory")
33+
34+
cmd := invokeCmd(mockCF)
35+
AddFlags(cmd)
36+
args := []string{"-n", "example02", "-p", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02",
37+
"-c", "{\"Args\": [\"invoke\",\"a\",\"b\",\"10\"]}"}
38+
cmd.SetArgs(args)
39+
err = cmd.Execute()
40+
assert.NoError(t, err, "Run chaincode invoke cmd error")
41+
42+
// Error case 1: no orderer endpoints
43+
t.Logf("Start error case 1: no orderer endpoints")
44+
getEndorserClient := common.GetEndorserClientFnc
45+
getOrdererEndpointOfChain := common.GetOrdererEndpointOfChainFnc
46+
getBroadcastClient := common.GetBroadcastClientFnc
47+
getDefaultSigner := common.GetDefaultSignerFnc
48+
defer func() {
49+
common.GetEndorserClientFnc = getEndorserClient
50+
common.GetOrdererEndpointOfChainFnc = getOrdererEndpointOfChain
51+
common.GetBroadcastClientFnc = getBroadcastClient
52+
common.GetDefaultSignerFnc = getDefaultSigner
53+
}()
54+
common.GetEndorserClientFnc = func() (pb.EndorserClient, error) {
55+
return mockCF.EndorserClient, nil
56+
}
57+
common.GetOrdererEndpointOfChainFnc = func(chainID string, signer msp.SigningIdentity, endorserClient pb.EndorserClient) ([]string, error) {
58+
return []string{}, nil
59+
}
60+
cmd = invokeCmd(nil)
61+
AddFlags(cmd)
62+
args = []string{"-n", "example02", "-p", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02",
63+
"-c", "{\"Args\": [\"invoke\",\"a\",\"b\",\"10\"]}"}
64+
cmd.SetArgs(args)
65+
err = cmd.Execute()
66+
assert.Error(t, err)
67+
68+
// Error case 2: getEndorserClient returns error
69+
t.Logf("Start error case 2: getEndorserClient returns error")
70+
common.GetEndorserClientFnc = func() (pb.EndorserClient, error) {
71+
return nil, errors.New("error")
72+
}
73+
err = cmd.Execute()
74+
assert.Error(t, err)
75+
76+
// Error case 3: getDefaultSignerFnc returns error
77+
t.Logf("Start error case 3: getDefaultSignerFnc returns error")
78+
common.GetEndorserClientFnc = func() (pb.EndorserClient, error) {
79+
return mockCF.EndorserClient, nil
80+
}
81+
common.GetDefaultSignerFnc = func() (msp.SigningIdentity, error) {
82+
return nil, errors.New("error")
83+
}
84+
err = cmd.Execute()
85+
assert.Error(t, err)
86+
common.GetDefaultSignerFnc = common.GetDefaultSigner
87+
88+
// Error case 4: getOrdererEndpointOfChainFnc returns error
89+
t.Logf("Start error case 4: getOrdererEndpointOfChainFnc returns error")
90+
common.GetEndorserClientFnc = func() (pb.EndorserClient, error) {
91+
return mockCF.EndorserClient, nil
92+
}
93+
common.GetOrdererEndpointOfChainFnc = func(chainID string, signer msp.SigningIdentity, endorserClient pb.EndorserClient) ([]string, error) {
94+
return nil, errors.New("error")
95+
}
96+
err = cmd.Execute()
97+
assert.Error(t, err)
98+
99+
// Error case 5: getBroadcastClient returns error
100+
t.Logf("Start error case 5: getBroadcastClient returns error")
101+
common.GetOrdererEndpointOfChainFnc = func(chainID string, signer msp.SigningIdentity, endorserClient pb.EndorserClient) ([]string, error) {
102+
return []string{"localhost:9999"}, nil
103+
}
104+
common.GetBroadcastClientFnc = func(orderingEndpoint string, tlsEnabled bool, caFile string) (common.BroadcastClient, error) {
105+
return nil, errors.New("error")
106+
}
107+
err = cmd.Execute()
108+
assert.Error(t, err)
109+
110+
// Success case
111+
t.Logf("Start success case")
112+
common.GetBroadcastClientFnc = func(orderingEndpoint string, tlsEnabled bool, caFile string) (common.BroadcastClient, error) {
113+
return mockCF.BroadcastClient, nil
114+
}
115+
err = cmd.Execute()
116+
assert.NoError(t, err)
117+
}
118+
119+
func TestInvokeCmdEndorseFail(t *testing.T) {
120+
InitMSP()
121+
mockCF, err := getMockChaincodeCmdFactoryWithErr()
122+
assert.NoError(t, err, "Error getting mock chaincode command factory")
123+
124+
cmd := invokeCmd(mockCF)
125+
AddFlags(cmd)
126+
args := []string{"-n", "example02", "-p", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02",
127+
"-c", "{\"Args\": [\"invoke\",\"a\",\"b\",\"10\"]}"}
128+
cmd.SetArgs(args)
129+
err = cmd.Execute()
130+
assert.Error(t, err, "Expected error executing invoke command")
131+
}
132+
133+
// Returns mock chaincode command factory
134+
func getMockChaincodeCmdFactory() (*ChaincodeCmdFactory, error) {
135+
signer, err := common.GetDefaultSigner()
136+
if err != nil {
137+
return nil, err
138+
}
139+
mockResponse := &pb.ProposalResponse{
140+
Response: &pb.Response{Status: 200},
141+
Endorsement: &pb.Endorsement{},
142+
}
143+
mockEndorserClient := common.GetMockEndorserClient(mockResponse, nil)
144+
mockBroadcastClient := common.GetMockBroadcastClient(nil)
145+
mockCF := &ChaincodeCmdFactory{
146+
EndorserClient: mockEndorserClient,
147+
Signer: signer,
148+
BroadcastClient: mockBroadcastClient,
149+
}
150+
return mockCF, nil
151+
}
152+
153+
// Returns mock chaincode command factory that is constructed with an endorser
154+
// client that returns an error for proposal request
155+
func getMockChaincodeCmdFactoryWithErr() (*ChaincodeCmdFactory, error) {
156+
signer, err := common.GetDefaultSigner()
157+
if err != nil {
158+
return nil, err
159+
}
160+
161+
errMsg := "invoke error"
162+
mockEndorerClient := common.GetMockEndorserClient(nil, errors.New(errMsg))
163+
mockBroadcastClient := common.GetMockBroadcastClient(nil)
164+
165+
mockCF := &ChaincodeCmdFactory{
166+
EndorserClient: mockEndorerClient,
167+
Signer: signer,
168+
BroadcastClient: mockBroadcastClient,
169+
}
170+
return mockCF, nil
171+
}

peer/chaincode/package_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestCDSPackage(t *testing.T) {
7272
ccpackfile := pdir + "/ccpack.file"
7373
err := createSignedCDSPackage([]string{"-n", "somecc", "-p", "some/go/package", "-v", "0", ccpackfile}, false)
7474
if err != nil {
75-
t.Fatalf("Run chaincode upgrade cmd error:%v", err)
75+
t.Fatalf("Run chaincode package cmd error:%v", err)
7676
}
7777

7878
b, err := ioutil.ReadFile(ccpackfile)

0 commit comments

Comments
 (0)