Delete functest.utils.functest_logger
[functest.git] / functest / core / testcase.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 """Define the parent class of all Functest TestCases."""
11
12 import logging
13 import os
14
15 import functest.utils.functest_utils as ft_utils
16
17 __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>"
18
19
20 class TestCase(object):
21     """Base model for single test case."""
22
23     EX_OK = os.EX_OK
24     """everything is OK"""
25
26     EX_RUN_ERROR = os.EX_SOFTWARE
27     """run() failed"""
28
29     EX_PUSH_TO_DB_ERROR = os.EX_SOFTWARE - 1
30     """push_to_db() failed"""
31
32     EX_TESTCASE_FAILED = os.EX_SOFTWARE - 2
33     """results are false"""
34
35     logger = logging.getLogger(__name__)
36
37     def __init__(self, **kwargs):
38         self.details = {}
39         self.project_name = kwargs.get('project_name', 'functest')
40         self.case_name = kwargs.get('case_name', '')
41         self.criteria = kwargs.get('criteria', 100)
42         self.result = ""
43         self.start_time = ""
44         self.stop_time = ""
45
46     def check_result(self):
47         """Interpret the result of the test case.
48
49         It allows getting the result of TestCase. It completes run()
50         which only returns the execution status.
51
52         It can be overriden if checking result is not suitable.
53
54         Returns:
55             TestCase.EX_OK if result is 'PASS'.
56             TestCase.EX_TESTCASE_FAILED otherwise.
57         """
58         try:
59             assert self.criteria
60             if isinstance(self.result, int) and isinstance(self.criteria, int):
61                 if self.result >= self.criteria:
62                     return TestCase.EX_OK
63             else:
64                 # Backward compatibility
65                 # It must be removed as soon as TestCase subclasses
66                 # stop setting result = 'PASS' or 'FAIL'.
67                 # In this case criteria is unread.
68                 self.logger.warning(
69                     "Please update result which must be an int!")
70                 if self.result == 'PASS':
71                     return TestCase.EX_OK
72         except AssertionError:
73             self.logger.error("Please run test before checking the results")
74         return TestCase.EX_TESTCASE_FAILED
75
76     def run(self, **kwargs):
77         """Run the test case.
78
79         It allows running TestCase and getting its execution
80         status.
81
82         The subclasses must override the default implementation which
83         is false on purpose.
84
85         The new implementation must set the following attributes to
86         push the results to DB:
87
88             * result,
89             * start_time,
90             * stop_time.
91
92         Args:
93             kwargs: Arbitrary keyword arguments.
94
95         Returns:
96             TestCase.EX_RUN_ERROR.
97         """
98         # pylint: disable=unused-argument
99         self.logger.error("Run must be implemented")
100         return TestCase.EX_RUN_ERROR
101
102     def push_to_db(self):
103         """Push the results of the test case to the DB.
104
105         It allows publishing the results and to check the status.
106
107         It could be overriden if the common implementation is not
108         suitable. The following attributes must be set before pushing
109         the results to DB:
110
111             * project_name,
112             * case_name,
113             * result,
114             * start_time,
115             * stop_time.
116
117         Returns:
118             TestCase.EX_OK if results were pushed to DB.
119             TestCase.EX_PUSH_TO_DB_ERROR otherwise.
120         """
121         try:
122             assert self.project_name
123             assert self.case_name
124             assert self.start_time
125             assert self.stop_time
126             pub_result = 'PASS' if self.check_result(
127                 ) == TestCase.EX_OK else 'FAIL'
128             if ft_utils.push_results_to_db(
129                     self.project_name, self.case_name, self.start_time,
130                     self.stop_time, pub_result, self.details):
131                 self.logger.info("The results were successfully pushed to DB")
132                 return TestCase.EX_OK
133             else:
134                 self.logger.error("The results cannot be pushed to DB")
135                 return TestCase.EX_PUSH_TO_DB_ERROR
136         except Exception:  # pylint: disable=broad-except
137             self.logger.exception("The results cannot be pushed to DB")
138             return TestCase.EX_PUSH_TO_DB_ERROR