1 # Copyright 2016 Intel Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 """Various helper functions for step driven testcases
23 class TestStepsTools(object):
24 """ Various tools and functions used by step driven testcases
26 # Functions use nonstandard names to avoid conflicts with
27 # standard python keywords.
28 # pylint: disable=invalid-name
30 """ TestStepsTools initialization
32 self._logger = logging.getLogger(__name__)
34 def Assert(self, condition):
35 """ Evaluate given `condition' and raise AssertionError
36 in case, that evaluation fails
39 assert self.Eval(condition)
40 except AssertionError:
41 self._logger.error('Condition %s is not True', condition)
47 def validate_Assert(result, dummy_condition):
48 """ Validate evaluation of given `condition'
54 """ Evaluate python `expression' and return its result
56 # pylint: disable=eval-used
57 return eval(expression)
60 def validate_Eval(result, dummy_expression):
61 """ Validate result of python `expression' evaluation
63 return result is not None
66 def Exec(command, regex=None):
67 """ Execute a shell `command' and return its output filtered
68 out by optional `regex' expression.
71 output = subprocess.check_output(command, shell=True)
75 output = output.decode(locale.getdefaultlocale()[1])
78 for line in output.split('\n'):
79 result = re.findall(regex, line)
87 def validate_Exec(result, dummy_command, dummy_regex=None):
88 """ validate result of shell `command' execution
90 return result is not None