def resolve_ports(self, ports): """Resolve NICs not yet bound to bridge(s) If hwaddress provided then returns resolved hwaddress otherwise NIC. """ if not ports: return None hwaddr_to_nic = {} hwaddr_to_ip = {} for nic in list_nics(): # Ignore virtual interfaces (bond masters will be identified from # their slaves) if not is_phy_iface(nic): continue _nic = get_bond_master(nic) if _nic: log("Replacing iface '%s' with bond master '%s'" % (nic, _nic), level=DEBUG) nic = _nic hwaddr = get_nic_hwaddr(nic) hwaddr_to_nic[hwaddr] = nic addresses = get_ipv4_addr(nic, fatal=False) addresses += get_ipv6_addr(iface=nic, fatal=False) hwaddr_to_ip[hwaddr] = addresses resolved = [] mac_regex = re.compile(r'([0-9A-F]{2}[:-]){5}([0-9A-F]{2})', re.I) for entry in ports: if re.match(mac_regex, entry): # NIC is in known NICs and does NOT hace an IP address if entry in hwaddr_to_nic and not hwaddr_to_ip[entry]: # If the nic is part of a bridge then don't use it if is_bridge_member(hwaddr_to_nic[entry]): continue # Entry is a MAC address for a valid interface that doesn't # have an IP address assigned yet. resolved.append(hwaddr_to_nic[entry]) else: # If the passed entry is not a MAC address, assume it's a valid # interface, and that the user put it there on purpose (we can # trust it to be the real external network). resolved.append(entry) # Ensure no duplicates return list(set(resolved))
def test_get_get_iface_addr_interface_has_no_ipv4(self, _interfaces, _ifaddresses): # This will raise a KeyError since we are looking for "2" # (actally, netiface.AF_INET). DUMMY_ADDRESSES = { 'eth0': { 10: [{'addr': 'fe80::3e97:eff:fe8b:1cf7%eth0', 'netmask': 'ffff:ffff:ffff:ffff::'}], } } _interfaces.return_value = DUMMY_ADDRESSES.keys() _ifaddresses.side_effect = DUMMY_ADDRESSES.__getitem__ result = net_ip.get_ipv4_addr("eth0", fatal=False) self.assertEqual([], result)
def resolve_ports(self, ports): """Resolve NICs not yet bound to bridge(s) If hwaddress provided then returns resolved hwaddress otherwise NIC. """ if not ports: return None hwaddr_to_nic = {} hwaddr_to_ip = {} for nic in list_nics(self.NIC_PREFIXES): hwaddr = get_nic_hwaddr(nic) hwaddr_to_nic[hwaddr] = nic addresses = get_ipv4_addr(nic, fatal=False) addresses += get_ipv6_addr(iface=nic, fatal=False) hwaddr_to_ip[hwaddr] = addresses resolved = [] mac_regex = re.compile(r'([0-9A-F]{2}[:-]){5}([0-9A-F]{2})', re.I) for entry in ports: if re.match(mac_regex, entry): # NIC is in known NICs and does NOT hace an IP address if entry in hwaddr_to_nic and not hwaddr_to_ip[entry]: # If the nic is part of a bridge then don't use it if is_bridge_member(hwaddr_to_nic[entry]): continue # Entry is a MAC address for a valid interface that doesn't # have an IP address assigned yet. resolved.append(hwaddr_to_nic[entry]) else: # If the passed entry is not a MAC address, assume it's a valid # interface, and that the user put it there on purpose (we can # trust it to be the real external network). resolved.append(entry) return resolved
def test_get_iface_addr_invalid_interface_fatal_incaliases( self, _interfaces): _interfaces.return_value = DUMMY_ADDRESSES.keys() with nose.tools.assert_raises(Exception): net_ip.get_ipv4_addr("eth3", fatal=True, inc_aliases=True)
def test_get_iface_addr_invalid_interface(self, _interfaces, _ifaddresses): _interfaces.return_value = DUMMY_ADDRESSES.keys() result = net_ip.get_ipv4_addr("eth3", fatal=False) self.assertEqual([], result)
def test_get_iface_addr_invalid_interface_fatal(self, _interfaces): _interfaces.return_value = DUMMY_ADDRESSES.keys() with self.assertRaises(Exception): net_ip.get_ipv4_addr("eth3", fatal=True)