ocket8888 commented on code in PR #7400:
URL: https://github.com/apache/trafficcontrol/pull/7400#discussion_r1142352394
##########
traffic_ops/testing/api_contract/v4/to_data.json:
##########
@@ -0,0 +1,9 @@
+{
+ "test": {
+ "user": "admin",
+ "password": "twelve12",
+ "url": "https://localhost/api/4.0",
+ "api_version": "4.0",
+ "port": "443"
+ }
+}
Review Comment:
missing newline at EOF
##########
traffic_ops/testing/api_contract/v4/to_data.json:
##########
@@ -0,0 +1,9 @@
+{
+ "test": {
+ "user": "admin",
+ "password": "twelve12",
+ "url": "https://localhost/api/4.0",
+ "api_version": "4.0",
+ "port": "443"
Review Comment:
numbers should be numbers, not strings
##########
traffic_ops/testing/api_contract/v4/test_cdns.py:
##########
@@ -0,0 +1,68 @@
+"""Api Contract Test Case for cdns endpoint"""
Review Comment:
file missing license header
##########
traffic_ops/testing/api_contract/v4/test_cdns.py:
##########
@@ -0,0 +1,68 @@
+"""Api Contract Test Case for cdns endpoint"""
+import json
+import logging
+import pytest
+
+# Create and configure logger
+logger = logging.getLogger()
+
+
[email protected](name="get_cdn_data")
+def get_cdn_prereq_data():
Review Comment:
missing return type on function
##########
traffic_ops/testing/api_contract/v4/test_cdns.py:
##########
@@ -0,0 +1,68 @@
+"""Api Contract Test Case for cdns endpoint"""
+import json
+import logging
+import pytest
+
+# Create and configure logger
+logger = logging.getLogger()
+
+
[email protected](name="get_cdn_data")
+def get_cdn_prereq_data():
+ """PyTest Fixture to store prereq data for cdns endpoint"""
+ # Response keys for cdns endpoint
+ with open('prerequisite_data.json', encoding="utf-8", mode='r') as
prereq_file:
+ data = json.load(prereq_file)
+ cdn_data = data["cdns"]
+ return cdn_data
+
+
+def test_get_cdn(to_session, get_cdn_data, cdn_prereq):
Review Comment:
missing argument types, function return types
##########
traffic_ops/testing/api_contract/v4/test_cdns.py:
##########
@@ -0,0 +1,68 @@
+"""Api Contract Test Case for cdns endpoint"""
+import json
+import logging
+import pytest
+
+# Create and configure logger
+logger = logging.getLogger()
+
+
[email protected](name="get_cdn_data")
+def get_cdn_prereq_data():
+ """PyTest Fixture to store prereq data for cdns endpoint"""
+ # Response keys for cdns endpoint
+ with open('prerequisite_data.json', encoding="utf-8", mode='r') as
prereq_file:
+ data = json.load(prereq_file)
+ cdn_data = data["cdns"]
+ return cdn_data
+
+
+def test_get_cdn(to_session, get_cdn_data, cdn_prereq):
+ """Test step to validate keys, values and data types from cdns endpoint
response
Review Comment:
missing punctuation at the end of the sentence
##########
traffic_ops/testing/api_contract/v4/test_cdns.py:
##########
@@ -0,0 +1,68 @@
+"""Api Contract Test Case for cdns endpoint"""
+import json
+import logging
+import pytest
+
+# Create and configure logger
+logger = logging.getLogger()
+
+
[email protected](name="get_cdn_data")
+def get_cdn_prereq_data():
+ """PyTest Fixture to store prereq data for cdns endpoint"""
Review Comment:
missing punctuation at the end of the sentence
##########
traffic_ops/testing/api_contract/v4/test_cdns.py:
##########
@@ -0,0 +1,68 @@
+"""Api Contract Test Case for cdns endpoint"""
+import json
+import logging
+import pytest
+
+# Create and configure logger
+logger = logging.getLogger()
+
+
[email protected](name="get_cdn_data")
+def get_cdn_prereq_data():
+ """PyTest Fixture to store prereq data for cdns endpoint"""
+ # Response keys for cdns endpoint
+ with open('prerequisite_data.json', encoding="utf-8", mode='r') as
prereq_file:
+ data = json.load(prereq_file)
+ cdn_data = data["cdns"]
Review Comment:
quotation style should be consistent; linter won't force you to use one or
the other, but for whatever it's worth I like to use double quotes because
that's more familiar to people who use languages like Go and Java, where `"`
and `'` aren't interchangeable.
##########
traffic_ops/testing/api_contract/v4/test_cdns.py:
##########
@@ -0,0 +1,68 @@
+"""Api Contract Test Case for cdns endpoint"""
+import json
+import logging
+import pytest
+
+# Create and configure logger
+logger = logging.getLogger()
+
+
[email protected](name="get_cdn_data")
+def get_cdn_prereq_data():
+ """PyTest Fixture to store prereq data for cdns endpoint"""
+ # Response keys for cdns endpoint
+ with open('prerequisite_data.json', encoding="utf-8", mode='r') as
prereq_file:
+ data = json.load(prereq_file)
+ cdn_data = data["cdns"]
+ return cdn_data
+
+
+def test_get_cdn(to_session, get_cdn_data, cdn_prereq):
+ """Test step to validate keys, values and data types from cdns endpoint
response
+ :param to_session: Fixture to get Traffic ops session
+ :type to_session: TOsession
+ :param get_cdn_data: Fixture to get cdn data from a prereq file
+ :type get_cdn_data: dict
+ :param cdn_prereq: Fixture to get sample cdn data and actual cdn response
+ :type cdn_prereq: list
+ """
+ # validate CDN keys from cdns get response
+ logger.info("Accessing Cdn endpoint through Traffic ops session")
+ cdn_name = cdn_prereq[0]["name"]
+ cdn_get_response = to_session.get_cdns(
+ query_params={"name": str(cdn_name)})
Review Comment:
is this cast necessary? Why would the `cdn_name` not be a string?
##########
traffic_ops/testing/api_contract/v4/test_cdns.py:
##########
@@ -0,0 +1,68 @@
+"""Api Contract Test Case for cdns endpoint"""
+import json
+import logging
+import pytest
+
+# Create and configure logger
+logger = logging.getLogger()
+
+
[email protected](name="get_cdn_data")
+def get_cdn_prereq_data():
+ """PyTest Fixture to store prereq data for cdns endpoint"""
+ # Response keys for cdns endpoint
+ with open('prerequisite_data.json', encoding="utf-8", mode='r') as
prereq_file:
+ data = json.load(prereq_file)
+ cdn_data = data["cdns"]
+ return cdn_data
+
+
+def test_get_cdn(to_session, get_cdn_data, cdn_prereq):
+ """Test step to validate keys, values and data types from cdns endpoint
response
+ :param to_session: Fixture to get Traffic ops session
+ :type to_session: TOsession
+ :param get_cdn_data: Fixture to get cdn data from a prereq file
+ :type get_cdn_data: dict
+ :param cdn_prereq: Fixture to get sample cdn data and actual cdn response
+ :type cdn_prereq: list
+ """
+ # validate CDN keys from cdns get response
+ logger.info("Accessing Cdn endpoint through Traffic ops session")
+ cdn_name = cdn_prereq[0]["name"]
+ cdn_get_response = to_session.get_cdns(
+ query_params={"name": str(cdn_name)})
+ try:
+ cdn_data = cdn_get_response[0]
+ cdn_keys = list(cdn_data[0].keys())
+ logger.info(
+ "CDN Keys from cdns endpoint response %s", cdn_keys)
+ # validate cdn values from prereq data in cdns get response
+ prereq_values = [cdn_prereq[0]['name'], cdn_prereq[0]
+ ['domainName'], cdn_prereq[0]['dnssecEnabled']]
+ get_values = [cdn_data[0]['name'], cdn_data[0]
+ ['domainName'], cdn_data[0]['dnssecEnabled']]
+ # validate data types for values from cdn get json response
+ for (prereq_value, get_value) in zip(prereq_values, get_values):
+ assert isinstance(prereq_value, type(get_value))
+ assert cdn_keys.sort() == list(get_cdn_data.keys()).sort()
+ assert get_values == prereq_values
+ except IndexError:
+ logger.error("No CDN data from cdns get request")
+ pytest.fail("Response from get request is empty, Failing test_get_cdn")
+
+
[email protected](autouse=True)
+def pytest_sessionfinish(cdn_prereq, to_session):
Review Comment:
missing argument types, function return type
##########
traffic_ops/testing/api_contract/v4/requirements.txt:
##########
@@ -0,0 +1,8 @@
+Apache-TrafficControl==3.1.0
Review Comment:
file missing license header
##########
traffic_ops/testing/api_contract/v4/test_cdns.py:
##########
@@ -0,0 +1,68 @@
+"""Api Contract Test Case for cdns endpoint"""
+import json
+import logging
+import pytest
+
+# Create and configure logger
+logger = logging.getLogger()
+
+
[email protected](name="get_cdn_data")
+def get_cdn_prereq_data():
+ """PyTest Fixture to store prereq data for cdns endpoint"""
+ # Response keys for cdns endpoint
+ with open('prerequisite_data.json', encoding="utf-8", mode='r') as
prereq_file:
+ data = json.load(prereq_file)
+ cdn_data = data["cdns"]
+ return cdn_data
+
+
+def test_get_cdn(to_session, get_cdn_data, cdn_prereq):
+ """Test step to validate keys, values and data types from cdns endpoint
response
+ :param to_session: Fixture to get Traffic ops session
+ :type to_session: TOsession
+ :param get_cdn_data: Fixture to get cdn data from a prereq file
+ :type get_cdn_data: dict
+ :param cdn_prereq: Fixture to get sample cdn data and actual cdn response
+ :type cdn_prereq: list
+ """
+ # validate CDN keys from cdns get response
+ logger.info("Accessing Cdn endpoint through Traffic ops session")
+ cdn_name = cdn_prereq[0]["name"]
+ cdn_get_response = to_session.get_cdns(
+ query_params={"name": str(cdn_name)})
+ try:
+ cdn_data = cdn_get_response[0]
+ cdn_keys = list(cdn_data[0].keys())
+ logger.info(
+ "CDN Keys from cdns endpoint response %s", cdn_keys)
+ # validate cdn values from prereq data in cdns get response
+ prereq_values = [cdn_prereq[0]['name'], cdn_prereq[0]
+ ['domainName'], cdn_prereq[0]['dnssecEnabled']]
+ get_values = [cdn_data[0]['name'], cdn_data[0]
+ ['domainName'], cdn_data[0]['dnssecEnabled']]
+ # validate data types for values from cdn get json response
+ for (prereq_value, get_value) in zip(prereq_values, get_values):
+ assert isinstance(prereq_value, type(get_value))
+ assert cdn_keys.sort() == list(get_cdn_data.keys()).sort()
+ assert get_values == prereq_values
+ except IndexError:
+ logger.error("No CDN data from cdns get request")
+ pytest.fail("Response from get request is empty, Failing test_get_cdn")
+
+
[email protected](autouse=True)
+def pytest_sessionfinish(cdn_prereq, to_session):
+ """Delete CDN after test execution to avoid redundancy
Review Comment:
missing punctuation
##########
traffic_ops/testing/api_contract/v4/test_cdns.py:
##########
@@ -0,0 +1,68 @@
+"""Api Contract Test Case for cdns endpoint"""
+import json
+import logging
+import pytest
+
+# Create and configure logger
+logger = logging.getLogger()
+
+
[email protected](name="get_cdn_data")
+def get_cdn_prereq_data():
+ """PyTest Fixture to store prereq data for cdns endpoint"""
+ # Response keys for cdns endpoint
+ with open('prerequisite_data.json', encoding="utf-8", mode='r') as
prereq_file:
+ data = json.load(prereq_file)
+ cdn_data = data["cdns"]
+ return cdn_data
+
+
+def test_get_cdn(to_session, get_cdn_data, cdn_prereq):
+ """Test step to validate keys, values and data types from cdns endpoint
response
+ :param to_session: Fixture to get Traffic ops session
+ :type to_session: TOsession
+ :param get_cdn_data: Fixture to get cdn data from a prereq file
+ :type get_cdn_data: dict
+ :param cdn_prereq: Fixture to get sample cdn data and actual cdn response
+ :type cdn_prereq: list
+ """
+ # validate CDN keys from cdns get response
+ logger.info("Accessing Cdn endpoint through Traffic ops session")
+ cdn_name = cdn_prereq[0]["name"]
+ cdn_get_response = to_session.get_cdns(
+ query_params={"name": str(cdn_name)})
+ try:
+ cdn_data = cdn_get_response[0]
+ cdn_keys = list(cdn_data[0].keys())
+ logger.info(
+ "CDN Keys from cdns endpoint response %s", cdn_keys)
+ # validate cdn values from prereq data in cdns get response
+ prereq_values = [cdn_prereq[0]['name'], cdn_prereq[0]
+ ['domainName'], cdn_prereq[0]['dnssecEnabled']]
+ get_values = [cdn_data[0]['name'], cdn_data[0]
+ ['domainName'], cdn_data[0]['dnssecEnabled']]
+ # validate data types for values from cdn get json response
+ for (prereq_value, get_value) in zip(prereq_values, get_values):
+ assert isinstance(prereq_value, type(get_value))
+ assert cdn_keys.sort() == list(get_cdn_data.keys()).sort()
+ assert get_values == prereq_values
+ except IndexError:
+ logger.error("No CDN data from cdns get request")
+ pytest.fail("Response from get request is empty, Failing test_get_cdn")
+
+
[email protected](autouse=True)
+def pytest_sessionfinish(cdn_prereq, to_session):
+ """Delete CDN after test execution to avoid redundancy
+ :param to_session: Fixture to get Traffic ops session
+ :type to_session: TOsession
+ :param cdn_prereq: Fixture to get sample cdn data and actual cdn response
+ :type cdn_prereq: list
+ """
+ yield
+ try:
+ cdn_response = cdn_prereq[1]
+ cdn_id = cdn_response["id"]
+ to_session.delete_cdn_by_id(cdn_id=cdn_id)
+ except IndexError:
+ logger.error("CDN doesn't created")
Review Comment:
nit: grammar: "CDN **wasn't** created"
##########
traffic_ops/testing/api_contract/v4/requirements.txt:
##########
@@ -0,0 +1,8 @@
+Apache-TrafficControl==3.1.0
+attrs==22.2.0
+munch==2.5.0
+packaging==23.0
Review Comment:
what is `packaging` used for? I don't see it being imported anywhere
##########
traffic_ops/testing/api_contract/v4/requirements.txt:
##########
@@ -0,0 +1,8 @@
+Apache-TrafficControl==3.1.0
+attrs==22.2.0
+munch==2.5.0
Review Comment:
what is `munch` used for? I don't see it being imported anywhere
##########
traffic_ops/testing/api_contract/v4/requirements.txt:
##########
@@ -0,0 +1,8 @@
+Apache-TrafficControl==3.1.0
+attrs==22.2.0
+munch==2.5.0
+packaging==23.0
+pytest==7.2.1
+requests==2.28.2
+trafficcontrol==1.0.0
+urllib3==1.26.14
Review Comment:
what is `urllib3` used for? I don't see it being imported anywhere
##########
traffic_ops/testing/api_contract/v4/pytest.ini:
##########
@@ -0,0 +1,6 @@
+[pytest]
Review Comment:
file missing license header
##########
traffic_ops/testing/api_contract/v4/pytest.ini:
##########
@@ -0,0 +1,6 @@
+[pytest]
+
+log_cli = 1
+log_cli_level = INFO
+log_cli_format = %(asctime)s [%(levelname)8s] %(message)s
(%(filename)s:%(lineno)s)
+log_cli_date_format=%Y-%m-%d %H:%M:%S
Review Comment:
We should use RFC3339 for consistency throughout the project. That would be
`%Y-%m-%dT%H:%M:%S`, optionally with a `%z` suffix.
##########
traffic_ops/testing/api_contract/v4/conftest.py:
##########
@@ -0,0 +1,103 @@
+"""This module is used to create a Traffic Ops session
+and to store prerequisite data for endpoints"""
+import json
+import logging
+import sys
+from random import randint
+from urllib.parse import urlparse
+import pytest
+from trafficops.tosession import TOSession
+from trafficops.restapi import OperationError
+
+
+# Create and configure logger
+logger = logging.getLogger()
+
+
+def pytest_addoption(parser):
+ """Passing in Traffic Ops Arguments [Username, Password, Url and Hostname]
from Command Line"""
+ parser.addoption(
+ '--to_user', action='store', help='User name for Traffic Ops Session'
+ )
+ parser.addoption(
+ '--to_password', action='store', help='Password for Traffic Ops
Session'
+ )
+ parser.addoption(
+ '--to_url', action='store', help='Traffic Ops URL'
+ )
+
+
[email protected](name="to_args")
+def to_data(pytestconfig):
+ """PyTest fixture to store Traffic ops Arguments passed from command
line"""
+ args = {}
+ with open('to_data.json', encoding="utf-8", mode='r') as session_file:
+ data = json.load(session_file)
+ session_data = data["test"]
+ args['api_version'] = session_data.get("api_version")
+ args['port'] = session_data.get("port")
+
+ to_user = pytestconfig.getoption('--to_user')
+ to_password = pytestconfig.getoption('--to_password')
+ to_url = pytestconfig.getoption('--to_url')
+
+ if not all([to_user, to_password, to_url]):
+ logger.info(
+ "Traffic Ops session data were not passed from Command line Args")
+ args['user'] = session_data.get('user')
+ args['password'] = session_data.get('password')
+ args['url'] = session_data.get('url')
+ else:
+ args['user'] = to_user
+ args['password'] = to_password
+ args['url'] = to_url
+ logger.info("Parsed Traffic ops session data from args %s", args)
+ return args
+
+
[email protected](name="to_session")
+def to_login(to_args):
+ """PyTest Fixture to create a Traffic Ops session from Traffic Ops
Arguments
+ passed as command line arguments in to_args fixture in conftest
+ :param to_args: Fixture to get Traffic ops session arguments
+ :type to_args: dict
+ """
+ # Create a Traffic Ops V4 session and login
+ to_url = urlparse(to_args["url"])
+ to_host = to_url.hostname
+ try:
+ to_session = TOSession(host_ip=to_host, host_port=to_args["port"],
+ api_version=to_args["api_version"], ssl=True,
verify_cert=False)
+ logger.info("Established Traffic Ops Session")
+ except OperationError:
+ sys.exit(-1)
+
+ # Login To TO_API
+ to_session.login(to_args["user"], to_args["password"])
+ logger.info("Successfully logged into Traffic Ops")
+ return to_session
+
+
[email protected]()
+def cdn_prereq(to_session, get_cdn_data):
Review Comment:
missing argument types and function return type
##########
traffic_ops/testing/api_contract/v4/conftest.py:
##########
@@ -0,0 +1,103 @@
+"""This module is used to create a Traffic Ops session
+and to store prerequisite data for endpoints"""
+import json
+import logging
+import sys
+from random import randint
+from urllib.parse import urlparse
+import pytest
+from trafficops.tosession import TOSession
+from trafficops.restapi import OperationError
+
+
+# Create and configure logger
+logger = logging.getLogger()
+
+
+def pytest_addoption(parser):
+ """Passing in Traffic Ops Arguments [Username, Password, Url and Hostname]
from Command Line"""
+ parser.addoption(
+ '--to_user', action='store', help='User name for Traffic Ops Session'
+ )
+ parser.addoption(
+ '--to_password', action='store', help='Password for Traffic Ops
Session'
+ )
+ parser.addoption(
+ '--to_url', action='store', help='Traffic Ops URL'
+ )
+
+
[email protected](name="to_args")
+def to_data(pytestconfig):
+ """PyTest fixture to store Traffic ops Arguments passed from command
line"""
+ args = {}
+ with open('to_data.json', encoding="utf-8", mode='r') as session_file:
+ data = json.load(session_file)
+ session_data = data["test"]
+ args['api_version'] = session_data.get("api_version")
+ args['port'] = session_data.get("port")
+
+ to_user = pytestconfig.getoption('--to_user')
+ to_password = pytestconfig.getoption('--to_password')
+ to_url = pytestconfig.getoption('--to_url')
+
+ if not all([to_user, to_password, to_url]):
+ logger.info(
+ "Traffic Ops session data were not passed from Command line Args")
+ args['user'] = session_data.get('user')
+ args['password'] = session_data.get('password')
+ args['url'] = session_data.get('url')
+ else:
+ args['user'] = to_user
+ args['password'] = to_password
+ args['url'] = to_url
+ logger.info("Parsed Traffic ops session data from args %s", args)
+ return args
+
+
[email protected](name="to_session")
+def to_login(to_args):
Review Comment:
missing argument type and function return type
##########
traffic_ops/testing/api_contract/v4/prerequisite_data.json:
##########
@@ -0,0 +1 @@
+{"cdns": {"name": "test631", "domainName": "quest574", "dnssecEnabled": false,
"id": null, "lastUpdated": null}}
Review Comment:
this document should be formatted to make it easier to both read and edit.
Also, `null` is not a valid value for either `id` or `lastUpdated`, so is
this meant to represent a CDN in a request or a response?
##########
traffic_ops/testing/api_contract/v4/requirements.txt:
##########
@@ -0,0 +1,8 @@
+Apache-TrafficControl==3.1.0
+attrs==22.2.0
+munch==2.5.0
+packaging==23.0
+pytest==7.2.1
+requests==2.28.2
Review Comment:
what is `requests` used for? I don't see it being imported anywhere
##########
traffic_ops/testing/api_contract/v4/requirements.txt:
##########
@@ -0,0 +1,8 @@
+Apache-TrafficControl==3.1.0
+attrs==22.2.0
+munch==2.5.0
+packaging==23.0
+pytest==7.2.1
+requests==2.28.2
+trafficcontrol==1.0.0
Review Comment:
There's no package named `trafficcontrol` in PyPI that I could find; where
is this coming from?
##########
traffic_ops/testing/api_contract/v4/conftest.py:
##########
@@ -0,0 +1,103 @@
+"""This module is used to create a Traffic Ops session
+and to store prerequisite data for endpoints"""
+import json
+import logging
+import sys
+from random import randint
+from urllib.parse import urlparse
+import pytest
+from trafficops.tosession import TOSession
+from trafficops.restapi import OperationError
+
+
+# Create and configure logger
+logger = logging.getLogger()
+
+
+def pytest_addoption(parser):
+ """Passing in Traffic Ops Arguments [Username, Password, Url and Hostname]
from Command Line"""
+ parser.addoption(
+ '--to_user', action='store', help='User name for Traffic Ops Session'
+ )
+ parser.addoption(
+ '--to_password', action='store', help='Password for Traffic Ops
Session'
+ )
+ parser.addoption(
+ '--to_url', action='store', help='Traffic Ops URL'
+ )
+
+
[email protected](name="to_args")
+def to_data(pytestconfig):
+ """PyTest fixture to store Traffic ops Arguments passed from command
line"""
+ args = {}
+ with open('to_data.json', encoding="utf-8", mode='r') as session_file:
+ data = json.load(session_file)
+ session_data = data["test"]
+ args['api_version'] = session_data.get("api_version")
+ args['port'] = session_data.get("port")
+
+ to_user = pytestconfig.getoption('--to_user')
+ to_password = pytestconfig.getoption('--to_password')
+ to_url = pytestconfig.getoption('--to_url')
+
+ if not all([to_user, to_password, to_url]):
+ logger.info(
+ "Traffic Ops session data were not passed from Command line Args")
+ args['user'] = session_data.get('user')
+ args['password'] = session_data.get('password')
+ args['url'] = session_data.get('url')
+ else:
+ args['user'] = to_user
+ args['password'] = to_password
+ args['url'] = to_url
+ logger.info("Parsed Traffic ops session data from args %s", args)
+ return args
+
+
[email protected](name="to_session")
+def to_login(to_args):
+ """PyTest Fixture to create a Traffic Ops session from Traffic Ops
Arguments
+ passed as command line arguments in to_args fixture in conftest
+ :param to_args: Fixture to get Traffic ops session arguments
+ :type to_args: dict
+ """
+ # Create a Traffic Ops V4 session and login
+ to_url = urlparse(to_args["url"])
+ to_host = to_url.hostname
+ try:
+ to_session = TOSession(host_ip=to_host, host_port=to_args["port"],
+ api_version=to_args["api_version"], ssl=True,
verify_cert=False)
+ logger.info("Established Traffic Ops Session")
+ except OperationError:
+ sys.exit(-1)
+
+ # Login To TO_API
+ to_session.login(to_args["user"], to_args["password"])
+ logger.info("Successfully logged into Traffic Ops")
+ return to_session
+
+
[email protected]()
+def cdn_prereq(to_session, get_cdn_data):
+ """PyTest Fixture to create POST data for cdns endpoint
Review Comment:
missing punctuation
##########
traffic_ops/testing/api_contract/v4/requirements.txt:
##########
@@ -0,0 +1,8 @@
+Apache-TrafficControl==3.1.0
+attrs==22.2.0
Review Comment:
What is `attrs` used for? I don't see it being imported anywhere
##########
traffic_ops/testing/api_contract/v4/conftest.py:
##########
@@ -0,0 +1,103 @@
+"""This module is used to create a Traffic Ops session
+and to store prerequisite data for endpoints"""
+import json
+import logging
+import sys
+from random import randint
+from urllib.parse import urlparse
+import pytest
+from trafficops.tosession import TOSession
+from trafficops.restapi import OperationError
+
+
+# Create and configure logger
+logger = logging.getLogger()
+
+
+def pytest_addoption(parser):
+ """Passing in Traffic Ops Arguments [Username, Password, Url and Hostname]
from Command Line"""
+ parser.addoption(
+ '--to_user', action='store', help='User name for Traffic Ops Session'
+ )
+ parser.addoption(
+ '--to_password', action='store', help='Password for Traffic Ops
Session'
+ )
+ parser.addoption(
+ '--to_url', action='store', help='Traffic Ops URL'
+ )
+
+
[email protected](name="to_args")
+def to_data(pytestconfig):
+ """PyTest fixture to store Traffic ops Arguments passed from command
line"""
+ args = {}
+ with open('to_data.json', encoding="utf-8", mode='r') as session_file:
+ data = json.load(session_file)
+ session_data = data["test"]
+ args['api_version'] = session_data.get("api_version")
+ args['port'] = session_data.get("port")
+
+ to_user = pytestconfig.getoption('--to_user')
+ to_password = pytestconfig.getoption('--to_password')
+ to_url = pytestconfig.getoption('--to_url')
Review Comment:
command-line long-options should separate words with a hyphen (<kbd>-</kbd>)
rather than an underscore (<kbd>_</kbd>) as it's easier to type
##########
traffic_ops/testing/api_contract/v4/conftest.py:
##########
@@ -0,0 +1,103 @@
+"""This module is used to create a Traffic Ops session
+and to store prerequisite data for endpoints"""
+import json
+import logging
+import sys
+from random import randint
+from urllib.parse import urlparse
+import pytest
+from trafficops.tosession import TOSession
+from trafficops.restapi import OperationError
+
+
+# Create and configure logger
+logger = logging.getLogger()
+
+
+def pytest_addoption(parser):
+ """Passing in Traffic Ops Arguments [Username, Password, Url and Hostname]
from Command Line"""
+ parser.addoption(
+ '--to_user', action='store', help='User name for Traffic Ops Session'
+ )
+ parser.addoption(
+ '--to_password', action='store', help='Password for Traffic Ops
Session'
+ )
+ parser.addoption(
+ '--to_url', action='store', help='Traffic Ops URL'
+ )
+
+
[email protected](name="to_args")
+def to_data(pytestconfig):
Review Comment:
missing argument type and function return type
##########
traffic_ops/testing/api_contract/README.md:
##########
@@ -0,0 +1,74 @@
+# Traffic Ops API Contract Tests
+
+The Traffic Ops API Contract tests are used to validate the Traffic Ops API's.
+
+## Setup
+
+In order to run the tests you will need a running instance of Traffic Ops and
Traffic Ops DB:
+
+1. **Traffic Ops Database** configured port access
+ - _Usually 5432 - should match the value set in database.conf and the
**trafficOpsDB port** in traffic-ops-test.conf_
+2. **Traffic Ops** configured port access
+ - _Usually 443 or 60443 - should match the value set in cdn.conf and the
**URL** in traffic-ops-test.conf_
+3. Running Postgres instance with a database that matches the **trafficOpsDB
dbname** value set in traffic-ops-test.conf
+ - For example to set up the `to_test` database do the following:
+
+ ```console
+ $ cd trafficcontrol/traffic_ops/app
+ $ db/admin --env=test reset
+ ```
+
+ The Traffic Ops users will be created by the tool for accessing the API
once the database is accessible.
+
+ To test if `db/admin` ran all migrations successfully, you can run the
following command from the `traffic_ops/app` directory:
+
+ ```console
+ db/admin -env=test dbversion
+ ```
+ The result should be something similar to:
+ ```
+ dbversion 2021070800000000
+ ```
+ If migrations did not run successfully, you may see:
+ ```
+ dbversion 20181206000000 (dirty)
+ ```
+ Make sure **trafficOpsDB dbname** in traffic-ops-test.conf is set to:
`to_test`
+
+ For more info see:
http://trafficcontrol.apache.org/docs/latest/development/traffic_ops.html?highlight=reset
+
+4. A running Traffic Ops Golang instance pointing to the `to_test` database.
+
+ ```console
+ $ cd trafficcontrol/traffic_ops/traffic_ops_golang
+ $ cp ../app/conf/cdn.conf $HOME/cdn.conf
+ $ go build && ./traffic_ops_golang -cfg $HOME/cdn.conf -dbcfg
../app/conf/test/database.conf
+ ```
+ Verify that the passwords defined for your `to_test` database match:
+ - `trafficcontrol/traffic_ops/app/conf/test/database.conf`
+ - `traffic-ops-test.conf`
+
+5. Install the requirements for testing API contract tests
+
+ ```console
+ pip install -r /path/to/requirements.txt
+ ```
+
+## Running the API Contract Tests
+
+The API Contract tests are run using `pytest` from the
**traffic_ops/testing/api_contract/v4** directory
Review Comment:
file paths should use mono space, not bold
##########
traffic_ops/testing/api_contract/v4/conftest.py:
##########
@@ -0,0 +1,103 @@
+"""This module is used to create a Traffic Ops session
+and to store prerequisite data for endpoints"""
Review Comment:
missing punctuation
##########
traffic_ops/testing/api_contract/v4/conftest.py:
##########
@@ -0,0 +1,103 @@
+"""This module is used to create a Traffic Ops session
+and to store prerequisite data for endpoints"""
+import json
+import logging
+import sys
+from random import randint
+from urllib.parse import urlparse
+import pytest
+from trafficops.tosession import TOSession
+from trafficops.restapi import OperationError
+
+
+# Create and configure logger
+logger = logging.getLogger()
+
+
+def pytest_addoption(parser):
+ """Passing in Traffic Ops Arguments [Username, Password, Url and Hostname]
from Command Line"""
+ parser.addoption(
+ '--to_user', action='store', help='User name for Traffic Ops Session'
+ )
+ parser.addoption(
+ '--to_password', action='store', help='Password for Traffic Ops
Session'
+ )
+ parser.addoption(
+ '--to_url', action='store', help='Traffic Ops URL'
+ )
Review Comment:
These should ideally default to the values of [our widely used environment
variables](https://traffic-control-cdn.readthedocs.io/en/latest/development/environment_variables.html)
##########
traffic_ops/testing/api_contract/README.md:
##########
@@ -0,0 +1,74 @@
+# Traffic Ops API Contract Tests
+
+The Traffic Ops API Contract tests are used to validate the Traffic Ops API's.
+
+## Setup
+
+In order to run the tests you will need a running instance of Traffic Ops and
Traffic Ops DB:
+
+1. **Traffic Ops Database** configured port access
+ - _Usually 5432 - should match the value set in database.conf and the
**trafficOpsDB port** in traffic-ops-test.conf_
+2. **Traffic Ops** configured port access
+ - _Usually 443 or 60443 - should match the value set in cdn.conf and the
**URL** in traffic-ops-test.conf_
+3. Running Postgres instance with a database that matches the **trafficOpsDB
dbname** value set in traffic-ops-test.conf
+ - For example to set up the `to_test` database do the following:
+
+ ```console
+ $ cd trafficcontrol/traffic_ops/app
+ $ db/admin --env=test reset
+ ```
+
+ The Traffic Ops users will be created by the tool for accessing the API
once the database is accessible.
+
+ To test if `db/admin` ran all migrations successfully, you can run the
following command from the `traffic_ops/app` directory:
+
+ ```console
+ db/admin -env=test dbversion
+ ```
+ The result should be something similar to:
+ ```
+ dbversion 2021070800000000
+ ```
+ If migrations did not run successfully, you may see:
+ ```
+ dbversion 20181206000000 (dirty)
+ ```
+ Make sure **trafficOpsDB dbname** in traffic-ops-test.conf is set to:
`to_test`
+
+ For more info see:
http://trafficcontrol.apache.org/docs/latest/development/traffic_ops.html?highlight=reset
+
+4. A running Traffic Ops Golang instance pointing to the `to_test` database.
+
+ ```console
+ $ cd trafficcontrol/traffic_ops/traffic_ops_golang
+ $ cp ../app/conf/cdn.conf $HOME/cdn.conf
+ $ go build && ./traffic_ops_golang -cfg $HOME/cdn.conf -dbcfg
../app/conf/test/database.conf
Review Comment:
indentation is inconsistent here. You're using spaces everywhere else, looks
like, but I'd encourage you to instead use real indentation. But either one is
fine, just has to be consistent.
##########
traffic_ops/testing/api_contract/README.md:
##########
@@ -0,0 +1,74 @@
+# Traffic Ops API Contract Tests
+
+The Traffic Ops API Contract tests are used to validate the Traffic Ops API's.
+
+## Setup
+
+In order to run the tests you will need a running instance of Traffic Ops and
Traffic Ops DB:
+
+1. **Traffic Ops Database** configured port access
+ - _Usually 5432 - should match the value set in database.conf and the
**trafficOpsDB port** in traffic-ops-test.conf_
+2. **Traffic Ops** configured port access
+ - _Usually 443 or 60443 - should match the value set in cdn.conf and the
**URL** in traffic-ops-test.conf_
+3. Running Postgres instance with a database that matches the **trafficOpsDB
dbname** value set in traffic-ops-test.conf
+ - For example to set up the `to_test` database do the following:
+
+ ```console
+ $ cd trafficcontrol/traffic_ops/app
+ $ db/admin --env=test reset
+ ```
+
+ The Traffic Ops users will be created by the tool for accessing the API
once the database is accessible.
+
+ To test if `db/admin` ran all migrations successfully, you can run the
following command from the `traffic_ops/app` directory:
+
+ ```console
+ db/admin -env=test dbversion
+ ```
+ The result should be something similar to:
+ ```
+ dbversion 2021070800000000
+ ```
+ If migrations did not run successfully, you may see:
+ ```
+ dbversion 20181206000000 (dirty)
+ ```
+ Make sure **trafficOpsDB dbname** in traffic-ops-test.conf is set to:
`to_test`
+
+ For more info see:
http://trafficcontrol.apache.org/docs/latest/development/traffic_ops.html?highlight=reset
+
+4. A running Traffic Ops Golang instance pointing to the `to_test` database.
+
+ ```console
+ $ cd trafficcontrol/traffic_ops/traffic_ops_golang
+ $ cp ../app/conf/cdn.conf $HOME/cdn.conf
+ $ go build && ./traffic_ops_golang -cfg $HOME/cdn.conf -dbcfg
../app/conf/test/database.conf
+ ```
Review Comment:
the instructions for building and running TO are in the docs, I'm not sure
about the value of repeating them here.
##########
traffic_ops/testing/api_contract/v4/conftest.py:
##########
@@ -0,0 +1,103 @@
+"""This module is used to create a Traffic Ops session
Review Comment:
missing license header
##########
traffic_ops/testing/api_contract/README.md:
##########
@@ -0,0 +1,74 @@
+# Traffic Ops API Contract Tests
Review Comment:
file missing license header
##########
traffic_ops/testing/api_contract/v4/to_data.json:
##########
@@ -0,0 +1,9 @@
+{
+ "test": {
+ "user": "admin",
+ "password": "twelve12",
+ "url": "https://localhost/api/4.0",
+ "api_version": "4.0",
Review Comment:
the API version is in the url - does it really need to be specified in both
places? What happens if they don't match?
##########
traffic_ops/testing/api_contract/v4/conftest.py:
##########
@@ -0,0 +1,103 @@
+"""This module is used to create a Traffic Ops session
+and to store prerequisite data for endpoints"""
+import json
+import logging
+import sys
+from random import randint
+from urllib.parse import urlparse
+import pytest
+from trafficops.tosession import TOSession
+from trafficops.restapi import OperationError
+
+
+# Create and configure logger
+logger = logging.getLogger()
+
+
+def pytest_addoption(parser):
+ """Passing in Traffic Ops Arguments [Username, Password, Url and Hostname]
from Command Line"""
+ parser.addoption(
+ '--to_user', action='store', help='User name for Traffic Ops Session'
+ )
+ parser.addoption(
+ '--to_password', action='store', help='Password for Traffic Ops
Session'
+ )
+ parser.addoption(
+ '--to_url', action='store', help='Traffic Ops URL'
+ )
+
+
[email protected](name="to_args")
+def to_data(pytestconfig):
+ """PyTest fixture to store Traffic ops Arguments passed from command
line"""
+ args = {}
+ with open('to_data.json', encoding="utf-8", mode='r') as session_file:
+ data = json.load(session_file)
+ session_data = data["test"]
+ args['api_version'] = session_data.get("api_version")
+ args['port'] = session_data.get("port")
+
+ to_user = pytestconfig.getoption('--to_user')
+ to_password = pytestconfig.getoption('--to_password')
+ to_url = pytestconfig.getoption('--to_url')
+
+ if not all([to_user, to_password, to_url]):
+ logger.info(
+ "Traffic Ops session data were not passed from Command line Args")
+ args['user'] = session_data.get('user')
+ args['password'] = session_data.get('password')
+ args['url'] = session_data.get('url')
+ else:
+ args['user'] = to_user
+ args['password'] = to_password
+ args['url'] = to_url
+ logger.info("Parsed Traffic ops session data from args %s", args)
+ return args
+
+
[email protected](name="to_session")
+def to_login(to_args):
+ """PyTest Fixture to create a Traffic Ops session from Traffic Ops
Arguments
+ passed as command line arguments in to_args fixture in conftest
+ :param to_args: Fixture to get Traffic ops session arguments
+ :type to_args: dict
+ """
+ # Create a Traffic Ops V4 session and login
+ to_url = urlparse(to_args["url"])
+ to_host = to_url.hostname
+ try:
+ to_session = TOSession(host_ip=to_host, host_port=to_args["port"],
+ api_version=to_args["api_version"], ssl=True,
verify_cert=False)
+ logger.info("Established Traffic Ops Session")
+ except OperationError:
+ sys.exit(-1)
+
+ # Login To TO_API
+ to_session.login(to_args["user"], to_args["password"])
+ logger.info("Successfully logged into Traffic Ops")
+ return to_session
+
+
[email protected]()
+def cdn_prereq(to_session, get_cdn_data):
+ """PyTest Fixture to create POST data for cdns endpoint
+ :param to_session: Fixture to get Traffic ops session
+ :type to_session: TOsession
+ :param get_cdn_data: Fixture to get cdn data from a prereq file
+ :type get_cdn_data: dict
+ """
+
+ # Return new post data and post response from cdns POST request
+ get_cdn_data["name"] = get_cdn_data["name"][:4]+str(randint(0, 1000))
+ get_cdn_data["domainName"] = get_cdn_data["domainName"][:5] + \
+ str(randint(0, 1000))
+ logger.info("New cdn data to hit POST method %s", get_cdn_data)
+ # Hitting cdns POST methed
+ response = to_session.create_cdn(data=get_cdn_data)
+ prerequisite_data = None
+ try:
+ cdn_response = response[0]
+ prerequisite_data = [get_cdn_data, cdn_response]
+ except IndexError:
+ logger.error("No CDN response data from cdns POST request")
Review Comment:
according to the docs, the `response` property of the response should be the
created CDN; that means that `response[0]` would never exist
##########
traffic_ops/testing/api_contract/v4/conftest.py:
##########
@@ -0,0 +1,103 @@
+"""This module is used to create a Traffic Ops session
+and to store prerequisite data for endpoints"""
+import json
+import logging
+import sys
+from random import randint
+from urllib.parse import urlparse
+import pytest
+from trafficops.tosession import TOSession
+from trafficops.restapi import OperationError
+
+
+# Create and configure logger
+logger = logging.getLogger()
+
+
+def pytest_addoption(parser):
+ """Passing in Traffic Ops Arguments [Username, Password, Url and Hostname]
from Command Line"""
+ parser.addoption(
+ '--to_user', action='store', help='User name for Traffic Ops Session'
+ )
+ parser.addoption(
+ '--to_password', action='store', help='Password for Traffic Ops
Session'
+ )
+ parser.addoption(
+ '--to_url', action='store', help='Traffic Ops URL'
+ )
+
+
[email protected](name="to_args")
+def to_data(pytestconfig):
+ """PyTest fixture to store Traffic ops Arguments passed from command
line"""
Review Comment:
missing punctuation
##########
traffic_ops/testing/api_contract/v4/conftest.py:
##########
@@ -0,0 +1,103 @@
+"""This module is used to create a Traffic Ops session
+and to store prerequisite data for endpoints"""
+import json
+import logging
+import sys
+from random import randint
+from urllib.parse import urlparse
+import pytest
+from trafficops.tosession import TOSession
+from trafficops.restapi import OperationError
+
+
+# Create and configure logger
+logger = logging.getLogger()
+
+
+def pytest_addoption(parser):
+ """Passing in Traffic Ops Arguments [Username, Password, Url and Hostname]
from Command Line"""
+ parser.addoption(
+ '--to_user', action='store', help='User name for Traffic Ops Session'
+ )
+ parser.addoption(
+ '--to_password', action='store', help='Password for Traffic Ops
Session'
+ )
+ parser.addoption(
+ '--to_url', action='store', help='Traffic Ops URL'
+ )
+
+
[email protected](name="to_args")
+def to_data(pytestconfig):
+ """PyTest fixture to store Traffic ops Arguments passed from command
line"""
+ args = {}
+ with open('to_data.json', encoding="utf-8", mode='r') as session_file:
+ data = json.load(session_file)
+ session_data = data["test"]
+ args['api_version'] = session_data.get("api_version")
+ args['port'] = session_data.get("port")
+
+ to_user = pytestconfig.getoption('--to_user')
+ to_password = pytestconfig.getoption('--to_password')
+ to_url = pytestconfig.getoption('--to_url')
+
+ if not all([to_user, to_password, to_url]):
+ logger.info(
+ "Traffic Ops session data were not passed from Command line Args")
+ args['user'] = session_data.get('user')
+ args['password'] = session_data.get('password')
+ args['url'] = session_data.get('url')
+ else:
+ args['user'] = to_user
+ args['password'] = to_password
+ args['url'] = to_url
+ logger.info("Parsed Traffic ops session data from args %s", args)
+ return args
+
+
[email protected](name="to_session")
+def to_login(to_args):
+ """PyTest Fixture to create a Traffic Ops session from Traffic Ops
Arguments
Review Comment:
missing punctuation
##########
traffic_ops/testing/api_contract/v4/conftest.py:
##########
@@ -0,0 +1,103 @@
+"""This module is used to create a Traffic Ops session
+and to store prerequisite data for endpoints"""
+import json
+import logging
+import sys
+from random import randint
+from urllib.parse import urlparse
+import pytest
+from trafficops.tosession import TOSession
+from trafficops.restapi import OperationError
+
+
+# Create and configure logger
+logger = logging.getLogger()
+
+
+def pytest_addoption(parser):
+ """Passing in Traffic Ops Arguments [Username, Password, Url and Hostname]
from Command Line"""
+ parser.addoption(
+ '--to_user', action='store', help='User name for Traffic Ops Session'
+ )
+ parser.addoption(
+ '--to_password', action='store', help='Password for Traffic Ops
Session'
+ )
+ parser.addoption(
+ '--to_url', action='store', help='Traffic Ops URL'
+ )
+
+
[email protected](name="to_args")
+def to_data(pytestconfig):
+ """PyTest fixture to store Traffic ops Arguments passed from command
line"""
+ args = {}
+ with open('to_data.json', encoding="utf-8", mode='r') as session_file:
+ data = json.load(session_file)
+ session_data = data["test"]
+ args['api_version'] = session_data.get("api_version")
+ args['port'] = session_data.get("port")
+
+ to_user = pytestconfig.getoption('--to_user')
+ to_password = pytestconfig.getoption('--to_password')
+ to_url = pytestconfig.getoption('--to_url')
+
+ if not all([to_user, to_password, to_url]):
+ logger.info(
+ "Traffic Ops session data were not passed from Command line Args")
+ args['user'] = session_data.get('user')
Review Comment:
as above, quote style should be consistent
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]