github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/test/feature/steps/endorser_util.py (about) 1 # Copyright IBM Corp. 2017 All Rights Reserved. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # 15 16 import os 17 import sys 18 import subprocess 19 import time 20 21 try: 22 pbFilePath = "../../bddtests" 23 sys.path.insert(0, pbFilePath) 24 from peer import chaincode_pb2 25 except: 26 print("ERROR! Unable to import the protobuf libraries from the hyperledger/fabric/bddtests directory: {0}".format(sys.exc_info()[0])) 27 sys.exit(1) 28 29 # The default channel ID 30 TEST_CHANNEL_ID = "behavesystest" 31 32 33 def get_chaincode_deploy_spec(projectDir, ccType, path, name, args): 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']) 70 71 72 def instantiate_chaincode(context, chaincode, containers): 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/Admin@org1.example.com/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 96 97 def create_channel(context, containers, orderers, channelId=TEST_CHANNEL_ID): 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/Admin@org1.example.com/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]) 140 141 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]) 198 199 200 def get_orderers(context): 201 orderers = [] 202 for container in context.composition.collectServiceNames(): 203 if container.startswith("orderer"): 204 orderers.append(container) 205 return orderers 206 207 208 def get_peers(context): 209 peers = [] 210 for container in context.composition.collectServiceNames(): 211 if container.startswith("peer"): 212 peers.append(container) 213 return peers 214 215 216 def deploy_chaincode(context, chaincode, containers, channelId=TEST_CHANNEL_ID): 217 for container in containers: 218 assert container in context.composition.collectServiceNames(), "Unknown component '{0}'".format(container) 219 220 orderers = get_orderers(context) 221 peers = get_peers(context) 222 assert orderers != [], "There are no active orderers in this network" 223 224 chaincode.update({"orderers": orderers, 225 "channelID": channelId, 226 }) 227 create_channel(context, containers, orderers, channelId) 228 join_channel(context, peers, orderers, channelId) 229 install_chaincode(context, chaincode, peers) 230 instantiate_chaincode(context, chaincode, containers)