Skip to content

Commit d7e056e

Browse files
Refactor BDD REST calls
Refactor the duplicated code for calling the peer REST endpoints into standalone functions that can be reused. Also refactor the 'Wait for transactions to be committed' BDD steps to utilize the newly created high level functions. Remove the msg parameter from the JSON checking function and generate the message at invoke time. Reduce some timeouts from 5 or 3 mins to a much more reasonable 1 min. I have tested all behave tests locally to verify they work in lieu of them being run on Jenkins while they are stabilised. Change-Id: I484c1cde677461a885863a492be786bae3be44f3 Signed-off-by: Julian Carrivick <[email protected]>
1 parent f7f5dc3 commit d7e056e

10 files changed

+270
-309
lines changed

bddtests/java_shim.feature

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Feature: Java chaincode example
3535
| arg1 | arg2 | arg3 | arg4 |
3636
| a | 100 | b | 200 |
3737
Then I should have received a chaincode name
38-
Then I wait up to "300" seconds for transaction to be committed to all peers
38+
Then I wait up to "60" seconds for transaction to be committed to all peers
3939

4040
When requesting "/chain" from "vp0"
4141
Then I should get a JSON response with "height" = "2"
@@ -72,7 +72,7 @@ Scenario: java RangeExample chaincode single peer
7272
||
7373
||
7474
Then I should have received a chaincode name
75-
Then I wait up to "300" seconds for transaction to be committed to all peers
75+
Then I wait up to "60" seconds for transaction to be committed to all peers
7676

7777
When requesting "/chain" from "vp0"
7878
Then I should get a JSON response with "height" = "2"

bddtests/peer_basic.feature

+2-3
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,6 @@ Feature: Network of Peers
10771077

10781078

10791079
@issue_1942
1080-
# @doNotDecompose
10811080
Scenario: chaincode example02 with 4 peers, stop and start alternates, reverse
10821081
Given we compose "docker-compose-4-consensus-batch.yml"
10831082
And I register with CA supplying username "binhn" and secret "7avZQLwcUe9q" on peers:
@@ -1113,7 +1112,7 @@ Scenario: chaincode example02 with 4 peers, stop and start alternates, reverse
11131112
|arg1|arg2|arg3|
11141113
| a | b | 1 |
11151114
Then I should have received a transactionID
1116-
Then I wait up to "180" seconds for transaction to be committed to peers:
1115+
Then I wait up to "60" seconds for transaction to be committed to peers:
11171116
| vp0 | vp1 | vp3 |
11181117

11191118
When I query chaincode "example2" function name "query" with value "a" on peers:
@@ -1130,7 +1129,7 @@ Scenario: chaincode example02 with 4 peers, stop and start alternates, reverse
11301129
When I invoke chaincode "example2" function name "invoke" on "vp3" "20" times
11311130
|arg1|arg2|arg3|
11321131
| a | b | 1 |
1133-
Then I wait up to "300" seconds for transactions to be committed to peers:
1132+
Then I wait up to "60" seconds for transactions to be committed to peers:
11341133
| vp0 | vp2 | vp3 |
11351134

11361135
When I query chaincode "example2" function name "query" with value "a" on peers:

bddtests/steps/bdd_compose_util.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import os, time, re, requests
1818

19-
from bdd_rest_util import buildUrl, CORE_REST_PORT
19+
from bdd_request_util import httpGetToContainer, CORE_REST_PORT
2020
from bdd_json_util import getAttributeFromJSON
2121
from bdd_test_util import cli_call, bdd_log
2222

@@ -229,13 +229,12 @@ def peerIsReady(context, thisPeer, allPeers):
229229
return numPeers == numConnectedPeers
230230

231231
def getConnectedPeersFromPeer(context, thisPeer):
232-
url = buildUrl(context, thisPeer.ipAddress, "/network/peers")
233-
response = requests.get(url, headers={'Accept': 'application/json'}, verify=False)
232+
response = httpGetToContainer(context, thisPeer, "/network/peers")
234233

235234
if response.status_code != 200:
236235
return None
237236

238-
return getAttributeFromJSON("peers", response.json(), "There should be a peer json attribute")
237+
return getAttributeFromJSON("peers", response.json())
239238

240239
def mapAliasesToContainers(context):
241240
aliasToContainerMap = {}
@@ -258,4 +257,4 @@ def mapContainerNamesToContainers(context):
258257
name = container.name
259258
nameToContainerMap[name] = container
260259

261-
return nameToContainerMap
260+
return nameToContainerMap

bddtests/steps/bdd_json_util.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,21 @@
1414
# limitations under the License.
1515
#
1616

17-
def getAttributeFromJSON(attribute, jsonObject, msg):
18-
return getHierarchyAttributesFromJSON(attribute.split("."), jsonObject, msg)
17+
def getAttributeFromJSON(attribute, json):
18+
foundJson = getHierarchyAttributesFromJSON(attribute.split("."), json)
19+
assert foundJson is not None, "Unable to locate {} in JSON".format(attribute)
1920

20-
def getHierarchyAttributesFromJSON(attributes, jsonObject, msg):
21-
if len(attributes) > 0:
22-
assert attributes[0] in jsonObject, msg
23-
return getHierarchyAttributesFromJSON(attributes[1:], jsonObject[attributes[0]], msg)
24-
return jsonObject
21+
return foundJson
22+
23+
def getHierarchyAttributesFromJSON(attributes, json):
24+
foundJson = None
25+
26+
currentAttribute = attributes[0]
27+
if currentAttribute in json:
28+
foundJson = json[currentAttribute]
29+
30+
attributesToGo = attributes[1:]
31+
if len(attributesToGo) > 0:
32+
foundJson = getHierarchyAttributesFromJSON(attributesToGo, foundJson)
33+
34+
return foundJson

bddtests/steps/bdd_request_util.py

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 requests, json
18+
from bdd_test_util import bdd_log
19+
20+
CORE_REST_PORT = "7050"
21+
ACCEPT_JSON_HEADER = {
22+
'Accept': 'application/json'
23+
}
24+
25+
def httpGetToContainerAlias(context, containerAlias, endpoint, \
26+
port=CORE_REST_PORT, expectSuccess=True):
27+
""" Performs a GET to the given container doing a lookup by alias first.
28+
Optional args are port (defaults to the default peer rest port) and the
29+
expectSuccess which validates the response returned a 200 """
30+
container = context.containerAliasMap[containerAlias]
31+
return httpGetToContainer(context, container, endpoint, port, expectSuccess)
32+
33+
def httpGetToContainer(context, container, endpoint, \
34+
port=CORE_REST_PORT, expectSuccess=True):
35+
""" Performs a GET to the given container. Optional args are port (defaults
36+
to the default peer rest port) and the expectSuccess which validates the
37+
response returned a 200 """
38+
request_url = buildContainerUrl(context, container, endpoint, port)
39+
return httpGet(request_url, expectSuccess)
40+
41+
def httpPostToContainerAlias(context, containerAlias, endpoint, body, \
42+
port=CORE_REST_PORT, expectSuccess=True):
43+
""" Performs a POST to the given container doing a lookup by alias first.
44+
Optional args are port (defaults to the default peer rest port) and the
45+
expectSuccess which validates the response returned a 200 """
46+
container = context.containerAliasMap[containerAlias]
47+
return httpPostToContainer(context, container, endpoint, body, port, expectSuccess)
48+
49+
def httpPostToContainer(context, container, endpoint, body, \
50+
port=CORE_REST_PORT, expectSuccess=True):
51+
""" Performs a GET to the given container. Optional args are port (defaults
52+
to the default peer rest port) and the expectSuccess which validates the
53+
response returned a 200 """
54+
request_url = buildContainerUrl(context, container, endpoint, port)
55+
return httpPost(request_url, body, expectSuccess)
56+
57+
def buildContainerAliasUrl(context, containerAlias, endpoint, port=CORE_REST_PORT):
58+
""" Build a URL to do a HTTP request to the given container looking up the
59+
alias first. Optionally provide a port too which defaults to the peer
60+
rest port """
61+
container = context.containerAliasMap[containerAlias]
62+
return buildContainerUrl(context, container, endpoint, port=port)
63+
64+
def buildContainerUrl(context, container, endpoint, port=CORE_REST_PORT):
65+
""" Build a URL to do a HTTP request to the given container. Optionally
66+
provide a port too which defaults to the peer rest port """
67+
return buildUrl(context, container.ipAddress, port, endpoint)
68+
69+
def buildUrl(context, ipAddress, port, endpoint):
70+
schema = "http"
71+
if 'TLS' in context.tags:
72+
schema = "https"
73+
return "{0}://{1}:{2}{3}".format(schema, ipAddress, port, endpoint)
74+
75+
def httpGet(url, expectSuccess=True):
76+
return _request("GET", url, expectSuccess=expectSuccess)
77+
78+
def httpPost(url, body, expectSuccess=True):
79+
return _request("POST", url, json=body, expectSuccess=expectSuccess)
80+
81+
def _request(method, url, expectSuccess=True, **kwargs):
82+
bdd_log("HTTP {} to url = {}".format(method, url))
83+
84+
response = requests.request(method, url, \
85+
headers=ACCEPT_JSON_HEADER, verify=False, **kwargs)
86+
87+
if expectSuccess:
88+
assert response.status_code == 200, \
89+
"Failed to {} to {}: {}".format(method, url, response.text)
90+
91+
bdd_log("Response from {}:".format(url))
92+
bdd_log(formatResponseText(response))
93+
94+
return response
95+
96+
def formatResponseText(response):
97+
# Limit to 300 chars because of the size of the Base64 encoded Certs
98+
return json.dumps(response.json(), indent = 4)[:300]

bddtests/steps/bdd_rest_util.py

-23
This file was deleted.

0 commit comments

Comments
 (0)