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 classes of all Xtesting Features.
12 Feature is considered as TestCase offered by Third-party. It offers
13 helpers to run any python method or any bash command.
22 from xtesting.core import testcase
24 __author__ = ("Serena Feng <feng.xiaowei@zte.com.cn>, "
25 "Cedric Ollivier <cedric.ollivier@orange.com>")
28 @six.add_metaclass(abc.ABCMeta)
29 class Feature(testcase.TestCase):
30 """Base model for single feature."""
32 __logger = logging.getLogger(__name__)
35 def execute(self, **kwargs):
36 """Execute the Python method.
38 The subclasses must override the default implementation which
41 The new implementation must return 0 if success or anything
45 kwargs: Arbitrary keyword arguments.
48 def run(self, **kwargs):
51 It allows executing any Python method by calling execute().
53 It sets the following attributes required to push the results
60 It doesn't fulfill details when pushing the results to the DB.
63 kwargs: Arbitrary keyword arguments.
66 TestCase.EX_OK if execute() returns 0,
67 TestCase.EX_RUN_ERROR otherwise.
69 self.start_time = time.time()
70 exit_code = testcase.TestCase.EX_RUN_ERROR
73 if self.execute(**kwargs) == 0:
74 exit_code = testcase.TestCase.EX_OK
76 except Exception: # pylint: disable=broad-except
77 self.__logger.exception("%s FAILED", self.project_name)
78 self.stop_time = time.time()
82 class BashFeature(Feature):
83 """Class designed to run any bash command."""
85 __logger = logging.getLogger(__name__)
87 def __init__(self, **kwargs):
88 super(BashFeature, self).__init__(**kwargs)
89 dir_results = "/var/lib/xtesting/results"
90 self.result_file = "{}/{}.log".format(dir_results, self.case_name)
92 def execute(self, **kwargs):
93 """Execute the cmd passed as arg
96 kwargs: Arbitrary keyword arguments.
104 with open(self.result_file, 'w+') as f_stdout:
105 subprocess.check_call(
106 cmd, shell=True, stdout=f_stdout, stderr=subprocess.STDOUT)
109 self.__logger.error("Please give cmd as arg. kwargs: %s", kwargs)
110 except subprocess.CalledProcessError:
111 self.__logger.error("Execute command: %s failed", cmd)