3 # Copyright (c) 2016 Orange and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
12 from functest.utils.constants import CONST
13 import functest.utils.functest_logger as ft_logger
14 import functest.utils.functest_utils as ft_utils
17 class TestcaseBase(object):
20 EX_RUN_ERROR = os.EX_SOFTWARE
21 EX_PUBLISH_RESULT_FAILED = os.EX_SOFTWARE - 1
22 EX_TESTCASE_FAILED = os.EX_SOFTWARE - 2
24 logger = ft_logger.Logger(__name__).getLogger()
28 self.project_name = "functest"
34 def check_criteria(self):
37 if self.criteria == 'PASS':
38 return TestcaseBase.EX_OK
40 self.logger.error("Please run test before checking the results")
41 return TestcaseBase.EX_TESTCASE_FAILED
43 def run(self, **kwargs):
44 self.logger.error("Run must be implemented")
45 return TestcaseBase.EX_RUN_ERROR
47 def publish_report(self):
48 if "RESULTS_STORE" in os.environ:
49 CONST.results_test_db_url = os.environ['RESULTS_STORE']
52 assert self.project_name
55 assert self.start_time
57 if CONST.results_test_db_url.lower().startswith(
58 ("http://", "https://")):
60 elif CONST.results_test_db_url.lower().startswith("file://"):
63 self.logger.error("Please check parameter test_db_url and "
64 "OS environ variable RESTULTS_STORE")
65 return TestcaseBase.EX_PUBLISH_RESULT_FAILED
67 self.logger.exception("The results cannot be stored")
68 return TestcaseBase.EX_PUBLISH_RESULT_FAILED
70 def write_to_file(self):
71 if ft_utils.write_results_to_file(
72 self.project_name, self.case_name, self.start_time,
73 self.stop_time, self.criteria, self.details):
74 self.logger.info("The results were successfully written to a file")
75 return TestcaseBase.EX_OK
77 self.logger.error("write results to a file failed")
78 return TestcaseBase.EX_PUBLISH_RESULT_FAILED
81 if ft_utils.push_results_to_db(
82 self.project_name, self.case_name, self.start_time,
83 self.stop_time, self.criteria, self.details):
84 self.logger.info("The results were successfully pushed to DB")
85 return TestcaseBase.EX_OK
87 self.logger.error("The results cannot be pushed to DB")
88 return TestcaseBase.EX_PUBLISH_RESULT_FAILED