Skip to content

Commit ec924b1

Browse files
Add tests for the peer network list command
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 <[email protected]>
1 parent eacd0e0 commit ec924b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+10045
-2
lines changed

bddtests/docker-compose-1-empty.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
empty:
2+
image: hyperledger/fabric-src
3+
command: bash -c "sleep inf"

bddtests/peer_cli.feature

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# Test Command Line Features of a Peer
3+
#
4+
5+
Feature: Peer Command Line Interface
6+
As a User of the Fabric
7+
I want the command line interface to work correctly
8+
9+
Scenario: List Peers when none are up
10+
Given we compose "docker-compose-1-empty.yml"
11+
When I execute "peer network list" in container empty
12+
Then the command should not complete successfully
13+
14+
Scenario: List Peers when one is up
15+
Given we compose "docker-compose-1.yml"
16+
When I execute "peer network list" in container vp0
17+
Then the command should complete successfully
18+
And stdout should contain JSON
19+
20+
Scenario: List Peers when two are up
21+
Given we compose "docker-compose-2.yml"
22+
When I execute "peer network list" in container vp0
23+
Then the command should complete successfully
24+
And stdout should contain JSON with "peers" array of length 1

bddtests/steps/bdd_test_util.py

-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ def cli_call(context, arg_list, expect_success=True):
2626
@param expect_success: use False to return even if an error occurred when executing the command
2727
@return: (string, string, int) output message, error message, return code
2828
"""
29-
#arg_list[0] = "update-" + arg_list[0]
30-
3129
# We need to run the cli command by actually calling the python command
3230
# the update-cli.py script has a #!/bin/python as the first line
3331
# which calls the system python, not the virtual env python we

bddtests/steps/peer_basic_impl.py

+5
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ def invokeChaincode(context, devopsFunc, functionName, containerName, idGenAlg=N
372372

373373
context.chaincodeSpec['ctorMsg']['args'] = args
374374
context.chaincodeSpec['attributes'] = attributes
375+
375376
#If idGenAlg is passed then, we still using the deprecated devops API because this parameter can't be passed in the new API.
376377
if idGenAlg != None:
377378
invokeUsingDevopsService(context, devopsFunc, functionName, containerName, idGenAlg)
@@ -825,3 +826,7 @@ def prepend(elem, l):
825826
if l is None or l == "":
826827
return [elem]
827828
return [elem] + l
829+
830+
@given(u'I do nothing')
831+
def step_impl(context):
832+
pass

bddtests/steps/peer_cli_impl.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#
2+
# Copyright IBM Corp. 2016 All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
import json
18+
from behave import *
19+
from bdd_test_util import cli_call, fullNameFromContainerNamePart
20+
from peer_basic_impl import getAttributeFromJSON
21+
22+
@when(u'I execute "{command}" in container {containerName}')
23+
def step_impl(context, command, containerName):
24+
executeCommandInContainer(context, command, containerName)
25+
26+
def executeCommandInContainer(context, command, container):
27+
fullContainerName = fullNameFromContainerNamePart(container, context.compose_containers)
28+
command = "docker exec {} {}".format(fullContainerName, command)
29+
executeCommand(context, command)
30+
31+
def executeCommand(context, command):
32+
# cli_call expects an array of arguments, hence splitting here.
33+
commandArgs = command.split()
34+
stdout, stderr, retcode = cli_call(context, commandArgs, expect_success=False)
35+
36+
context.command = {
37+
"stdout": stdout,
38+
"stderr": stderr,
39+
"returnCode": retcode
40+
}
41+
42+
@then(u'the command should not complete successfully')
43+
def step_impl(context):
44+
assert not commandCompletedSuccessfully(context)
45+
46+
@then(u'the command should complete successfully')
47+
def step_impl(context):
48+
assert commandCompletedSuccessfully(context)
49+
50+
def commandCompletedSuccessfully(context):
51+
return isSuccessfulReturnCode(context.command["returnCode"])
52+
53+
def isSuccessfulReturnCode(returnCode):
54+
return returnCode == 0
55+
56+
@then(u'{stream} should contain JSON')
57+
def step_impl(context, stream):
58+
assertIsJson(context.command[stream])
59+
60+
@then(u'{stream} should contain JSON with "{attribute}" array of length {length}')
61+
def step_impl(context, stream, attribute, length):
62+
data = context.command[stream]
63+
assertIsJson(data)
64+
65+
json = decodeJson(data)
66+
array = getAttribute(attribute, json)
67+
assertLength(array, int(length))
68+
69+
def assertIsJson(data):
70+
assert isJson(data), "Data is not in JSON format"
71+
72+
def isJson(data):
73+
try:
74+
decodeJson(data)
75+
except ValueError:
76+
return False
77+
78+
return True
79+
80+
def decodeJson(data):
81+
return json.loads(data)
82+
83+
def getAttribute(attribute, json):
84+
return getAttributeFromJSON(attribute, json,
85+
"Attribute '{}' missing from JSON".format(attribute))
86+
87+
def assertLength(array, length):
88+
arrayLength = len(array)
89+
assert arrayLength == length, "Unexpected array length. Expected {}, got {}".format(length, arrayLength)

peer/network/list_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package network
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
func TestListCmd(t *testing.T) {
26+
require := require.New(t)
27+
cmd := listCmd()
28+
29+
require.NotNil(cmd)
30+
require.Equal("list", cmd.Name())
31+
require.NotNil(cmd.RunE)
32+
}

vendor/github.com/davecgh/go-spew/LICENSE

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/davecgh/go-spew/spew/bypass.go

+151
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/davecgh/go-spew/spew/bypasssafe.go

+37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)