-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added API contract testcase for Jobs endpoint (#7573)
* Added Jobs TC * modified code * Reactored code and added deletion of prerequisites data after every test case execution * Added logging for deletion of prerequisites endpoint data and resolved pylint errors * Added fixture names * Refactored coordinates TC * Removed unnecessary comments
- Loading branch information
1 parent
c1bcb90
commit 2c1c0d9
Showing
20 changed files
with
332 additions
and
232 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 jobs 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_job_contract(to_session: TOSession, | ||
response_template_data: dict[str, Union[Primitive, | ||
list[Union[Primitive, dict[str, object], list[object]]], | ||
dict[object, object]]], job_post_data: dict[str, object]) -> None: | ||
""" | ||
Test step to validate keys, values and data types from jobs 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 job_post_data: Fixture to get sample job data and actual job response. | ||
""" | ||
# validate job keys from jobs get response | ||
logger.info("Accessing /jobs endpoint through Traffic ops session.") | ||
|
||
job_id = job_post_data.get("id") | ||
if not isinstance(job_id, int): | ||
raise TypeError("malformed job in prerequisite data; 'id' not a integer") | ||
|
||
job_get_response: tuple[ | ||
Union[dict[str, object], list[Union[dict[str, object], list[object], Primitive]], Primitive], | ||
requests.Response | ||
] = to_session.get_jobs(query_params={"id": job_id}) | ||
try: | ||
job_data = job_get_response[0] | ||
if not isinstance(job_data, list): | ||
raise TypeError("malformed API response; 'response' property not an array") | ||
|
||
first_job = job_data[0] | ||
if not isinstance(first_job, dict): | ||
raise TypeError("malformed API response; first job in response is not an dict") | ||
logger.info("job Api get response %s", first_job) | ||
|
||
job_response_template = response_template_data.get("jobs") | ||
if not isinstance(job_response_template, dict): | ||
raise TypeError( | ||
f"job response template data must be a dict, not '{type(job_response_template)}'") | ||
|
||
# validate job values from prereq data in jobs get response. | ||
keys = ["deliveryService", "invalidationType", "startTime", "ttlHours"] | ||
prereq_values = [job_post_data[key] for key in keys] | ||
get_values = [first_job[key] for key in keys] | ||
|
||
assert validate(instance=first_job, schema=job_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 endpoint: API response was malformed") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters