16
16
import os
17
17
import sys
18
18
import subprocess
19
+ import time
19
20
20
21
try :
21
22
pbFilePath = "../../bddtests"
22
23
sys .path .insert (0 , pbFilePath )
23
- from common import common_pb2
24
24
from peer import chaincode_pb2
25
- from peer import proposal_pb2
26
- from peer import transaction_pb2
27
25
except :
28
26
print ("ERROR! Unable to import the protobuf libraries from the hyperledger/fabric/bddtests directory: {0}" .format (sys .exc_info ()[0 ]))
29
27
sys .exit (1 )
30
28
31
-
32
29
# The default channel ID
33
30
TEST_CHANNEL_ID = "behavesystest"
34
31
35
32
36
33
def get_chaincode_deploy_spec (projectDir , ccType , path , name , args ):
37
- pass
34
+ subprocess .call (["peer" , "chaincode" , "package" ,
35
+ "-n" , name ,
36
+ "-c" , '{"Args":{0}}' .format (args ),
37
+ "-p" , path ,
38
+ "configs/{0}/test.file" .format (projectDir )], shell = True )
39
+ ccDeploymentSpec = chaincode_pb2 .ChaincodeDeploymentSpec ()
40
+ with open ("test.file" , 'rb' ) as f :
41
+ ccDeploymentSpec .ParseFromString (f .read ())
42
+ return ccDeploymentSpec
43
+
44
+
45
+ def install_chaincode (context , chaincode , peers ):
46
+ configDir = "/var/hyperledger/configs/{0}" .format (context .composition .projectName )
47
+ for peer in peers :
48
+ peerParts = peer .split ('.' )
49
+ org = '.' .join (peerParts [1 :])
50
+ command = ["/bin/bash" , "-c" ,
51
+ '"CORE_PEER_MSPCONFIGPATH={0}/peerOrganizations/{1}/users/Admin@{1}/msp' .format (configDir , org ),
52
+ 'CORE_PEER_LOCALMSPID={0}' .format (org ),
53
+ 'CORE_PEER_ID={0}' .format (peer ),
54
+ 'CORE_PEER_ADDRESS={0}:7051' .format (peer ),
55
+ "peer" , "chaincode" , "install" ,
56
+ #"--lang", chaincode['language'],
57
+ "--name" , chaincode ['name' ],
58
+ "--version" , str (chaincode .get ('version' , 0 )),
59
+ "--chainID" , str (chaincode .get ('channelID' , TEST_CHANNEL_ID )),
60
+ "--path" , chaincode ['path' ]]
61
+ if "orderers" in chaincode :
62
+ command = command + ["--orderer" , '{0}:7050' .format (chaincode ["orderers" ][0 ])]
63
+ if "user" in chaincode :
64
+ command = command + ["--username" , chaincode ["user" ]]
65
+ if "policy" in chaincode :
66
+ command = command + ["--policy" , chaincode ["policy" ]]
67
+ command .append ('"' )
68
+ ret = context .composition .docker_exec (command , ['cli' ])
69
+ assert "Error occurred" not in str (ret ['cli' ]), str (ret ['cli' ])
38
70
39
- def install_chaincode (context , chaincode , containers ):
40
- pass
41
71
42
72
def instantiate_chaincode (context , chaincode , containers ):
43
- pass
73
+ configDir = "/var/hyperledger/configs/{0}" .format (context .composition .projectName )
74
+ args = chaincode .get ('args' , '[]' ).replace ('"' , r'\"' )
75
+ command = ["/bin/bash" , "-c" ,
76
+ '"CORE_PEER_MSPCONFIGPATH={0}/peerOrganizations/org1.example.com/users/[email protected] /msp' .
format (
configDir ),
77
+ 'CORE_PEER_LOCALMSPID=org1.example.com' ,
78
+ 'CORE_PEER_ID=peer0.org1.example.com' ,
79
+ 'CORE_PEER_ADDRESS=peer0.org1.example.com:7051' ,
80
+ "peer" , "chaincode" , "instantiate" ,
81
+ #"--lang", chaincode['language'],
82
+ "--name" , chaincode ['name' ],
83
+ "--version" , str (chaincode .get ('version' , 0 )),
84
+ "--chainID" , str (chaincode .get ('channelID' , TEST_CHANNEL_ID )),
85
+ "--ctor" , r"""'{\"Args\": %s}'""" % (args )]
86
+ if "orderers" in chaincode :
87
+ command = command + ["--orderer" , '{0}:7050' .format (chaincode ["orderers" ][0 ])]
88
+ if "user" in chaincode :
89
+ command = command + ["--username" , chaincode ["user" ]]
90
+ if "policy" in chaincode :
91
+ command = command + ["--policy" , chaincode ["policy" ]]
92
+ command .append ('"' )
93
+ ret = context .composition .docker_exec (command , ['peer0.org1.example.com' ])
94
+ assert "Error occurred" not in str (ret ['peer0.org1.example.com' ]), str (ret ['peer0.org1.example.com' ])
95
+
44
96
45
97
def create_channel (context , containers , orderers , channelId = TEST_CHANNEL_ID ):
46
- pass
98
+ configDir = "/var/hyperledger/configs/{0}" .format (context .composition .projectName )
99
+ ret = context .composition .docker_exec (["ls" , configDir ], containers )
100
+
101
+ command = ["/bin/bash" , "-c" ,
102
+ '"CORE_PEER_MSPCONFIGPATH={0}/peerOrganizations/org1.example.com/users/[email protected] /msp' .
format (
configDir ),
103
+ 'CORE_PEER_LOCALMSPID=org1.example.com' ,
104
+ 'CORE_PEER_ID=peer0.org1.example.com' ,
105
+ 'CORE_PEER_ADDRESS=peer0.org1.example.com:7051' ,
106
+ "peer" , "channel" , "create" ,
107
+ "--file" , "/var/hyperledger/configs/{0}/{1}.tx" .format (context .composition .projectName , channelId ),
108
+ "--chain" , channelId ,
109
+ "--orderer" , '{0}:7050"' .format (orderers [0 ])]
110
+ print ("Create command: {0}" .format (command ))
111
+ output = context .composition .docker_exec (command , ['cli' ])
112
+
113
+ for item in output :
114
+ assert "Error occurred" not in str (output [item ]), str (output [item ])
115
+
116
+ # For now, copy the channel block to the config directory
117
+ output = context .composition .docker_exec (["cp" ,
118
+ "{0}.block" .format (channelId ),
119
+ configDir ],
120
+ ['cli' ])
121
+ output = context .composition .docker_exec (["ls" , configDir ], ['cli' ])
122
+ print ("Create: {0}" .format (output ))
123
+
124
+
125
+ def fetch_channel (context , peers , orderers , channelId = TEST_CHANNEL_ID ):
126
+ configDir = "/var/hyperledger/configs/{0}" .format (context .composition .projectName )
127
+ for peer in peers :
128
+ peerParts = peer .split ('.' )
129
+ org = '.' .join (peerParts [1 :])
130
+ command = ["/bin/bash" , "-c" ,
131
+ '"CORE_PEER_MSPCONFIGPATH={0}/peerOrganizations/{1}/users/Admin@{1}/msp' .format (configDir , org ),
132
+ "peer" , "channel" , "fetch" , "config" ,
133
+ "/var/hyperledger/configs/{0}/{1}.block" .format (context .composition .projectName , channelId ),
134
+ "--file" , "/var/hyperledger/configs/{0}/{1}.tx" .format (context .composition .projectName , channelId ),
135
+ "--chain" , channelId ,
136
+ "--orderer" , '{0}:7050"' .format (orderers [0 ])]
137
+ output = context .composition .docker_exec (command , [peer ])
138
+ print ("Fetch: {0}" .format (str (output )))
139
+ assert "Error occurred" not in str (output [peer ]), str (output [peer ])
47
140
48
- def join_channel (context , peers , orderers , channelId = TEST_CHANNEL_ID ):
49
- pass
50
141
51
- def invoke_chaincode (context , chaincode , orderers , containers , channelId = TEST_CHANNEL_ID ):
52
- pass
142
+ def join_channel (context , peers , orderers , channelId = TEST_CHANNEL_ID ):
143
+ configDir = "/var/hyperledger/configs/{0}" .format (context .composition .projectName )
144
+
145
+ # fetch_channel(context, peers, orderers, channelId)
146
+
147
+ for peer in peers :
148
+ peerParts = peer .split ('.' )
149
+ org = '.' .join (peerParts [1 :])
150
+ command = ["/bin/bash" , "-c" ,
151
+ '"CORE_PEER_MSPCONFIGPATH={0}/peerOrganizations/{1}/users/Admin@{1}/msp' .format (configDir , org ),
152
+ "peer" , "channel" , "join" ,
153
+ "--blockpath" , '/var/hyperledger/configs/{0}/{1}.block"' .format (context .composition .projectName , channelId )]
154
+ count = 0
155
+ output = "Error"
156
+ while count < 5 and "Error" in output :
157
+ output = context .composition .docker_exec (command , [peer ])
158
+ print ("Join: {0}" .format (str (output )))
159
+ time .sleep (2 )
160
+ count = count + 1
161
+ output = output [peer ]
162
+
163
+ # If the LedgerID doesn't already exist check for other errors
164
+ if "due to LedgerID already exists" not in output :
165
+ assert "Error occurred" not in str (output ), str (output )
166
+
167
+
168
+ def invoke_chaincode (context , chaincode , orderers , peer , channelId = TEST_CHANNEL_ID ):
169
+ configDir = "/var/hyperledger/configs/{0}" .format (context .composition .projectName )
170
+ args = chaincode .get ('args' , '[]' ).replace ('"' , r'\"' )
171
+ peerParts = peer .split ('.' )
172
+ org = '.' .join (peerParts [1 :])
173
+ command = ["/bin/bash" , "-c" ,
174
+ '"CORE_PEER_MSPCONFIGPATH={0}/peerOrganizations/{1}/users/Admin@{1}/msp' .format (configDir , org ),
175
+ "peer" , "chaincode" , "invoke" ,
176
+ "--name" , chaincode ['name' ],
177
+ "--ctor" , r"""'{\"Args\": %s}'""" % (args ),
178
+ "--chainID" , channelId ,
179
+ "--orderer" , '{0}:7050"' .format (orderers [0 ])]
180
+ output = context .composition .docker_exec (command , [peer ])
181
+ print ("Invoke[{0}]: {1}" .format (command , str (output )))
182
+ assert "Error occurred" not in output [peer ], output [peer ]
183
+
184
+
185
+ def query_chaincode (context , chaincode , peer , channelId = TEST_CHANNEL_ID ):
186
+ configDir = "/var/hyperledger/configs/{0}" .format (context .composition .projectName )
187
+ peerParts = peer .split ('.' )
188
+ org = '.' .join (peerParts [1 :])
189
+ args = chaincode .get ('args' , '[]' ).replace ('"' , r'\"' )
190
+ command = ["/bin/bash" , "-c" ,
191
+ '"CORE_PEER_MSPCONFIGPATH={0}/peerOrganizations/{1}/users/Admin@{1}/msp' .format (configDir , org ),
192
+ "peer" , "chaincode" , "query" ,
193
+ "--name" , chaincode ['name' ],
194
+ "--ctor" , r"""'{\"Args\": %s}'""" % (args ),
195
+ "--chainID" , channelId , '"' ]
196
+ print ("Query Exec command: {0}" .format (command ))
197
+ return context .composition .docker_exec (command , [peer ])
53
198
54
- def query_chaincode (context , chaincode , containers , channelId = TEST_CHANNEL_ID ):
55
- pass
56
199
57
200
def get_orderers (context ):
58
201
orderers = []
@@ -78,7 +221,10 @@ def deploy_chaincode(context, chaincode, containers, channelId=TEST_CHANNEL_ID):
78
221
peers = get_peers (context )
79
222
assert orderers != [], "There are no active orderers in this network"
80
223
224
+ chaincode .update ({"orderers" : orderers ,
225
+ "channelID" : channelId ,
226
+ })
81
227
create_channel (context , containers , orderers , channelId )
82
228
join_channel (context , peers , orderers , channelId )
83
- install_chaincode (context , chaincode , containers )
229
+ install_chaincode (context , chaincode , peers )
84
230
instantiate_chaincode (context , chaincode , containers )
0 commit comments