github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/test/feature/steps/endorser_impl.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 from behave import * 17 import json 18 import time 19 import endorser_util 20 import config_util 21 22 23 @when(u'a user deploys chaincode at path "{path}" with {args} with name "{name}" to "{containerName}" on channel "{channel}"') 24 def deploy_impl(context, path, args, name, containerName, channel): 25 # Be sure there is a transaction block for this channel 26 config_util.generateChannelConfig(channel, config_util.CHANNEL_PROFILE, context.composition.projectName) 27 28 chaincode = { 29 "path": path, 30 "language": "GOLANG", 31 "name": name, 32 "channelID": channel, 33 "args": args, 34 } 35 context.results = endorser_util.deploy_chaincode(context, chaincode, [containerName], channel) 36 # Save chaincode name and path and args 37 context.chaincode = chaincode 38 39 @when(u'a user deploys chaincode at path "{path}" with {args} with name "{name}" on channel "{channel}"') 40 def step_impl(context, path, args, name, channel): 41 deploy_impl(context, path, args, name, "peer0.org1.example.com", channel) 42 43 @when(u'a user deploys chaincode at path "{path}" with {args} with name "{name}"') 44 def step_impl(context, path, args, name): 45 deploy_impl(context, path, args, name, "peer0.org1.example.com", endorser_util.TEST_CHANNEL_ID) 46 47 @when(u'a user deploys chaincode at path "{path}" with {args}') 48 def step_impl(context, path, args): 49 deploy_impl(context, path, args, "mycc", "peer0.org1.example.com", endorser_util.TEST_CHANNEL_ID) 50 51 @when(u'a user deploys chaincode') 52 def step_impl(context): 53 deploy_impl(context, 54 "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", 55 '["init", "a", "100" , "b", "200"]', 56 "mycc", 57 "peer0.org1.example.com", 58 (endorser_util.TEST_CHANNEL_ID)) 59 60 @when(u'a user queries on the channel "{channel}" using chaincode named "{name}" with args {args} on "{component}"') 61 def query_impl(context, channel, name, args, component): 62 # Temporarily sleep for 2 sec. This delay should be able to be removed once we start using the python sdk 63 time.sleep(2) 64 chaincode = {"args": args, 65 "name": name} 66 context.result = endorser_util.query_chaincode(context, chaincode, component, channel) 67 68 @when(u'a user queries on the chaincode named "{name}" with args {args} on "{component}"') 69 def step_impl(context, name, args, component): 70 query_impl(context, endorser_util.TEST_CHANNEL_ID, name, args, component) 71 72 @when(u'a user queries on the chaincode named "{name}" with args {args}') 73 def step_impl(context, name, args): 74 query_impl(context, endorser_util.TEST_CHANNEL_ID, name, args, "peer0.org1.example.com") 75 76 @when(u'a user queries on the chaincode with args {args}') 77 def step_impl(context, args): 78 query_impl(context, endorser_util.TEST_CHANNEL_ID, "mycc", args, "peer0.org1.example.com") 79 80 @when(u'a user queries on the chaincode named "{name}"') 81 def step_impl(context, name): 82 query_impl(context, endorser_util.TEST_CHANNEL_ID, name, '["query","a"]', "peer0.org1.example.com") 83 84 @when(u'a user queries on the chaincode') 85 def step_impl(context): 86 query_impl(context, endorser_util.TEST_CHANNEL_ID, "mycc", '["query","a"]', "peer0.org1.example.com") 87 88 @when(u'a user invokes {numInvokes} times on the channel "{channel}" using chaincode named "{name}" with args {args} on "{component}"') 89 def invokes_impl(context, numInvokes, channel, name, args, component): 90 chaincode = {"args": args, 91 "name": name} 92 orderers = endorser_util.get_orderers(context) 93 for count in range(int(numInvokes)): 94 context.result = endorser_util.invoke_chaincode(context, chaincode, orderers, component, channel) 95 96 @when(u'a user invokes {numInvokes} times on the channel "{channel}" using chaincode named "{name}" with args {args}') 97 def step_impl(context, numInvokes, channel, name, args): 98 invokes_impl(context, numInvokes, channel, name, args, "peer0.org1.example.com") 99 100 @when(u'a user invokes {numInvokes} times using chaincode named "{name}" with args {args}') 101 def step_impl(context, numInvokes, name, args): 102 invokes_impl(context, numInvokes, endorser_util.TEST_CHANNEL_ID, name, args, "peer0.org1.example.com") 103 104 @when(u'a user invokes on the chaincode named "{name}" with args {args}') 105 def step_impl(context, name, args): 106 invokes_impl(context, 1, endorser_util.TEST_CHANNEL_ID, name, args, "peer0.org1.example.com") 107 108 @when(u'a user invokes {numInvokes} times on the chaincode') 109 def step_impl(context, numInvokes): 110 invokes_impl(context, numInvokes, endorser_util.TEST_CHANNEL_ID, "mycc", '["invoke","a","b","5"]', "peer0.org1.example.com") 111 112 @when(u'a user invokes on the chaincode named "{name}"') 113 def step_impl(context, name): 114 invokes_impl(context, 1, endorser_util.TEST_CHANNEL_ID, name, '["invoke","a","b","5"]', "peer0.org1.example.com") 115 116 @when(u'a user invokes on the chaincode') 117 def step_impl(context): 118 invokes_impl(context, 1, endorser_util.TEST_CHANNEL_ID, "mycc", '["invoke","a","b","5"]', "peer0.org1.example.com") 119 120 @then(u'the chaincode is deployed') 121 def step_impl(context): 122 # Temporarily sleep for 2 sec. This delay should be able to be removed once we start using the python sdk 123 time.sleep(2) 124 125 # Grab the key/value pair from the deploy 126 key = value = None 127 print(context.chaincode['args']) 128 print(json.loads(context.chaincode['args'])) 129 args = json.loads(context.chaincode['args']) 130 for arg in args: 131 if arg == 'init': 132 keyIndex = args.index(arg)+1 133 valueIndex = args.index(arg)+2 134 key = args[keyIndex] 135 value = args[valueIndex] 136 assert key is not None, "The last message was not a deploy: {0}".format(context.chaincode['args']) 137 138 chaincode = { 139 "path": context.chaincode['path'], 140 "language": context.chaincode['language'], 141 "name": context.chaincode['name'], 142 "args": r'["query","{0}"]'.format(key), 143 } 144 orderers = endorser_util.get_orderers(context) 145 peers = endorser_util.get_peers(context) 146 result = endorser_util.query_chaincode(context, chaincode, peers[0], endorser_util.TEST_CHANNEL_ID) 147 print(result) 148 assert result[peers[0]] == "Query Result: {0}\n".format(value), "Expect {0} = {1}, received from the deploy: {2}".format(key, value, result[peers[0]]) 149 150 @then(u'a user receives expected response of {response} from "{peer}"') 151 def expected_impl(context, response, peer): 152 assert peer in context.result, "There is no response from {0}".format(peer) 153 assert context.result[peer] == "Query Result: {0}\n".format(response), "Expected response was {0}; received {1}".format(response, context.result[peer]) 154 155 @then(u'a user receives an error response of {response} from "{peer}"') 156 def step_impl(context, response, peer): 157 assert peer in context.result, "There is no response from {0}".format(peer) 158 assert context.result[peer] == "Error: {0}\n".format(response), "Expected response was {0}; received {1}".format(response, context.result[peer]) 159 160 @then(u'a user receives expected response of {response}') 161 def step_impl(context, response): 162 expected_impl(context, response, "peer0.org1.example.com")