00c7ec74cc5b7d0a9b0df81cad1709d00cf6f013
[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 time
17
18 import functest.core.testcase as base
19 import functest.utils.functest_utils as ft_utils
20 import functest.utils.functest_logger as ft_logger
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     """Parent class of Functest Feature."""
29
30     def __init__(self, **kwargs):
31         super(Feature, self).__init__(**kwargs)
32         self.result_file = "{}/{}.log".format(
33             CONST.__getattribute__('dir_results'), self.project_name)
34         self.logger = ft_logger.Logger(self.project_name).getLogger()
35
36     def execute(self, **kwargs):
37         """Execute Feature.
38
39         The subclasses must override the default implementation which
40         is false on purpose. The only prerequisite is to return 0 if
41         success or anything else if failure.
42
43         Args:
44             kwargs: Arbitrary keyword arguments.
45
46         Returns:
47             -1.
48         """
49         # pylint: disable=unused-argument,no-self-use
50         return -1
51
52     def run(self, **kwargs):
53         """Run Feature.
54
55         It allows executing any Python method by calling execute().
56
57         It sets the following attributes required to push the results
58         to DB:
59
60             * criteria,
61             * start_time,
62             * stop_time.
63
64         It doesn't fulfill details when pushing the results to the DB.
65
66         Args:
67             kwargs: Arbitrary keyword arguments.
68
69         Returns:
70             TestCase.EX_OK if execute() returns 0,
71             TestCase.EX_RUN_ERROR otherwise.
72         """
73         self.start_time = time.time()
74         exit_code = base.TestCase.EX_RUN_ERROR
75         self.criteria = "FAIL"
76         try:
77             if self.execute(**kwargs) == 0:
78                 exit_code = base.TestCase.EX_OK
79                 self.criteria = 'PASS'
80             ft_utils.logger_test_results(
81                 self.project_name, self.case_name,
82                 self.criteria, self.details)
83             self.logger.info("%s %s", self.project_name, self.criteria)
84         except Exception:  # pylint: disable=broad-except
85             self.logger.exception("%s FAILED", self.project_name)
86         self.logger.info("Test result is stored in '%s'", self.result_file)
87         self.stop_time = time.time()
88         return exit_code
89
90
91 class BashFeature(Feature):
92     """Class designed to run any bash command."""
93
94     def execute(self, **kwargs):
95         """Execute the cmd passed as arg
96
97         Args:
98             kwargs: Arbitrary keyword arguments.
99
100         Returns:
101             0 if cmd returns 0,
102             -1 otherwise.
103         """
104         ret = -1
105         try:
106             cmd = kwargs["cmd"]
107             ret = ft_utils.execute_command(cmd, output_file=self.result_file)
108         except KeyError:
109             self.logger.error("Please give cmd as arg. kwargs: %s", kwargs)
110         except Exception:  # pylint: disable=broad-except
111             self.logger.exception("Execute cmd: %s failed", cmd)
112         return ret