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 264e743468 Added Api Contract Test case for cdn_locks endpoint (#7591)
264e743468 is described below

commit 264e743468e7215769b9eba0b7c7c9303183bb4d
Author: Gokula Krishnan <[email protected]>
AuthorDate: Mon Jun 26 21:20:50 2023 +0530

    Added Api Contract Test case for cdn_locks endpoint (#7591)
    
    Added Api Contract Tc for cdn_locks endpoint
---
 .../clients/python/trafficops/tosession.py         | 32 +++++++++
 traffic_ops/testing/api_contract/v4/conftest.py    | 32 ++++++++-
 .../api_contract/v4/data/request_template.json     | 10 +++
 .../api_contract/v4/data/response_template.json    | 34 ++++++++++
 .../testing/api_contract/v4/test_cdn_locks.py      | 76 ++++++++++++++++++++++
 .../testing/api_contract/v4/test_topologies.py     |  2 +-
 traffic_ops/testing/api_contract/v4/test_users.py  |  2 +-
 7 files changed, 184 insertions(+), 4 deletions(-)

diff --git a/traffic_control/clients/python/trafficops/tosession.py 
b/traffic_control/clients/python/trafficops/tosession.py
index 215d57a305..8a3fa872ca 100644
--- a/traffic_control/clients/python/trafficops/tosession.py
+++ b/traffic_control/clients/python/trafficops/tosession.py
@@ -624,6 +624,38 @@ class TOSession(RestApiSession):
                :rtype: Tuple[Dict[str, Any], requests.Response]
                :raises: Union[LoginError, OperationError]
                """
+       
+       #
+       # CDN Lock
+       #
+       @api_request('get', 'cdn_locks', ('3.0', '4.0', '4.1', '5.0'))
+       def get_cdn_locks(self, query_params=None):
+               """
+               Returns information for all CDN locks.
+               :ref:`to-api-cdn-locks`
+               :rtype: Tuple[Dict[str, Any], requests.Response]
+               :raises: Union[LoginError, OperationError]
+               """
+       
+       @api_request('post', 'cdn_locks', ('3.0', '4.0', '4.1', '5.0'))
+       def create_cdn_lock(self, data=None):
+               """
+               Create a new CDN lock.
+               :ref:`to-api-cdn-locks`
+               :param data: The parameter data to use for cdn_lock creation.
+               :type data: Dict[str, Any]
+               :rtype: Tuple[Dict[str, Any], requests.Response]
+               :raises: Union[LoginError, OperationError]
+               """
+
+       @api_request('delete', 'cdn_locks', ('3.0', '4.0', '4.1', '5.0'))
+       def delete_cdn_lock(self, query_params=None):
+               """
+               Deletes an existing CDN Lock.
+               :ref:`to-api-cdn-locks`
+               :rtype: Tuple[Dict[str, Any], requests.Response]
+               :raises: Union[LoginError, OperationError]
+               """
 
        #
        # Change Logs
diff --git a/traffic_ops/testing/api_contract/v4/conftest.py 
b/traffic_ops/testing/api_contract/v4/conftest.py
index e2514a3a3b..e936c931f5 100644
--- a/traffic_ops/testing/api_contract/v4/conftest.py
+++ b/traffic_ops/testing/api_contract/v4/conftest.py
@@ -1419,7 +1419,7 @@ def topology_data_post(to_session: TOSession, 
request_template_data: list[JSONDa
        """
        PyTest Fixture to create POST data for topologies endpoint.
        :param to_session: Fixture to get Traffic Ops session.
-       :param request_template_data: Fixture to get coordinate request 
template from a prerequisites file.
+       :param request_template_data: Fixture to get topology request template 
from a prerequisites file.
        :returns: Sample POST data and the actual API response.
        """
 
@@ -1448,5 +1448,33 @@ def topology_data_post(to_session: TOSession, 
request_template_data: list[JSONDa
        msg = to_session.delete_topology(name=topology_name)
        logger.info("Deleting topology data... %s", msg)
        if msg is None:
-               logger.error("topology returned by Traffic Ops is missing an 
'id' property")
+               logger.error("topology returned by Traffic Ops is missing an 
'name' property")
+               pytest.fail("Response from delete request is empty, Failing 
test_case")
+
+
[email protected](name="cdn_lock_post_data")
+def cdn_lock_data_post(to_session: TOSession, request_template_data: 
list[JSONData],
+               user_post_data:dict[str, object], cdn_post_data:dict[str, 
object]) -> dict[str, object]:
+       """
+       PyTest Fixture to create POST data for cdn_locks endpoint.
+       :param to_session: Fixture to get Traffic Ops session.
+       :param request_template_data: Fixture to get cdn_locks request template 
from a prerequisites file.
+       :returns: Sample POST data and the actual API response.
+       """
+
+       cdn_lock = check_template_data(request_template_data["cdn_locks"], 
"cdn_locks")
+
+       # Return new post data and post response from cdn_locks POST request
+       cdn_lock["cdn"] = cdn_post_data["name"]
+       cdn_lock["sharedUserNames"][0] = user_post_data["username"]
+       logger.info("New cdn_lock data to hit POST method %s", cdn_lock)
+       # Hitting cdn_locks POST methed
+       response: tuple[JSONData, requests.Response] = 
to_session.create_cdn_lock(data=cdn_lock)
+       resp_obj = check_template_data(response, "cdn_lock")
+       yield resp_obj
+       cdn_name = resp_obj.get("cdn")
+       msg = to_session.delete_cdn_lock(query_params={"cdn":cdn_name})
+       logger.info("Deleting cdn_lock data... %s", msg)
+       if msg is None:
+               logger.error("cdn_lock returned by Traffic Ops is missing an 
'cdn' 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 8c91fd820b..0030b2826e 100644
--- a/traffic_ops/testing/api_contract/v4/data/request_template.json
+++ b/traffic_ops/testing/api_contract/v4/data/request_template.json
@@ -261,5 +261,15 @@
                                }
                        ]
                }
+       ],
+       "cdn_locks": [
+               {
+                       "cdn": "test",
+                       "message": "acquiring lock to snap CDN",
+                       "sharedUserNames": [
+                               "test"
+                       ],
+                       "soft": true
+               }
        ]
 }
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 3ac11309ba..ee3d77f065 100644
--- a/traffic_ops/testing/api_contract/v4/data/response_template.json
+++ b/traffic_ops/testing/api_contract/v4/data/response_template.json
@@ -1364,5 +1364,39 @@
                 "type": "string"
             }
         }
+    },
+    "cdn_locks": {
+        "type": "object",
+        "required": [
+            "userName",
+            "cdn",
+            "message",
+            "soft",
+            "sharedUserNames",
+            "lastUpdated"
+        ],
+        "properties": {
+            "userName": {
+                "type": "string"
+            },
+            "cdn": {
+                "type": "string"
+            },
+            "message": {
+                "type": "string"
+            },
+            "soft": {
+                "type": "boolean"
+            },
+            "sharedUserNames": {
+                "type": "array",
+                "items": {
+                    "type": "string"
+                }
+            },
+            "lastUpdated": {
+                "type": "string"
+            }
+        }
     }
 }
diff --git a/traffic_ops/testing/api_contract/v4/test_cdn_locks.py 
b/traffic_ops/testing/api_contract/v4/test_cdn_locks.py
new file mode 100644
index 0000000000..cc0cb883cf
--- /dev/null
+++ b/traffic_ops/testing/api_contract/v4/test_cdn_locks.py
@@ -0,0 +1,76 @@
+#
+# 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 cdn_locks 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_cdn_lock_contract(to_session: TOSession,
+       response_template_data: dict[str, Union[Primitive,
+                                        list[Union[Primitive, dict[str, 
object], list[object]]],
+       dict[object, object]]], cdn_lock_post_data: dict[str, object]) -> None:
+       """
+       Test step to validate keys, values and data types from cdn_locks 
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 cdn_lock_post_data: Fixture to get sample cdn_lock data and 
actual cdn_lock response.
+       """
+       # validate cdn_lock keys from cdn_locks get response
+       logger.info("Accessing /cdn_locks endpoint through Traffic ops 
session.")
+
+       cdn_name = cdn_lock_post_data.get("cdn")
+       if not isinstance(cdn_name, str):
+               raise TypeError("malformed cdn_name in prerequisite data; 
'cdn_name' not a string")
+
+       cdn_lock_get_response: tuple[
+               Union[dict[str, object], list[Union[dict[str, object], 
list[object], Primitive]], Primitive],
+               requests.Response
+       ] = to_session.get_cdn_locks(query_params={"name":cdn_name})
+       try:
+               cdn_lock_data = cdn_lock_get_response[0]
+               if not isinstance(cdn_lock_data, list):
+                       raise TypeError("malformed API response; 'response' 
property not an array")
+
+               first_cdn_lock = cdn_lock_data[0]
+               if not isinstance(first_cdn_lock, dict):
+                       raise TypeError("malformed API response; first cdn_lock 
in response is not an dict")
+               logger.info("cdn_lock Api get response %s", first_cdn_lock)
+
+               cdn_lock_response_template = 
response_template_data.get("cdn_locks")
+               if not isinstance(cdn_lock_response_template, dict):
+                       raise TypeError(
+                               f"cdn_lock response template data must be a 
dict, not '{type(cdn_lock_response_template)}'")
+
+               # validate cdn_lock values from prereq data in cdn_locks get 
response.
+               keys = ["cdn", "message", "soft"]
+               prereq_values = [cdn_lock_post_data[key] for key in keys]
+               get_values = [first_cdn_lock[key] for key in keys]
+
+               assert validate(instance=first_cdn_lock, 
schema=cdn_lock_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 cdn_locks endpoint: 
API response was malformed")
diff --git a/traffic_ops/testing/api_contract/v4/test_topologies.py 
b/traffic_ops/testing/api_contract/v4/test_topologies.py
index 0d9c1c39c0..23b2513ac8 100644
--- a/traffic_ops/testing/api_contract/v4/test_topologies.py
+++ b/traffic_ops/testing/api_contract/v4/test_topologies.py
@@ -72,4 +72,4 @@ def test_topology_contract(to_session: TOSession,
                assert get_values == prereq_values
        except IndexError:
                logger.error("Either prerequisite data or API response was 
malformed")
-               pytest.fail("API contract test failed for cdn endpoint: API 
response was malformed")
+               pytest.fail("API contract test failed for topologies endpoint: 
API response was malformed")
diff --git a/traffic_ops/testing/api_contract/v4/test_users.py 
b/traffic_ops/testing/api_contract/v4/test_users.py
index e5689785f5..d565c630b5 100644
--- a/traffic_ops/testing/api_contract/v4/test_users.py
+++ b/traffic_ops/testing/api_contract/v4/test_users.py
@@ -72,4 +72,4 @@ def test_user_contract(to_session: TOSession,
                assert get_values == prereq_values
        except IndexError:
                logger.error("Either prerequisite data or API response was 
malformed")
-               pytest.fail("API contract test failed for cdn endpoint: API 
response was malformed")
+               pytest.fail("API contract test failed for users endpoint: API 
response was malformed")

Reply via email to