Skip to content

Commit

Permalink
[FAB-3754]: Add endorser_util for behave tests
Browse files Browse the repository at this point in the history
This CR contains the changes needed for the implementation
of the chaincode and channel functionality.

Change-Id: I9f74abc785179e35b465b982c96877d1b5704e06
Signed-off-by: Latitia M Haskins <latitia.haskins@gmail.com>
  • Loading branch information
lhaskins committed Jun 12, 2017
1 parent 9802bdf commit e9ff71e
Show file tree
Hide file tree
Showing 2 changed files with 270 additions and 55 deletions.
147 changes: 108 additions & 39 deletions test/feature/steps/endorser_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,136 @@

from behave import *
import json


@when(u'a user deploys chaincode at path {path} with {args} with name {name}')
def deploy_impl(context, path, args, name):
pass

@when(u'a user deploys chaincode at path {path} with {args}')
import time
import endorser_util
import config_util


@when(u'a user deploys chaincode at path "{path}" with {args} with name "{name}" to "{containerName}" on channel "{channel}"')
def deploy_impl(context, path, args, name, containerName, channel):
# Be sure there is a transaction block for this channel
config_util.generateChannelConfig(channel+"-1", config_util.CHANNEL_PROFILE, context.composition.projectName)

chaincode = {
"path": path,
"language": "GOLANG",
"name": name,
"channelID": channel+"-1",
"args": args,
}
context.results = endorser_util.deploy_chaincode(context, chaincode, [containerName], channel+"-1")
# Save chaincode name and path and args
context.chaincode = chaincode

@when(u'a user deploys chaincode at path "{path}" with {args} with name "{name}"')
def step_impl(context, path, args, name):
deploy_impl(context, path, args, name, "peer0.org1.example.com", endorser_util.TEST_CHANNEL_ID)

@when(u'a user deploys chaincode at path "{path}" with {args}')
def step_impl(context, path, args):
deploy_impl(context, path, json.loads(args), "mycc")
deploy_impl(context, path, args, "mycc", "peer0.org1.example.com", endorser_util.TEST_CHANNEL_ID)

@when(u'a user deploys chaincode')
def step_impl(context):
deploy_impl(context,
"github.com/hyperledger/fabric//chaincode_example02",
["init", "a", "100" , "b", "200"],
"mycc")

@when(u'a user queries on the chaincode named {name} with args {args} on {component}')
def query_impl(context, name, args, component):
pass

@when(u'a user queries on the chaincode named {name} with args {args}')
"github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02",
'["init", "a", "100" , "b", "200"]',
"mycc",
"peer0.org1.example.com",
(endorser_util.TEST_CHANNEL_ID))

@when(u'a user queries on the channel "{channel}" using chaincode named "{name}" with args {args} on "{component}"')
def query_impl(context, channel, name, args, component):
# Temporarily sleep for 2 sec. This delay should be able to be removed once we start using the python sdk
time.sleep(2)
chaincode = {"args": args,
"name": name}
context.result = endorser_util.query_chaincode(context, chaincode, component, channel+"-1")

@when(u'a user queries on the chaincode named "{name}" with args {args} on "{component}"')
def step_impl(context, name, args, component):
query_impl(context, endorser_util.TEST_CHANNEL_ID, name, args, component)

@when(u'a user queries on the chaincode named "{name}" with args {args}')
def step_impl(context, name, args):
query_impl(context, name, json.loads(args), "peer0")
query_impl(context, endorser_util.TEST_CHANNEL_ID, name, args, "peer0.org1.example.com")

@when(u'a user queries on the chaincode named {name}')
@when(u'a user queries on the chaincode with args {args}')
def step_impl(context, args):
query_impl(context, endorser_util.TEST_CHANNEL_ID, "mycc", args, "peer0.org1.example.com")

@when(u'a user queries on the chaincode named "{name}"')
def step_impl(context, name):
query_impl(context, name, ["query", "a"], "peer0")
query_impl(context, endorser_util.TEST_CHANNEL_ID, name, '["query","a"]', "peer0.org1.example.com")

@when(u'a user queries on the chaincode')
def step_impl(context):
query_impl(context, "mycc", ["query", "a"], "peer0")
query_impl(context, endorser_util.TEST_CHANNEL_ID, "mycc", '["query","a"]', "peer0.org1.example.com")

@when(u'a user invokes {numInvokes} times on the channel "{channel}" using chaincode named "{name}" with args {args} on "{component}"')
def invokes_impl(context, numInvokes, channel, name, args, component):
chaincode = {"args": args,
"name": name}
orderers = endorser_util.get_orderers(context)
for count in range(int(numInvokes)):
context.result = endorser_util.invoke_chaincode(context, chaincode, orderers, component, channel+"-1")

@when(u'a user invokes {numInvokes} times on the channel "{channel}" using chaincode named "{name}" with args {args}')
def step_impl(context, numInvokes, channel, name, args):
invokes_impl(context, numInvokes, channel, name, args, "peer0.org1.example.com")

@when(u'a user invokes {count} times on the chaincode named {name} with args {args}')
def invokes_impl(context, count, name, args):
pass
@when(u'a user invokes {numInvokes} times using chaincode named "{name}" with args {args}')
def step_impl(context, numInvokes, name, args):
invokes_impl(context, numInvokes, endorser_util.TEST_CHANNEL_ID, name, args, "peer0.org1.example.com")

@when(u'a user invokes on the chaincode named {name} with args {args}')
@when(u'a user invokes on the chaincode named "{name}" with args {args}')
def step_impl(context, name, args):
invokes_impl(context, 1, name, json.loads(args))
invokes_impl(context, 1, endorser_util.TEST_CHANNEL_ID, name, args, "peer0.org1.example.com")

@when(u'a user invokes {count} times on the chaincode')
def step_impl(context, count):
invokes_impl(context, count, "mycc", ["txId1", "invoke", "a", 5])
@when(u'a user invokes {numInvokes} times on the chaincode')
def step_impl(context, numInvokes):
invokes_impl(context, numInvokes, endorser_util.TEST_CHANNEL_ID, "mycc", '["invoke","a","b","5"]', "peer0.org1.example.com")

@when(u'a user invokes on the chaincode named {name}')
@when(u'a user invokes on the chaincode named "{name}"')
def step_impl(context, name):
invokes_impl(context, 1, name, ["txId1", "invoke", "a", 5])
invokes_impl(context, 1, endorser_util.TEST_CHANNEL_ID, name, '["invoke","a","b","5"]', "peer0.org1.example.com")

@when(u'a user invokes on the chaincode')
def step_impl(context):
invokes_impl(context, 1, "mycc", ["txId1", "invoke", "a", 5])
invokes_impl(context, 1, endorser_util.TEST_CHANNEL_ID, "mycc", '["invoke","a","b","5"]', "peer0.org1.example.com")

@then(u'the chaincode is deployed')
def step_impl(context):
pass
# Grab the key/value pair from the deploy
key = value = None
print(context.chaincode['args'])
print(json.loads(context.chaincode['args']))
args = json.loads(context.chaincode['args'])
for arg in args:
if arg == 'init':
keyIndex = args.index(arg)+1
valueIndex = args.index(arg)+2
key = args[keyIndex]
value = args[valueIndex]
assert key is not None, "The last message was not a deploy: {0}".format(context.chaincode['args'])

chaincode = {
"path": context.chaincode['path'],
"language": context.chaincode['language'],
"name": context.chaincode['name'],
"args": r'["query","{0}"]'.format(key),
}
orderers = endorser_util.get_orderers(context)
peers = endorser_util.get_peers(context)
result = endorser_util.query_chaincode(context, chaincode, peers[0], endorser_util.TEST_CHANNEL_ID+'-1')
print(result)
assert result[peers[0]] == "Query Result: {0}\n".format(value), "Expect {0} = {1}, received from the deploy: {2}".format(key, value, result[peers[0]])

@then(u'a user receives expected response of {response} from "{peer}"')
def expected_impl(context, response, peer):
assert peer in context.result, "There is no response from {0}".format(peer)
assert context.result[peer] == "Query Result: {0}\n".format(response), "Expected response was {0}; received {1}".format(response, context.result[peer])

@then(u'a user receives expected response of {response}')
def expected_impl(context, response):
pass

@then(u'a user receives expected response')
def step_impl(context):
expected_impl(context, 1000)
def step_impl(context, response):
expected_impl(context, response, "peer0.org1.example.com")
178 changes: 162 additions & 16 deletions test/feature/steps/endorser_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,186 @@
import os
import sys
import subprocess
import time

try:
pbFilePath = "../../bddtests"
sys.path.insert(0, pbFilePath)
from common import common_pb2
from peer import chaincode_pb2
from peer import proposal_pb2
from peer import transaction_pb2
except:
print("ERROR! Unable to import the protobuf libraries from the hyperledger/fabric/bddtests directory: {0}".format(sys.exc_info()[0]))
sys.exit(1)


# The default channel ID
TEST_CHANNEL_ID = "behavesystest"


def get_chaincode_deploy_spec(projectDir, ccType, path, name, args):
pass
subprocess.call(["peer", "chaincode", "package",
"-n", name,
"-c", '{"Args":{0}}'.format(args),
"-p", path,
"configs/{0}/test.file".format(projectDir)], shell=True)
ccDeploymentSpec = chaincode_pb2.ChaincodeDeploymentSpec()
with open("test.file", 'rb') as f:
ccDeploymentSpec.ParseFromString(f.read())
return ccDeploymentSpec


def install_chaincode(context, chaincode, peers):
configDir = "/var/hyperledger/configs/{0}".format(context.composition.projectName)
for peer in peers:
peerParts = peer.split('.')
org = '.'.join(peerParts[1:])
command = ["/bin/bash", "-c",
'"CORE_PEER_MSPCONFIGPATH={0}/peerOrganizations/{1}/users/Admin@{1}/msp'.format(configDir, org),
'CORE_PEER_LOCALMSPID={0}'.format(org),
'CORE_PEER_ID={0}'.format(peer),
'CORE_PEER_ADDRESS={0}:7051'.format(peer),
"peer", "chaincode", "install",
#"--lang", chaincode['language'],
"--name", chaincode['name'],
"--version", str(chaincode.get('version', 0)),
"--chainID", str(chaincode.get('channelID', TEST_CHANNEL_ID)),
"--path", chaincode['path']]
if "orderers" in chaincode:
command = command + ["--orderer", '{0}:7050'.format(chaincode["orderers"][0])]
if "user" in chaincode:
command = command + ["--username", chaincode["user"]]
if "policy" in chaincode:
command = command + ["--policy", chaincode["policy"]]
command.append('"')
ret = context.composition.docker_exec(command, ['cli'])
assert "Error occurred" not in str(ret['cli']), str(ret['cli'])

def install_chaincode(context, chaincode, containers):
pass

def instantiate_chaincode(context, chaincode, containers):
pass
configDir = "/var/hyperledger/configs/{0}".format(context.composition.projectName)
args = chaincode.get('args', '[]').replace('"', r'\"')
command = ["/bin/bash", "-c",
'"CORE_PEER_MSPCONFIGPATH={0}/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp'.format(configDir),
'CORE_PEER_LOCALMSPID=org1.example.com',
'CORE_PEER_ID=peer0.org1.example.com',
'CORE_PEER_ADDRESS=peer0.org1.example.com:7051',
"peer", "chaincode", "instantiate",
#"--lang", chaincode['language'],
"--name", chaincode['name'],
"--version", str(chaincode.get('version', 0)),
"--chainID", str(chaincode.get('channelID', TEST_CHANNEL_ID)),
"--ctor", r"""'{\"Args\": %s}'""" % (args)]
if "orderers" in chaincode:
command = command + ["--orderer", '{0}:7050'.format(chaincode["orderers"][0])]
if "user" in chaincode:
command = command + ["--username", chaincode["user"]]
if "policy" in chaincode:
command = command + ["--policy", chaincode["policy"]]
command.append('"')
ret = context.composition.docker_exec(command, ['peer0.org1.example.com'])
assert "Error occurred" not in str(ret['peer0.org1.example.com']), str(ret['peer0.org1.example.com'])


def create_channel(context, containers, orderers, channelId=TEST_CHANNEL_ID):
pass
configDir = "/var/hyperledger/configs/{0}".format(context.composition.projectName)
ret = context.composition.docker_exec(["ls", configDir], containers)

command = ["/bin/bash", "-c",
'"CORE_PEER_MSPCONFIGPATH={0}/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp'.format(configDir),
'CORE_PEER_LOCALMSPID=org1.example.com',
'CORE_PEER_ID=peer0.org1.example.com',
'CORE_PEER_ADDRESS=peer0.org1.example.com:7051',
"peer", "channel", "create",
"--file", "/var/hyperledger/configs/{0}/{1}.tx".format(context.composition.projectName, channelId),
"--chain", channelId,
"--orderer", '{0}:7050"'.format(orderers[0])]
print("Create command: {0}".format(command))
output = context.composition.docker_exec(command, ['cli'])

for item in output:
assert "Error occurred" not in str(output[item]), str(output[item])

# For now, copy the channel block to the config directory
output = context.composition.docker_exec(["cp",
"{0}.block".format(channelId),
configDir],
['cli'])
output = context.composition.docker_exec(["ls", configDir], ['cli'])
print("Create: {0}".format(output))


def fetch_channel(context, peers, orderers, channelId=TEST_CHANNEL_ID):
configDir = "/var/hyperledger/configs/{0}".format(context.composition.projectName)
for peer in peers:
peerParts = peer.split('.')
org = '.'.join(peerParts[1:])
command = ["/bin/bash", "-c",
'"CORE_PEER_MSPCONFIGPATH={0}/peerOrganizations/{1}/users/Admin@{1}/msp'.format(configDir, org),
"peer", "channel", "fetch", "config",
"/var/hyperledger/configs/{0}/{1}.block".format(context.composition.projectName, channelId),
"--file", "/var/hyperledger/configs/{0}/{1}.tx".format(context.composition.projectName, channelId),
"--chain", channelId,
"--orderer", '{0}:7050"'.format(orderers[0])]
output = context.composition.docker_exec(command, [peer])
print("Fetch: {0}".format(str(output)))
assert "Error occurred" not in str(output[peer]), str(output[peer])

def join_channel(context, peers, orderers, channelId=TEST_CHANNEL_ID):
pass

def invoke_chaincode(context, chaincode, orderers, containers, channelId=TEST_CHANNEL_ID):
pass
def join_channel(context, peers, orderers, channelId=TEST_CHANNEL_ID):
configDir = "/var/hyperledger/configs/{0}".format(context.composition.projectName)

# fetch_channel(context, peers, orderers, channelId)

for peer in peers:
peerParts = peer.split('.')
org = '.'.join(peerParts[1:])
command = ["/bin/bash", "-c",
'"CORE_PEER_MSPCONFIGPATH={0}/peerOrganizations/{1}/users/Admin@{1}/msp'.format(configDir, org),
"peer", "channel", "join",
"--blockpath", '/var/hyperledger/configs/{0}/{1}.block"'.format(context.composition.projectName, channelId)]
count = 0
output = "Error"
while count < 5 and "Error" in output:
output = context.composition.docker_exec(command, [peer])
print("Join: {0}".format(str(output)))
time.sleep(2)
count = count + 1
output = output[peer]

# If the LedgerID doesn't already exist check for other errors
if "due to LedgerID already exists" not in output:
assert "Error occurred" not in str(output), str(output)


def invoke_chaincode(context, chaincode, orderers, peer, channelId=TEST_CHANNEL_ID):
configDir = "/var/hyperledger/configs/{0}".format(context.composition.projectName)
args = chaincode.get('args', '[]').replace('"', r'\"')
peerParts = peer.split('.')
org = '.'.join(peerParts[1:])
command = ["/bin/bash", "-c",
'"CORE_PEER_MSPCONFIGPATH={0}/peerOrganizations/{1}/users/Admin@{1}/msp'.format(configDir, org),
"peer", "chaincode", "invoke",
"--name", chaincode['name'],
"--ctor", r"""'{\"Args\": %s}'""" % (args),
"--chainID", channelId,
"--orderer", '{0}:7050"'.format(orderers[0])]
output = context.composition.docker_exec(command, [peer])
print("Invoke[{0}]: {1}".format(command, str(output)))
assert "Error occurred" not in output[peer], output[peer]


def query_chaincode(context, chaincode, peer, channelId=TEST_CHANNEL_ID):
configDir = "/var/hyperledger/configs/{0}".format(context.composition.projectName)
peerParts = peer.split('.')
org = '.'.join(peerParts[1:])
args = chaincode.get('args', '[]').replace('"', r'\"')
command = ["/bin/bash", "-c",
'"CORE_PEER_MSPCONFIGPATH={0}/peerOrganizations/{1}/users/Admin@{1}/msp'.format(configDir, org),
"peer", "chaincode", "query",
"--name", chaincode['name'],
"--ctor", r"""'{\"Args\": %s}'""" % (args),
"--chainID", channelId, '"']
print("Query Exec command: {0}".format(command))
return context.composition.docker_exec(command, [peer])

def query_chaincode(context, chaincode, containers, channelId=TEST_CHANNEL_ID):
pass

def get_orderers(context):
orderers = []
Expand All @@ -78,7 +221,10 @@ def deploy_chaincode(context, chaincode, containers, channelId=TEST_CHANNEL_ID):
peers = get_peers(context)
assert orderers != [], "There are no active orderers in this network"

chaincode.update({"orderers": orderers,
"channelID": channelId,
})
create_channel(context, containers, orderers, channelId)
join_channel(context, peers, orderers, channelId)
install_chaincode(context, chaincode, containers)
install_chaincode(context, chaincode, peers)
instantiate_chaincode(context, chaincode, containers)

0 comments on commit e9ff71e

Please sign in to comment.