diff --git a/neutron/cmd/sanity/checks.py b/neutron/cmd/sanity/checks.py index 21eb5aca7c1..439ce253954 100644 --- a/neutron/cmd/sanity/checks.py +++ b/neutron/cmd/sanity/checks.py @@ -32,10 +32,10 @@ from neutron.agent.linux import ip_link_support from neutron.agent.linux import keepalived from neutron.agent.linux import utils as agent_utils from neutron.common import constants as n_consts -from neutron.common import utils from neutron.plugins.common import constants as const from neutron.plugins.ml2.drivers.openvswitch.agent.common \ import constants as ovs_const +from neutron.tests import base LOG = logging.getLogger(__name__) @@ -45,14 +45,14 @@ MINIMUM_DIBBLER_VERSION = '1.0.1' def ovs_vxlan_supported(from_ip='192.0.2.1', to_ip='192.0.2.2'): - name = "vxlantest-" + utils.get_random_string(6) + name = base.get_rand_device_name(prefix='vxlantest-') with ovs_lib.OVSBridge(name) as br: port = br.add_tunnel_port(from_ip, to_ip, const.TYPE_VXLAN) return port != ovs_lib.INVALID_OFPORT def ovs_geneve_supported(from_ip='192.0.2.3', to_ip='192.0.2.4'): - name = "genevetest-" + utils.get_random_string(6) + name = base.get_rand_device_name(prefix='genevetest-') with ovs_lib.OVSBridge(name) as br: port = br.add_tunnel_port(from_ip, to_ip, const.TYPE_GENEVE) return port != ovs_lib.INVALID_OFPORT @@ -60,17 +60,15 @@ def ovs_geneve_supported(from_ip='192.0.2.3', to_ip='192.0.2.4'): def iproute2_vxlan_supported(): ip = ip_lib.IPWrapper() - name = "vxlantest-" + utils.get_random_string(4) + name = base.get_rand_device_name(prefix='vxlantest-') port = ip.add_vxlan(name, 3000) ip.del_veth(name) return name == port.name def patch_supported(): - seed = utils.get_random_string(6) - name = "patchtest-" + seed - peer_name = "peertest0-" + seed - patch_name = "peertest1-" + seed + name, peer_name, patch_name = base.get_related_rand_device_names( + ['patchtest-', 'peertest0-', 'peertest1-']) with ovs_lib.OVSBridge(name) as br: port = br.add_patch_port(patch_name, peer_name) return port != ovs_lib.INVALID_OFPORT @@ -91,7 +89,7 @@ def ofctl_arg_supported(cmd, **kwargs): :param **kwargs: arguments to test with the command. :returns: a boolean if the supplied arguments are supported. """ - br_name = 'br-test-%s' % utils.get_random_string(6) + br_name = base.get_rand_device_name(prefix='br-test-') with ovs_lib.OVSBridge(br_name) as test_br: full_args = ["ovs-ofctl", cmd, test_br.br_name, ovs_lib._build_flow_expr_str(kwargs, cmd.split('-')[0])] @@ -281,10 +279,8 @@ def keepalived_ipv6_supported(): 6. Verify if IPv6 default route is configured by keepalived. """ - random_str = utils.get_random_string(6) - br_name = "ka-test-" + random_str - ha_port = ha_router.HA_DEV_PREFIX + random_str - gw_port = namespaces.INTERNAL_DEV_PREFIX + random_str + br_name, ha_port, gw_port = base.get_related_rand_device_names( + ['ka-test-', ha_router.HA_DEV_PREFIX, namespaces.INTERNAL_DEV_PREFIX]) gw_vip = 'fdf8:f53b:82e4::10/64' expected_default_gw = 'fe80:f816::1' @@ -333,8 +329,7 @@ def ovsdb_native_supported(): def ovs_conntrack_supported(): - random_str = utils.get_random_string(6) - br_name = "ovs-test-" + random_str + br_name = base.get_rand_device_name(prefix="ovs-test-") with ovs_lib.OVSBridge(br_name) as br: try: diff --git a/neutron/tests/base.py b/neutron/tests/base.py index 079a73cb503..e888ca27b62 100644 --- a/neutron/tests/base.py +++ b/neutron/tests/base.py @@ -70,6 +70,24 @@ def fake_consume_in_threads(self): return [] +def get_related_rand_names(prefixes, max_length=None): + """Returns a list of the prefixes with the same random characters appended + + :param prefixes: A list of prefix strings + :param max_length: The maximum length of each returned string + :returns: A list with each prefix appended with the same random characters + """ + + if max_length: + length = max_length - max(len(p) for p in prefixes) + if length <= 0: + raise ValueError("'max_length' must be longer than all prefixes") + else: + length = 8 + rndchrs = utils.get_random_string(length) + return [p + rndchrs for p in prefixes] + + def get_rand_name(max_length=None, prefix='test'): """Return a random string. @@ -78,16 +96,7 @@ def get_rand_name(max_length=None, prefix='test'): hexadecimal, will be added. In case len(prefix) <= len(max_length), ValueError will be raised to indicate the problem. """ - - if max_length: - length = max_length - len(prefix) - if length <= 0: - raise ValueError("'max_length' must be bigger than 'len(prefix)'.") - - else: - length = 8 - - return prefix + utils.get_random_string(length) + return get_related_rand_names([prefix], max_length)[0] def get_rand_device_name(prefix='test'): @@ -95,6 +104,11 @@ def get_rand_device_name(prefix='test'): max_length=constants.DEVICE_NAME_MAX_LEN, prefix=prefix) +def get_related_rand_device_names(prefixes): + return get_related_rand_names(prefixes, + max_length=constants.DEVICE_NAME_MAX_LEN) + + def bool_from_env(key, strict=False, default=False): value = os.environ.get(key) return strutils.bool_from_string(value, strict=strict, default=default)