gokulakrishnansvm commented on code in PR #7400: URL: https://github.com/apache/trafficcontrol/pull/7400#discussion_r1142563937
########## 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: Response property has two values in it. 1. Json response 2. Response status code Inorder to access Json response, We should use response[0] -- 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]
