@@ -14,6 +14,7 @@ import (
14
14
"github.com/golang/protobuf/proto"
15
15
"github.com/hyperledger/fabric/peer/common"
16
16
pb "github.com/hyperledger/fabric/protos/peer"
17
+ lb "github.com/hyperledger/fabric/protos/peer/lifecycle"
17
18
"github.com/stretchr/testify/assert"
18
19
)
19
20
@@ -31,7 +32,7 @@ func TestChaincodeListCmd(t *testing.T) {
31
32
}
32
33
installedCqrBytes , err := proto .Marshal (installedCqr )
33
34
if err != nil {
34
- t .Fatalf ("Marshale error: %s" , err )
35
+ t .Fatalf ("Marshal error: %s" , err )
35
36
}
36
37
37
38
mockResponse := & pb.ProposalResponse {
@@ -46,61 +47,99 @@ func TestChaincodeListCmd(t *testing.T) {
46
47
BroadcastClient : mockBroadcastClient ,
47
48
}
48
49
49
- // reset channelID, it might have been set by previous test
50
- channelID = ""
51
-
52
- // Get installed chaincodes
53
- installedChaincodesCmd := listCmd (mockCF )
54
-
55
- args := []string {"--installed" }
56
- installedChaincodesCmd .SetArgs (args )
57
- if err := installedChaincodesCmd .Execute (); err != nil {
58
- t .Errorf ("Run chaincode list cmd to get installed chaincodes error:%v" , err )
59
- }
60
-
61
- resetFlags ()
62
-
63
- // Get instantiated chaincodes
64
- instantiatedChaincodesCmd := listCmd (mockCF )
65
- args = []string {"--instantiated" }
66
- instantiatedChaincodesCmd .SetArgs (args )
67
- err = instantiatedChaincodesCmd .Execute ()
68
- assert .Error (t , err , "Run chaincode list cmd to get instantiated chaincodes should fail if invoked without -C flag" )
69
-
70
- args = []string {"--instantiated" , "-C" , "mychannel" }
71
- instantiatedChaincodesCmd .SetArgs (args )
72
- if err := instantiatedChaincodesCmd .Execute (); err != nil {
73
- t .Errorf ("Run chaincode list cmd to get instantiated chaincodes error:%v" , err )
74
- }
75
-
76
- resetFlags ()
77
-
78
- // Wrong case: Set both "--installed" and "--instantiated"
79
- Cmd := listCmd (mockCF )
80
- args = []string {"--installed" , "--instantiated" }
81
- Cmd .SetArgs (args )
82
- err = Cmd .Execute ()
83
- assert .Error (t , err , "Run chaincode list cmd to get instantiated/installed chaincodes should fail if invoked without -C flag" )
84
-
85
- args = []string {"--installed" , "--instantiated" , "-C" , "mychannel" }
86
- Cmd .SetArgs (args )
87
- expectErr := fmt .Errorf ("Must explicitly specify \" --installed\" or \" --instantiated\" " )
88
- if err := Cmd .Execute (); err == nil || err .Error () != expectErr .Error () {
89
- t .Errorf ("Expect error: %s" , expectErr )
90
- }
91
-
92
- resetFlags ()
93
-
94
- // Wrong case: Miss "--intsalled" and "--instantiated"
95
- nilCmd := listCmd (mockCF )
96
-
97
- args = []string {"-C" , "mychannel" }
98
- nilCmd .SetArgs (args )
99
-
100
- expectErr = fmt .Errorf ("Must explicitly specify \" --installed\" or \" --instantiated\" " )
101
- if err := nilCmd .Execute (); err == nil || err .Error () != expectErr .Error () {
102
- t .Errorf ("Expect error: %s" , expectErr )
103
- }
50
+ cmd := listCmd (mockCF )
51
+
52
+ t .Run ("get installed chaincodes - legacy lscc" , func (t * testing.T ) {
53
+ resetFlags ()
54
+
55
+ args := []string {"--installed" }
56
+ cmd .SetArgs (args )
57
+ if err := cmd .Execute (); err != nil {
58
+ t .Errorf ("Run chaincode list cmd to get installed chaincodes error:%v" , err )
59
+ }
60
+ })
61
+
62
+ t .Run ("get installed chaincodes - +lifecycle" , func (t * testing.T ) {
63
+ resetFlags ()
64
+ queryInstalledChaincodeResult := & lb.QueryInstalledChaincodesResult {
65
+ InstalledChaincodes : []* lb.QueryInstalledChaincodesResult_InstalledChaincode {
66
+ {Name : "test1" , Version : "v1.0" , Hash : []byte ("hash1" )},
67
+ {Name : "testcc2" , Version : "v2.0" , Hash : []byte ("hash2" )},
68
+ },
69
+ }
70
+ qicrBytes , err := proto .Marshal (queryInstalledChaincodeResult )
71
+ if err != nil {
72
+ t .Fatalf ("Marshal error: %s" , err )
73
+ }
74
+
75
+ mockResponse .Response = & pb.Response {Status : 200 , Payload : qicrBytes }
76
+
77
+ args := []string {"--installed" , "--newLifecycle" }
78
+ cmd .SetArgs (args )
79
+ if err := cmd .Execute (); err != nil {
80
+ t .Errorf ("Run chaincode list cmd to get installed chaincodes error:%v" , err )
81
+ }
82
+ })
83
+
84
+ t .Run ("get instantiated chaincodes - no channel" , func (t * testing.T ) {
85
+ resetFlags ()
86
+
87
+ args := []string {"--instantiated" }
88
+ cmd .SetArgs (args )
89
+ err = cmd .Execute ()
90
+ assert .Error (t , err , "Run chaincode list cmd to get instantiated chaincodes should fail if invoked without -C flag" )
91
+ })
92
+
93
+ t .Run ("get instantiated chaincodes - no channel" , func (t * testing.T ) {
94
+ resetFlags ()
95
+
96
+ args := []string {"--instantiated" }
97
+ cmd .SetArgs (args )
98
+ err = cmd .Execute ()
99
+ assert .Error (t , err , "Run chaincode list cmd to get instantiated chaincodes should fail if invoked without -C flag" )
100
+ })
101
+
102
+ t .Run ("get instantiated chaincodes - success" , func (t * testing.T ) {
103
+ resetFlags ()
104
+ instantiatedChaincodesCmd := listCmd (mockCF )
105
+ args := []string {"--instantiated" , "-C" , "mychannel" }
106
+ instantiatedChaincodesCmd .SetArgs (args )
107
+ if err := instantiatedChaincodesCmd .Execute (); err != nil {
108
+ t .Errorf ("Run chaincode list cmd to get instantiated chaincodes error:%v" , err )
109
+ }
110
+ })
111
+
112
+ t .Run ("both --installed and --instantiated set - no channel" , func (t * testing.T ) {
113
+ resetFlags ()
114
+
115
+ // Wrong case: Set both "--installed" and "--instantiated"
116
+ cmd = listCmd (mockCF )
117
+ args := []string {"--installed" , "--instantiated" }
118
+ cmd .SetArgs (args )
119
+ err = cmd .Execute ()
120
+ assert .Error (t , err , "Run chaincode list cmd to get instantiated/installed chaincodes should fail if invoked without -C flag" )
121
+ })
122
+
123
+ t .Run ("both --installed and --instantiated set - no channel" , func (t * testing.T ) {
124
+ resetFlags ()
125
+ args := []string {"--installed" , "--instantiated" , "-C" , "mychannel" }
126
+ cmd .SetArgs (args )
127
+ expectErr := fmt .Errorf ("must explicitly specify \" --installed\" or \" --instantiated\" " )
128
+ if err := cmd .Execute (); err == nil || err .Error () != expectErr .Error () {
129
+ t .Errorf ("Expect error: %s" , expectErr )
130
+ }
131
+ })
132
+
133
+ t .Run ("neither --installed nor --instantiated set" , func (t * testing.T ) {
134
+ resetFlags ()
135
+ args := []string {"-C" , "mychannel" }
136
+ cmd .SetArgs (args )
137
+
138
+ expectErr := fmt .Errorf ("must explicitly specify \" --installed\" or \" --instantiated\" " )
139
+ if err := cmd .Execute (); err == nil || err .Error () != expectErr .Error () {
140
+ t .Errorf ("Expect error: %s" , expectErr )
141
+ }
142
+ })
104
143
}
105
144
106
145
func TestChaincodeListFailure (t * testing.T ) {
@@ -132,7 +171,7 @@ func TestChaincodeListFailure(t *testing.T) {
132
171
instantiatedChaincodesCmd .SetArgs (args )
133
172
err = instantiatedChaincodesCmd .Execute ()
134
173
assert .Error (t , err )
135
- assert .Regexp (t , "Bad response: 500 - error message" , err .Error ())
174
+ assert .Regexp (t , "bad response: 500 - error message" , err .Error ())
136
175
}
137
176
138
177
func TestString (t * testing.T ) {
0 commit comments