teststeps: Improvements of step driven TC
[vswitchperf.git] / tools / teststepstools.py
1 # Copyright 2016-2017 Intel Corporation.
2 #
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
6 #
7 #   http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 """Various helper functions for step driven testcases
16 """
17
18 import logging
19 import subprocess
20 import locale
21 from tools.functions import filter_output
22
23 _LOGGER = logging.getLogger(__name__)
24
25 class TestStepsTools(object):
26     """ Various tools and functions used by step driven testcases
27     """
28     # Functions use nonstandard names to avoid conflicts with
29     # standard python keywords.
30     # pylint: disable=invalid-name
31     @staticmethod
32     def Assert(condition):
33         """ Evaluate given `condition' and raise AssertionError
34             in case, that evaluation fails
35         """
36         try:
37             assert TestStepsTools.Eval(condition)
38         except AssertionError:
39             _LOGGER.error('Condition %s is not True', condition)
40             raise
41
42         return True
43
44     @staticmethod
45     def validate_Assert(result, dummy_condition):
46         """ Validate evaluation of given `condition'
47         """
48         return result
49
50     @staticmethod
51     def Eval(expression):
52         """ Evaluate python `expression' and return its result
53         """
54         # pylint: disable=eval-used
55         return eval(expression)
56
57     @staticmethod
58     def validate_Eval(result, dummy_expression):
59         """ Validate result of python `expression' evaluation
60         """
61         return result is not None
62
63     @staticmethod
64     def Exec_Python(code):
65         """ Execute a python `code' and return True on success
66         """
67         # pylint: disable=exec-used
68         try:
69             exec(code, globals())
70         # pylint: disable=broad-except
71         # pylint: disable=bare-except
72         except:
73             _LOGGER.error('Execution of following code has failed %s', code)
74             return False
75         return True
76
77     @staticmethod
78     def validate_Exec_Python(result, dummy_code):
79         """ Validate result of python `code' execution
80         """
81         return result
82
83     @staticmethod
84     def Exec_Shell(command, regex=None):
85         """ Execute a shell `command' and return its output filtered
86             out by optional `regex' expression.
87         """
88         try:
89             output = subprocess.check_output(command, shell=True)
90         except OSError:
91             return None
92
93         output = output.decode(locale.getdefaultlocale()[1])
94
95         if regex:
96             return filter_output(output, regex)
97
98         return output
99
100     @staticmethod
101     def validate_Exec_Shell(result, dummy_command, dummy_regex=None):
102         """ validate result of shell `command' execution
103         """
104         return result is not None