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