def test_release_fixed_ip_no_mac_support(self): # Tests that the mac kwarg is not passed when we can't send version # 1.14 to the network manager. ctxt = context.RequestContext('fake_user', 'fake_project') address = '192.168.65.158' host = 'fake-host' mac = '00:0c:29:2c:b2:64' rpcapi = network_rpcapi.NetworkAPI() cast_mock = mock.Mock() cctxt_mock = mock.Mock(cast=cast_mock) with test.nested( mock.patch.object(rpcapi.client, 'can_send_version', return_value=False), mock.patch.object(rpcapi.client, 'prepare', return_value=cctxt_mock)) as (can_send_mock, prepare_mock): rpcapi.release_fixed_ip(ctxt, address, host, mac) # assert our mocks were called as expected 232 can_send_mock.assert_called_once_with('1.14') prepare_mock.assert_called_once_with(server=host, version='1.0') cast_mock.assert_called_once_with(ctxt, 'release_fixed_ip', address=address)
def test_release_dhcp(self): ctxt = context.RequestContext('fake_user', 'fake_project') dev = 'eth0' address = '192.168.65.158' vif_address = '00:0c:29:2c:b2:64' host = 'fake-host' rpcapi = network_rpcapi.NetworkAPI() call_mock = mock.Mock() cctxt_mock = mock.Mock(call=call_mock) with test.nested( mock.patch.object(rpcapi.client, 'can_send_version', return_value=True), mock.patch.object(rpcapi.client, 'prepare', return_value=cctxt_mock) ) as ( can_send_mock, prepare_mock ): rpcapi.release_dhcp(ctxt, host, dev, address, vif_address) can_send_mock.assert_called_once_with('1.17') prepare_mock.assert_called_once_with(server=host, version='1.17') call_mock.assert_called_once_with(ctxt, 'release_dhcp', dev=dev, address=address, vif_address=vif_address)
def test_setup_networks_on_host_v1_0(self): ctxt = context.RequestContext('fake_user', 'fake_project') instance = fake_instance.fake_instance_obj(ctxt) host = 'fake_host' teardown = True rpcapi = network_rpcapi.NetworkAPI() call_mock = mock.Mock() cctxt_mock = mock.Mock(call=call_mock) with test.nested( mock.patch.object(rpcapi.client, 'can_send_version', return_value=False), mock.patch.object(rpcapi.client, 'prepare', return_value=cctxt_mock)) as (can_send_mock, prepare_mock): rpcapi.setup_networks_on_host(ctxt, instance.id, host, teardown, instance) # assert our mocks were called as expected can_send_mock.assert_called_once_with('1.16') prepare_mock.assert_called_once_with(version='1.0') call_mock.assert_called_once_with(ctxt, 'setup_networks_on_host', host=host, teardown=teardown, instance_id=instance.id)
def __init__(self, stubs=None): self.db = self.FakeDB() if stubs: stubs.Set(vif_obj, 'db', self.db) self.deallocate_called = None self.deallocate_fixed_ip_calls = [] self.network_rpcapi = network_rpcapi.NetworkAPI()
def __init__(self, **kwargs): self.network_rpcapi = network_rpcapi.NetworkAPI() helper = utils.ExceptionHelper # NOTE(vish): this local version of floating_manager has to convert # ClientExceptions back since they aren't going over rpc. self.floating_manager = helper(floating_ips.LocalManager()) super(API, self).__init__(**kwargs)
def _test_network_api(self, method, rpc_method, **kwargs): ctxt = context.RequestContext('fake_user', 'fake_project') rpcapi = network_rpcapi.NetworkAPI() expected_retval = 'foo' if method == 'call' else None expected_version = kwargs.pop('version', rpcapi.BASE_RPC_API_VERSION) expected_topic = CONF.network_topic expected_msg = rpcapi.make_msg(method, **kwargs) if 'source_compute' in expected_msg['args']: # Fix up for migrate_instance_* calls. args = expected_msg['args'] args['source'] = args.pop('source_compute') args['dest'] = args.pop('dest_compute') targeted_methods = [ 'lease_fixed_ip', 'release_fixed_ip', 'rpc_setup_network_on_host', '_rpc_allocate_fixed_ip', 'deallocate_fixed_ip', 'update_dns', '_associate_floating_ip', '_disassociate_floating_ip', 'lease_fixed_ip', 'release_fixed_ip', 'migrate_instance_start', 'migrate_instance_finish', 'allocate_for_instance', 'deallocate_for_instance', ] if method in targeted_methods and 'host' in kwargs: if method not in ['allocate_for_instance', 'deallocate_for_instance', 'deallocate_fixed_ip']: del expected_msg['args']['host'] host = kwargs['host'] if CONF.multi_host: expected_topic = rpc.queue_get_for(ctxt, CONF.network_topic, host) expected_msg['version'] = expected_version self.fake_args = None self.fake_kwargs = None def _fake_rpc_method(*args, **kwargs): self.fake_args = args self.fake_kwargs = kwargs if expected_retval: return expected_retval self.stubs.Set(rpc, rpc_method, _fake_rpc_method) retval = getattr(rpcapi, method)(ctxt, **kwargs) self.assertEqual(retval, expected_retval) expected_args = [ctxt, expected_topic, expected_msg] for arg, expected_arg in zip(self.fake_args, expected_args): try: self.assertEqual(arg, expected_arg) except AssertionError: # actual_args may contain optional args, like the one that # have default values; therefore if arg and excepted_arg # do not match verify at least that the required ones do if isinstance(arg, dict) and isinstance(expected_arg, dict): actual_args = arg.get('args') required_args = expected_arg.get('args') if actual_args and required_args: self.assertThat(required_args, matchers.IsSubDictOf(actual_args))
def _test_network_api(self, method, rpc_method, **kwargs): ctxt = context.RequestContext('fake_user', 'fake_project') rpcapi = network_rpcapi.NetworkAPI() expected_retval = 'foo' if method == 'call' else None expected_version = kwargs.pop('version', rpcapi.BASE_RPC_API_VERSION) expected_topic = CONF.network_topic expected_msg = rpcapi.make_msg(method, **kwargs) if 'source_compute' in expected_msg['args']: # Fix up for migrate_instance_* calls. args = expected_msg['args'] args['source'] = args.pop('source_compute') args['dest'] = args.pop('dest_compute') targeted_methods = [ 'lease_fixed_ip', 'release_fixed_ip', 'rpc_setup_network_on_host', '_rpc_allocate_fixed_ip', 'deallocate_fixed_ip', 'update_dns', '_associate_floating_ip', '_disassociate_floating_ip', 'lease_fixed_ip', 'release_fixed_ip', 'migrate_instance_start', 'migrate_instance_finish', 'get_backdoor_port', 'allocate_for_instance', 'deallocate_for_instance', ] if method in targeted_methods and 'host' in kwargs: if method not in [ 'allocate_for_instance', 'deallocate_for_instance', 'deallocate_fixed_ip' ]: del expected_msg['args']['host'] host = kwargs['host'] if CONF.multi_host: expected_topic = rpc.queue_get_for(ctxt, CONF.network_topic, host) expected_msg['version'] = expected_version self.fake_args = None self.fake_kwargs = None def _fake_rpc_method(*args, **kwargs): self.fake_args = args self.fake_kwargs = kwargs if expected_retval: return expected_retval self.stubs.Set(rpc, rpc_method, _fake_rpc_method) retval = getattr(rpcapi, method)(ctxt, **kwargs) self.assertEqual(retval, expected_retval) expected_args = [ctxt, expected_topic, expected_msg] for arg, expected_arg in zip(self.fake_args, expected_args): self.assertEqual(arg, expected_arg)
def add_lease(mac, ip_address): """Set the IP that was assigned by the DHCP server.""" if CONF.fake_rabbit: LOG.debug(_("leasing ip")) network_manager = importutils.import_object(CONF.network_manager) network_manager.lease_fixed_ip(context.get_admin_context(), ip_address) else: api = network_rpcapi.NetworkAPI() api.lease_fixed_ip(context.get_admin_context(), ip_address, CONF.host)
def del_lease(mac, ip_address): """Called when a lease expires.""" if CONF.fake_rabbit: LOG.debug(_("releasing ip")) network_manager = importutils.import_object(CONF.network_manager) network_manager.release_fixed_ip(context.get_admin_context(), ip_address) else: api = network_rpcapi.NetworkAPI() api.release_fixed_ip(context.get_admin_context(), ip_address, CONF.host)
def __init__(self): super(LocalManager, self).__init__() # NOTE(vish): setting the host to none ensures that the actual # l3driver commands for l3 are done via rpc. self.host = None self.servicegroup_api = servicegroup.API() self.network_rpcapi = network_rpcapi.NetworkAPI() self.floating_dns_manager = importutils.import_object( CONF.floating_ip_dns_manager) self.instance_dns_manager = importutils.import_object( CONF.instance_dns_manager)
def test_release_dhcp_v116(self): ctxt = context.RequestContext('fake_user', 'fake_project') dev = 'eth0' address = '192.168.65.158' vif_address = '00:0c:29:2c:b2:64' host = 'fake-host' rpcapi = network_rpcapi.NetworkAPI() with mock.patch.object(rpcapi.client, 'can_send_version', return_value=False) as can_send_mock: self.assertRaises(exception.RPCPinnedToOldVersion, rpcapi.release_dhcp, ctxt, host, dev, address, vif_address) can_send_mock.assert_called_once_with('1.17')
def test_set_network_host_network_object_to_primitive(self): # Tests that the network object is converted to a primitive if it # can't send version 1.15. ctxt = context.RequestContext('fake_user', 'fake_project') network = fake_network.fake_network_obj(ctxt) network_dict = objects_base.obj_to_primitive(network) rpcapi = network_rpcapi.NetworkAPI() call_mock = mock.Mock() cctxt_mock = mock.Mock(call=call_mock) with test.nested( mock.patch.object(rpcapi.client, 'can_send_version', return_value=False), mock.patch.object(rpcapi.client, 'prepare', return_value=cctxt_mock) ) as ( can_send_mock, prepare_mock ): rpcapi.set_network_host(ctxt, network) # assert our mocks were called as expected can_send_mock.assert_called_once_with('1.15') prepare_mock.assert_called_once_with(version='1.0') call_mock.assert_called_once_with(ctxt, 'set_network_host', network_ref=network_dict)
def _test_network_api(self, method, rpc_method, **kwargs): ctxt = context.RequestContext('fake_user', 'fake_project') rpcapi = network_rpcapi.NetworkAPI() self.assertIsNotNone(rpcapi.client) self.assertEqual(CONF.network_topic, rpcapi.client.target.topic) expected_retval = 'foo' if rpc_method == 'call' else None expected_version = kwargs.pop('version', None) expected_fanout = kwargs.pop('fanout', None) expected_kwargs = kwargs.copy() for k, v in expected_kwargs.items(): if isinstance(v, self.DefaultArg): expected_kwargs[k] = v.value kwargs.pop(k) prepare_kwargs = {} if expected_version: prepare_kwargs['version'] = expected_version if expected_fanout: prepare_kwargs['fanout'] = True if 'source_compute' in expected_kwargs: # Fix up for migrate_instance_* calls. expected_kwargs['source'] = expected_kwargs.pop('source_compute') expected_kwargs['dest'] = expected_kwargs.pop('dest_compute') targeted_methods = [ 'lease_fixed_ip', 'release_fixed_ip', 'rpc_setup_network_on_host', '_rpc_allocate_fixed_ip', 'deallocate_fixed_ip', 'update_dns', '_associate_floating_ip', '_disassociate_floating_ip', 'lease_fixed_ip', 'release_fixed_ip', 'migrate_instance_start', 'migrate_instance_finish', 'allocate_for_instance', 'deallocate_for_instance', ] targeted_by_instance = ['deallocate_for_instance'] if method in targeted_methods and ('host' in expected_kwargs or 'instance' in expected_kwargs): if method in targeted_by_instance: host = expected_kwargs['instance']['host'] else: host = expected_kwargs['host'] if method not in [ 'allocate_for_instance', 'deallocate_fixed_ip' ]: expected_kwargs.pop('host') if CONF.multi_host: prepare_kwargs['server'] = host with test.nested( mock.patch.object(rpcapi.client, rpc_method), mock.patch.object(rpcapi.client, 'prepare'), mock.patch.object(rpcapi.client, 'can_send_version'), ) as (rpc_mock, prepare_mock, csv_mock): version_check = [ 'deallocate_for_instance', 'deallocate_fixed_ip', 'allocate_for_instance', 'release_fixed_ip', 'set_network_host', 'setup_networks_on_host' ] if method in version_check: csv_mock.return_value = True if prepare_kwargs: prepare_mock.return_value = rpcapi.client if rpc_method == 'call': rpc_mock.return_value = 'foo' else: rpc_mock.return_value = None retval = getattr(rpcapi, method)(ctxt, **kwargs) self.assertEqual(expected_retval, retval) if method in version_check: csv_mock.assert_called_once_with(mock.ANY) if prepare_kwargs: prepare_mock.assert_called_once_with(**prepare_kwargs) rpc_mock.assert_called_once_with(ctxt, method, **expected_kwargs)
def del_lease(mac, ip_address): """Called when a lease expires.""" api = network_rpcapi.NetworkAPI() api.release_fixed_ip(context.get_admin_context(), ip_address, CONF.host)
def add_lease(mac, ip_address): """Set the IP that was assigned by the DHCP server.""" api = network_rpcapi.NetworkAPI() api.lease_fixed_ip(context.get_admin_context(), ip_address, CONF.host)
def __init__(self, **kwargs): self.network_rpcapi = network_rpcapi.NetworkAPI() super(API, self).__init__(**kwargs)
def __init__(self): self.db = self.FakeDB() self.deallocate_called = None self.deallocate_fixed_ip_calls = [] self.network_rpcapi = network_rpcapi.NetworkAPI()
def _test_network_api(self, method, rpc_method, **kwargs): ctxt = context.RequestContext('fake_user', 'fake_project') rpcapi = network_rpcapi.NetworkAPI() self.assertIsNotNone(rpcapi.client) self.assertEqual(rpcapi.client.target.topic, CONF.network_topic) expected_retval = 'foo' if rpc_method == 'call' else None expected_version = kwargs.pop('version', None) expected_fanout = kwargs.pop('fanout', None) expected_kwargs = kwargs.copy() for k, v in expected_kwargs.items(): if isinstance(v, self.DefaultArg): expected_kwargs[k] = v.value kwargs.pop(k) prepare_kwargs = {} if expected_version: prepare_kwargs['version'] = expected_version if expected_fanout: prepare_kwargs['fanout'] = True if 'source_compute' in expected_kwargs: # Fix up for migrate_instance_* calls. expected_kwargs['source'] = expected_kwargs.pop('source_compute') expected_kwargs['dest'] = expected_kwargs.pop('dest_compute') targeted_methods = [ 'lease_fixed_ip', 'release_fixed_ip', 'rpc_setup_network_on_host', '_rpc_allocate_fixed_ip', 'deallocate_fixed_ip', 'update_dns', '_associate_floating_ip', '_disassociate_floating_ip', 'lease_fixed_ip', 'release_fixed_ip', 'migrate_instance_start', 'migrate_instance_finish', 'allocate_for_instance', 'deallocate_for_instance', ] targeted_by_instance = ['deallocate_for_instance'] if method in targeted_methods and ('host' in expected_kwargs or 'instance' in expected_kwargs): if method in targeted_by_instance: host = expected_kwargs['instance']['host'] else: host = expected_kwargs['host'] if method not in ['allocate_for_instance', 'deallocate_fixed_ip']: expected_kwargs.pop('host') if CONF.multi_host: prepare_kwargs['server'] = host self.mox.StubOutWithMock(rpcapi, 'client') version_check = [ 'deallocate_for_instance', 'deallocate_fixed_ip', 'allocate_for_instance', ] if method in version_check: rpcapi.client.can_send_version(mox.IgnoreArg()).AndReturn(True) if prepare_kwargs: rpcapi.client.prepare(**prepare_kwargs).AndReturn(rpcapi.client) rpc_method = getattr(rpcapi.client, rpc_method) rpc_method(ctxt, method, **expected_kwargs).AndReturn('foo') self.mox.ReplayAll() retval = getattr(rpcapi, method)(ctxt, **kwargs) self.assertEqual(retval, expected_retval)