TerekhovaKate commented on code in PR #7400: URL: https://github.com/apache/trafficcontrol/pull/7400#discussion_r1134154129
########## traffic_ops/testing/api_contract/v4/conftest.py: ########## @@ -0,0 +1,94 @@ +import pytest +from trafficops.tosession import TOSession +from urllib.parse import urlparse +import sys +import json +from random import randint +import logging + + +""" +Passing in Traffic Ops Arguments [Username, Password, Url and Hostname] from Command Line +""" +def pytest_addoption(parser): + 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' + ) + parser.addoption( + '--hostname', action='store', help='Traffic Ops hostname' + ) + + +""" +PyTest fixture to store Traffic ops Arguments passed from command line +""" [email protected] +def to_data(pytestconfig): + args = {} + args['user'] = pytestconfig.getoption('--to_user') + args['password'] = pytestconfig.getoption('--to_password') + args['url'] = pytestconfig.getoption('--to_url') + args['hostname'] = pytestconfig.getoption('--hostname') + return args + + +""" +PyTest Fixture to create a Traffic Ops session from Traffic Ops Arguments +passed as command line arguments in to_data fixture in conftest +""" [email protected]() +def to_login(to_data): + + # Create a Traffic Ops V4 session and login + print("Parsed TO args {}".format(to_data)) + if to_data["user"] == None: + f = open('to_data.json') + data = json.load(f) + to_data = data["test"] + to_url = urlparse(to_data["url"]) + to_host = to_url.hostname + + TO=TOSession(host_ip=to_host,host_port=443,api_version='4.0',ssl=True,verify_cert=False) + print("established connection") + + # Login To TO_API + TO.login(to_data["user"], to_data["password"]) + + if not TO.logged_in: + print("Failure Logging into Traffic Ops") + sys.exit(-1) + else: + print("Successfully logged into Traffic Ops") Review Comment: please change print statements to python loggin library ########## setup.py: ########## @@ -0,0 +1,37 @@ +from setuptools import setup + +setup( + name='trafficcontrol', + version='1.0.0', + long_description=open('README.md').read(), Review Comment: please create a readme file ########## setup.py: ########## @@ -0,0 +1,37 @@ +from setuptools import setup + +setup( + name='trafficcontrol', + version='1.0.0', + long_description=open('README.md').read(), + long_description_content_type='text/markdown', + py_modules= [], + install_requires=[ Review Comment: please create a requarement.txt file and list only libraries what is requred not sure we need anything this complex for API testing 'numpy==1.24.2', 'opencv-python==4.7.0.68', ########## traffic_ops/testing/api_contract/v4/conftest.py: ########## @@ -0,0 +1,94 @@ +import pytest +from trafficops.tosession import TOSession +from urllib.parse import urlparse +import sys +import json +from random import randint +import logging + + +""" +Passing in Traffic Ops Arguments [Username, Password, Url and Hostname] from Command Line +""" +def pytest_addoption(parser): + 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' + ) + parser.addoption( + '--hostname', action='store', help='Traffic Ops hostname' + ) + + +""" +PyTest fixture to store Traffic ops Arguments passed from command line +""" [email protected] +def to_data(pytestconfig): + args = {} + args['user'] = pytestconfig.getoption('--to_user') + args['password'] = pytestconfig.getoption('--to_password') + args['url'] = pytestconfig.getoption('--to_url') + args['hostname'] = pytestconfig.getoption('--hostname') + return args + + +""" +PyTest Fixture to create a Traffic Ops session from Traffic Ops Arguments +passed as command line arguments in to_data fixture in conftest +""" [email protected]() +def to_login(to_data): + + # Create a Traffic Ops V4 session and login + print("Parsed TO args {}".format(to_data)) + if to_data["user"] == None: + f = open('to_data.json') + data = json.load(f) + to_data = data["test"] + to_url = urlparse(to_data["url"]) + to_host = to_url.hostname + + TO=TOSession(host_ip=to_host,host_port=443,api_version='4.0',ssl=True,verify_cert=False) + print("established connection") + + # Login To TO_API + TO.login(to_data["user"], to_data["password"]) + + if not TO.logged_in: + print("Failure Logging into Traffic Ops") + sys.exit(-1) + else: + print("Successfully logged into Traffic Ops") + return TO + + +""" +PyTest Fixture to create POST data for cdns endpoint +""" [email protected]() +def cdn_prereq(to_login): + + #Return new post data and post response from cdns POST request + f = open('prerequisite_data.json') + data = json.load(f) + + data["cdns"]["name"] =data["cdns"]["name"][:4]+str(randint(0,1000)) Review Comment: please run lint and flake foe cogging standard missing white spaces ########## traffic_ops/testing/api_contract/v4/conftest.py: ########## @@ -0,0 +1,94 @@ +import pytest +from trafficops.tosession import TOSession +from urllib.parse import urlparse +import sys +import json +from random import randint +import logging + + +""" +Passing in Traffic Ops Arguments [Username, Password, Url and Hostname] from Command Line +""" +def pytest_addoption(parser): + 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' + ) + parser.addoption( + '--hostname', action='store', help='Traffic Ops hostname' + ) + + +""" +PyTest fixture to store Traffic ops Arguments passed from command line +""" [email protected] +def to_data(pytestconfig): + args = {} + args['user'] = pytestconfig.getoption('--to_user') + args['password'] = pytestconfig.getoption('--to_password') + args['url'] = pytestconfig.getoption('--to_url') + args['hostname'] = pytestconfig.getoption('--hostname') + return args + + +""" +PyTest Fixture to create a Traffic Ops session from Traffic Ops Arguments +passed as command line arguments in to_data fixture in conftest +""" [email protected]() +def to_login(to_data): + + # Create a Traffic Ops V4 session and login + print("Parsed TO args {}".format(to_data)) + if to_data["user"] == None: + f = open('to_data.json') + data = json.load(f) + to_data = data["test"] + to_url = urlparse(to_data["url"]) + to_host = to_url.hostname + + TO=TOSession(host_ip=to_host,host_port=443,api_version='4.0',ssl=True,verify_cert=False) + print("established connection") + + # Login To TO_API + TO.login(to_data["user"], to_data["password"]) + + if not TO.logged_in: + print("Failure Logging into Traffic Ops") + sys.exit(-1) + else: + print("Successfully logged into Traffic Ops") + return TO + + +""" +PyTest Fixture to create POST data for cdns endpoint +""" [email protected]() +def cdn_prereq(to_login): + + #Return new post data and post response from cdns POST request + f = open('prerequisite_data.json') Review Comment: utilaze `@pytest.fixture def get_cdn_keys():` ########## traffic_ops/testing/api_contract/v4/conftest.py: ########## @@ -0,0 +1,94 @@ +import pytest +from trafficops.tosession import TOSession +from urllib.parse import urlparse +import sys +import json +from random import randint +import logging + + +""" +Passing in Traffic Ops Arguments [Username, Password, Url and Hostname] from Command Line +""" +def pytest_addoption(parser): + 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' + ) + parser.addoption( + '--hostname', action='store', help='Traffic Ops hostname' + ) + + +""" +PyTest fixture to store Traffic ops Arguments passed from command line +""" [email protected] +def to_data(pytestconfig): + args = {} + args['user'] = pytestconfig.getoption('--to_user') + args['password'] = pytestconfig.getoption('--to_password') + args['url'] = pytestconfig.getoption('--to_url') + args['hostname'] = pytestconfig.getoption('--hostname') + return args + + +""" +PyTest Fixture to create a Traffic Ops session from Traffic Ops Arguments +passed as command line arguments in to_data fixture in conftest +""" [email protected]() +def to_login(to_data): + + # Create a Traffic Ops V4 session and login + print("Parsed TO args {}".format(to_data)) + if to_data["user"] == None: + f = open('to_data.json') + data = json.load(f) + to_data = data["test"] + to_url = urlparse(to_data["url"]) + to_host = to_url.hostname + + TO=TOSession(host_ip=to_host,host_port=443,api_version='4.0',ssl=True,verify_cert=False) + print("established connection") + + # Login To TO_API + TO.login(to_data["user"], to_data["password"]) + + if not TO.logged_in: + print("Failure Logging into Traffic Ops") + sys.exit(-1) + else: + print("Successfully logged into Traffic Ops") + return TO + + +""" +PyTest Fixture to create POST data for cdns endpoint +""" [email protected]() +def cdn_prereq(to_login): + + #Return new post data and post response from cdns POST request + f = open('prerequisite_data.json') + data = json.load(f) + + data["cdns"]["name"] =data["cdns"]["name"][:4]+str(randint(0,1000)) + data["cdns"]["domainName"] = data["cdns"]["domainName"][:5] + str(randint(0,1000)) + logging.info("New post data to hit POST method {}".format(data)) + with open('prerequisite_data.json', 'w') as f: Review Comment: with open('prerequisite_data.json', 'w') as f: - insted replace values in your json object you got from @pytest.fixture def get_cdn_keys(): do not override original config values in a file ########## traffic_ops/testing/api_contract/v4/conftest.py: ########## @@ -0,0 +1,94 @@ +import pytest +from trafficops.tosession import TOSession +from urllib.parse import urlparse +import sys +import json +from random import randint +import logging + + +""" +Passing in Traffic Ops Arguments [Username, Password, Url and Hostname] from Command Line +""" +def pytest_addoption(parser): + 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' + ) + parser.addoption( + '--hostname', action='store', help='Traffic Ops hostname' + ) + + +""" +PyTest fixture to store Traffic ops Arguments passed from command line +""" [email protected] +def to_data(pytestconfig): + args = {} + args['user'] = pytestconfig.getoption('--to_user') + args['password'] = pytestconfig.getoption('--to_password') + args['url'] = pytestconfig.getoption('--to_url') + args['hostname'] = pytestconfig.getoption('--hostname') + return args + + +""" +PyTest Fixture to create a Traffic Ops session from Traffic Ops Arguments +passed as command line arguments in to_data fixture in conftest +""" [email protected]() +def to_login(to_data): + + # Create a Traffic Ops V4 session and login + print("Parsed TO args {}".format(to_data)) + if to_data["user"] == None: Review Comment: please add check to_data["user"] == None by reading config file durum test init and passing default values from file @pytest.fixture def to_data(pytestconfig): ########## traffic_ops/testing/api_contract/v4/conftest.py: ########## @@ -0,0 +1,94 @@ +import pytest +from trafficops.tosession import TOSession +from urllib.parse import urlparse +import sys +import json +from random import randint +import logging + + +""" +Passing in Traffic Ops Arguments [Username, Password, Url and Hostname] from Command Line +""" +def pytest_addoption(parser): + 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' + ) + parser.addoption( + '--hostname', action='store', help='Traffic Ops hostname' + ) + + +""" +PyTest fixture to store Traffic ops Arguments passed from command line +""" [email protected] +def to_data(pytestconfig): + args = {} + args['user'] = pytestconfig.getoption('--to_user') + args['password'] = pytestconfig.getoption('--to_password') + args['url'] = pytestconfig.getoption('--to_url') + args['hostname'] = pytestconfig.getoption('--hostname') + return args + + +""" +PyTest Fixture to create a Traffic Ops session from Traffic Ops Arguments +passed as command line arguments in to_data fixture in conftest +""" [email protected]() +def to_login(to_data): + + # Create a Traffic Ops V4 session and login + print("Parsed TO args {}".format(to_data)) + if to_data["user"] == None: + f = open('to_data.json') + data = json.load(f) + to_data = data["test"] + to_url = urlparse(to_data["url"]) + to_host = to_url.hostname + + TO=TOSession(host_ip=to_host,host_port=443,api_version='4.0',ssl=True,verify_cert=False) + print("established connection") + + # Login To TO_API + TO.login(to_data["user"], to_data["password"]) + + if not TO.logged_in: + print("Failure Logging into Traffic Ops") + sys.exit(-1) + else: + print("Successfully logged into Traffic Ops") + return TO + + +""" +PyTest Fixture to create POST data for cdns endpoint +""" [email protected]() +def cdn_prereq(to_login): + + #Return new post data and post response from cdns POST request + f = open('prerequisite_data.json') + data = json.load(f) + + data["cdns"]["name"] =data["cdns"]["name"][:4]+str(randint(0,1000)) + data["cdns"]["domainName"] = data["cdns"]["domainName"][:5] + str(randint(0,1000)) + logging.info("New post data to hit POST method {}".format(data)) + with open('prerequisite_data.json', 'w') as f: Review Comment: please create a method to read config data and reduce code redundancy or use memory object with fixture what you have created @pytest.fixture def get_cdn_keys(): ########## traffic_ops/testing/api_contract/v4/to_data.json: ########## @@ -0,0 +1,8 @@ +{ + "test": { + "user": "admin", + "password": "twelve", + "url": "https://localhost/api/4.0", + "localhost": "localhost" + } +} Review Comment: please make sure to add api_version / port in config file replace hardcoded values TO=TOSession(host_ip=to_host,host_port=443,api_version='4.0',ssl=True,verify_cert=False) note: "localhost": "localhost" not in use -> please remove ########## traffic_ops/testing/api_contract/v4/conftest.py: ########## @@ -0,0 +1,94 @@ +import pytest +from trafficops.tosession import TOSession +from urllib.parse import urlparse +import sys +import json +from random import randint +import logging + + +""" +Passing in Traffic Ops Arguments [Username, Password, Url and Hostname] from Command Line +""" +def pytest_addoption(parser): Review Comment: https://docs.pytest.org/en/7.1.x/example/simple.html # content of conftest.py def pytest_addoption(parser): parser.addoption( "--cmdopt", action="store", default="type1", help="my option: type1 or type2" ) @pytest.fixture def cmdopt(request): return request.config.getoption("--cmdopt") better practice to add **help** section validation for type of param type=str/int could be useful as well -- 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]
