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