def test_get_interface_long_post_and_prefix(self): """Prefix and postfix alone overcome the max character limit.""" long_prefix = "long_pre" long_postfix = "long_pos" much_too_long_prefix = "much_too_long_prefix" much_too_long_postfix = "much_too_long_postfix" self.assertEqual( "long_preSHORT", utils.get_interface_name(SHORT_NAME, prefix=long_prefix)) self.assertEqual( "SHORTlong_pos", utils.get_interface_name(SHORT_NAME, postfix=long_postfix)) self.assertRaises(ValueError, utils.get_interface_name, SHORT_NAME, prefix=long_prefix, postfix=long_postfix) self.assertRaises(ValueError, utils.get_interface_name, SHORT_NAME, prefix=much_too_long_prefix) self.assertRaises(ValueError, utils.get_interface_name, SHORT_NAME, postfix=much_too_long_postfix)
def test_get_interface_max_len(self, mock_sha1): self.assertEqual(constants.DEVICE_NAME_MAX_LEN, len(utils.get_interface_name(LONG_NAME1))) self.assertEqual(10, len(utils.get_interface_name(LONG_NAME1, max_len=10))) self.assertEqual(12, len(utils.get_interface_name(LONG_NAME1, prefix="pre-", max_len=12)))
def test_get_interface_name(self, mock_sha1): prefix = "pre-" prefix_long = "long_prefix" prefix_exceeds_max_dev_len = "much_too_long_prefix" hash_used = MOCKED_HASH[0:6] self.assertEqual("A_REALLY_" + hash_used, utils.get_interface_name(LONG_NAME1)) self.assertEqual("SHORT", utils.get_interface_name(SHORT_NAME)) self.assertEqual("pre-A_REA" + hash_used, utils.get_interface_name(LONG_NAME1, prefix=prefix)) self.assertEqual("pre-SHORT", utils.get_interface_name(SHORT_NAME, prefix=prefix)) # len(prefix) > max_device_len - len(hash_used) self.assertRaises(ValueError, utils.get_interface_name, SHORT_NAME, prefix_long) # len(prefix) > max_device_len self.assertRaises(ValueError, utils.get_interface_name, SHORT_NAME, prefix=prefix_exceeds_max_dev_len)
def test_get_interface_name(self, mock_sha1): prefix = "pre-" postfix = ".1111" if_default_long = utils.get_interface_name(LONG_NAME1) if_default_short = utils.get_interface_name(SHORT_NAME) if_prefix_long = utils.get_interface_name(LONG_NAME1, prefix=prefix) if_prefix_short = utils.get_interface_name(SHORT_NAME, prefix=prefix) if_postfix_long = utils.get_interface_name(LONG_NAME1, postfix=postfix) if_postfix_short = utils.get_interface_name(SHORT_NAME, postfix=postfix) if_prefix_postfix_long = utils.get_interface_name(LONG_NAME1, prefix=prefix, postfix=postfix) if_prefix_postfix_short = utils.get_interface_name(SHORT_NAME, prefix=prefix, postfix=postfix) # Each combination is a tuple of the following values: # the calculated name, the expected name" combinations = [(if_default_long, "A_REALLY_mocked"), (if_default_short, "SHORT"), (if_prefix_long, "pre-A_REAmocked"), (if_prefix_short, "pre-SHORT"), (if_postfix_long, "A_REmocked.1111"), (if_postfix_short, "SHORT.1111"), (if_prefix_postfix_long, "pre-mocked.1111"), (if_prefix_postfix_short, "pre-SHORT.1111")] for if_new_name, if_expected_new_name in combinations: self.assertEqual(if_new_name, if_expected_new_name)
def get_subinterface_name(self, physical_interface, vlan_id): if not vlan_id: LOG.warning( _LW("Invalid VLAN ID, will lead to incorrect " "subinterface name")) vlan_postfix = '.%s' % vlan_id # For the vlan subinterface name prefix we use: # * the physical_interface, if len(physical_interface) + # len(vlan_postifx) <= 15 for backward compatibility reasons # Example: physical_interface = eth0 # prefix = eth0.1 # prefix = eth0.1111 # # * otherwise a unique hash per physical_interface to help debugging # Example: physical_interface = long_interface # prefix = longHASHED.1 # prefix = longHASHED.1111 # # Remark: For some physical_interface values, the used prefix can be # both, the physical_interface itself or a hash, depending # on the vlan_postfix length. # Example: physical_interface = mix_interface # prefix = mix_interface.1 (backward compatible) # prefix = mix_iHASHED.1111 if (len(physical_interface) + len(vlan_postfix) > constants.DEVICE_NAME_MAX_LEN): physical_interface = p_utils.get_interface_name( physical_interface, max_len=(constants.DEVICE_NAME_MAX_LEN - MAX_VLAN_POSTFIX_LEN)) return "%s%s" % (physical_interface, vlan_postfix)
def get_subinterface_name(self, physical_interface, vlan_id): if not vlan_id: LOG.warning(_LW("Invalid VLAN ID, will lead to incorrect " "subinterface name")) vlan_postfix = '.%s' % vlan_id # For the vlan subinterface name prefix we use: # * the physical_interface, if len(physical_interface) + # len(vlan_postifx) <= 15 for backward compatibility reasons # Example: physical_interface = eth0 # prefix = eth0.1 # prefix = eth0.1111 # # * otherwise a unique hash per physical_interface to help debugging # Example: physical_interface = long_interface # prefix = longHASHED.1 # prefix = longHASHED.1111 # # Remark: For some physical_interface values, the used prefix can be # both, the physical_interface itself or a hash, depending # on the vlan_postfix length. # Example: physical_interface = mix_interface # prefix = mix_interface.1 (backward compatible) # prefix = mix_iHASHED.1111 if (len(physical_interface) + len(vlan_postfix) > constants.DEVICE_NAME_MAX_LEN): physical_interface = p_utils.get_interface_name( physical_interface, max_len=(constants.DEVICE_NAME_MAX_LEN - MAX_VLAN_POSTFIX_LEN)) return "%s%s" % (physical_interface, vlan_postfix)
def get_vlan_device_name(src_dev, vlan): """Generating the vlan device name.""" # Ensure that independent of the vlan len the same name prefix is used. src_dev = p_utils.get_interface_name(src_dev, max_len=n_const.DEVICE_NAME_MAX_LEN - MAX_VLAN_POSTFIX_LEN) return "%s.%s" % (src_dev, vlan)
def test_get_interface_uniqueness(self): prefix = "prefix-" if_prefix1 = utils.get_interface_name(LONG_NAME1, prefix=prefix) if_prefix2 = utils.get_interface_name(LONG_NAME2, prefix=prefix) self.assertNotEqual(if_prefix1, if_prefix2)
def test_get_interface_max_len(self, mock_sha1): self.assertTrue(len(utils.get_interface_name(LONG_NAME1)) == 15) self.assertTrue( len(utils.get_interface_name(LONG_NAME1, max_len=10)) == 10)