pkt_gen: MoonGen updated to keep parity with master
[vswitchperf.git] / tools / teststepstools.py
1 # Copyright 2016 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 re
19 import logging
20 import subprocess
21 import locale
22
23 class TestStepsTools(object):
24     """ Various tools and functions used by step driven testcases
25     """
26     # Functions use nonstandard names to avoid conflicts with
27     # standard python keywords.
28     # pylint: disable=invalid-name
29     def __init__(self):
30         """ TestStepsTools initialization
31         """
32         self._logger = logging.getLogger(__name__)
33
34     def Assert(self, condition):
35         """ Evaluate given `condition' and raise AssertionError
36             in case, that evaluation fails
37         """
38         try:
39             assert self.Eval(condition)
40         except AssertionError:
41             self._logger.error('Condition %s is not True', condition)
42             raise
43
44         return True
45
46     @staticmethod
47     def validate_Assert(result, dummy_condition):
48         """ Validate evaluation of given `condition'
49         """
50         return result
51
52     @staticmethod
53     def Eval(expression):
54         """ Evaluate python `expression' and return its result
55         """
56         # pylint: disable=eval-used
57         return eval(expression)
58
59     @staticmethod
60     def validate_Eval(result, dummy_expression):
61         """ Validate result of python `expression' evaluation
62         """
63         return result is not None
64
65     @staticmethod
66     def Exec(command, regex=None):
67         """ Execute a shell `command' and return its output filtered
68             out by optional `regex' expression.
69         """
70         try:
71             output = subprocess.check_output(command, shell=True)
72         except OSError:
73             return None
74
75         output = output.decode(locale.getdefaultlocale()[1])
76
77         if regex:
78             for line in output.split('\n'):
79                 result = re.findall(regex, line)
80                 if result:
81                     return result
82             return []
83
84         return output
85
86     @staticmethod
87     def validate_Exec(result, dummy_command, dummy_regex=None):
88         """ validate result of shell `command' execution
89         """
90         return result is not None