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
22 from tools.tasks import run_background_task
24 _LOGGER = logging.getLogger(__name__)
26 class TestStepsTools(object):
27 """ Various tools and functions used by step driven testcases
29 # Functions use nonstandard names to avoid conflicts with
30 # standard python keywords.
31 # pylint: disable=invalid-name
33 def Assert(condition):
34 """ Evaluate given `condition' and raise AssertionError
35 in case, that evaluation fails
38 assert TestStepsTools.Eval(condition)
39 except AssertionError:
40 _LOGGER.error('Condition %s is not True', condition)
46 def validate_Assert(result, _dummy_condition):
47 """ Validate evaluation of given `condition'
53 """ Evaluate python `expression' and return its result
55 # pylint: disable=eval-used
56 return eval(expression)
59 def validate_Eval(result, _dummy_expression):
60 """ Validate result of python `expression' evaluation
62 return result is not None
65 def Exec_Python(code):
66 """ Execute a python `code' and return True on success
68 # pylint: disable=exec-used
71 # pylint: disable=broad-except
72 # pylint: disable=bare-except
74 _LOGGER.error('Execution of following code has failed %s', code)
79 def validate_Exec_Python(result, _dummy_code):
80 """ Validate result of python `code' execution
85 def Exec_Shell(command, regex=None):
86 """ Execute a shell `command' and return its output filtered
87 out by optional `regex' expression.
90 output = subprocess.check_output(command, shell=True)
94 output = output.decode(locale.getdefaultlocale()[1])
97 return filter_output(output, regex)
102 def validate_Exec_Shell(result, _dummy_command, _dummy_regex=None):
103 """ validate result of shell `command' execution
105 return result is not None
108 def Exec_Shell_Background(command):
109 """ Execute a shell `command' at the background and return its PID id
112 pid = run_background_task(command.split(), _LOGGER, "Background task: {}".format(command))
118 def validate_Exec_Shell_Background(result, _dummy_command, _dummy_regex=None):
119 """ validate result of shell `command' execution on the background
121 return result is not None