def test_ensure_mapping(self): attr = 'get_interfaces' with mock.patch.object(ip.IPManager, attr) as get_ifaces: mgr = ip.IPManager() mgr.ensure_mapping() get_ifaces.assert_called_once_with()
def test_address_add(self): cmd = 'ip' v4 = netaddr.IPNetwork('192.168.105.2/24') v6 = netaddr.IPNetwork('fdca:3ba5:a17a:acda:20c:29ff:fe94:723d/64') iface = mock.Mock(all_addresses=[v4, v6], ifname='em0') old_iface = mock.Mock(all_addresses=[], ifname='em0') mgr = ip.IPManager() with mock.patch.object(mgr, 'generic_to_host', lambda x: x.replace('ge', 'em')): mgr._update_addresses('em0', iface, old_iface) assert self.mock_execute.call_args_list == [ mock.call([ cmd, 'addr', 'add', '192.168.105.2/24', 'brd', '+', 'dev', 'em0' ], 'sudo astara-rootwrap /etc/rootwrap.conf'), mock.call([cmd, 'link', 'set', 'em0', 'up'], 'sudo astara-rootwrap /etc/rootwrap.conf'), mock.call([cmd, 'addr', 'show', 'em0']), mock.call([ cmd, '-6', 'addr', 'add', 'fdca:3ba5:a17a:acda:20c:29ff:fe94:723d/64', 'dev', 'em0' ], 'sudo astara-rootwrap /etc/rootwrap.conf'), mock.call([cmd, 'link', 'set', 'em0', 'up'], 'sudo astara-rootwrap /etc/rootwrap.conf'), mock.call([cmd, 'addr', 'show', 'em0']) ]
def ip_mgr(self): ip_mgr = ip.IPManager() ip_mgr.ensure_mapping() if not self._config: # we do not yet have config, so use standard ip manager for # ensuring initial intrefaces return ip_mgr if self._config and self._config.ha: if not self._vrrp_ip_mgr: self._vrrp_ip_mgr = ip.VRRPIPManager() self._reload_callbacks.append(self._vrrp_ip_mgr.reload) # peers and prio can change and be updated via config, need to # ensure the vrrp manager is up to date every access. self._vrrp_ip_mgr.set_peers(self._config.ha_config.get( 'peers', [])) self._vrrp_ip_mgr.set_priority( self._config.ha_config.get('priority', 0)) return self._vrrp_ip_mgr else: # we may not yet have config, so use standard ip manager for # ensuring initial interfaces return ip_mgr
def configure_management(): parser = argparse.ArgumentParser( description='Configure Management Interface' ) parser.add_argument('mac_address', metavar='lladdr', type=str) parser.add_argument('ip_address', metavar='ipaddr', type=str) parser.add_argument('--mtu', metavar='mtu', type=int, default=1280) args = parser.parse_args() ip_addr = netaddr.IPNetwork(args.ip_address) mgr = ip.IPManager() for intf in mgr.get_interfaces(): if args.mac_address == intf.lladdr: if not intf.is_up: mgr.up(intf) intf.mtu = args.mtu if ip_addr not in intf.addresses: if ip_addr.version == 6: real_ifname = mgr.generic_to_host(intf.ifname) utils.execute([ 'sysctl', '-w', 'net.ipv6.conf.%s.accept_dad=0' % real_ifname ]) intf.addresses.append(ip_addr) mgr.update_interface(intf) configure_ssh(ip_addr.ip) configure_gunicorn(ip_addr.ip) break
def test_dad_for_external(self): mgr = ip.IPManager() with mock.patch.object(mgr, 'generic_to_host', lambda x: x): mgr.disable_duplicate_address_detection( models.Network('ABC123', models.Interface('eth1'), network_type=models.Network.TYPE_EXTERNAL)) assert self.mock_execute.call_count == 0
def test_update_interfaces(self): iface_a = mock.Mock() iface_a.mtu = 1500 iface_b = mock.Mock() iface_b.mtu = 1500 attr = 'update_interface' with mock.patch.object(ip.IPManager, attr) as update: mgr = ip.IPManager() mgr.update_interfaces([iface_a, iface_b]) update.assert_has_calls([mock.call(iface_a), mock.call(iface_b)])
def test_dad_for_internal(self): mgr = ip.IPManager() with mock.patch.object(mgr, 'generic_to_host', lambda x: x): mgr.disable_duplicate_address_detection( models.Network('ABC123', models.Interface('eth2'), network_type=models.Network.TYPE_INTERNAL)) assert self.mock_execute.call_count == 1 assert self.mock_execute.call_args_list == [ mock.call(['sysctl', '-w', 'net.ipv6.conf.eth2.accept_dad=0'], 'sudo astara-rootwrap /etc/rootwrap.conf'), ]
def test_update_set_no_diff(self): a = netaddr.IPNetwork('192.168.101.2/24') b = netaddr.IPNetwork('192.168.102.2/24') iface = mock.Mock() iface.all_addresses = [a, b] old_iface = mock.Mock() old_iface.all_addresses = [a, b] mgr = ip.IPManager() mgr._update_set('em0', iface, old_iface, 'all_addresses', lambda g: ('em0', 'add', g), lambda g: ('em0', 'del', g)) self.assertEqual(self.mock_execute.call_count, 0)
def __init__(self, state_path='.'): self.state_path = os.path.abspath(state_path) self.ip_mgr = ip.IPManager() self.ip_mgr.ensure_mapping() # Holds the common system config self._system_config = models.SystemConfiguration() # Holds config models for various services (router, loadbalancer) self._service_configs = [] self._service_managers = {'system': SystemManager()} self._load_managers()
def test_up(self): iface = mock.Mock() iface.ifname = 'ge0' attr = 'ensure_mapping' with mock.patch.object(ip.IPManager, attr): mgr = ip.IPManager() mgr.host_mapping = {'em0': 'ge0'} mgr.generic_mapping = {'ge0': 'em0'} mgr.up(iface) self.mock_execute.assert_has_calls([ mock.call(['ip', 'link', 'set', 'em0', 'up'], 'sudo astara-rootwrap /etc/rootwrap.conf') ])
def test_get_interfaces(self): iface_a = mock.Mock() iface_a.ifname = 'em0' iface_a.mtu = 1500 iface_b = mock.Mock() iface_b.ifname = 'em1' ifaces = 'astara_router.drivers.ip._parse_interfaces' with mock.patch(ifaces) as parse: parse.return_value = [iface_a, iface_b] mgr = ip.IPManager() interfaces = mgr.get_interfaces() self.assertEqual(interfaces, [iface_a, iface_b]) self.mock_execute.assert_has_calls([mock.call(['ip', 'addr', 'show'])])
def test_update_addresses(self): iface = mock.Mock() old_iface = mock.Mock() with mock.patch.object(ip.IPManager, '_update_set') as us: mgr = ip.IPManager() mgr._update_addresses('em0', iface, old_iface) us.assert_called_once_with( 'em0', iface, old_iface, 'all_addresses', mock.ANY, mock.ANY, )
def test_get_interface(self): iface_a = mock.Mock() iface_a.ifname = 'em0' iface_a.mtu = 1500 iface = 'astara_router.drivers.ip._parse_interface' ifaces = 'astara_router.drivers.ip._parse_interfaces' with mock.patch(iface) as parse: with mock.patch(ifaces) as pi: pi.return_value = [iface_a] parse.return_value = iface_a mgr = ip.IPManager() interface = mgr.get_interface('ge0') self.assertEqual(interface, iface_a) self.assertEqual(iface_a.ifname, 'ge0') self.mock_execute.assert_has_calls([mock.call(['ip', 'addr', 'show'])])
def test_sysctl_failure(self): logger = ip.LOG logger.level = logging.DEBUG buff = StringIO() handler = logging.StreamHandler(buff) self.mock_execute.side_effect = RuntimeError mgr = ip.IPManager() with mock.patch.object(mgr, 'generic_to_host', lambda x: x): try: logger.addHandler(handler) mgr.disable_duplicate_address_detection( models.Network( 'ABC123', models.Interface('eth0'), network_type=models.Network.TYPE_MANAGEMENT)) assert 'Failed to disable v6 dad on eth0' in buff.getvalue() finally: logger.removeHandler(handler)
def _update_interface_test_hlpr(self, new_iface, old_iface, ignore_link_local=True): mock_methods = { 'generic_to_host': mock.Mock(return_value='em0'), 'get_interface': mock.Mock(return_value=old_iface), '_update_addresses': mock.Mock() } with mock.patch.multiple(ip.IPManager, **mock_methods): mgr = ip.IPManager() mgr.update_interface( new_iface, ignore_link_local=ignore_link_local, ) mock_methods['generic_to_host'].assert_called_once_with('ge0') mock_methods['get_interface'].assert_called_once_with('ge0') mock_methods['_update_addresses'].assert_called_once_with( 'em0', new_iface, old_iface)
def test_address_remove(self): cmd = 'ip' v4 = netaddr.IPNetwork('192.168.105.2/24') v6 = netaddr.IPNetwork('fdca:3ba5:a17a:acda:20c:29ff:fe94:723d/64') iface = mock.Mock(all_addresses=[]) old_iface = mock.Mock(all_addresses=[v4, v6]) mgr = ip.IPManager() mgr._update_addresses('em0', iface, old_iface) assert self.mock_execute.call_args_list == [ mock.call( [cmd, 'addr', 'del', str(v4), 'dev', 'em0'], 'sudo astara-rootwrap /etc/rootwrap.conf'), mock.call( ['conntrack', '-D', '-d', str(v4.ip)], 'sudo astara-rootwrap /etc/rootwrap.conf'), mock.call( ['conntrack', '-D', '-q', str(v4.ip)], 'sudo astara-rootwrap /etc/rootwrap.conf'), mock.call([cmd, '-6', 'addr', 'del', str(v6), 'dev', 'em0'], 'sudo astara-rootwrap /etc/rootwrap.conf'), ]
def test_update_set(self): iface = mock.Mock() a = netaddr.IPNetwork('192.168.101.2/24') b = netaddr.IPNetwork('192.168.102.2/24') c = netaddr.IPNetwork('192.168.103.2/24') iface.all_addresses = [a, b] iface.ifname = 'em0' old_iface = mock.Mock() old_iface.all_addresses = [b, c] old_iface.ifname = 'em0' mgr = ip.IPManager() with mock.patch.object(mgr, 'generic_to_host', lambda x: x.replace('ge', 'em')): mgr._update_set( 'em0', iface, old_iface, 'all_addresses', lambda g: ('addr', 'add', '/'.join(map(str, g)), 'dev', 'em0'), lambda g: ('addr', 'del', '/'.join(map(str, g)), 'dev', 'em0')) assert self.mock_execute.call_args_list == [ mock.call(['ip', 'addr', 'add', str(a), 'dev', 'em0'], 'sudo astara-rootwrap /etc/rootwrap.conf'), mock.call(['ip', 'link', 'set', 'em0', 'up'], 'sudo astara-rootwrap /etc/rootwrap.conf'), mock.call(['ip', 'addr', 'show', 'em0']), mock.call(['ip', 'addr', 'del', str(c), 'dev', 'em0'], 'sudo astara-rootwrap /etc/rootwrap.conf'), mock.call(['conntrack', '-D', '-d', str(c.ip)], 'sudo astara-rootwrap /etc/rootwrap.conf'), mock.call(['conntrack', '-D', '-q', str(c.ip)], 'sudo astara-rootwrap /etc/rootwrap.conf'), ]
def test_is_valid(self, mock_ensure): mgr = ip.IPManager() mgr.host_mapping = {'em0': 'ge0'} mgr.generic_mapping = {'ge0': 'em0'} self.assertTrue(mgr.is_valid('ge0'))
def setUp(self): super(RouteTest, self).setUp() self.mgr = ip.IPManager() self.host_patch = mock.patch.object(self.mgr, 'generic_to_host', lambda x: x.replace('ge', 'eth')) self.host_patch.start()
def test_host_to_generic(self, ensure_mapping): mgr = ip.IPManager() mgr.host_mapping = {'em0': 'ge0'} mgr.generic_mapping = {'ge0': 'em0'} self.assertEqual(mgr.host_to_generic('em0'), 'ge0') self.assertIsNone(mgr.host_to_generic('em1'))
def test_generic_to_host(self, mock_ensure): mgr = ip.IPManager() mgr.host_mapping = {'em0': 'ge0'} mgr.generic_mapping = {'ge0': 'em0'} self.assertEqual(mgr.generic_to_host('ge0'), 'em0') self.assertIsNone(mgr.generic_to_host('ge1'))
def test_init(self): mgr = ip.IPManager() self.assertEqual(mgr.host_mapping.keys(), [])