1 # Copyright 2016-2017 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
21 from tools.functions import filter_output
23 _LOGGER = logging.getLogger(__name__)
25 class TestStepsTools(object):
26 """ Various tools and functions used by step driven testcases
28 # Functions use nonstandard names to avoid conflicts with
29 # standard python keywords.
30 # pylint: disable=invalid-name
32 def Assert(condition):
33 """ Evaluate given `condition' and raise AssertionError
34 in case, that evaluation fails
37 assert TestStepsTools.Eval(condition)
38 except AssertionError:
39 _LOGGER.error('Condition %s is not True', condition)
45 def validate_Assert(result, dummy_condition):
46 """ Validate evaluation of given `condition'
52 """ Evaluate python `expression' and return its result
54 # pylint: disable=eval-used
55 return eval(expression)
58 def validate_Eval(result, dummy_expression):
59 """ Validate result of python `expression' evaluation
61 return result is not None
64 def Exec_Python(code):
65 """ Execute a python `code' and return True on success
67 # pylint: disable=exec-used
70 # pylint: disable=broad-except
71 # pylint: disable=bare-except
73 _LOGGER.error('Execution of following code has failed %s', code)
78 def validate_Exec_Python(result, dummy_code):
79 """ Validate result of python `code' execution
84 def Exec_Shell(command, regex=None):
85 """ Execute a shell `command' and return its output filtered
86 out by optional `regex' expression.
89 output = subprocess.check_output(command, shell=True)
93 output = output.decode(locale.getdefaultlocale()[1])
96 return filter_output(output, regex)
101 def validate_Exec_Shell(result, dummy_command, dummy_regex=None):
102 """ validate result of shell `command' execution
104 return result is not None