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