Merge "Turn Sphinx warnings into errors"
[functest.git] / functest / core / feature.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 ZTE Corp 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 Features.
11
12 Feature is considered as TestCase offered by Third-party. It offers
13 helpers to run any python method or any bash command.
14 """
15
16 import logging
17 import time
18
19 import functest.core.testcase as base
20 import functest.utils.functest_utils as ft_utils
21 from functest.utils.constants import CONST
22
23 __author__ = ("Serena Feng <feng.xiaowei@zte.com.cn>, "
24               "Cedric Ollivier <cedric.ollivier@orange.com>")
25
26
27 class Feature(base.TestCase):
28     """Base model for single feature."""
29
30     __logger = logging.getLogger(__name__)
31
32     def __init__(self, **kwargs):
33         super(Feature, self).__init__(**kwargs)
34         self.result_file = "{}/{}.log".format(
35             CONST.__getattribute__('dir_results'), self.case_name)
36
37     def execute(self, **kwargs):
38         """Execute the Python method.
39
40         The subclasses must override the default implementation which
41         is false on purpose.
42
43         The new implementation must return 0 if success or anything
44         else if failure.
45
46         Args:
47             kwargs: Arbitrary keyword arguments.
48
49         Returns:
50             -1.
51         """
52         # pylint: disable=unused-argument,no-self-use
53         return -1
54
55     def run(self, **kwargs):
56         """Run the feature.
57
58         It allows executing any Python method by calling execute().
59
60         It sets the following attributes required to push the results
61         to DB:
62
63             * result,
64             * start_time,
65             * stop_time.
66
67         It doesn't fulfill details when pushing the results to the DB.
68
69         Args:
70             kwargs: Arbitrary keyword arguments.
71
72         Returns:
73             TestCase.EX_OK if execute() returns 0,
74             TestCase.EX_RUN_ERROR otherwise.
75         """
76         self.start_time = time.time()
77         exit_code = base.TestCase.EX_RUN_ERROR
78         self.result = 0
79         try:
80             if self.execute(**kwargs) == 0:
81                 exit_code = base.TestCase.EX_OK
82                 self.result = 100
83             ft_utils.logger_test_results(
84                 self.project_name, self.case_name,
85                 self.result, self.details)
86         except Exception:  # pylint: disable=broad-except
87             self.__logger.exception("%s FAILED", self.project_name)
88         self.__logger.info("Test result is stored in '%s'", self.result_file)
89         self.stop_time = time.time()
90         return exit_code
91
92
93 class BashFeature(Feature):
94     """Class designed to run any bash command."""
95
96     __logger = logging.getLogger(__name__)
97
98     def execute(self, **kwargs):
99         """Execute the cmd passed as arg
100
101         Args:
102             kwargs: Arbitrary keyword arguments.
103
104         Returns:
105             0 if cmd returns 0,
106             -1 otherwise.
107         """
108         ret = -1
109         try:
110             cmd = kwargs["cmd"]
111             ret = ft_utils.execute_command(cmd, output_file=self.result_file)
112         except KeyError:
113             self.__logger.error("Please give cmd as arg. kwargs: %s", kwargs)
114         except Exception:  # pylint: disable=broad-except
115             self.__logger.exception("Execute cmd: %s failed", cmd)
116         return ret