Merge "documentation: Traffic Capture methods"
[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 from tools.tasks import run_background_task
23
24 _LOGGER = logging.getLogger(__name__)
25
26 class TestStepsTools(object):
27     """ Various tools and functions used by step driven testcases
28     """
29     # Functions use nonstandard names to avoid conflicts with
30     # standard python keywords.
31     # pylint: disable=invalid-name
32     @staticmethod
33     def Assert(condition):
34         """ Evaluate given `condition' and raise AssertionError
35             in case, that evaluation fails
36         """
37         try:
38             assert TestStepsTools.Eval(condition)
39         except AssertionError:
40             _LOGGER.error('Condition %s is not True', condition)
41             raise
42
43         return True
44
45     @staticmethod
46     def validate_Assert(result, dummy_condition):
47         """ Validate evaluation of given `condition'
48         """
49         return result
50
51     @staticmethod
52     def Eval(expression):
53         """ Evaluate python `expression' and return its result
54         """
55         # pylint: disable=eval-used
56         return eval(expression)
57
58     @staticmethod
59     def validate_Eval(result, dummy_expression):
60         """ Validate result of python `expression' evaluation
61         """
62         return result is not None
63
64     @staticmethod
65     def Exec_Python(code):
66         """ Execute a python `code' and return True on success
67         """
68         # pylint: disable=exec-used
69         try:
70             exec(code, globals())
71         # pylint: disable=broad-except
72         # pylint: disable=bare-except
73         except:
74             _LOGGER.error('Execution of following code has failed %s', code)
75             return False
76         return True
77
78     @staticmethod
79     def validate_Exec_Python(result, dummy_code):
80         """ Validate result of python `code' execution
81         """
82         return result
83
84     @staticmethod
85     def Exec_Shell(command, regex=None):
86         """ Execute a shell `command' and return its output filtered
87             out by optional `regex' expression.
88         """
89         try:
90             output = subprocess.check_output(command, shell=True)
91         except OSError:
92             return None
93
94         output = output.decode(locale.getdefaultlocale()[1])
95
96         if regex:
97             return filter_output(output, regex)
98
99         return output
100
101     @staticmethod
102     def validate_Exec_Shell(result, dummy_command, dummy_regex=None):
103         """ validate result of shell `command' execution
104         """
105         return result is not None
106
107     @staticmethod
108     def Exec_Shell_Background(command):
109         """ Execute a shell `command' at the background and return its PID id
110         """
111         try:
112             pid = run_background_task(command.split(), _LOGGER, "Background task: {}".format(command))
113             return pid
114         except OSError:
115             return None
116
117     @staticmethod
118     def validate_Exec_Shell_Background(result, dummy_command, dummy_regex=None):
119         """ validate result of shell `command' execution on the background
120         """
121         return result is not None