def testFakeNics(self): with MonkeyPatchScope([(ipwrapper.Link, '_fakeNics', ['veth_*', 'dummy_*'])]): with veth.pair() as (v1a, v1b): with dummy.device() as d1: fakes = set([d1, v1a, v1b]) nics = netinfo.nics() self.assertTrue(fakes.issubset(nics), 'Fake devices %s are' ' not listed in nics %s' % (fakes, nics)) with veth.pair(prefix='mehv_') as (v2a, v2b): with dummy.device(prefix='mehd_') as d2: hiddens = set([d2, v2a, v2b]) nics = netinfo.nics() self.assertFalse(hiddens.intersection(nics), 'Some of ' 'hidden devices %s is shown in nics %s' % (hiddens, nics))
def testHostSampleReportsNewInterface(self): interfaces_before = set( sampling._get_interfaces_and_samples().iterkeys()) with dummy.device() as dummy_name: interfaces_after = set( sampling._get_interfaces_and_samples().iterkeys()) interfaces_diff = interfaces_after - interfaces_before self.assertEqual(interfaces_diff, {dummy_name})
def testFakeNics(self): with MonkeyPatchScope([(ipwrapper.Link, '_fakeNics', ['veth_*', 'dummy_*'])]): with veth.pair() as (v1a, v1b): with dummy.device() as d1: fakes = set([d1, v1a, v1b]) nics = netinfo.nics() self.assertTrue( fakes.issubset(nics), 'Fake devices %s are' ' not listed in nics %s' % (fakes, nics)) with veth.pair(prefix='mehv_') as (v2a, v2b): with dummy.device(prefix='mehd_') as d2: hiddens = set([d2, v2a, v2b]) nics = netinfo.nics() self.assertFalse( hiddens.intersection(nics), 'Some of ' 'hidden devices %s is shown in nics %s' % (hiddens, nics))
def testHostSampleHandlesDisappearingVlanInterfaces(self): original_getLinks = ipwrapper.getLinks def faultyGetLinks(): all_links = list(original_getLinks()) ipwrapper.linkDel(self.NEW_VLAN) return iter(all_links) with MonkeyPatchScope([(ipwrapper, 'getLinks', faultyGetLinks)]): with dummy.device() as dummy_name, vlan( self.NEW_VLAN, dummy_name, 999): interfaces_and_samples = sampling._get_interfaces_and_samples() self.assertNotIn(self.NEW_VLAN, interfaces_and_samples)
def testHostSampleHandlesDisappearingVlanInterfaces(self): original_getLinks = ipwrapper.getLinks def faultyGetLinks(): all_links = list(original_getLinks()) ipwrapper.linkDel(self.NEW_VLAN) return iter(all_links) with MonkeyPatchScope([(ipwrapper, 'getLinks', faultyGetLinks)]): with dummy.device() as dummy_name, vlan(self.NEW_VLAN, dummy_name, 999): interfaces_and_samples = sampling._get_interfaces_and_samples() self.assertNotIn(self.NEW_VLAN, interfaces_and_samples)
def test_ip_info(self): def get_ip_info(*a, **kw): """filter away ipv6 link local addresses that may or may not exist on the device depending on OS configuration""" ipv4addr, ipv4netmask, ipv4addrs, ipv6addrs = \ netinfo.getIpInfo(*a, **kw) ipv6addrs = [ addr for addr in ipv6addrs if not netaddr.IPAddress(addr.split('/')[0]).is_link_local()] return ipv4addr, ipv4netmask, ipv4addrs, ipv6addrs IP_ADDR = '192.0.2.2' IP_ADDR_SECOND = '192.0.2.3' IP_ADDR_GW = '192.0.2.1' IP_ADDR2 = '198.51.100.9' IP_ADDR3 = '198.51.100.11' IP_ADDR2_GW = '198.51.100.1' IPV6_ADDR = '2607:f0d0:1002:51::4' NET_MASK = '255.255.255.0' PREFIX_LENGTH = 24 IPV6_PREFIX_LENGTH = 64 IP_ADDR_CIDR = self._cidr_form(IP_ADDR, PREFIX_LENGTH) IP_ADDR_SECOND_CIDR = self._cidr_form(IP_ADDR_SECOND, PREFIX_LENGTH) IP_ADDR2_CIDR = self._cidr_form(IP_ADDR2, PREFIX_LENGTH) IP_ADDR3_CIDR = self._cidr_form(IP_ADDR3, 32) IPV6_ADDR_CIDR = self._cidr_form(IPV6_ADDR, IPV6_PREFIX_LENGTH) with dummy.device() as device: ipwrapper.addrAdd(device, IP_ADDR, PREFIX_LENGTH) ipwrapper.addrAdd(device, IP_ADDR_SECOND, PREFIX_LENGTH) ipwrapper.addrAdd(device, IP_ADDR2, PREFIX_LENGTH) ipwrapper.addrAdd(device, IPV6_ADDR, IPV6_PREFIX_LENGTH, family=6) # 32 bit addresses are reported slashless by netlink ipwrapper.addrAdd(device, IP_ADDR3, 32) self.assertEqual( get_ip_info(device), (IP_ADDR, NET_MASK, [IP_ADDR_CIDR, IP_ADDR2_CIDR, IP_ADDR3_CIDR, IP_ADDR_SECOND_CIDR], [IPV6_ADDR_CIDR])) self.assertEqual( get_ip_info(device, ipv4_gateway=IP_ADDR_GW), (IP_ADDR, NET_MASK, [IP_ADDR_CIDR, IP_ADDR2_CIDR, IP_ADDR3_CIDR, IP_ADDR_SECOND_CIDR], [IPV6_ADDR_CIDR])) self.assertEqual( get_ip_info(device, ipv4_gateway=IP_ADDR2_GW), (IP_ADDR2, NET_MASK, [IP_ADDR_CIDR, IP_ADDR2_CIDR, IP_ADDR3_CIDR, IP_ADDR_SECOND_CIDR], [IPV6_ADDR_CIDR]))