Ejemplo n.º 1
0
    def _test_ovs_api(self, rpcapi, topic, method, rpc_method, **kwargs):
        ctxt = context.RequestContext('fake_user', 'fake_project')
        expected_retval = 'foo' if method == 'call' else None
        expected_kwargs = {}
        if topic:
            expected_kwargs['topic'] = topic
        if 'version' in kwargs:
            expected_kwargs['version'] = kwargs.pop('version')
        expected_msg = rpcapi.make_msg(method, **kwargs)
        if rpc_method == 'cast' and method == 'run_instance':
            kwargs['call'] = False

        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.useFixture(
            fixtures.MonkeyPatch('neutron.common.rpc.RpcProxy.' + rpc_method,
                                 _fake_rpc_method))

        retval = getattr(rpcapi, method)(ctxt, **kwargs)

        self.assertEqual(retval, expected_retval)
        expected_args = [ctxt, expected_msg]

        # skip the first argument which is 'self'
        for arg, expected_arg in zip(self.fake_args[1:], expected_args):
            self.assertEqual(arg, expected_arg)
        self.assertEqual(expected_kwargs, self.fake_kwargs)
Ejemplo n.º 2
0
    def _test_mlnx_api(self, rpcapi, topic, method, rpc_method,
                       expected_msg=None, **kwargs):
        ctxt = context.RequestContext('fake_user', 'fake_project')
        expected_retval = 'foo' if method == 'call' else None
        if not expected_msg:
            expected_msg = rpcapi.make_msg(method, **kwargs)
        expected_msg['version'] = rpcapi.BASE_RPC_API_VERSION
        if rpc_method == 'cast' and method == 'run_instance':
            kwargs['call'] = False

        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.useFixture(fixtures.MonkeyPatch(
            'neutron.openstack.common.rpc.' + rpc_method, _fake_rpc_method))

        retval = getattr(rpcapi, method)(ctxt, **kwargs)

        self.assertEqual(expected_retval, retval)
        expected_args = [ctxt, topic, expected_msg]

        for arg, expected_arg in zip(self.fake_args, expected_args):
            self.assertEqual(expected_arg, arg)
Ejemplo n.º 3
0
 def _setup_rpc(self):
     # RPC support
     self.service_topics = {
         svc_constants.CORE: topics.PLUGIN,
         svc_constants.L3_ROUTER_NAT: topics.L3PLUGIN
     }
     self.rpc_context = context.RequestContext('neutron',
                                               'neutron',
                                               is_admin=False)
     self.conn = n_rpc.create_connection(new=True)
     self.endpoints = [
         BridgeRpcCallbacks(),
         securitygroups_rpc.SecurityGroupServerRpcCallback(),
         dhcp_rpc.DhcpRpcCallback(),
         l3_rpc.L3RpcCallback(),
         agents_db.AgentExtRpcCallback(),
         metadata_rpc.MetadataRpcCallback()
     ]
     for svc_topic in self.service_topics.values():
         self.conn.create_consumer(svc_topic, self.endpoints, fanout=False)
     # Consume from all consumers in threads
     self.conn.consume_in_threads()
     self.notifier = AgentNotifierApi(topics.AGENT)
     self.agent_notifiers[q_const.AGENT_TYPE_DHCP] = (
         dhcp_rpc_agent_api.DhcpAgentNotifyAPI())
     self.agent_notifiers[q_const.AGENT_TYPE_L3] = (
         l3_rpc_agent_api.L3AgentNotifyAPI())
Ejemplo n.º 4
0
    def _test_mlnx_api(self, rpcapi, topic, method, rpc_method, **kwargs):
        ctxt = context.RequestContext('fake_user', 'fake_project')
        expected_retval = 'foo' if rpc_method == 'call' else None
        expected_version = kwargs.pop('version', None)
        fanout = kwargs.pop('fanout', False)

        with contextlib.nested(
                mock.patch.object(rpcapi.client, rpc_method),
                mock.patch.object(rpcapi.client, 'prepare'),
        ) as (rpc_mock, prepare_mock):
            prepare_mock.return_value = rpcapi.client
            rpc_mock.return_value = expected_retval
            retval = getattr(rpcapi, method)(ctxt, **kwargs)

        prepare_args = {}
        if expected_version:
            prepare_args['version'] = expected_version
        if fanout:
            prepare_args['fanout'] = True
        if topic:
            prepare_args['topic'] = topic
        prepare_mock.assert_called_once_with(**prepare_args)

        if method == 'port_update':
            kwargs['segmentation_id'] = kwargs['vlan_id']
            if not cfg.CONF.AGENT.rpc_support_old_agents:
                del kwargs['vlan_id']

        self.assertEqual(retval, expected_retval)
        rpc_mock.assert_called_once_with(ctxt, method, **kwargs)
Ejemplo n.º 5
0
    def _test_lb_api(self,
                     rpcapi,
                     topic,
                     method,
                     rpc_method,
                     expected_msg=None,
                     **kwargs):
        ctxt = context.RequestContext('fake_user', 'fake_project')
        expected_retval = 'foo' if method == 'call' else None
        if not expected_msg:
            expected_msg = rpcapi.make_msg(method, **kwargs)
        expected_msg['version'] = rpcapi.BASE_RPC_API_VERSION
        if rpc_method == 'cast' and method == 'run_instance':
            kwargs['call'] = False

        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 = stubout.StubOutForTesting()
        self.stubs.Set(rpc, rpc_method, _fake_rpc_method)

        retval = getattr(rpcapi, method)(ctxt, **kwargs)

        self.assertEqual(expected_retval, retval)
        expected_args = [ctxt, topic, expected_msg]

        for arg, expected_arg in zip(self.fake_args, expected_args):
            self.assertEqual(expected_arg, arg)
Ejemplo n.º 6
0
    def _test_hyperv_neutron_api(
            self, rpcapi, topic, method, rpc_method, **kwargs):
        ctxt = context.RequestContext('fake_user', 'fake_project')
        expected_retval = 'foo' if rpc_method == 'call' else None
        expected_version = kwargs.pop('version', None)
        fanout = kwargs.pop('fanout', False)

        with contextlib.nested(
            mock.patch.object(rpcapi.client, rpc_method),
            mock.patch.object(rpcapi.client, 'prepare'),
        ) as (
            rpc_mock, prepare_mock
        ):
            prepare_mock.return_value = rpcapi.client
            rpc_mock.return_value = expected_retval
            retval = getattr(rpcapi, method)(ctxt, **kwargs)

        self.assertEqual(retval, expected_retval)

        prepare_args = {}
        if expected_version:
            prepare_args['version'] = expected_version
        if fanout:
            prepare_args['fanout'] = True
        if topic:
            prepare_args['topic'] = topic
        prepare_mock.assert_called_once_with(**prepare_args)

        rpc_mock.assert_called_once_with(ctxt, method, **kwargs)
Ejemplo n.º 7
0
    def _test_hyperv_neutron_api(self, rpcapi, topic, method, rpc_method,
                                 **kwargs):
        ctxt = context.RequestContext('fake_user', 'fake_project')
        expected_retval = 'foo' if method == 'call' else None
        expected_version = kwargs.pop('version', None)
        expected_msg = rpcapi.make_msg(method, **kwargs)
        if rpc_method == 'cast' and method == 'run_instance':
            kwargs['call'] = False

        proxy = n_rpc.RpcProxy
        with mock.patch.object(proxy, rpc_method) as rpc_method_mock:
            rpc_method_mock.return_value = expected_retval
            retval = getattr(rpcapi, method)(ctxt, **kwargs)

        self.assertEqual(retval, expected_retval)
        if expected_version:
            expected = [
                mock.call(ctxt,
                          expected_msg,
                          topic=topic,
                          version=expected_version)
            ]
        else:
            expected = [mock.call(ctxt, expected_msg, topic=topic)]
        rpc_method_mock.assert_has_calls(expected)
Ejemplo n.º 8
0
 def test_devices_details_list_unsupported(self):
     agent = rpc.PluginApi('fake_topic')
     ctxt = context.RequestContext('fake_user', 'fake_project')
     expect_val_get_device_details = 'foo'
     expect_val = [expect_val_get_device_details]
     with mock.patch('neutron.common.rpc.RpcProxy.call') as rpc_call:
         rpc_call.side_effect = [messaging.UnsupportedVersion('1.2'),
                                 expect_val_get_device_details]
         func_obj = getattr(agent, 'get_devices_details_list')
         actual_val = func_obj(ctxt, ['fake_device'], 'fake_agent_id')
     self.assertEqual(actual_val, expect_val)
Ejemplo n.º 9
0
 def _test_rpc_call(self, method):
     agent = rpc.PluginApi('fake_topic')
     ctxt = context.RequestContext('fake_user', 'fake_project')
     expect_val = 'foo'
     with mock.patch('neutron.common.rpc.RpcProxy.call') as rpc_call:
         rpc_call.return_value = expect_val
         func_obj = getattr(agent, method)
         if method == 'tunnel_sync':
             actual_val = func_obj(ctxt, 'fake_tunnel_ip')
         else:
             actual_val = func_obj(ctxt, 'fake_device', 'fake_agent_id')
     self.assertEqual(actual_val, expect_val)
Ejemplo n.º 10
0
 def test_plugin_report_state_cast(self):
     topic = 'test'
     reportStateAPI = rpc.PluginReportStateAPI(topic)
     expected_agent_state = {'agent': 'test'}
     with mock.patch.object(reportStateAPI, 'cast') as cast:
         ctxt = context.RequestContext('fake_user', 'fake_project')
         reportStateAPI.report_state(ctxt, expected_agent_state)
         self.assertEqual(cast.call_args[0][0], ctxt)
         self.assertEqual(cast.call_args[0][1]['method'], 'report_state')
         self.assertEqual(cast.call_args[0][1]['args']['agent_state'],
                          {'agent_state': expected_agent_state})
         self.assertIsInstance(cast.call_args[0][1]['args']['time'], str)
Ejemplo n.º 11
0
 def test_devices_details_list_unsupported(self):
     agent = rpc.PluginApi('fake_topic')
     ctxt = context.RequestContext('fake_user', 'fake_project')
     expect_val_get_device_details = 'foo'
     expect_val = [expect_val_get_device_details]
     with contextlib.nested(
         mock.patch.object(agent.client, 'call'),
         mock.patch.object(agent.client, 'prepare'),
     ) as (
         mock_call, mock_prepare
     ):
         mock_prepare.return_value = agent.client
         mock_call.side_effect = [messaging.UnsupportedVersion('1.2'),
                                 expect_val_get_device_details]
         func_obj = getattr(agent, 'get_devices_details_list')
         actual_val = func_obj(ctxt, ['fake_device'], 'fake_agent_id')
     self.assertEqual(actual_val, expect_val)
Ejemplo n.º 12
0
 def _setup_rpc(self):
     # RPC support
     self.topic = topics.PLUGIN
     self.rpc_context = context.RequestContext('neutron',
                                               'neutron',
                                               is_admin=False)
     self.conn = rpc.create_connection(new=True)
     self.callbacks = BridgeRpcCallbacks()
     self.dispatcher = self.callbacks.create_rpc_dispatcher()
     self.conn.create_consumer(self.topic, self.dispatcher, fanout=False)
     # Consume from all consumers in a thread
     self.conn.consume_in_thread()
     self.notifier = AgentNotifierApi(topics.AGENT)
     self.agent_notifiers[q_const.AGENT_TYPE_DHCP] = (
         dhcp_rpc_agent_api.DhcpAgentNotifyAPI())
     self.agent_notifiers[q_const.AGENT_TYPE_L3] = (
         l3_rpc_agent_api.L3AgentNotify)
Ejemplo n.º 13
0
 def _test_rpc_call(self, method):
     agent = rpc.PluginApi('fake_topic')
     ctxt = context.RequestContext('fake_user', 'fake_project')
     expect_val = 'foo'
     with contextlib.nested(
         mock.patch.object(agent.client, 'call'),
         mock.patch.object(agent.client, 'prepare'),
     ) as (
         mock_call, mock_prepare
     ):
         mock_prepare.return_value = agent.client
         mock_call.return_value = expect_val
         func_obj = getattr(agent, method)
         if method == 'tunnel_sync':
             actual_val = func_obj(ctxt, 'fake_tunnel_ip')
         else:
             actual_val = func_obj(ctxt, 'fake_device', 'fake_agent_id')
     self.assertEqual(actual_val, expect_val)
Ejemplo n.º 14
0
    def _test_hyperv_neutron_api(self, rpcapi, topic, method, rpc_method,
                                 **kwargs):
        ctxt = context.RequestContext('fake_user', 'fake_project')
        expected_retval = 'foo' if method == 'call' else None
        expected_msg = rpcapi.make_msg(method, **kwargs)
        expected_msg['version'] = rpcapi.BASE_RPC_API_VERSION
        if rpc_method == 'cast' and method == 'run_instance':
            kwargs['call'] = False

        with mock.patch.object(rpc, rpc_method) as rpc_method_mock:
            rpc_method_mock.return_value = expected_retval
            retval = getattr(rpcapi, method)(ctxt, **kwargs)

        self.assertEqual(retval, expected_retval)
        expected_args = [ctxt, topic, expected_msg]
        for arg, expected_arg in zip(rpc_method_mock.call_args[0],
                                     expected_args):
            self.assertEqual(arg, expected_arg)
Ejemplo n.º 15
0
 def test_plugin_report_state_cast(self):
     topic = 'test'
     reportStateAPI = rpc.PluginReportStateAPI(topic)
     expected_agent_state = {'agent': 'test'}
     with contextlib.nested(
         mock.patch.object(reportStateAPI.client, 'call'),
         mock.patch.object(reportStateAPI.client, 'cast'),
         mock.patch.object(reportStateAPI.client, 'prepare'),
     ) as (
         mock_call, mock_cast, mock_prepare
     ):
         mock_prepare.return_value = reportStateAPI.client
         ctxt = context.RequestContext('fake_user', 'fake_project')
         reportStateAPI.report_state(ctxt, expected_agent_state)
         self.assertEqual(mock_cast.call_args[0][0], ctxt)
         self.assertEqual(mock_cast.call_args[0][1], 'report_state')
         self.assertEqual(mock_cast.call_args[1]['agent_state'],
                          {'agent_state': expected_agent_state})
         self.assertIsInstance(mock_cast.call_args[1]['time'], str)
Ejemplo n.º 16
0
    def _test_rpc_api(self, rpcapi, topic, method, rpc_method, **kwargs):
        ctxt = context.RequestContext('fake_user', 'fake_project')
        expected_retval = 'foo' if method == 'call' else None
        expected_version = kwargs.pop('version', None)
        expected_msg = rpcapi.make_msg(method, **kwargs)
        if rpc_method == 'cast' and method == 'run_instance':
            kwargs['call'] = False

        rpc = n_rpc.RpcProxy
        with mock.patch.object(rpc, rpc_method) as rpc_method_mock:
            rpc_method_mock.return_value = expected_retval
            retval = getattr(rpcapi, method)(ctxt, **kwargs)

        self.assertEqual(retval, expected_retval)
        additional_args = {}
        if topic:
            additional_args['topic'] = topic
        if expected_version:
            additional_args['version'] = expected_version
        expected = [mock.call(ctxt, expected_msg, **additional_args)]
        rpc_method_mock.assert_has_calls(expected)
Ejemplo n.º 17
0
    def _test_rpc_api_legacy(self, rpcapi, topic, method, rpc_method,
                             **kwargs):
        # NOTE(russellb) This can be removed once AgentNotifierApi has been
        # converted over to no longer use the RpcProxy compatibility class.
        ctxt = context.RequestContext('fake_user', 'fake_project')
        expected_retval = 'foo' if rpc_method == 'call' else None
        expected_version = kwargs.pop('version', None)
        expected_msg = rpcapi.make_msg(method, **kwargs)

        rpc = n_rpc.RpcProxy
        with mock.patch.object(rpc, rpc_method) as rpc_method_mock:
            rpc_method_mock.return_value = expected_retval
            retval = getattr(rpcapi, method)(ctxt, **kwargs)

        self.assertEqual(retval, expected_retval)
        additional_args = {}
        if topic:
            additional_args['topic'] = topic
        if expected_version:
            additional_args['version'] = expected_version
        expected = [mock.call(ctxt, expected_msg, **additional_args)]
        rpc_method_mock.assert_has_calls(expected)