Merge "Fix for opnfv onos-sfc scripts"
[functest.git] / functest / core / testcase_base.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Orange and others.
4 #
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
9
10 import os
11
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
15
16
17 class TestcaseBase(object):
18
19     EX_OK = os.EX_OK
20     EX_RUN_ERROR = os.EX_SOFTWARE
21     EX_PUBLISH_RESULT_FAILED = os.EX_SOFTWARE - 1
22     EX_TESTCASE_FAILED = os.EX_SOFTWARE - 2
23
24     logger = ft_logger.Logger(__name__).getLogger()
25
26     def __init__(self):
27         self.details = {}
28         self.project_name = "functest"
29         self.case_name = ""
30         self.criteria = ""
31         self.start_time = ""
32         self.stop_time = ""
33
34     def check_criteria(self):
35         try:
36             assert self.criteria
37             if self.criteria == 'PASS':
38                 return TestcaseBase.EX_OK
39         except:
40             self.logger.error("Please run test before checking the results")
41         return TestcaseBase.EX_TESTCASE_FAILED
42
43     def run(self, **kwargs):
44         self.logger.error("Run must be implemented")
45         return TestcaseBase.EX_RUN_ERROR
46
47     def publish_report(self):
48         if "RESULTS_STORE" in os.environ:
49             CONST.results_test_db_url = os.environ['RESULTS_STORE']
50
51         try:
52             assert self.project_name
53             assert self.case_name
54             assert self.criteria
55             assert self.start_time
56             assert self.stop_time
57             if CONST.results_test_db_url.lower().startswith(
58                     ("http://", "https://")):
59                 self.push_to_db()
60             elif CONST.results_test_db_url.lower().startswith("file://"):
61                 self.write_to_file()
62             else:
63                 self.logger.error("Please check parameter test_db_url and "
64                                   "OS environ variable RESTULTS_STORE")
65                 return TestcaseBase.EX_PUBLISH_RESULT_FAILED
66         except Exception:
67             self.logger.exception("The results cannot be stored")
68             return TestcaseBase.EX_PUBLISH_RESULT_FAILED
69
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
76         else:
77             self.logger.error("write results to a file failed")
78             return TestcaseBase.EX_PUBLISH_RESULT_FAILED
79
80     def push_to_db(self):
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
86         else:
87             self.logger.error("The results cannot be pushed to DB")
88             return TestcaseBase.EX_PUBLISH_RESULT_FAILED