Skip to content

Commit

Permalink
Merge pull request #1940 from jeffgarratt/issues/1922/grpcio-pip-install
Browse files Browse the repository at this point in the history
Issues/1922/grpcio pip install: Moved from Makefile to hosts.sh script, vagrant destroy/up required
  • Loading branch information
srderson committed Jun 24, 2016
2 parents 129a62a + 4264676 commit ef5c8c7
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 141 deletions.
10 changes: 1 addition & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,7 @@ unit-test: peer-image gotools
.PHONY: images
images: $(patsubst %,build/image/%/.dummy, $(IMAGES))

build/behave/.grpc-dummy:
sudo pip install -q 'grpcio==0.13.1'
mkdir -p build/behave
touch build/behave/.grpc-dummy

behave-grpc: build/behave/.grpc-dummy


behave-deps: images peer behave-grpc
behave-deps: images peer
behave: behave-deps
@echo "Running behave tests"
@cd bddtests; behave $(BEHAVE_OPTS)
Expand Down
143 changes: 143 additions & 0 deletions bddtests/steps/bdd_grpc_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@

# Copyright IBM Corp. 2016 All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import os
import re
import subprocess
import devops_pb2
import fabric_pb2
import chaincode_pb2

import bdd_test_util

from grpc.beta import implementations

def getSecretForUserRegistration(userRegistration):
return devops_pb2.Secret(enrollId=userRegistration.secretMsg['enrollId'],enrollSecret=userRegistration.secretMsg['enrollSecret'])

def getTxResult(context, enrollId):
'''Returns the TransactionResult using the enrollId supplied'''
assert 'users' in context, "users not found in context. Did you register a user?"
assert 'compose_containers' in context, "compose_containers not found in context"

(channel, userRegistration) = getGRPCChannelAndUser(context, enrollId)
stub = devops_pb2.beta_create_Devops_stub(channel)

txRequest = devops_pb2.TransactionRequest(transactionUuid = context.transactionID)
response = stub.GetTransactionResult(txRequest, 2)
assert response.status == fabric_pb2.Response.SUCCESS, 'Failure getting Transaction Result from {0}, for user "{1}": {2}'.format(userRegistration.composeService,enrollId, response.msg)
# Now grab the TransactionResult from the Msg bytes
txResult = fabric_pb2.TransactionResult()
txResult.ParseFromString(response.msg)
return txResult

def getGRPCChannel(ipAddress):
channel = implementations.insecure_channel(ipAddress, 30303)
print("Returning GRPC for address: {0}".format(ipAddress))
return channel

def getGRPCChannelAndUser(context, enrollId):
'''Returns a tuple of GRPC channel and UserRegistration instance. The channel is open to the composeService that the user registered with.'''
userRegistration = bdd_test_util.getUserRegistration(context, enrollId)

# Get the IP address of the server that the user registered on
ipAddress = bdd_test_util.ipFromContainerNamePart(userRegistration.composeService, context.compose_containers)

channel = getGRPCChannel(ipAddress)

return (channel, userRegistration)


def getDeployment(context, ccAlias):
'''Return a deployment with chaincode alias from prior deployment, or None if not found'''
deployment = None
if 'deployments' in context:
pass
else:
context.deployments = {}
if ccAlias in context.deployments:
deployment = context.deployments[ccAlias]
# else:
# raise Exception("Deployment alias not found: '{0}'. Are you sure you have deployed a chaincode with this alias?".format(ccAlias))
return deployment

def deployChaincode(context, enrollId, chaincodePath, ccAlias, ctor):
'''Deploy a chaincode with the specified alias for the specfied enrollId'''
(channel, userRegistration) = getGRPCChannelAndUser(context, enrollId)
stub = devops_pb2.beta_create_Devops_stub(channel)

# Make sure deployment alias does NOT already exist
assert getDeployment(context, ccAlias) == None, "Deployment alias already exists: '{0}'.".format(ccAlias)

args = getArgsFromContextForUser(context, enrollId)
ccSpec = chaincode_pb2.ChaincodeSpec(type = chaincode_pb2.ChaincodeSpec.GOLANG,
chaincodeID = chaincode_pb2.ChaincodeID(name="",path=chaincodePath),
ctorMsg = chaincode_pb2.ChaincodeInput(function = ctor, args = args))
ccSpec.secureContext = userRegistration.getUserName()
if 'metadata' in context:
ccSpec.metadata = context.metadata
try:
ccDeploymentSpec = stub.Deploy(ccSpec, 60)
ccSpec.chaincodeID.name = ccDeploymentSpec.chaincodeSpec.chaincodeID.name
context.grpcChaincodeSpec = ccSpec
context.deployments[ccAlias] = ccSpec
except:
del stub
raise

def invokeChaincode(context, enrollId, ccAlias, functionName):
# Get the deployment for the supplied chaincode alias
deployedCcSpec = getDeployment(context, ccAlias)
assert deployedCcSpec != None, "Deployment NOT found for chaincode alias '{0}'".format(ccAlias)

# Create a new ChaincodeSpec by copying the deployed one
newChaincodeSpec = chaincode_pb2.ChaincodeSpec()
newChaincodeSpec.CopyFrom(deployedCcSpec)

# Update hte chaincodeSpec ctorMsg for invoke
args = getArgsFromContextForUser(context, enrollId)

chaincodeInput = chaincode_pb2.ChaincodeInput(function = functionName, args = args )
newChaincodeSpec.ctorMsg.CopyFrom(chaincodeInput)

ccInvocationSpec = chaincode_pb2.ChaincodeInvocationSpec(chaincodeSpec = newChaincodeSpec)

(channel, userRegistration) = getGRPCChannelAndUser(context, enrollId)

stub = devops_pb2.beta_create_Devops_stub(channel)
response = stub.Invoke(ccInvocationSpec,2)
return response

def getArgsFromContextForUser(context, enrollId):
# Update the chaincodeSpec ctorMsg for invoke
args = []
if 'table' in context:
# There are function arguments
userRegistration = bdd_test_util.getUserRegistration(context, enrollId)
# Allow the user to specify expressions referencing tags in the args list
pattern = re.compile('\{(.*)\}$')
for arg in context.table[0].cells:
m = pattern.match(arg)
if m:
# tagName reference found in args list
tagName = m.groups()[0]
# make sure the tagName is found in the users tags
assert tagName in userRegistration.tags, "TagName '{0}' not found for user '{1}'".format(tagName, userRegistration.getUserName())
args.append(userRegistration.tags[tagName])
else:
#No tag referenced, pass the arg
args.append(arg)
return args
122 changes: 0 additions & 122 deletions bddtests/steps/bdd_test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
import os
import re
import subprocess
import devops_pb2
import fabric_pb2
import chaincode_pb2

from grpc.beta import implementations

def cli_call(context, arg_list, expect_success=True):
"""Executes a CLI command in a subprocess and return the results.
Expand Down Expand Up @@ -58,10 +53,6 @@ def __init__(self, secretMsg, composeService):
def getUserName(self):
return self.secretMsg['enrollId']

def getSecret(self):
return devops_pb2.Secret(enrollId=self.secretMsg['enrollId'],enrollSecret=self.secretMsg['enrollSecret'])


# Registerses a user on a specific composeService
def registerUser(context, secretMsg, composeService):
userName = secretMsg['enrollId']
Expand Down Expand Up @@ -98,119 +89,6 @@ def ipFromContainerNamePart(namePart, containerDataList):
raise Exception("Could not find container with namePart = {0}".format(namePart))
return ip

def getTxResult(context, enrollId):
'''Returns the TransactionResult using the enrollId supplied'''
assert 'users' in context, "users not found in context. Did you register a user?"
assert 'compose_containers' in context, "compose_containers not found in context"

(channel, userRegistration) = getGRPCChannelAndUser(context, enrollId)
stub = devops_pb2.beta_create_Devops_stub(channel)

txRequest = devops_pb2.TransactionRequest(transactionUuid = context.transactionID)
response = stub.GetTransactionResult(txRequest, 2)
assert response.status == fabric_pb2.Response.SUCCESS, 'Failure getting Transaction Result from {0}, for user "{1}": {2}'.format(userRegistration.composeService,enrollId, response.msg)
# Now grab the TransactionResult from the Msg bytes
txResult = fabric_pb2.TransactionResult()
txResult.ParseFromString(response.msg)
return txResult

def getGRPCChannel(ipAddress):
channel = implementations.insecure_channel(ipAddress, 30303)
print("Returning GRPC for address: {0}".format(ipAddress))
return channel

def getGRPCChannelAndUser(context, enrollId):
'''Returns a tuple of GRPC channel and UserRegistration instance. The channel is open to the composeService that the user registered with.'''
userRegistration = getUserRegistration(context, enrollId)

# Get the IP address of the server that the user registered on
ipAddress = ipFromContainerNamePart(userRegistration.composeService, context.compose_containers)

channel = getGRPCChannel(ipAddress)

return (channel, userRegistration)


def getDeployment(context, ccAlias):
'''Return a deployment with chaincode alias from prior deployment, or None if not found'''
deployment = None
if 'deployments' in context:
pass
else:
context.deployments = {}
if ccAlias in context.deployments:
deployment = context.deployments[ccAlias]
# else:
# raise Exception("Deployment alias not found: '{0}'. Are you sure you have deployed a chaincode with this alias?".format(ccAlias))
return deployment

def deployChaincode(context, enrollId, chaincodePath, ccAlias, ctor):
'''Deploy a chaincode with the specified alias for the specfied enrollId'''
(channel, userRegistration) = getGRPCChannelAndUser(context, enrollId)
stub = devops_pb2.beta_create_Devops_stub(channel)

# Make sure deployment alias does NOT already exist
assert getDeployment(context, ccAlias) == None, "Deployment alias already exists: '{0}'.".format(ccAlias)

args = getArgsFromContextForUser(context, enrollId)
ccSpec = chaincode_pb2.ChaincodeSpec(type = chaincode_pb2.ChaincodeSpec.GOLANG,
chaincodeID = chaincode_pb2.ChaincodeID(name="",path=chaincodePath),
ctorMsg = chaincode_pb2.ChaincodeInput(function = ctor, args = args))
ccSpec.secureContext = userRegistration.getUserName()
if 'metadata' in context:
ccSpec.metadata = context.metadata
try:
ccDeploymentSpec = stub.Deploy(ccSpec, 60)
ccSpec.chaincodeID.name = ccDeploymentSpec.chaincodeSpec.chaincodeID.name
context.grpcChaincodeSpec = ccSpec
context.deployments[ccAlias] = ccSpec
except:
del stub
raise

def invokeChaincode(context, enrollId, ccAlias, functionName):
# Get the deployment for the supplied chaincode alias
deployedCcSpec = getDeployment(context, ccAlias)
assert deployedCcSpec != None, "Deployment NOT found for chaincode alias '{0}'".format(ccAlias)

# Create a new ChaincodeSpec by copying the deployed one
newChaincodeSpec = chaincode_pb2.ChaincodeSpec()
newChaincodeSpec.CopyFrom(deployedCcSpec)

# Update hte chaincodeSpec ctorMsg for invoke
args = getArgsFromContextForUser(context, enrollId)

chaincodeInput = chaincode_pb2.ChaincodeInput(function = functionName, args = args )
newChaincodeSpec.ctorMsg.CopyFrom(chaincodeInput)

ccInvocationSpec = chaincode_pb2.ChaincodeInvocationSpec(chaincodeSpec = newChaincodeSpec)

(channel, userRegistration) = getGRPCChannelAndUser(context, enrollId)

stub = devops_pb2.beta_create_Devops_stub(channel)
response = stub.Invoke(ccInvocationSpec,2)
return response

def getArgsFromContextForUser(context, enrollId):
# Update the chaincodeSpec ctorMsg for invoke
args = []
if 'table' in context:
# There are function arguments
userRegistration = getUserRegistration(context, enrollId)
# Allow the user to specify expressions referencing tags in the args list
pattern = re.compile('\{(.*)\}$')
for arg in context.table[0].cells:
m = pattern.match(arg)
if m:
# tagName reference found in args list
tagName = m.groups()[0]
# make sure the tagName is found in the users tags
assert tagName in userRegistration.tags, "TagName '{0}' not found for user '{1}'".format(tagName, userRegistration.getUserName())
args.append(userRegistration.tags[tagName])
else:
#No tag referenced, pass the arg
args.append(arg)
return args

def getContainerDataValuesFromContext(context, aliases, callback):
"""Returns the IPAddress based upon a name part of the full container name"""
Expand Down
19 changes: 10 additions & 9 deletions bddtests/steps/chaincode_rbac_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import bdd_test_util

import bdd_grpc_util
from grpc.beta import implementations

import fabric_pb2
Expand All @@ -21,11 +22,11 @@
@when(u'user "{enrollId}" requests a new application TCert')
def step_impl(context, enrollId):
assert 'users' in context, "users not found in context. Did you register a user?"
(channel, userRegistration) = bdd_test_util.getGRPCChannelAndUser(context, enrollId)
(channel, userRegistration) = bdd_grpc_util.getGRPCChannelAndUser(context, enrollId)

stub = devops_pb2.beta_create_Devops_stub(channel)

secret = userRegistration.getSecret()
secret = bdd_grpc_util.getSecretForUserRegistration(userRegistration)
response = stub.EXP_GetApplicationTCert(secret,2)
assert response.status == fabric_pb2.Response.SUCCESS, 'Failure getting TCert from {0}, for user "{1}": {2}'.format(userRegistration.composeService,enrollId, response.msg)
tcert = response.msg
Expand All @@ -50,7 +51,7 @@ def step_impl(context, enrollId, tagName):

@when(u'user "{enrollId}" deploys chaincode "{chaincodePath}" aliased as "{ccAlias}" with ctor "{ctor}" and args')
def step_impl(context, enrollId, chaincodePath, ccAlias, ctor):
bdd_test_util.deployChaincode(context, enrollId, chaincodePath, ccAlias, ctor)
bdd_grpc_util.deployChaincode(context, enrollId, chaincodePath, ccAlias, ctor)


@when(u'user "{enrollId}" gives stored value "{tagName}" to "{recipientEnrollId}"')
Expand All @@ -68,12 +69,12 @@ def step_impl(context, enrollId, assignerAppTCert, role, assigneeAppTCert):
assert 'users' in context, "users not found in context. Did you register a user?"
assert 'compose_containers' in context, "compose_containers not found in context"

(channel, userRegistration) = bdd_test_util.getGRPCChannelAndUser(context, enrollId)
(channel, userRegistration) = bdd_grpc_util.getGRPCChannelAndUser(context, enrollId)

stub = devops_pb2.beta_create_Devops_stub(channel)

# First get binding with EXP_PrepareForTx
secret = userRegistration.getSecret()
secret = bdd_grpc_util.getSecretForUserRegistration(userRegistration)
response = stub.EXP_PrepareForTx(secret,2)
assert response.status == fabric_pb2.Response.SUCCESS, 'Failure getting Binding from {0}, for user "{1}": {2}'.format(userRegistration.composeService,enrollId, response.msg)
binding = response.msg
Expand Down Expand Up @@ -112,18 +113,18 @@ def step_impl(context, enrollId, assignerAppTCert, role, assigneeAppTCert):
def step_impl(context, enrollId, msg):
assert 'users' in context, "users not found in context. Did you register a user?"
assert 'compose_containers' in context, "compose_containers not found in context"
txResult = bdd_test_util.getTxResult(context, enrollId)
txResult = bdd_grpc_util.getTxResult(context, enrollId)
assert txResult.errorCode > 0, "Expected failure (errorCode > 0), instead found errorCode={0}".format(txResult.errorCode)
assert msg in txResult.error, "Expected error to contain'{0}', instead found '{1}".format(msg, txResult.error)

@then(u'"{enrollId}"\'s last transaction should have succeeded')
def step_impl(context, enrollId):
txResult = bdd_test_util.getTxResult(context, enrollId)
txResult = bdd_grpc_util.getTxResult(context, enrollId)
assert txResult.errorCode == 0, "Expected success (errorCode == 0), instead found errorCode={0}, error={1}".format(txResult.errorCode, txResult.error)

@when(u'user "{enrollId}" invokes chaincode "{ccAlias}" function name "{functionName}" with args')
def step_impl(context, enrollId, ccAlias, functionName):
response = bdd_test_util.invokeChaincode(context, enrollId, ccAlias, functionName)
response = bdd_grpc_util.invokeChaincode(context, enrollId, ccAlias, functionName)
context.response = response
context.transactionID = response.msg
#assert response.status == fabric_pb2.Response.SUCCESS, 'Failure invoking chaincode {0} on {1}, for user "{2}": {3}'.format(ccAlias, userRegistration.composeService,enrollId, response.msg)
Expand All @@ -132,6 +133,6 @@ def step_impl(context, enrollId, ccAlias, functionName):
def step_impl(context, enrollId, ccAlias, tagName):
# Retrieve the userRegistration from the context
userRegistration = bdd_test_util.getUserRegistration(context, enrollId)
deployedCcSpec = bdd_test_util.getDeployment(context, ccAlias)
deployedCcSpec = bdd_grpc_util.getDeployment(context, ccAlias)
assert deployedCcSpec != None, "Deployment NOT found for chaincode alias '{0}'".format(ccAlias)
userRegistration.tags[tagName] = deployedCcSpec.chaincodeID.name
2 changes: 1 addition & 1 deletion bddtests/steps/peer_basic_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ def compose_op(context, op):
# Loop through services and start/stop them, and modify the container data list if successful.
for service in services:
context.compose_output, context.compose_error, context.compose_returncode = \
bdd_test_util.cli_call(context, ["docker-compose", "-f", context.compose_yaml, op, service], expect_success=True)
bdd_test_util.cli_call(context, ["docker-compose"] + fileArgsToDockerCompose + [op, service], expect_success=True)
assert context.compose_returncode == 0, "docker-compose failed to {0} {0}".format(op, service)
if op == "stop" or op == "pause":
context.compose_containers = [containerData for containerData in context.compose_containers if containerData.composeService != service]
Expand Down

0 comments on commit ef5c8c7

Please sign in to comment.