Add docstings in feature.py
[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 os
13
14 import functest.utils.functest_logger as ft_logger
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     """Parent class of Functest TestCase."""
22
23     EX_OK = os.EX_OK
24     """Status code returned when everything is OK"""
25
26     EX_RUN_ERROR = os.EX_SOFTWARE
27     """Status code returned when run() fails"""
28
29     EX_PUSH_TO_DB_ERROR = os.EX_SOFTWARE - 1
30     """Status code returned when push_to_db() fails"""
31
32     EX_TESTCASE_FAILED = os.EX_SOFTWARE - 2
33     """Status code returned when results are false"""
34
35     logger = ft_logger.Logger(__name__).getLogger()
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 = ""
42         self.start_time = ""
43         self.stop_time = ""
44
45     def check_criteria(self):
46         """Interpret the results of TestCase.
47
48         It allows getting the results of TestCase. It completes run()
49         which only returns the execution status.
50
51         It can be overriden if checking criteria is not suitable.
52
53         Returns:
54             TestCase.EX_OK if criteria is 'PASS'.
55             TestCase.EX_TESTCASE_FAILED otherwise.
56         """
57         try:
58             assert self.criteria
59             if self.criteria == 'PASS':
60                 return TestCase.EX_OK
61         except AssertionError:
62             self.logger.error("Please run test before checking the results")
63         return TestCase.EX_TESTCASE_FAILED
64
65     def run(self, **kwargs):
66         """Run TestCase.
67
68         It allows running TestCase and getting its execution
69         status.
70
71         The subclasses must override the default implementation which
72         is false on purpose. The only prerequisite is to set the
73         following attributes to push the results to DB:
74
75             * case_name,
76             * criteria,
77             * start_time,
78             * stop_time.
79
80         Args:
81             kwargs: Arbitrary keyword arguments.
82
83         Returns:
84             TestCase.EX_RUN_ERROR.
85         """
86         # pylint: disable=unused-argument
87         self.logger.error("Run must be implemented")
88         return TestCase.EX_RUN_ERROR
89
90     def push_to_db(self):
91         """Push the results of TestCase to the DB.
92
93         It allows publishing the results and to check the status.
94
95         It could be overriden if the common implementation is not
96         suitable. The following attributes must be set before pushing
97         the results to DB:
98
99             * project_name,
100             * case_name,
101             * criteria,
102             * start_time,
103             * stop_time.
104
105         Returns:
106             TestCase.EX_OK if results were pushed to DB.
107             TestCase.EX_PUSH_TO_DB_ERROR otherwise.
108         """
109         try:
110             assert self.project_name
111             assert self.case_name
112             assert self.criteria
113             assert self.start_time
114             assert self.stop_time
115             if ft_utils.push_results_to_db(
116                     self.project_name, self.case_name, self.start_time,
117                     self.stop_time, self.criteria, self.details):
118                 self.logger.info("The results were successfully pushed to DB")
119                 return TestCase.EX_OK
120             else:
121                 self.logger.error("The results cannot be pushed to DB")
122                 return TestCase.EX_PUSH_TO_DB_ERROR
123         except Exception:  # pylint: disable=broad-except
124             self.logger.exception("The results cannot be pushed to DB")
125             return TestCase.EX_PUSH_TO_DB_ERROR