Truncate IPDevice's name to interface max size

An interface's name is limited to INTERFACE_MAX_SIZE. In some cases, IP
utils truncate this name automatically. In Ubuntu Xenial, ip link
returns an error instead of this behaviour.

This change truncates the device's name upon read (The full name is
still stored on the object). This way, the code does not rely on IP
utils to do the truncation. This solves the error in the test.

Change-Id: I41b226e4b52239b861e2b6e806296025502b9b60
Closes-Bug: 1637993
This commit is contained in:
Omer Anson 2016-10-31 08:52:43 +02:00
parent 3c7c8ee67a
commit 0140a183dc
1 changed files with 12 additions and 3 deletions

View File

@ -258,7 +258,7 @@ class IPWrapper(SubProcessBase):
class IPDevice(SubProcessBase):
def __init__(self, name, namespace=None):
super(IPDevice, self).__init__(namespace=namespace)
self.name = name
self._name = name
self.link = IpLinkCommand(self)
self.addr = IpAddrCommand(self)
self.route = IpRouteCommand(self)
@ -350,6 +350,16 @@ class IPDevice(SubProcessBase):
cmd = 'net.ipv6.conf.%s.disable_ipv6=1' % sysctl_name
return self._sysctl([cmd])
@property
def name(self):
if self._name:
return self._name[:constants.DEVICE_NAME_MAX_LEN]
return self._name
@name.setter
def name(self, name):
self._name = name
class IpCommandBase(object):
COMMAND = ''
@ -938,8 +948,7 @@ def device_exists_with_ips_and_mac(device_name, ip_cidrs, mac, namespace=None):
def get_device_mac(device_name, namespace=None):
"""Return the MAC address of the device."""
dev = device_name[:constants.DEVICE_NAME_MAX_LEN]
return IPDevice(dev, namespace=namespace).link.address
return IPDevice(device_name, namespace=namespace).link.address
def get_routing_table(ip_version, namespace=None):