3 # Copyright (c) 2016 ZTE Corp and others.
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
10 """Define the parent class of all Functest Features.
12 Feature is considered as TestCase offered by Third-party. It offers
13 helpers to run any python method or any bash command.
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
23 __author__ = ("Serena Feng <feng.xiaowei@zte.com.cn>, "
24 "Cedric Ollivier <cedric.ollivier@orange.com>")
27 class Feature(base.TestCase):
28 """Base model for single feature."""
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()
36 def execute(self, **kwargs):
37 """Execute the Python method.
39 The subclasses must override the default implementation which
42 The new implementation must return 0 if success or anything
46 kwargs: Arbitrary keyword arguments.
51 # pylint: disable=unused-argument,no-self-use
54 def run(self, **kwargs):
57 It allows executing any Python method by calling execute().
59 It sets the following attributes required to push the results
66 It doesn't fulfill details when pushing the results to the DB.
69 kwargs: Arbitrary keyword arguments.
72 TestCase.EX_OK if execute() returns 0,
73 TestCase.EX_RUN_ERROR otherwise.
75 self.start_time = time.time()
76 exit_code = base.TestCase.EX_RUN_ERROR
79 if self.execute(**kwargs) == 0:
80 exit_code = base.TestCase.EX_OK
82 ft_utils.logger_test_results(
83 self.project_name, self.case_name,
84 self.result, self.details)
85 self.logger.info("%s %s", self.project_name, self.result)
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()
93 class BashFeature(Feature):
94 """Class designed to run any bash command."""
96 def execute(self, **kwargs):
97 """Execute the cmd passed as arg
100 kwargs: Arbitrary keyword arguments.
109 ret = ft_utils.execute_command(cmd, output_file=self.result_file)
111 self.logger.error("Please give cmd as arg. kwargs: %s", kwargs)
112 except Exception: # pylint: disable=broad-except
113 self.logger.exception("Execute cmd: %s failed", cmd)