Skip to content

Commit

Permalink
Merge "Remove context parameter from the cli_call method"
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanLevi authored and Gerrit Code Review committed Aug 22, 2016
2 parents 47c3f6c + ca413e4 commit 8ea25a9
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 33 deletions.
20 changes: 10 additions & 10 deletions bddtests/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from steps.bdd_test_util import cli_call

from steps.coverage import saveCoverageFiles, createCoverageAggregate
from steps.coverage import saveCoverageFiles, createCoverageAggregate

def coverageEnabled(context):
return context.config.userdata.get("coverage", "false") == "true"
Expand All @@ -30,7 +30,7 @@ def after_scenario(context, scenario):
print("Cannot get logs for {0}. Docker rc = {1}".format(containerData.containerName,sys_rc))
# get logs from the chaincode containers
cc_output, cc_error, cc_returncode = \
cli_call(context, ["docker", "ps", "-f", "name=dev-", "--format", "{{.Names}}"], expect_success=True)
cli_call(["docker", "ps", "-f", "name=dev-", "--format", "{{.Names}}"], expect_success=True)
for containerName in cc_output.splitlines():
namePart,sep,junk = containerName.rpartition("-")
with open(namePart + file_suffix, "w+") as logfile:
Expand All @@ -46,34 +46,34 @@ def after_scenario(context, scenario):

print("Decomposing with yaml '{0}' after scenario {1}, ".format(context.compose_yaml, scenario.name))
context.compose_output, context.compose_error, context.compose_returncode = \
cli_call(context, ["docker-compose"] + fileArgsToDockerCompose + ["unpause"], expect_success=True)
cli_call(["docker-compose"] + fileArgsToDockerCompose + ["unpause"], expect_success=True)
context.compose_output, context.compose_error, context.compose_returncode = \
cli_call(context, ["docker-compose"] + fileArgsToDockerCompose + ["stop"], expect_success=True)
cli_call(["docker-compose"] + fileArgsToDockerCompose + ["stop"], expect_success=True)

if coverageEnabled(context):
#Save the coverage files for this scenario before removing containers
containerNames = [containerData.containerName for containerData in context.compose_containers]
saveCoverageFiles("coverage", scenario.name.replace(" ", "_"), containerNames, "cov")
saveCoverageFiles("coverage", scenario.name.replace(" ", "_"), containerNames, "cov")

context.compose_output, context.compose_error, context.compose_returncode = \
cli_call(context, ["docker-compose"] + fileArgsToDockerCompose + ["rm","-f"], expect_success=True)
cli_call(["docker-compose"] + fileArgsToDockerCompose + ["rm","-f"], expect_success=True)
# now remove any other containers (chaincodes)
context.compose_output, context.compose_error, context.compose_returncode = \
cli_call(context, ["docker", "ps", "-qa"], expect_success=True)
cli_call(["docker", "ps", "-qa"], expect_success=True)
if context.compose_returncode == 0:
# Remove each container
for containerId in context.compose_output.splitlines():
#print("docker rm {0}".format(containerId))
context.compose_output, context.compose_error, context.compose_returncode = \
cli_call(context, ["docker", "rm", "-f", containerId], expect_success=True)
cli_call(["docker", "rm", "-f", containerId], expect_success=True)

# stop any running peer that could get in the way before starting the tests
def before_all(context):
cli_call(context, ["../build/bin/peer", "node", "stop"], expect_success=False)
cli_call(["../build/bin/peer", "node", "stop"], expect_success=False)

# stop any running peer that could get in the way before starting the tests
def after_all(context):
print("context.failed = {0}".format(context.failed))

if coverageEnabled(context):
createCoverageAggregate()
4 changes: 1 addition & 3 deletions bddtests/steps/bdd_test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
import re
import subprocess

def cli_call(context, arg_list, expect_success=True):
def cli_call(arg_list, expect_success=True):
"""Executes a CLI command in a subprocess and return the results.
@param context: the behave context
@param arg_list: a list command arguments
@param expect_success: use False to return even if an error occurred when executing the command
@return: (string, string, int) output message, error message, return code
Expand Down Expand Up @@ -115,7 +114,6 @@ def getContainerDataValuesFromContext(context, aliases, callback):
break
return values


def start_background_process(context, program_name, arg_list):
p = subprocess.Popen(arg_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
setattr(context, program_name, p)
18 changes: 9 additions & 9 deletions bddtests/steps/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ def testCoverage():

# Now collect the filenames for coverage files.
files = glob.glob(os.path.join('coverage','*.cov'))

#Create the aggregate coverage file
coverageContents = createCoverageFile(files)
coverageContents = createCoverageFile(files)

#Ouput the aggregate coverage file
with open('coverage.total', 'w') as outfile:
outfile.write(coverageContents)
Expand All @@ -25,15 +25,15 @@ def testCoverage():
def createCoverageAggregate():
# Now collect the filenames for coverage files.
files = glob.glob(os.path.join('coverage','*.cov'))

#Create the aggregate coverage file
coverageContents = createCoverageFile(files)
coverageContents = createCoverageFile(files)

#Ouput the aggregate coverage file
with open('coverage-behave.cov', 'w') as outfile:
outfile.write(coverageContents)
outfile.close()


def saveCoverageFiles(folderName, rootName, containerNames, extension):
'Will save the converage files to folderName'
Expand All @@ -48,7 +48,7 @@ def saveCoverageFiles(folderName, rootName, containerNames, extension):
print("sourcepath = {0}".format(srcPath))
destPath = os.path.join(folderName, "{0}-{1}.{2}".format(rootName, containerName, extension))
output, error, returncode = \
bdd_test_util.cli_call(None, ["docker"] + ["cp", srcPath, destPath], expect_success=False)
bdd_test_util.cli_call(["docker", "cp", srcPath, destPath], expect_success=False)

def testCreateSystemCoverageFile(folderName, rootName, containerNames, extension):
'Will create a single aggregate coverage file fromsave the converage files to folderName'
Expand All @@ -57,7 +57,7 @@ def testCreateSystemCoverageFile(folderName, rootName, containerNames, extension
srcPath = "{0}:/opt/gopath/src/github.com/hyperledger/fabric/peer/coverage.cov".format(containerName)
destPath = os.path.join(folderName, "{0}-{1}.{2}".format(rootName, containerName, extension))
output, error, returncode = \
bdd_test_util.cli_call(None, ["docker"] + ["cp", srcPath, destPath], expect_success=False)
bdd_test_util.cli_call(["docker", "cp", srcPath, destPath], expect_success=False)


def createCoverageFile(filenames):
Expand Down
10 changes: 5 additions & 5 deletions bddtests/steps/peer_basic_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ def parseComposeOutput(context):
containerDataList = []
for containerName in containerNames:
output, error, returncode = \
bdd_test_util.cli_call(context, ["docker", "inspect", "--format", "{{ .NetworkSettings.IPAddress }}", containerName], expect_success=True)
bdd_test_util.cli_call(["docker", "inspect", "--format", "{{ .NetworkSettings.IPAddress }}", containerName], expect_success=True)
print("container {0} has address = {1}".format(containerName, output.splitlines()[0]))
ipAddress = output.splitlines()[0]

# Get the environment array
output, error, returncode = \
bdd_test_util.cli_call(context, ["docker", "inspect", "--format", "{{ .Config.Env }}", containerName], expect_success=True)
bdd_test_util.cli_call(["docker", "inspect", "--format", "{{ .Config.Env }}", containerName], expect_success=True)
env = output.splitlines()[0][1:-1].split()

# Get the Labels to access the com.docker.compose.service value
output, error, returncode = \
bdd_test_util.cli_call(context, ["docker", "inspect", "--format", "{{ .Config.Labels }}", containerName], expect_success=True)
bdd_test_util.cli_call(["docker", "inspect", "--format", "{{ .Config.Labels }}", containerName], expect_success=True)
labels = output.splitlines()[0][4:-1].split()
dockerComposeService = [composeService[27:] for composeService in labels if composeService.startswith("com.docker.compose.service:")][0]
print("dockerComposeService = {0}".format(dockerComposeService))
Expand Down Expand Up @@ -116,7 +116,7 @@ def step_impl(context, composeYamlFile):
context.compose_yaml = composeYamlFile
fileArgsToDockerCompose = getDockerComposeFileArgsFromYamlFile(context.compose_yaml)
context.compose_output, context.compose_error, context.compose_returncode = \
bdd_test_util.cli_call(context, ["docker-compose"] + fileArgsToDockerCompose + ["up","--force-recreate", "-d"], expect_success=True)
bdd_test_util.cli_call(["docker-compose"] + fileArgsToDockerCompose + ["up","--force-recreate", "-d"], expect_success=True)
assert context.compose_returncode == 0, "docker-compose failed to bring up {0}".format(composeYamlFile)
parseComposeOutput(context)
time.sleep(10) # Should be replaced with a definitive interlock guaranteeing that all peers/membersrvc are ready
Expand Down Expand Up @@ -811,7 +811,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"] + fileArgsToDockerCompose + [op, service], expect_success=True)
bdd_test_util.cli_call(["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
2 changes: 1 addition & 1 deletion bddtests/steps/peer_cli_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def executeCommandInContainer(context, command, container):
def executeCommand(context, command):
# cli_call expects an array of arguments, hence splitting here.
commandArgs = command.split()
stdout, stderr, retcode = cli_call(context, commandArgs, expect_success=False)
stdout, stderr, retcode = cli_call(commandArgs, expect_success=False)

context.command = {
"stdout": stdout,
Expand Down
2 changes: 1 addition & 1 deletion bddtests/steps/peer_logging_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def step_impl(context, waitTime, peerName):

def getPeerLogs(context, peerName):
fullContainerName = bdd_test_util.fullNameFromContainerNamePart(peerName, context.compose_containers)
stdout, stderr, retcode = bdd_test_util.cli_call(context, ["docker", "logs", fullContainerName], expect_success=True)
stdout, stderr, retcode = bdd_test_util.cli_call(["docker", "logs", fullContainerName], expect_success=True)

return stdout, stderr

Expand Down
2 changes: 1 addition & 1 deletion tools/dbutility/bddtests/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
def before_feature(context, feature):
print("\nRunning go build")
cmd = ["go", "build", "../dump_db_stats.go"]
test_util.cli_call(context, cmd, expect_success=True)
test_util.cli_call(cmd, expect_success=True)
print("go build complete")

def after_feature(context, feature):
Expand Down
4 changes: 2 additions & 2 deletions tools/dbutility/bddtests/steps/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ def step_impl(contxt, dirPath):
@when(u'I execute utility with no flag')
def step_impl(context):
cmd = ["./dump_db_stats"]
context.output, context.error, context.returncode = test_util.cli_call(context, cmd, expect_success=False)
context.output, context.error, context.returncode = test_util.cli_call(cmd, expect_success=False)

@when(u'I execute utility with flag "{flag}" and path "{path}"')
def step_impl(context, flag, path):
cmd = ["./dump_db_stats"]
cmd.append(flag)
cmd.append(path)
context.output, context.error, context.returncode = test_util.cli_call(context, cmd, expect_success=False)
context.output, context.error, context.returncode = test_util.cli_call(cmd, expect_success=False)

@then(u'I should get a process exit code "{expectedReturncode}"')
def step_impl(context, expectedReturncode):
Expand Down
2 changes: 1 addition & 1 deletion tools/dbutility/bddtests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import subprocess

def cli_call(context, arg_list, expect_success=True):
def cli_call(arg_list, expect_success=True):
p = subprocess.Popen(arg_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate()
if p.returncode != 0:
Expand Down

0 comments on commit 8ea25a9

Please sign in to comment.