Skip to content

Commit

Permalink
Added API contract testcase for Jobs endpoint (#7573)
Browse files Browse the repository at this point in the history
* 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
gokulakrishnansvm committed Jun 16, 2023
1 parent c1bcb90 commit 2c1c0d9
Show file tree
Hide file tree
Showing 20 changed files with 332 additions and 232 deletions.
287 changes: 211 additions & 76 deletions traffic_ops/testing/api_contract/v4/conftest.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@
"cachegroupId": 10
}
],
"jobs": [
{
"deliveryService": "test",
"invalidationType": "REFRESH",
"regex": "/.+",
"startTime": "2030-11-09T01:02:03Z",
"ttlHours": 72
}
],
"coordinates": [
{
"name": "test",
Expand Down
35 changes: 35 additions & 0 deletions traffic_ops/testing/api_contract/v4/data/response_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,41 @@
}
}
},
"jobs": {
"type": "object",
"required": [
"id",
"assetUrl",
"createdBy",
"deliveryService",
"ttlHours",
"invalidationType",
"startTime"
],
"properties": {
"id": {
"type": "integer"
},
"assetUrl": {
"type": "string"
},
"createdBy": {
"type": "string"
},
"deliveryService": {
"type": "string"
},
"ttlHours": {
"type": "integer"
},
"invalidationType": {
"type": "string"
},
"startTime": {
"type": "string"
}
}
},
"coordinates": {
"type": "object",
"required": [
Expand Down
11 changes: 0 additions & 11 deletions traffic_ops/testing/api_contract/v4/test_asns.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,3 @@ def test_asn_contract(to_session: TOSession,
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")
6 changes: 0 additions & 6 deletions traffic_ops/testing/api_contract/v4/test_cachegroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,3 @@ def test_cache_group_contract(to_session: TOSession,
except IndexError:
logger.error("Either prerequisite data or API response was malformed")
pytest.fail("API contract test failed for cachegroup endpoint: API response was malformed")
finally:
# Delete Cache group after test execution to avoid redundancy.
cache_group_id = cache_group_post_data.get("id")
if to_session.delete_cachegroups(cache_group_id=cache_group_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_cachegroup_contract")
6 changes: 0 additions & 6 deletions traffic_ops/testing/api_contract/v4/test_cdns.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,3 @@ def test_cdn_contract(
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")
finally:
# Delete CDN after test execution to avoid redundancy.
cdn_id = cdn_post_data.get("id")
if to_session.delete_cdn_by_id(cdn_id=cdn_id) is None:
logger.error("CDN returned by Traffic Ops is missing an 'id' property")
pytest.fail("Response from delete request is empty, Failing test_get_cdn")
6 changes: 0 additions & 6 deletions traffic_ops/testing/api_contract/v4/test_coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,3 @@ def test_coordinate_contract(to_session: TOSession,
except IndexError:
logger.error("Either prerequisite data or API response was malformed")
pytest.fail("API contract test failed for coordinates endpoint: API response was malformed")
finally:
# Delete coordinate after test execution to avoid redundancy.
coordinate_id = coordinate_post_data.get("id")
if to_session.delete_coordinates(query_params={"id": coordinate_id}) is None:
logger.error("coordinate returned by Traffic Ops is missing a 'id' property")
pytest.fail("Response from delete request is empty, Failing test_coordinates_contract")
16 changes: 0 additions & 16 deletions traffic_ops/testing/api_contract/v4/test_delivery_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,3 @@ def test_delivery_services_contract(to_session: TOSession,
except IndexError:
logger.error("Either prerequisite data or API response was malformed")
pytest.fail("API contract test failed for delivery_services endpoint: API response was malformed")
finally:
# Delete delivery_services after test execution to avoid redundancy.
delivery_service_id = delivery_services_post_data.get("id")
if to_session.delete_deliveryservice_by_id(delivery_service_id=delivery_service_id) is None:
logger.error("delivery_services returned by Traffic Ops is missing an 'id' property")
pytest.fail("Response from delete request is empty, Failing test_delivery_services_contract")

profile_id = delivery_services_post_data.get("profileId")
if to_session.delete_profile_by_id(profile_id=profile_id) is None:
logger.error("profile returned by Traffic Ops is missing an 'id' property")
pytest.fail("Response from delete request is empty, Failing test_delivery_services_contract")

cdn_id = delivery_services_post_data.get("cdnId")
if to_session.delete_cdn_by_id(cdn_id=cdn_id) is None:
logger.error("cdn returned by Traffic Ops is missing an 'id' property")
pytest.fail("Response from delete request is empty, Failing test_delivery_services_contract")
6 changes: 0 additions & 6 deletions traffic_ops/testing/api_contract/v4/test_divisions.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,3 @@ def test_division_contract(to_session: TOSession,
except IndexError:
logger.error("Either prerequisite data or API response was malformed")
pytest.fail("Failed due to malformation")
finally:
# Delete division after test execution to avoid redundancy.
division_id = division_post_data.get("id")
if to_session.delete_division(division_id=division_id) is None:
logger.error("Division returned by Traffic Ops is missing an 'id' property")
pytest.fail("Response from delete request is empty, Failing test_get_division")
76 changes: 76 additions & 0 deletions traffic_ops/testing/api_contract/v4/test_jobs.py
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")
11 changes: 0 additions & 11 deletions traffic_ops/testing/api_contract/v4/test_origins.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,3 @@ def test_origin_contract(
except IndexError:
logger.error("Either prerequisite data or API response was malformed")
pytest.fail("API contract test failed for origin endpoint: API response was malformed")
finally:
# Delete origin after test execution to avoid redundancy.
origin_id = origin_post_data.get("id")
if to_session.delete_origins(query_params={"id": origin_id}) is None:
logger.error("Origin returned by Traffic Ops is missing an 'id' property")
pytest.fail("Response from delete request is empty, Failing test_origin_contract")

delivery_service_id = origin_post_data.get("deliveryServiceId")
if to_session.delete_deliveryservice_by_id(delivery_service_id=delivery_service_id) is None:
logger.error("Delivery service returned by Traffic Ops is missing an 'id' property")
pytest.fail("Response from delete request is empty, Failing test_origin_contract")
6 changes: 0 additions & 6 deletions traffic_ops/testing/api_contract/v4/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,3 @@ def test_parameter_contract(to_session: TOSession,
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")
finally:
# Delete Parameter after test execution to avoid redundancy.
parameter_id = parameter_post_data.get("id")
if to_session.delete_parameter(parameter_id=parameter_id) is None:
logger.error("Parameter returned by Traffic Ops is missing an 'id' property")
pytest.fail("Response from delete request is empty, Failing test_parameter_contract")
11 changes: 0 additions & 11 deletions traffic_ops/testing/api_contract/v4/test_phys_locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,3 @@ def test_phys_locations_contract(to_session: TOSession,
except IndexError:
logger.error("Either prerequisite data or API response was malformed")
pytest.fail("API contract test failed for phys_locations endpoint: API response was malformed")
finally:
# Delete phys_location after test execution to avoid redundancy.
physical_location_id = phys_locations_post_data["id"]
if to_session.delete_physical_location(physical_location_id=physical_location_id) is None:
logger.error("phys_location returned by Traffic Ops is missing an 'id' property")
pytest.fail("Response from delete request is empty, Failing test_get_phys_location")

region_id = phys_locations_post_data["regionId"]
if to_session.delete_region(query_params={"id": region_id}) is None:
logger.error("Region returned by Traffic Ops is missing an 'id' property")
pytest.fail("Response from delete request is empty, Failing test_get_phys_location")
11 changes: 0 additions & 11 deletions traffic_ops/testing/api_contract/v4/test_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,3 @@ def test_profile_contract(to_session: TOSession,
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")
finally:
# Delete Profile after test execution to avoid redundancy.
profile_id = profile_post_data.get("id")
if to_session.delete_profile_by_id(profile_id=profile_id) is None:
logger.error("Profile returned by Traffic Ops is missing an 'id' property")
pytest.fail("Response from delete request is empty, Failing test_profile_contract")

cdn_id = profile_post_data.get("cdn")
if to_session.delete_cdn_by_id(cdn_id=cdn_id) is None:
logger.error("Cdn returned by Traffic Ops is missing an 'id' property")
pytest.fail("Response from delete request is empty, Failing test_profile_contract")
11 changes: 0 additions & 11 deletions traffic_ops/testing/api_contract/v4/test_regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,3 @@ def test_region_contract(to_session: TOSession,
except IndexError:
logger.error("Either prerequisite data or API response was malformed")
pytest.fail("API contract test failed for regions endpoint: API response was malformed")
finally:
# Delete region after test execution to avoid redundancy.
region_name = region_post_data.get("name")
if to_session.delete_region(query_params={"name": region_name}) is None:
logger.error("region returned by Traffic Ops is missing a 'name' property")
pytest.fail("Response from delete request is empty, Failing test_region_contract")

division_id = region_post_data.get("division")
if to_session.delete_division(division_id=division_id) is None:
logger.error("division returned by Traffic Ops is missing a 'name' property")
pytest.fail("Response from delete request is empty, Failing test_region_contract")
6 changes: 0 additions & 6 deletions traffic_ops/testing/api_contract/v4/test_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,3 @@ def test_role_contract(to_session: TOSession,
except IndexError:
logger.error("Either prerequisite data or API response was malformed")
pytest.fail("API contract test failed for roles endpoint: API response was malformed")
finally:
# Delete Role after test execution to avoid redundancy.
role_name = role_post_data.get("name")
if to_session.delete_role(query_params={"name": role_name}) is None:
logger.error("Role returned by Traffic Ops is missing an 'name' property")
pytest.fail("Response from delete request is empty, Failing test_role_contract")
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,3 @@ def test_server_capabilities_contract(to_session: TOSession,
except IndexError:
logger.error("Either prerequisite data or API response was malformed")
pytest.fail("API contract test failed for server_capabilities: API response was malformed")
finally:
# Delete server_capabilities after test execution to avoid redundancy.
server_capability_name = server_capabilities_post_data.get("name")
if to_session.delete_server_capabilities(query_params={"name": server_capability_name}) is None:
logger.error("server_capabilities returned by Traffic Ops is missing an 'id' property")
pytest.fail("Response from delete request is empty, Failing test_server_capabilities_contract")
30 changes: 1 addition & 29 deletions traffic_ops/testing/api_contract/v4/test_servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@

Primitive = Union[bool, int, float, str, None]


def test_server_contract(to_session: TOSession,
response_template_data: dict[str, Union[Primitive, list[Union[Primitive,
dict[str, object], list[object]]],
dict[object, object]]], server_post_data: dict[str, object]) -> None:
dict[object, object]]], server_post_data: dict[str, object],request: pytest.FixtureRequest) -> None:
"""
Test step to validate keys, values and data types from servers endpoint
response.
Expand All @@ -41,8 +40,6 @@ def test_server_contract(to_session: TOSession,
"""
# validate server keys from server get response
logger.info("Accessing /servers endpoint through Traffic ops session.")
profile_id = server_post_data[1]
server_post_data = server_post_data[0]

server_id = server_post_data.get("id")
if not isinstance(server_id, int):
Expand Down Expand Up @@ -76,28 +73,3 @@ def test_server_contract(to_session: TOSession,
except IndexError:
logger.error("Either prerequisite data or API response was malformed")
pytest.fail("API contract test failed for server endpoint: API response was malformed")
finally:
# Delete Server after test execution to avoid redundancy.
server_id = server_post_data.get("id")
if to_session.delete_server_by_id(server_id=server_id) is None:
logger.error("Server returned by Traffic Ops is missing an 'id' property")
pytest.fail("Response from delete request is empty, Failing test_server_contract")

cachegroup_id = server_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_server_contract")

if to_session.delete_profile_by_id(profile_id=profile_id) is None:
logger.error("Profile returned by Traffic Ops is missing an 'id' property")
pytest.fail("Response from delete request is empty, Failing test_server_contract")

cdn_id = server_post_data.get("cdnId")
if to_session.delete_cdn_by_id(cdn_id=cdn_id) is None:
logger.error("Cdn returned by Traffic Ops is missing an 'id' property")
pytest.fail("Response from delete request is empty, Failing test_server_contract")

phys_loc_id = server_post_data.get("physLocationId")
if to_session.delete_physical_location(physical_location_id=phys_loc_id) is None:
logger.error("Physical location returned by Traffic Ops is missing an 'id' property")
pytest.fail("Response from delete request is empty, Failing test_server_contract")
8 changes: 0 additions & 8 deletions traffic_ops/testing/api_contract/v4/test_statuses.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,3 @@ def test_status_contract(
except IndexError:
logger.error("Either prerequisite data or API response was malformed")
pytest.fail("API contract test failed for status endpoint: API response was malformed")
finally:
# Delete Status after test execution to avoid redundancy.
try:
status_id = status_post_data["id"]
to_session.delete_status_by_id(status_id=status_id)
except IndexError:
logger.error("Status returned by Traffic Ops is missing an 'id' property")
pytest.fail("Response from delete request is empty, Failing test_get_status")
6 changes: 0 additions & 6 deletions traffic_ops/testing/api_contract/v4/test_tenants.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,3 @@ def test_tenant_contract(to_session: TOSession,
except IndexError:
logger.error("Either prerequisite data or API response was malformed")
pytest.fail("API contract test failed for tenants endpoint: API response was malformed")
finally:
# Delete tenant after test execution to avoid redundancy.
tenant_id = tenant_post_data.get("id")
if to_session.delete_tenant(tenant_id=tenant_id) is None:
logger.error("Tenant returned by Traffic Ops is missing an 'id' property")
pytest.fail("Response from delete request is empty, Failing test_tenant_contract")

0 comments on commit 2c1c0d9

Please sign in to comment.