Skip to content

Commit

Permalink
Add tests for the peer network list command
Browse files Browse the repository at this point in the history
Add tests for the peer network list command while also bringing in the
testify package into the vendor folder to use as a library for
assertions and mocking.

Change-Id: Iffbd966c84b132c78dc0e60655ad16231746a9b7
Signed-off-by: Julian Carrivick <cjulian@au1.ibm.com>
  • Loading branch information
juliancarrivick-ibm committed Aug 11, 2016
1 parent eacd0e0 commit ec924b1
Show file tree
Hide file tree
Showing 47 changed files with 10,045 additions and 2 deletions.
3 changes: 3 additions & 0 deletions bddtests/docker-compose-1-empty.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
empty:
image: hyperledger/fabric-src
command: bash -c "sleep inf"
24 changes: 24 additions & 0 deletions bddtests/peer_cli.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
# Test Command Line Features of a Peer
#

Feature: Peer Command Line Interface
As a User of the Fabric
I want the command line interface to work correctly

Scenario: List Peers when none are up
Given we compose "docker-compose-1-empty.yml"
When I execute "peer network list" in container empty
Then the command should not complete successfully

Scenario: List Peers when one is up
Given we compose "docker-compose-1.yml"
When I execute "peer network list" in container vp0
Then the command should complete successfully
And stdout should contain JSON

Scenario: List Peers when two are up
Given we compose "docker-compose-2.yml"
When I execute "peer network list" in container vp0
Then the command should complete successfully
And stdout should contain JSON with "peers" array of length 1
2 changes: 0 additions & 2 deletions bddtests/steps/bdd_test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ def cli_call(context, arg_list, expect_success=True):
@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
"""
#arg_list[0] = "update-" + arg_list[0]

# We need to run the cli command by actually calling the python command
# the update-cli.py script has a #!/bin/python as the first line
# which calls the system python, not the virtual env python we
Expand Down
5 changes: 5 additions & 0 deletions bddtests/steps/peer_basic_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ def invokeChaincode(context, devopsFunc, functionName, containerName, idGenAlg=N

context.chaincodeSpec['ctorMsg']['args'] = args
context.chaincodeSpec['attributes'] = attributes

#If idGenAlg is passed then, we still using the deprecated devops API because this parameter can't be passed in the new API.
if idGenAlg != None:
invokeUsingDevopsService(context, devopsFunc, functionName, containerName, idGenAlg)
Expand Down Expand Up @@ -825,3 +826,7 @@ def prepend(elem, l):
if l is None or l == "":
return [elem]
return [elem] + l

@given(u'I do nothing')
def step_impl(context):
pass
89 changes: 89 additions & 0 deletions bddtests/steps/peer_cli_impl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#
# 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 json
from behave import *
from bdd_test_util import cli_call, fullNameFromContainerNamePart
from peer_basic_impl import getAttributeFromJSON

@when(u'I execute "{command}" in container {containerName}')
def step_impl(context, command, containerName):
executeCommandInContainer(context, command, containerName)

def executeCommandInContainer(context, command, container):
fullContainerName = fullNameFromContainerNamePart(container, context.compose_containers)
command = "docker exec {} {}".format(fullContainerName, command)
executeCommand(context, command)

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)

context.command = {
"stdout": stdout,
"stderr": stderr,
"returnCode": retcode
}

@then(u'the command should not complete successfully')
def step_impl(context):
assert not commandCompletedSuccessfully(context)

@then(u'the command should complete successfully')
def step_impl(context):
assert commandCompletedSuccessfully(context)

def commandCompletedSuccessfully(context):
return isSuccessfulReturnCode(context.command["returnCode"])

def isSuccessfulReturnCode(returnCode):
return returnCode == 0

@then(u'{stream} should contain JSON')
def step_impl(context, stream):
assertIsJson(context.command[stream])

@then(u'{stream} should contain JSON with "{attribute}" array of length {length}')
def step_impl(context, stream, attribute, length):
data = context.command[stream]
assertIsJson(data)

json = decodeJson(data)
array = getAttribute(attribute, json)
assertLength(array, int(length))

def assertIsJson(data):
assert isJson(data), "Data is not in JSON format"

def isJson(data):
try:
decodeJson(data)
except ValueError:
return False

return True

def decodeJson(data):
return json.loads(data)

def getAttribute(attribute, json):
return getAttributeFromJSON(attribute, json,
"Attribute '{}' missing from JSON".format(attribute))

def assertLength(array, length):
arrayLength = len(array)
assert arrayLength == length, "Unexpected array length. Expected {}, got {}".format(length, arrayLength)
32 changes: 32 additions & 0 deletions peer/network/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
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.
*/

package network

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestListCmd(t *testing.T) {
require := require.New(t)
cmd := listCmd()

require.NotNil(cmd)
require.Equal("list", cmd.Name())
require.NotNil(cmd.RunE)
}
13 changes: 13 additions & 0 deletions vendor/github.com/davecgh/go-spew/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

151 changes: 151 additions & 0 deletions vendor/github.com/davecgh/go-spew/spew/bypass.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions vendor/github.com/davecgh/go-spew/spew/bypasssafe.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ec924b1

Please sign in to comment.