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.
20 from xtesting.core import testcase
22 __author__ = ("Serena Feng <feng.xiaowei@zte.com.cn>, "
23 "Cedric Ollivier <cedric.ollivier@orange.com>")
26 class Feature(testcase.TestCase):
27 """Base model for single feature."""
29 __logger = logging.getLogger(__name__)
30 dir_results = "/home/opnfv/xtesting/results"
32 def __init__(self, **kwargs):
33 super(Feature, self).__init__(**kwargs)
34 self.result_file = "{}/{}.log".format(self.dir_results, self.case_name)
36 module = kwargs['run']['module']
37 self.logger = logging.getLogger(module)
39 self.__logger.warning(
40 "Cannot get module name %s. Using %s as fallback",
41 kwargs, self.case_name)
42 self.logger = logging.getLogger(self.case_name)
43 handler = logging.StreamHandler()
44 handler.setLevel(logging.WARN)
45 self.logger.addHandler(handler)
46 handler = logging.FileHandler(self.result_file)
47 handler.setLevel(logging.DEBUG)
48 self.logger.addHandler(handler)
49 formatter = logging.Formatter(
50 '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
51 handler.setFormatter(formatter)
52 self.logger.addHandler(handler)
54 def execute(self, **kwargs):
55 """Execute the Python method.
57 The subclasses must override the default implementation which
60 The new implementation must return 0 if success or anything
64 kwargs: Arbitrary keyword arguments.
69 # pylint: disable=unused-argument,no-self-use
72 def run(self, **kwargs):
75 It allows executing any Python method by calling execute().
77 It sets the following attributes required to push the results
84 It doesn't fulfill details when pushing the results to the DB.
87 kwargs: Arbitrary keyword arguments.
90 TestCase.EX_OK if execute() returns 0,
91 TestCase.EX_RUN_ERROR otherwise.
93 self.start_time = time.time()
94 exit_code = testcase.TestCase.EX_RUN_ERROR
97 if self.execute(**kwargs) == 0:
98 exit_code = testcase.TestCase.EX_OK
100 except Exception: # pylint: disable=broad-except
101 self.__logger.exception("%s FAILED", self.project_name)
102 self.__logger.info("Test result is stored in '%s'", self.result_file)
103 self.stop_time = time.time()
107 class BashFeature(Feature):
108 """Class designed to run any bash command."""
110 __logger = logging.getLogger(__name__)
112 def execute(self, **kwargs):
113 """Execute the cmd passed as arg
116 kwargs: Arbitrary keyword arguments.
125 with open(self.result_file, 'w+') as f_stdout:
126 proc = subprocess.Popen(cmd.split(), stdout=f_stdout,
127 stderr=subprocess.STDOUT)
130 self.__logger.error("Execute command: %s failed", cmd)
132 self.__logger.error("Please give cmd as arg. kwargs: %s", kwargs)