summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tempest/lib/common/rest_client.py13
-rw-r--r--tempest/tests/lib/common/test_rest_client.py20
2 files changed, 31 insertions, 2 deletions
diff --git a/tempest/lib/common/rest_client.py b/tempest/lib/common/rest_client.py
index fc86914f6..a11b7c133 100644
--- a/tempest/lib/common/rest_client.py
+++ b/tempest/lib/common/rest_client.py
@@ -492,7 +492,7 @@ class RestClient(object):
self._log_request_full(resp, req_headers, req_body,
resp_body, extra)
- def _parse_resp(self, body):
+ def _parse_resp(self, body, top_key_to_verify=None):
try:
body = json.loads(body)
except ValueError:
@@ -516,8 +516,17 @@ class RestClient(object):
if not hasattr(body, "keys") or len(body.keys()) != 1:
return body
# Just return the "wrapped" element
- _, first_item = tuple(body.items())[0]
+ first_key, first_item = tuple(body.items())[0]
if isinstance(first_item, (dict, list)):
+ if top_key_to_verify is not None:
+ msg_args = {
+ 'top_key': top_key_to_verify,
+ 'actual_key': first_key,
+ }
+ assert_msg = ("The expected top level key is "
+ "'%(top_key)s' but we found "
+ "'%(actual_key)s'." % msg_args)
+ assert top_key_to_verify == first_key, assert_msg
return first_item
except (ValueError, IndexError):
pass
diff --git a/tempest/tests/lib/common/test_rest_client.py b/tempest/tests/lib/common/test_rest_client.py
index 1dea5f544..910756fac 100644
--- a/tempest/tests/lib/common/test_rest_client.py
+++ b/tempest/tests/lib/common/test_rest_client.py
@@ -280,6 +280,26 @@ class TestRestClientParseRespJSON(BaseRestClientTestClass):
body = self.rest_client._parse_resp(json.dumps(empty_list))
self.assertEqual(empty_list, body)
+ def test_parse_top_key_match(self):
+ body = self.rest_client._parse_resp(json.dumps(self.dict_expected),
+ top_key_to_verify="body_dict")
+ self.assertEqual(self.dict_expected["body_dict"], body)
+
+
+class TestRestClientParseErrorRespJSON(BaseRestClientTestClass):
+
+ dict_expected = {"body_dict": {"fake_key": "fake_value"}}
+
+ def setUp(self):
+ self.fake_http = fake_http.fake_httplib2()
+ super(TestRestClientParseErrorRespJSON, self).setUp()
+
+ def test_parse_top_key_no_match(self):
+ self.assertRaises(AssertionError,
+ self.rest_client._parse_resp,
+ json.dumps(self.dict_expected),
+ top_key_to_verify="body_key")
+
class TestRestClientErrorCheckerJSON(base.TestCase):
c_type = "application/json"