Merge "paths: Modify algorithm for PATHS verification"
[vswitchperf.git] / tools / functions.py
index da43edc..c0d1e5f 100644 (file)
@@ -19,6 +19,7 @@ import os
 import logging
 import glob
 import shutil
+import re
 from conf import settings as S
 
 MAX_L4_FLOWS = 65536
@@ -65,15 +66,23 @@ def settings_update_paths():
         but testpmd can't be used as a guest loopback. This is useful in case, that other guest
         loopback applications (e.g. buildin) are used by CI jobs, etc.
     """
-    # set dpdk and ovs paths accorfing to VNF and VSWITCH
+    # set dpdk and ovs paths according to VNF, VSWITCH and TRAFFICGEN selection
     paths = {}
-    vswitch_type = S.getValue('PATHS')['vswitch'][S.getValue('VSWITCH')]['type']
-    paths['vswitch'] = S.getValue('PATHS')['vswitch'][S.getValue('VSWITCH')][vswitch_type]
-    paths['dpdk'] = S.getValue('PATHS')['dpdk'][S.getValue('PATHS')['dpdk']['type']]
-    paths['qemu'] = S.getValue('PATHS')['qemu'][S.getValue('PATHS')['qemu']['type']]
-    paths['paths'] = {}
-    paths['paths']['ovs_var_tmp'] = S.getValue('PATHS')['vswitch']['ovs_var_tmp']
-    paths['paths']['ovs_etc_tmp'] = S.getValue('PATHS')['vswitch']['ovs_etc_tmp']
+    if S.getValue("mode") != 'trafficgen':
+        # VSWITCH & (probably) VNF are needed
+        vswitch_type = S.getValue('PATHS')['vswitch'][S.getValue('VSWITCH')]['type']
+        paths['vswitch'] = S.getValue('PATHS')['vswitch'][S.getValue('VSWITCH')][vswitch_type]
+        paths['dpdk'] = S.getValue('PATHS')['dpdk'][S.getValue('PATHS')['dpdk']['type']]
+        paths['qemu'] = S.getValue('PATHS')['qemu'][S.getValue('PATHS')['qemu']['type']]
+        paths['paths'] = {}
+        paths['paths']['ovs_var_tmp'] = S.getValue('PATHS')['vswitch']['ovs_var_tmp']
+        paths['paths']['ovs_etc_tmp'] = S.getValue('PATHS')['vswitch']['ovs_etc_tmp']
+
+    if S.getValue("mode") != 'trafficgen-off':
+        # TRAFFCIGEN is required
+        if S.getValue('TRAFFICGEN') in S.getValue('PATHS')['trafficgen']:
+            tmp_trafficgen = S.getValue('PATHS')['trafficgen'][S.getValue('TRAFFICGEN')]
+            paths['trafficgen'] = tmp_trafficgen[tmp_trafficgen['type']]
 
     tools = {}
     # pylint: disable=too-many-nested-blocks
@@ -146,10 +155,6 @@ def check_traffic(traffic):
     """Check traffic definition and correct it if possible.
     """
     # check if requested networking layers make sense
-    if traffic['vlan']['enabled']:
-        if not (traffic['l3']['enabled'] and traffic['l4']['enabled']):
-            raise RuntimeError('TRAFFIC misconfiguration: both l3 and l4 must '
-                               'be enabled if vlan is enabled.')
     if traffic['l4']['enabled']:
         if not traffic['l3']['enabled']:
             raise RuntimeError('TRAFFIC misconfiguration: l3 must be enabled '
@@ -175,3 +180,44 @@ def check_traffic(traffic):
                 traffic['multistream'] = MAX_L4_FLOWS
 
     return traffic
+
+def filter_output(output, regex):
+    """Filter output by defined regex. Output can be either string, list or tuple.
+       Every string is split into list line by line. After that regex is applied
+       to filter only matching lines, which are returned back.
+
+       :returns: list of matching records
+    """
+    result = []
+    if isinstance(output, str):
+        for line in output.split('\n'):
+            result += re.findall(regex, line)
+        return result
+    elif isinstance(output, list) or isinstance(output, tuple):
+        tmp_res = []
+        for item in output:
+            tmp_res.append(filter_output(item, regex))
+        return tmp_res
+    else:
+        raise RuntimeError('Only strings and lists are supported by filter_output(), '
+                           'but output has type {}'.format(type(output)))
+
+def format_description(desc, length):
+    """ Split description into multiple lines based on given line length.
+
+    :param desc: A string with testcase description
+    :param length: A maximum line length
+    """
+    # split description to multiple lines
+    words = desc.split()
+    output = []
+    line = ''
+    for word in words:
+        if len(line) + len(word) < length:
+            line += '{} '.format(word)
+        else:
+            output.append(line.strip())
+            line = '{} '.format(word)
+
+    output.append(line.strip())
+    return output