You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by er...@apache.org on 2023/06/09 15:05:31 UTC

[trafficcontrol] branch master updated: Added Api contract test case for asns endpoint (#7554)

This is an automated email from the ASF dual-hosted git repository.

ericholguin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new 6b3aa4516c Added Api contract test case for asns endpoint (#7554)
6b3aa4516c is described below

commit 6b3aa4516cfa2fa0688c2504ad3bc8fe6da86898
Author: Gokula Krishnan <67...@users.noreply.github.com>
AuthorDate: Fri Jun 9 20:35:25 2023 +0530

    Added Api contract test case for asns endpoint (#7554)
    
    * Added asns TC
    
    * Added asns Api contract test case
    
    * Changed comments
---
 traffic_ops/testing/api_contract/v4/conftest.py    | 28 ++++++-
 .../api_contract/v4/data/request_template.json     |  8 +-
 .../api_contract/v4/data/response_template.json    | 29 +++++++-
 .../testing/api_contract/v4/data/to_data.json      |  2 +-
 traffic_ops/testing/api_contract/v4/test_asns.py   | 86 ++++++++++++++++++++++
 5 files changed, 148 insertions(+), 5 deletions(-)

diff --git a/traffic_ops/testing/api_contract/v4/conftest.py b/traffic_ops/testing/api_contract/v4/conftest.py
index e42fa1ddd5..f9f1b81f20 100644
--- a/traffic_ops/testing/api_contract/v4/conftest.py
+++ b/traffic_ops/testing/api_contract/v4/conftest.py
@@ -489,8 +489,8 @@ def cdn_data_post(to_session: TOSession, request_template_data: list[JSONData]
 	return resp_obj
 
 
-@pytest.fixture()
-def cache_group_post_data(to_session: TOSession, request_template_data: list[JSONData]
+@pytest.fixture(name="cache_group_post_data")
+def cache_group_data_post(to_session: TOSession, request_template_data: list[JSONData]
 			 ) -> dict[str, object]:
 	"""
 	PyTest Fixture to create POST data for cachegroup endpoint.
@@ -966,3 +966,27 @@ def status_post_data(to_session: TOSession, request_template_data: list[JSONData
 	response: tuple[JSONData, requests.Response] = to_session.create_statuses(data=status)
 	resp_obj = check_template_data(response, "statuses")
 	return resp_obj
+
+@pytest.fixture(name="asn_post_data")
+def asn_data_post(to_session: TOSession, request_template_data: list[JSONData],
+		      cache_group_post_data:dict[str, object]) -> dict[str, object]:
+	"""
+	PyTest Fixture to create POST data for asn endpoint.
+
+	:param to_session: Fixture to get Traffic Ops session.
+	:param request_template_data: Fixture to get asn data from a prerequisites file.
+	:returns: Sample POST data and the actual API response.
+	"""
+	asn = check_template_data(request_template_data["asns"], "asns")
+	# Return new post data and post response from asns POST request
+	randstr = randint(0, 1000)
+	asn["asn"] = randstr
+
+	# Check if cachegroup already exists, otherwise create it
+	asn["cachegroupId"] = cache_group_post_data["id"]
+	logger.info("New profile data to hit POST method %s", asn)
+
+	# Hitting asns POST method
+	response: tuple[JSONData, requests.Response] = to_session.create_asn(data=asn)
+	resp_obj = check_template_data(response, "asn")
+	return resp_obj
diff --git a/traffic_ops/testing/api_contract/v4/data/request_template.json b/traffic_ops/testing/api_contract/v4/data/request_template.json
index a54f115edd..3e2c7a66c6 100644
--- a/traffic_ops/testing/api_contract/v4/data/request_template.json
+++ b/traffic_ops/testing/api_contract/v4/data/request_template.json
@@ -213,5 +213,11 @@
 			"protocol": "http",
 			"tenantId": 1
 		}
+	],
+	"asns": [
+		{
+			"asn": 1,
+			"cachegroupId": 10
+		}
 	]
-}
\ No newline at end of file
+}
diff --git a/traffic_ops/testing/api_contract/v4/data/response_template.json b/traffic_ops/testing/api_contract/v4/data/response_template.json
index cd94bcebd5..82a2376edc 100644
--- a/traffic_ops/testing/api_contract/v4/data/response_template.json
+++ b/traffic_ops/testing/api_contract/v4/data/response_template.json
@@ -1095,5 +1095,32 @@
                 "type": "integer"
             }
         }
+    },
+    "asns": {
+        "type": "object",
+        "required": [
+            "asn",
+            "cachegroup",
+            "cachegroupId",
+            "id",
+            "lastUpdated"
+        ],
+        "properties": {
+            "asn": {
+                "type": "integer"
+            },
+            "cachegroup": {
+                "type": "string"
+            },
+            "cachegroupId": {
+                "type": "integer"
+            },
+            "id": {
+                "type": "integer"
+            },
+            "lastUpdated": {
+                "type": "string"
+            }
+        }
     }
-}
\ No newline at end of file
+}
diff --git a/traffic_ops/testing/api_contract/v4/data/to_data.json b/traffic_ops/testing/api_contract/v4/data/to_data.json
index c7b1fbec34..4b8e8b74f9 100644
--- a/traffic_ops/testing/api_contract/v4/data/to_data.json
+++ b/traffic_ops/testing/api_contract/v4/data/to_data.json
@@ -1,6 +1,6 @@
 {
     "user": "admin",
-    "password": "twelve",
+    "password": "twelve12",
     "url": "https://localhost/api/4.0",
     "port": 443
 }
diff --git a/traffic_ops/testing/api_contract/v4/test_asns.py b/traffic_ops/testing/api_contract/v4/test_asns.py
new file mode 100644
index 0000000000..9a41adaa5e
--- /dev/null
+++ b/traffic_ops/testing/api_contract/v4/test_asns.py
@@ -0,0 +1,86 @@
+#
+# 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.
+#
+
+"""API Contract Test Case for asns endpoint."""
+import logging
+from typing import Union
+
+import pytest
+import requests
+from jsonschema import validate
+
+from trafficops.tosession import TOSession
+
+# Create and configure logger
+logger = logging.getLogger()
+
+Primitive = Union[bool, int, float, str, None]
+
+def test_asn_contract(to_session: TOSession,
+	response_template_data: dict[str, Union[Primitive,
+					 list[Union[Primitive, dict[str, object], list[object]]],
+	dict[object, object]]], asn_post_data: dict[str, object]) -> None:
+	"""
+	Test step to validate keys, values and data types from asns endpoint
+	response.
+	:param to_session: Fixture to get Traffic Ops session.
+	:param response_template_data: Fixture to get response template data from a prerequisites file.
+	:param asn_post_data: Fixture to get sample asn data and actual asn response.
+	"""
+	# validate asn keys from asns get response
+	logger.info("Accessing /asns endpoint through Traffic ops session.")
+
+	asn_id = asn_post_data.get("id")
+	if not isinstance(asn_id, int):
+		raise TypeError("malformed asn in prerequisite data; 'asn_id' not a integer")
+
+	asn_get_response: tuple[
+		Union[dict[str, object], list[Union[dict[str, object], list[object], Primitive]], Primitive],
+		requests.Response
+	] = to_session.get_asns(query_params={"id": asn_id})
+	try:
+		asn_data = asn_get_response[0]
+		if not isinstance(asn_data, list):
+			raise TypeError("malformed API response; 'response' property not an array")
+
+		first_asn = asn_data[0]
+		if not isinstance(first_asn, dict):
+			raise TypeError("malformed API response; first asn in response is not an dict")
+		logger.info("asn Api get response %s", first_asn)
+
+		asn_response_template = response_template_data.get("asns")
+		if not isinstance(asn_response_template, dict):
+			raise TypeError(
+				f"asn response template data must be a dict, not '{type(asn_response_template)}'")
+
+		# validate asn values from prereq data in asns get response.
+		prereq_values = asn_post_data["asn"]
+		get_values = first_asn["asn"]
+
+		assert validate(instance=first_asn, schema=asn_response_template) is None
+		assert get_values == prereq_values
+	except IndexError:
+		logger.error("Either prerequisite data or API response was malformed")
+		pytest.fail("API contract test failed for asns endpoint: API response was malformed")
+	finally:
+		# Delete asn after test execution to avoid redundancy.
+		asn_id = asn_post_data.get("id")
+		if to_session.delete_asn(query_params={"id": asn_id}) is None:
+			logger.error("asn returned by Traffic Ops is missing an 'id' property")
+			pytest.fail("Response from delete request is empty, Failing test_asn_contract")
+
+		cachegroup_id = asn_post_data.get("cachegroupId")
+		if to_session.delete_cachegroups(cache_group_id=cachegroup_id) is None:
+			logger.error("Cachegroup returned by Traffic Ops is missing an 'id' property")
+			pytest.fail("Response from delete request is empty, Failing test_asn_contract")