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 22d28144af Added api contract test case for staticdnsentries endpoint 
(#7702)
22d28144af is described below

commit 22d28144afeeaf625cf3e7848248f6c363cc72af
Author: Gokula Krishnan <[email protected]>
AuthorDate: Tue Aug 8 00:38:32 2023 +0530

    Added api contract test case for staticdnsentries endpoint (#7702)
---
 traffic_ops/testing/api_contract/v4/conftest.py    | 33 +++++++++
 .../api_contract/v4/data/request_template.json     |  9 +++
 .../api_contract/v4/data/response_template.json    | 57 +++++++++++++++
 .../api_contract/v4/test_static_dns_entries.py     | 81 ++++++++++++++++++++++
 4 files changed, 180 insertions(+)

diff --git a/traffic_ops/testing/api_contract/v4/conftest.py 
b/traffic_ops/testing/api_contract/v4/conftest.py
index fd5cb0f218..07d1d3eb12 100644
--- a/traffic_ops/testing/api_contract/v4/conftest.py
+++ b/traffic_ops/testing/api_contract/v4/conftest.py
@@ -1826,3 +1826,36 @@ def federation_resolver_data_post(to_session: TOSession, 
request_template_data:
        if msg is None:
                logger.error("federation_resolver returned by Traffic Ops is 
missing an 'id' property")
                pytest.fail("Response from delete request is empty, Failing 
test_case")
+
+
[email protected](name="static_dns_entries_post_data")
+def static_dns_entries_data_post(to_session: TOSession, request_template_data: 
list[JSONData],
+                                delivery_services_post_data:dict[str, object]
+                                 ) -> dict[str, object]:
+       """
+       PyTest Fixture to create POST data for static_dns_entries endpoint.
+       :param to_session: Fixture to get Traffic Ops session.
+       :param request_template_data: Fixture to get static_dns_entries request 
template.
+       :returns: Sample POST data and the actual API response.
+       """
+       static_dns_entries = check_template_data(
+               request_template_data["static_dns_entries"], 
"static_dns_entries")
+
+       # Check if type already exists, otherwise create it
+       type_data = check_template_data(request_template_data["types"], "types")
+       type_object = create_or_get_existing(to_session, "types", "type", 
type_data,
+                                     {"useInTable": "staticdnsentry"})
+       static_dns_entries["typeId"] = type_object["id"]
+       static_dns_entries["deliveryServiceId"] = 
delivery_services_post_data["id"]
+
+       logger.info("New static_dns_entries data to hit POST method %s", 
static_dns_entries)
+       # Hitting static_dns_entries POST methed
+       response: tuple[JSONData, requests.Response] = 
to_session.create_staticdnsentries(data=static_dns_entries)
+       resp_obj = check_template_data(response, "static_dns_entries")
+       yield resp_obj
+       static_dns_entries_id = resp_obj.get("id")
+       msg = 
to_session.delete_staticdnsentries(query_params={"id":static_dns_entries_id})
+       logger.info("Deleting static_dns_entries data... %s", msg)
+       if msg is None:
+               logger.error("static_dns_entries returned by Traffic Ops is 
missing an 'id' property")
+               pytest.fail("Response from delete request is empty, Failing 
test_case")
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 d9bc202065..ba1364518e 100644
--- a/traffic_ops/testing/api_contract/v4/data/request_template.json
+++ b/traffic_ops/testing/api_contract/v4/data/request_template.json
@@ -446,5 +446,14 @@
                        "ipAddress": "::1/1",
                        "typeId": 37
                }
+       ],
+       "static_dns_entries": [
+               {
+                       "address": "2001::cafe:c935",
+                       "deliveryserviceId": 37,
+                       "host": "test",
+                       "ttl": 300,
+                       "typeId": 41
+               }
        ]
 }
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 a90ca3ce23..ba0151f196 100644
--- a/traffic_ops/testing/api_contract/v4/data/response_template.json
+++ b/traffic_ops/testing/api_contract/v4/data/response_template.json
@@ -1897,5 +1897,62 @@
                 "type": "string"
             }
         }
+    },
+    "static_dns_entries": {
+        "type": "object",
+        "required": [
+            "address",
+            "cachegroup",
+            "cachegroupId",
+            "deliveryservice",
+            "deliveryserviceId",
+            "host",
+            "id",
+            "lastUpdated",
+            "ttl",
+            "type",
+            "typeId"
+        ],
+        "properties": {
+            "address": {
+                "type": "string"
+            },
+            "cachegroup": {
+                "type": [
+                    "null",
+                    "string"
+                ]
+            },
+            "cachegroupId": {
+                "type": [
+                    "null",
+                    "integer"
+                ]
+            },
+            "deliveryservice": {
+                "type": "string"
+            },
+            "deliveryserviceId": {
+                "type": "integer"
+            },
+            "host": {
+                "type": "string"
+            },
+            "id": {
+                "type": "integer"
+            },
+            "lastUpdated": {
+                "type": "string"
+            },
+            "ttl": {
+                "type": "integer"
+            },
+            "type": {
+                "type": "string"
+            },
+            "typeId": {
+                "type": "integer"
+            }
+        }
     }
 }
diff --git a/traffic_ops/testing/api_contract/v4/test_static_dns_entries.py 
b/traffic_ops/testing/api_contract/v4/test_static_dns_entries.py
new file mode 100644
index 0000000000..824f18dc44
--- /dev/null
+++ b/traffic_ops/testing/api_contract/v4/test_static_dns_entries.py
@@ -0,0 +1,81 @@
+#
+# 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 staticdnsentries 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_static_dns_entries_contract(to_session: TOSession,
+       response_template_data: dict[str, Union[Primitive, list[Union[Primitive,
+                                                       dict[str, object], 
list[object]]], dict[object, object]]],
+       static_dns_entries_post_data: dict[str, object]
+) -> None:
+       """
+       Test step to validate keys, values and data types from 
static_dns_entries 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 static_dns_entries_post_data: Fixture to get delivery services 
sslkeys data.
+       """
+       # validate static_dns_entries keys from api get response
+       logger.info("Accessing /static_dns_entries endpoint through Traffic ops 
session.")
+
+       static_dns_entries_id = static_dns_entries_post_data["id"]
+       if not isinstance(static_dns_entries_id, int):
+               raise TypeError("malformed API response; 'id' property not a 
string")
+
+       static_dns_entries_get_response: tuple[
+               Union[dict[str, object], list[Union[dict[str, object], 
list[object], Primitive]], Primitive],
+               requests.Response
+       ] = 
to_session.get_staticdnsentries(query_params={"id":static_dns_entries_id})
+       try:
+               static_dns_entries_data = static_dns_entries_get_response[0]
+               if not isinstance(static_dns_entries_data, list):
+                       raise TypeError(
+                               "malformed API response; static_dns_entries in 
response is not an list")
+               first_static_dns_entries = static_dns_entries_data[0]
+               if not isinstance(first_static_dns_entries, dict):
+                       raise TypeError(
+                               "malformed API response; first 
static_dns_entries in response is not a dict")
+
+               logger.info("static_dns_entries Api get response %s", 
first_static_dns_entries)
+
+               static_dns_entries_response_template = 
response_template_data.get(
+                       "static_dns_entries")
+               if not isinstance(static_dns_entries_response_template, dict):
+                       raise TypeError(f"static_dns_entries response template 
data must be a dict, not '"
+                                                       
f"{type(static_dns_entries_response_template)}'")
+
+               keys = ["deliveryserviceId", "address", "ttl", "typeId", "host"]
+               prereq_values = [static_dns_entries_post_data[key] for key in 
keys]
+               get_values = [first_static_dns_entries[key] for key in keys]
+
+               assert validate(instance=first_static_dns_entries,
+                 schema=static_dns_entries_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 static_dns_entries 
endpoint: API response was malformed")

Reply via email to