behave_tests: refactor TestAPI DB lookup
[nfvbench.git] / behave_tests / features / steps / testapi.py
index aff7589..f211ee5 100644 (file)
@@ -14,7 +14,6 @@
 #    under the License.
 #
 
-import json
 import logging
 import requests
 
@@ -49,7 +48,7 @@ class TestapiClient:
                 to filter the testapi results.  The following keys are currently
                 supported:
                 - mandatory keys: 'duration_sec', 'frame_sizes', 'flow_count', 'rate'
-                - optional keys: 'user_label', 'flavor_type'
+                - optional keys: 'user_label'
 
         Returns:
             None if no result matching the filters can be found, else a dictionary
@@ -118,19 +117,31 @@ class TestapiClient:
         Perform an HTTP GET request on testapi, check status code and return JSON
         results as dictionary.
 
-        Args: testapi_url: a complete URL to request testapi results (with base
-            endpoint and parameters)
+        Args:
+            testapi_url: a complete URL to request testapi results (with base
+                endpoint and parameters)
 
         Returns:
             The JSON document from testapi as a Python dictionary
 
         Raises:
+            * requests.exceptions.ConnectionError in case of network problem
+              when trying to establish a connection with the TestAPI database
+              (DNS failure, refused connection, ...)
+
+            * requests.exceptions.ConnectTimeout in case of timeout during the
+              request.
+
+            * requests.exception.HTTPError if the HTTP request returned an
+              unsuccessful status code.
 
+            * another exception derived from requests.exceptions.RequestException
+              in case of problem during the HTTP request.
         """
         response = requests.get(testapi_url)
-        assert response.status_code == 200  # TODO: better error message
-        results = json.loads(response.text)
-        return results
+        # raise an HTTPError if the HTTP request returned an unsuccessful status code:
+        response.raise_for_status()
+        return response.json()
 
 
 def equal_test_conditions(testapi_input, nfvbench_input):
@@ -148,7 +159,7 @@ def equal_test_conditions(testapi_input, nfvbench_input):
 
     The following dict keys are currently supported:
         - mandatory keys: 'duration_sec', 'frame_sizes', 'flow_count', 'rate'
-        - optional keys: 'user_label', 'flavor_type'
+        - optional keys: 'user_label'
 
     Optional keys are taken into account only when they can be found in
     `nfvbench_input`, else they are ignored.
@@ -161,8 +172,6 @@ def equal_test_conditions(testapi_input, nfvbench_input):
     required_keys = ['duration_sec', 'frame_sizes', 'flow_count', 'rate']
     if 'user_label' in nfvbench_input:
         required_keys.append('user_label')
-    if 'flavor_type' in nfvbench_input:
-        required_keys.append('flavor_type')
 
     try:
         testapi_subset = {k: testapi_input[k] for k in required_keys}
@@ -180,7 +189,7 @@ def nfvbench_input_to_str(nfvbench_input: dict) -> str:
         nfvbench_input: dict of nfvbench test parameters
     """
     string = ""
-    for key in ['user_label', 'flavor_type', 'frame_sizes', 'flow_count', 'rate', 'duration_sec']:
+    for key in ['user_label', 'frame_sizes', 'flow_count', 'rate', 'duration_sec']:
         if key in nfvbench_input:
             string += f"{key}={nfvbench_input[key]} "
     return string