Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion cafy_pytest/cafy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from allure_commons._allure import StepContext as AllureStepContext
import pytest
from utils.cafyexception import CafyException
from logger.cafylog import CafyLog
log = CafyLog("cafy_pytest_step")

class Cafy:

class Globals:
Expand Down Expand Up @@ -45,8 +48,10 @@ def __init__(self, title, params, logger=None, blocking=True):
super().__init__(title, params)
self.blocking = blocking
self.logger = logger
self.title=title

def __enter__(self):
log.title(f" Start of step: {self.title}")
super().__enter__()

def __exit__(self, exc_type, exc_val, exc_tb):
Expand All @@ -58,13 +63,15 @@ def __exit__(self, exc_type, exc_val, exc_tb):
exc_type=exc_type,
exc_tb=exc_tb))
Cafy.RunInfo.active_exceptions.append(exc_val)

log.title(f" Finish of step: {self.title}")
return not self.blocking


class StepScope():
def __init__(self, title):
self.title = title
#log.title(title)


def __enter__(self):
Cafy.RunInfo.active_exceptions = list()
Expand Down
78 changes: 57 additions & 21 deletions cafy_pytest/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@
from debug import DebugLibrary
import pluggy
import _pytest

#Check if CONTAINER_MODE env variable is set, this mode is set when cafykit scripts are run using docker/podman
CONTAINER_MODE = os.environ.get("CONTAINER_MODE", None)

from utils.collectors.confest import Config
collection_setup = Config()
#Check with CAFYKIT_HOME or GIT_REPO or CAFYAP_REPO environment is set,
#if all are set, CAFYAP_REPO takes precedence
CAFY_REPO = os.environ.get("CAFYAP_REPO", None)


setattr(pytest,"allure",allure)
if CAFY_REPO is None:
#If CAFYAP_REPO is not set, check if GIT_REPO or CAFYKIT_HOME is set
Expand All @@ -69,11 +75,15 @@
if os.path.exists(os.path.join(CAFY_REPO, 'work', 'pytest_cafy_config.yaml')):
os.environ['CAFY_REPO'] = CAFY_REPO
print('GIT_REPO variable has been set to correct repo')
if CONTAINER_MODE:
CONTAINER_MODE_CAFY_REPO = os.environ.get("HOST_GIT_REPO", None)
else:
msg = 'GIT_REPO has not been set to correct repo'
pytest.exit(msg)
else:
os.environ['CAFY_REPO'] = CAFY_REPO
if CONTAINER_MODE:
CONTAINER_MODE_CAFY_REPO = os.environ.get("HOST_CAFYAP_REPO", None)

if not CAFY_REPO:
msg = "Please set the environment variable GIT_REPO or CAFYKIT_HOME or CAFYAP_REPO "
Expand Down Expand Up @@ -235,7 +245,7 @@ def is_valid_cafyarg(arg):
#if block is executed when arg is not a file,instead it is string of dict
if not os.path.exists(arg):
eval(arg)
argdict = yaml.load(arg)
argdict = yaml.safe_load(arg)
CafyLog.script_args = argdict
else:
#else block is executed when arg is a file
Expand All @@ -253,7 +263,7 @@ def load_config_file(filename=None):
_filename = os.path.join(
git_repo, "work", "pytest_cafy_config.yaml")
with open(_filename, 'r') as stream:
return (yaml.load(stream))
return (yaml.safe_load(stream))
except:
return {}

Expand Down Expand Up @@ -366,7 +376,6 @@ def pytest_configure(config):
_current_time = get_datentime()
pid = os.getpid()
work_dir_name = "%s_%s_p%s" % (script_name,_current_time, pid )

if not config.option.reportdir:
#If workdir option is set, execute the if block and if not given,
#execute the else block where the workdir will be set to
Expand All @@ -375,20 +384,34 @@ def pytest_configure(config):
if os.path.exists(config.option.workdir):
custom_workdir = config.option.workdir
work_dir = custom_workdir
if CONTAINER_MODE:
if os.environ.get("HOST_WORK_DIR", None):
container_mode_work_dir = os.environ.get("HOST_WORK_DIR")
else:
print('workdir path not found: {}'.format(config.option.workdir))
pytest.exit('workdir path not found')
else:
if CAFY_REPO:
work_dir = os.path.join(CAFY_REPO, 'work', "archive", work_dir_name)
if CONTAINER_MODE:
if CONTAINER_MODE_CAFY_REPO:
container_mode_work_dir = os.path.join(
CONTAINER_MODE_CAFY_REPO, 'work', "archive", work_dir_name)

else:
if os.path.exists(config.option.reportdir):
work_dir = os.path.join(config.option.reportdir, work_dir_name)
if CONTAINER_MODE:
if os.environ.get("HOST_REPORT_DIR", None):
container_mode_work_dir = os.environ.get("HOST_REPORT_DIR")
else:
print('reportdir path not found: {}'.format(config.option.reportdir))
pytest.exit('reportdir path not found')

CafyLog.work_dir = work_dir
if CONTAINER_MODE:
CafyLog.container_mode_work_dir = container_mode_work_dir
print (f"container_mode_work_dir: {CafyLog.container_mode_work_dir}")
#Set CafyLog.work_dir option for all.log
CafyLog.work_dir = work_dir
_setup_env()
Expand Down Expand Up @@ -555,7 +578,10 @@ def pytest_configure(config):
reporter = TerminalReporter(config, sys.stdout)
#reporter.write_line("all.log location: %s" %CafyLog.work_dir)
reporter.write_line("Virtual Env: %s" %(os.getenv("VIRTUAL_ENV")))
reporter.write_line("Complete Log Location: %s/all.log" %CafyLog.work_dir)
if CONTAINER_MODE:
reporter.write_line("Complete Log Location: %s/all.log" % CafyLog.container_mode_work_dir)
else:
reporter.write_line("Complete Log Location: %s/all.log" %CafyLog.work_dir)
reporter.write_line("Registration Id: %s" % CafyLog.registration_id)


Expand Down Expand Up @@ -803,6 +829,8 @@ def __init__(self, email_addr_list, email_from, email_from_passwd,
#self.archive_name = CafyLog.work_dir + '.zip'
#self.archive = os.path.join(CafyLog.work_dir, self.archive_name)
self.archive = CafyLog.work_dir
if CONTAINER_MODE:
self.host_archive = CafyLog.container_mode_work_dir
self.email_report = os.path.join(CafyLog.work_dir,
email_report)
else:
Expand Down Expand Up @@ -863,17 +891,18 @@ def _generate_with_template(self, terminalreporter):
cafy_kwargs = {'terminalreporter': terminalreporter,
'testcase_dict': self.testcase_dict,
'testcase_failtrace_dict':self.testcase_failtrace_dict,
'archive': self.archive,
'archive': self.host_archive if CONTAINER_MODE else self.archive,
'topo_file': self.topo_file,
'collection_report': self.collection_report,
'hybrid_mode_status_dict':self.hybrid_mode_status_dict}
else:
cafy_kwargs = {'terminalreporter': terminalreporter,
'testcase_dict': self.testcase_dict,
'testcase_failtrace_dict':self.testcase_failtrace_dict,
'archive': self.archive,
'archive': self.host_archive if CONTAINER_MODE else self.archive,
'topo_file': self.topo_file,
'collection_report': self.collection_report}

report = CafyReportData(**cafy_kwargs)
setattr(report,"tabulate_html", self.tabulate_html)
template_file = os.path.join(self.CURRENT_DIR,
Expand Down Expand Up @@ -1835,8 +1864,12 @@ def pytest_terminal_summary(self, terminalreporter):
self.tabulate_result = tabulate(temp_list, headers=headers[:], tablefmt='grid')
terminalreporter.write_line(self.tabulate_result)
self.dump_model_coverage_report()
terminalreporter.write_line("Results: {work_dir}".format(work_dir=CafyLog.work_dir))
terminalreporter.write_line("Reports: {allure_html_report}".format(allure_html_report=self.allure_html_report))
if CONTAINER_MODE:
terminalreporter.write_line(f"Results: {CafyLog.container_mode_work_dir}")
else:
terminalreporter.write_line(f"Results: {CafyLog.work_dir}")
terminalreporter.write_line(f"Reports: {self.allure_html_report}")

self.collect_collection_report()
self._generate_email_report(terminalreporter)

Expand Down Expand Up @@ -1879,7 +1912,6 @@ def _parse_all_log(self, input_file_handler):
pass
else:
if log_parsing_state is LogState.NONE:
# print("Starting generic log")
log_grouping = GenericLogGrouping()
all_log_groupings.append(log_grouping)
log_parsing_state = LogState.GENERIC
Expand Down Expand Up @@ -1938,14 +1970,18 @@ def _generate_allure_report(self):
allure_path=allure_path,
allure_source_dir=allure_source_dir,
allure_report_dir=allure_report_dir)
#print("Allure Command Line Used: {cmd}".format(cmd=cmd))
os.system(cmd)
allure_report = os.path.join(allure_report_dir,"index.html")
CafyLog.htmlfile_link = allure_report
allure_html_report = os.path.join(_CafyConfig.allure_server,allure_report.strip("/"))
if CONTAINER_MODE:
allure_report_dir = os.path.join(self.host_archive,"reports")
allure_report = os.path.join(allure_report_dir,"index.html")
allure_html_report = os.path.join(_CafyConfig.allure_server,allure_report.strip("/"))

self.allure_html_report = allure_html_report
os.system(cmd)
#print("Report Generated at: {allure_report}".format(allure_report=allure_report))
self.log.info("Report: {allure_html_report}".format(allure_html_report=allure_html_report))
CafyLog.htmlfile_link = allure_report
self.log.info(f"Report: {allure_html_report}")

_CafyConfig.summary["allure2"] = {
"commandline" : cmd,
"html" : allure_html_report,
Expand Down Expand Up @@ -1976,7 +2012,6 @@ def pytest_sessionfinish(self):
headers = {'content-type': 'application/json'}
try:
url = 'http://{0}:5001/uploadcollectorlogfile/'.format(CafyLog.debug_server)
print("url = ", url)
self.log.info("Calling registration upload collector logfile service (url:%s)" %url)
response = _requests_retry(self.log, url, 'POST', json=params, headers=headers, timeout=300)
if response is not None and response.status_code == 200:
Expand Down Expand Up @@ -2067,6 +2102,9 @@ def __init__(self, terminalreporter, testcase_dict, testcase_failtrace_dict, arc
# Basic details
option = self.terminalreporter.config.option
self.script_list = option.file_or_dir
# TODO check if CONTAINER_MODE and then set the above script_list variable accordingly
if CONTAINER_MODE:
pass
self.title = ' '.join(self.script_list)
self.image = os.getenv("IMAGE", "Unknown")
if os.environ.get("BUILD_URL"):
Expand Down Expand Up @@ -2103,14 +2141,16 @@ def __init__(self, terminalreporter, testcase_dict, testcase_failtrace_dict, arc
self.testbed = None
self.registration_id = CafyLog.registration_id
self.submitter = EmailReport.USER
self.cafy_repo = EmailReport.CAFY_REPO
self.cafy_repo = EmailReport.CAFY_REPO if not CONTAINER_MODE else os.environ.get("HOST_GIT_REPO", None)
self.topo_file = topo_file
#TODO: topo_file variable needs to print host path for CONTAINER mode
self.run_dir = self.terminalreporter.startdir.strpath
# TODO: run_dir variable needs to print host path for CONTAINER mode
try:
self.git_commit_id = subprocess.check_output(['git', 'rev-parse', 'origin/master'], timeout=5).decode("utf-8").replace('\n', '')
except Exception:
self.git_commit_id = None
self.archive = CafyLog.work_dir
self.archive = archive
# summary result
passed_list = self.terminalreporter.getreports('passed')
failed_list = self.terminalreporter.getreports('failed')
Expand Down Expand Up @@ -2161,10 +2201,6 @@ def __init__(self, terminalreporter, testcase_dict, testcase_failtrace_dict, arc
else:
print('\n Email html report not generated')





if CafyLog.email_htmlfile_link:
self.summary_report = CafyLog.email_htmlfile_link
if CafyLog.web_host:
Expand Down