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__)
31 def execute(self, **kwargs):
32 """Execute the Python method.
34 The subclasses must override the default implementation which
37 The new implementation must return 0 if success or anything
41 kwargs: Arbitrary keyword arguments.
46 # pylint: disable=unused-argument,no-self-use
49 def run(self, **kwargs):
52 It allows executing any Python method by calling execute().
54 It sets the following attributes required to push the results
61 It doesn't fulfill details when pushing the results to the DB.
64 kwargs: Arbitrary keyword arguments.
67 TestCase.EX_OK if execute() returns 0,
68 TestCase.EX_RUN_ERROR otherwise.
70 self.start_time = time.time()
71 exit_code = testcase.TestCase.EX_RUN_ERROR
74 if self.execute(**kwargs) == 0:
75 exit_code = testcase.TestCase.EX_OK
77 except Exception: # pylint: disable=broad-except
78 self.__logger.exception("%s FAILED", self.project_name)
79 self.stop_time = time.time()
83 class BashFeature(Feature):
84 """Class designed to run any bash command."""
86 __logger = logging.getLogger(__name__)
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)
93 def execute(self, **kwargs):
94 """Execute the cmd passed as arg
97 kwargs: Arbitrary keyword arguments.
106 with open(self.result_file, 'w+') as f_stdout:
107 proc = subprocess.Popen(cmd.split(), stdout=f_stdout,
108 stderr=subprocess.STDOUT)
111 "Test result is stored in '%s'", self.result_file)
113 self.__logger.error("Execute command: %s failed", cmd)
115 self.__logger.error("Please give cmd as arg. kwargs: %s", kwargs)