Skip to content

Commit

Permalink
Now coverage-behave.cov is generated if behave run with '-D coverage'…
Browse files Browse the repository at this point in the history
… userdata. Updated .gitignore for generated coverage folder and .cov files. Added main_test.go file to allow for generation of coverage binary and proper execution of running peer.
  • Loading branch information
jeffgarratt committed Jun 18, 2016
1 parent 87146e4 commit 8e26834
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ tags
.#*
# bddtest log files
*.log
# bddtest coverage files
bddtests/coverage
*.cov
# Makefile dummy artifacts
.*-dummy
# go-carpet output files
go-carpet-coverage*
# make node-sdk copied files
Expand Down
4 changes: 2 additions & 2 deletions bddtests/chaincode_rbac.feature
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ Feature: Role Based Access Control (RBAC)
# TODO: Must manage TCert expiration for all parties involved.

Examples: Consensus Options
| ComposeFile | WaitTime |
| docker-compose-4-consensus-batch.yml | 60 |
| ComposeFile | WaitTime |
| docker-compose-4-consensus-batch.yml | 120 |


#@doNotDecompose
Expand Down
26 changes: 18 additions & 8 deletions bddtests/environment.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import subprocess
import os
import glob

from steps.bdd_test_util import cli_call

from steps.coverage import saveCoverageFiles, createCoverageAggregate

def coverageEnabled(context):
return context.config.userdata.get("coverage", "false") == "true"


def getDockerComposeFileArgsFromYamlFile(compose_yaml):
parts = compose_yaml.split()
args = []
Expand Down Expand Up @@ -41,7 +48,13 @@ def after_scenario(context, scenario):
context.compose_output, context.compose_error, context.compose_returncode = \
cli_call(context, ["docker-compose"] + fileArgsToDockerCompose + ["unpause"], expect_success=True)
context.compose_output, context.compose_error, context.compose_returncode = \
cli_call(context, ["docker-compose"] + fileArgsToDockerCompose + ["kill"], expect_success=True)
cli_call(context, ["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")

context.compose_output, context.compose_error, context.compose_returncode = \
cli_call(context, ["docker-compose"] + fileArgsToDockerCompose + ["rm","-f"], expect_success=True)
# now remove any other containers (chaincodes)
Expand All @@ -60,10 +73,7 @@ def before_all(context):

# stop any running peer that could get in the way before starting the tests
def after_all(context):
# print("Here!!!!!!")
# print("")
# os._exit(0)
#cli_call(context, ["../peer/peer", "node", "stop"], expect_success=False)
print("context.failed = {0}".format(context.failed))
pass

print("context.failed = {0}".format(context.failed))

if coverageEnabled(context):
createCoverageAggregate()
94 changes: 94 additions & 0 deletions bddtests/steps/coverage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import cStringIO
import os
import glob
import errno
from collections import OrderedDict

import bdd_test_util


def testCoverage():
#First save the coverage files
saveCoverageFiles("coverage","scenario_1", ["bddtests_vp0_1","bddtests_vp1_1","bddtests_vp2_1","bddtests_vp3_1",], "cov")

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

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

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

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)

#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'
# Make the directory
try:
os.makedirs(folderName)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
for containerName in containerNames:
srcPath = "{0}:/opt/gopath/src/github.com/hyperledger/fabric/peer/coverage.cov".format(containerName)
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)

def testCreateSystemCoverageFile(folderName, rootName, containerNames, extension):
'Will create a single aggregate coverage file fromsave the converage files to folderName'
files = glob.glob(os.path.join('coverage','*.cov'))
for containerName in containerNames:
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)


def createCoverageFile(filenames):
"""Creates an aggregated coverage file"""
output = cStringIO.StringIO()
output.write('mode: count\n')
linesMap = {}
#with open('coverage.total', 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
firstLine = True
for line in infile:
if firstLine:
firstLine = False
continue
else:
# Split the line based upon white space
lineParts = line.split()
if lineParts[0] in linesMap:
# Found, keep the greater
newCount = long(lineParts[2])
oldCount = long(linesMap[lineParts[0]].split()[2])
if newCount > oldCount:
linesMap[lineParts[0]] = line
else:
linesMap[lineParts[0]] = line
# Now sort the output
od = OrderedDict(sorted(linesMap.items(), key=lambda i: i[1]))

for (key, line) in od.items():
output.write(line)
contents = output.getvalue()
output.close()
return contents
23 changes: 21 additions & 2 deletions peer/main_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<<<<<<< HEAD
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Expand All @@ -16,10 +17,28 @@ limitations under the License.

package main

// This file is mandatory as otherwise the filebeat.test binary is not generated correctly.

import (
"flag"
"testing"
)

func TestMain(t *testing.T) {
t.Skip("No needs to test main since it is covered by other tests")
var systemTest *bool

func init() {
systemTest = flag.Bool("systemTest", false, "Set to true when running system tests")
// err := flag.Set("test.coverprofile", "coverage.cov")
// if err != nil {
// panic("Could not set flag")
// }
}

// Test started when the test binary is started. Only calls main.
func TestSystem(t *testing.T) {
//test.coverprofile=coverage.cov
// if *systemTest {
// main()
// }
main()
}

0 comments on commit 8e26834

Please sign in to comment.