X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=tools%2Ffunctions.py;h=9292867d9166b6d07aea63a3b7b7311fc38f43cf;hb=b1534957e463b5e34957a8d48ce5c6b0552ffbb4;hp=3bd8cc4d36fdf87d6e0be27ec5728adc92bb9997;hpb=e3c52e2eeacc1ec995b9492ce8315fb166886fdd;p=vswitchperf.git diff --git a/tools/functions.py b/tools/functions.py index 3bd8cc4d..9292867d 100644 --- a/tools/functions.py +++ b/tools/functions.py @@ -1,4 +1,4 @@ -# Copyright 2016 Intel Corporation. +# Copyright 2016-2017 Intel Corporation. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -19,12 +19,15 @@ import os import logging import glob import shutil +import re from conf import settings as S +MAX_L4_FLOWS = 65536 + # # Support functions # - +# pylint: disable=too-many-branches def settings_update_paths(): """ Configure paths to OVS, DPDK and QEMU sources and binaries based on selected vswitch type and src/binary switch. Data are taken from @@ -74,6 +77,7 @@ def settings_update_paths(): paths['paths']['ovs_etc_tmp'] = S.getValue('PATHS')['vswitch']['ovs_etc_tmp'] tools = {} + # pylint: disable=too-many-nested-blocks for path_class in paths: for tool in paths[path_class]: tmp_tool = paths[path_class][tool] @@ -123,8 +127,8 @@ def settings_update_paths(): tmp_tool = tmp_glob[0] elif not os.path.exists(tmp_tool): if tool.endswith('_tmp'): - logging.getLogger().debug('Temporary path to the {} does not ' - 'exist: {}.'.format(tool, tmp_tool)) + logging.getLogger().debug('Temporary path to the %s does not ' + 'exist: %s', tool, tmp_tool) else: raise RuntimeError('Path to the {} is not valid: {}'.format(tool, tmp_tool)) @@ -138,3 +142,74 @@ def settings_update_paths(): tools['dpdk_src'] = S.getValue('PATHS')['dpdk']['src']['path'] S.setValue('TOOLS', tools) + +def check_traffic(traffic): + """Check traffic definition and correct it if possible. + """ + # check if requested networking layers make sense + if traffic['l4']['enabled']: + if not traffic['l3']['enabled']: + raise RuntimeError('TRAFFIC misconfiguration: l3 must be enabled ' + 'if l4 is enabled.') + + # check if multistream configuration makes sense + if traffic['multistream']: + if traffic['stream_type'] == 'L3': + if not traffic['l3']['enabled']: + raise RuntimeError('TRAFFIC misconfiguration: l3 must be ' + 'enabled if l3 streams are requested.') + if traffic['stream_type'] == 'L4': + if not traffic['l4']['enabled']: + raise RuntimeError('TRAFFIC misconfiguration: l4 must be ' + 'enabled if l4 streams are requested.') + + # in case of UDP ports we have only 65536 (0-65535) unique options + if traffic['multistream'] > MAX_L4_FLOWS: + logging.getLogger().warning( + 'Requested amount of L4 flows %s is bigger than number of ' + 'transport protocol ports. It was set to %s.', + traffic['multistream'], MAX_L4_FLOWS) + 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