]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Disembowel register_l3_agent code duplication in tests
authorAssaf Muller <amuller@redhat.com>
Tue, 28 Apr 2015 15:44:16 +0000 (11:44 -0400)
committerAssaf Muller <amuller@redhat.com>
Wed, 29 Apr 2015 18:05:21 +0000 (14:05 -0400)
Change-Id: I32fe50ce0904ff439c615d9860782d76e94c48c3

neutron/tests/common/helpers.py
neutron/tests/unit/db/test_l3_hamode_db.py
neutron/tests/unit/extensions/test_agent.py
neutron/tests/unit/extensions/test_l3.py
neutron/tests/unit/plugins/openvswitch/test_agent_scheduler.py
neutron/tests/unit/scheduler/test_l3_agent_scheduler.py
neutron/tests/unit/services/metering/test_metering_plugin.py

index f6065c0640ca8cd3e1a6fb925fe71672257393d6..bf385117a117cc904a9f9e754cc303ef25644aa7 100644 (file)
 import os
 
 import neutron
+from neutron.common import constants
+from neutron.common import topics
+from neutron import context
+from neutron import manager
+
+HOST = 'localhost'
 
 
 def find_file(filename, path):
@@ -29,3 +35,36 @@ def find_sample_file(filename):
     return find_file(
         filename,
         path=os.path.join(neutron.__path__[0], '..', 'etc'))
+
+
+def _get_l3_agent_dict(host, agent_mode, internal_only=True,
+                       ext_net_id='', ext_bridge='', router_id=None):
+    return {
+        'agent_type': constants.AGENT_TYPE_L3,
+        'binary': 'neutron-l3-agent',
+        'host': host,
+        'topic': topics.L3_AGENT,
+        'configurations': {'agent_mode': agent_mode,
+                           'handle_internal_only_routers': internal_only,
+                           'external_network_bridge': ext_bridge,
+                           'gateway_external_network_id': ext_net_id,
+                           'router_id': router_id,
+                           'use_namespaces': router_id is None}}
+
+
+def _register_agent(agent):
+    core_plugin = manager.NeutronManager.get_plugin()
+    admin_context = context.get_admin_context()
+    core_plugin.create_or_update_agent(admin_context, agent)
+    return core_plugin.get_agents_db(
+        admin_context,
+        filters={'host': [agent['host']],
+                 'agent_type': [agent['agent_type']]})[0]
+
+
+def register_l3_agent(host=HOST, agent_mode=constants.L3_AGENT_MODE_LEGACY,
+                      internal_only=True, ext_net_id='', ext_bridge='',
+                      router_id=None):
+    agent = _get_l3_agent_dict(host, agent_mode, internal_only, ext_net_id,
+                               ext_bridge, router_id)
+    return _register_agent(agent)
index bd8f9e9cd443c9e5cda47eca47b9b89eea30e44a..ca918698ac2b4fe61621a93791a805395aa7d54f 100644 (file)
@@ -29,6 +29,7 @@ from neutron.extensions import l3_ext_ha_mode
 from neutron import manager
 from neutron.openstack.common import uuidutils
 from neutron.scheduler import l3_agent_scheduler
+from neutron.tests.common import helpers
 from neutron.tests.unit import testlib_api
 
 _uuid = uuidutils.generate_uuid
@@ -54,21 +55,9 @@ class L3HATestFramework(testlib_api.SqlTestCase):
         cfg.CONF.set_override('allow_overlapping_ips', True)
 
         self.plugin = FakeL3PluginWithAgents()
-        self.agent1 = self._register_agent('legacy', 'l3host')
-        self.agent2 = self._register_agent('dvr_snat', 'l3host_2')
-
-    def _register_agent(self, agent_mode, host):
-        agent_status = {
-            'agent_type': constants.AGENT_TYPE_L3,
-            'binary': 'neutron-l3-agent',
-            'host': host,
-            'topic': 'N/A',
-            'configurations': {'agent_mode': agent_mode}
-        }
-
-        self.plugin.create_or_update_agent(self.admin_ctx, agent_status)
-        return self.plugin.get_agents(
-            self.admin_ctx, filters={'host': [host]})[0]
+        self.agent1 = helpers.register_l3_agent()
+        self.agent2 = helpers.register_l3_agent(
+            'host_2', constants.L3_AGENT_MODE_DVR_SNAT)
 
     def _bring_down_agent(self, agent_id):
         update = {
@@ -474,21 +463,14 @@ class L3HATestCase(L3HATestFramework):
         # Test setup registers two l3 agents.
         # Register another l3 agent with dvr mode and assert that
         # get_number_of_ha_agent_candidates return 2.
-        dvr_agent_status = {
-            'agent_type': constants.AGENT_TYPE_L3,
-            'binary': 'neutron-l3-agent',
-            'host': 'l3host_3',
-            'topic': 'N/A',
-            'configurations': {'agent_mode': 'dvr'}
-        }
-        self.plugin.create_or_update_agent(self.admin_ctx, dvr_agent_status)
+        helpers.register_l3_agent('host_3', constants.L3_AGENT_MODE_DVR)
         num_ha_candidates = self.plugin.get_number_of_agents_for_scheduling(
             self.admin_ctx)
         self.assertEqual(2, num_ha_candidates)
 
     def test_get_number_of_agents_for_scheduling_not_enough_agents(self):
         cfg.CONF.set_override('min_l3_agents_per_router', 3)
-        agent_to_bring_down = self._register_agent('legacy', 'l3host_3')
+        agent_to_bring_down = helpers.register_l3_agent(host='l3host_3')
         self._bring_down_agent(agent_to_bring_down['id'])
         self.assertRaises(l3_ext_ha_mode.HANotEnoughAvailableAgents,
                           self.plugin.get_number_of_agents_for_scheduling,
index 39e48e98e95c699275c4f615cc308698853b8c91..33886b145d1a57c1ca128ce6aae012f332484137 100644 (file)
@@ -23,12 +23,12 @@ from webob import exc
 
 from neutron.api.v2 import attributes
 from neutron.common import constants
-from neutron.common import topics
 from neutron import context
 from neutron.db import agents_db
 from neutron.db import db_base_plugin_v2
 from neutron.extensions import agent
 from neutron.openstack.common import uuidutils
+from neutron.tests.common import helpers
 from neutron.tests import tools
 from neutron.tests.unit.api.v2 import test_base
 from neutron.tests.unit.db import test_db_base_plugin_v2
@@ -85,21 +85,10 @@ class AgentDBTestMixIn(object):
 
     def _register_agent_states(self, lbaas_agents=False):
         """Register two L3 agents and two DHCP agents."""
-        l3_hosta = {
-            'binary': 'neutron-l3-agent',
-            'host': L3_HOSTA,
-            'topic': topics.L3_AGENT,
-            'configurations': {'use_namespaces': True,
-                               'router_id': None,
-                               'handle_internal_only_routers':
-                               True,
-                               'gateway_external_network_id':
-                               None,
-                               'interface_driver': 'interface_driver',
-                               },
-            'agent_type': constants.AGENT_TYPE_L3}
-        l3_hostb = copy.deepcopy(l3_hosta)
-        l3_hostb['host'] = L3_HOSTB
+        l3_hosta = helpers._get_l3_agent_dict(
+            L3_HOSTA, constants.L3_AGENT_MODE_LEGACY)
+        l3_hostb = helpers._get_l3_agent_dict(
+            L3_HOSTB, constants.L3_AGENT_MODE_LEGACY)
         dhcp_hosta = {
             'binary': 'neutron-dhcp-agent',
             'host': DHCP_HOSTA,
@@ -119,12 +108,8 @@ class AgentDBTestMixIn(object):
         lbaas_hostb = copy.deepcopy(lbaas_hosta)
         lbaas_hostb['host'] = LBAAS_HOSTB
         callback = agents_db.AgentExtRpcCallback()
-        callback.report_state(self.adminContext,
-                              agent_state={'agent_state': l3_hosta},
-                              time=timeutils.strtime())
-        callback.report_state(self.adminContext,
-                              agent_state={'agent_state': l3_hostb},
-                              time=timeutils.strtime())
+        helpers.register_l3_agent(host=L3_HOSTA)
+        helpers.register_l3_agent(host=L3_HOSTB)
         callback.report_state(self.adminContext,
                               agent_state={'agent_state': dhcp_hosta},
                               time=timeutils.strtime())
@@ -160,25 +145,6 @@ class AgentDBTestMixIn(object):
                               time=timeutils.strtime())
         return [dhcp_host]
 
-    def _register_one_l3_agent(self, host=L3_HOSTA, internal_only=True,
-                               ext_net_id='', ext_bridge=''):
-        l3 = {
-            'binary': 'neutron-l3-agent',
-            'host': host,
-            'topic': topics.L3_AGENT,
-            'configurations': {'use_namespaces': True,
-                               'router_id': None,
-                               'handle_internal_only_routers': internal_only,
-                               'external_network_bridge': ext_bridge,
-                               'gateway_external_network_id': ext_net_id,
-                               'interface_driver': 'interface_driver',
-                               },
-            'agent_type': constants.AGENT_TYPE_L3}
-        callback = agents_db.AgentExtRpcCallback()
-        callback.report_state(self.adminContext,
-                              agent_state={'agent_state': l3},
-                              time=timeutils.strtime())
-
 
 class AgentDBTestCase(AgentDBTestMixIn,
                       test_db_base_plugin_v2.NeutronDbPluginV2TestCase):
index 8c824d6c07831049220a95ee7c9569ada500c07d..cd08b0559b8bd3dc1363eb694936a9d4f32cd8a4 100644 (file)
@@ -46,6 +46,7 @@ from neutron import manager
 from neutron.openstack.common import uuidutils
 from neutron.plugins.common import constants as service_constants
 from neutron.tests import base
+from neutron.tests.common import helpers
 from neutron.tests import fake_notifier
 from neutron.tests.unit.api.v2 import test_base
 from neutron.tests.unit.db import test_db_base_plugin_v2
@@ -2502,10 +2503,10 @@ class L3NatDBIntAgentSchedulingTestCase(L3BaseForIntTests,
                                self.subnet()) as (r, s1, s2):
             self._set_net_external(s1['subnet']['network_id'])
             l3_rpc_cb = l3_rpc.L3RpcCallback()
-            self._register_one_l3_agent(
+            helpers.register_l3_agent(
                 host='host1',
                 ext_net_id=s1['subnet']['network_id'])
-            self._register_one_l3_agent(
+            helpers.register_l3_agent(
                 host='host2', internal_only=False,
                 ext_net_id=s2['subnet']['network_id'])
             l3_rpc_cb.sync_routers(self.adminContext,
@@ -2529,10 +2530,10 @@ class L3NatDBIntAgentSchedulingTestCase(L3BaseForIntTests,
                                self.subnet()) as (r, s1, s2):
             self._set_net_external(s1['subnet']['network_id'])
             l3_rpc_cb = l3_rpc.L3RpcCallback()
-            self._register_one_l3_agent(
+            helpers.register_l3_agent(
                 host='host1',
                 ext_net_id=s1['subnet']['network_id'])
-            self._register_one_l3_agent(
+            helpers.register_l3_agent(
                 host='host2', internal_only=False,
                 ext_net_id='', ext_bridge='')
             l3_rpc_cb.sync_routers(self.adminContext,
index 20be55287ac8de39378bae69981ae9375fdb01ee..23cfca113face744b7fb19da8fca04f16a2d2273 100644 (file)
@@ -40,6 +40,7 @@ from neutron.extensions import l3agentscheduler
 from neutron import manager
 from neutron.openstack.common import uuidutils
 from neutron.plugins.common import constants as service_constants
+from neutron.tests.common import helpers
 from neutron.tests import fake_notifier
 from neutron.tests import tools
 from neutron.tests.unit.api import test_extensions
@@ -879,29 +880,12 @@ class OvsAgentSchedulerTestCase(OvsAgentSchedulerTestCaseBase):
     def test_router_auto_schedule_with_hosted_2(self):
         # one agent hosts one router
         l3_rpc_cb = l3_rpc.L3RpcCallback()
-        l3_hosta = {
-            'binary': 'neutron-l3-agent',
-            'host': L3_HOSTA,
-            'topic': 'L3_AGENT',
-            'configurations': {'use_namespaces': True,
-                               'router_id': None,
-                               'handle_internal_only_routers':
-                               True,
-                               'gateway_external_network_id':
-                               None,
-                               'interface_driver': 'interface_driver',
-                               },
-            'agent_type': constants.AGENT_TYPE_L3}
-        l3_hostb = copy.deepcopy(l3_hosta)
-        l3_hostb['host'] = L3_HOSTB
         with self.router() as router1:
-            self._register_one_agent_state(l3_hosta)
+            hosta_id = helpers.register_l3_agent(host=L3_HOSTA).id
             l3_rpc_cb.sync_routers(self.adminContext, host=L3_HOSTA)
-            hosta_id = self._get_agent_id(constants.AGENT_TYPE_L3,
-                                          L3_HOSTA)
             self._disable_agent(hosta_id, admin_state_up=False)
             with self.router() as router2:
-                self._register_one_agent_state(l3_hostb)
+                hostb_id = helpers.register_l3_agent(host=L3_HOSTB).id
                 l3_rpc_cb.sync_routers(self.adminContext, host=L3_HOSTB)
                 l3_agents_1 = self._list_l3_agents_hosting_router(
                     router1['router']['id'])
@@ -909,9 +893,6 @@ class OvsAgentSchedulerTestCase(OvsAgentSchedulerTestCaseBase):
                     router2['router']['id'])
                 hosta_routers = self._list_routers_hosted_by_l3_agent(hosta_id)
                 num_hosta_routers = len(hosta_routers['routers'])
-                hostb_id = self._get_agent_id(
-                    constants.AGENT_TYPE_L3,
-                    L3_HOSTB)
                 hostb_routers = self._list_routers_hosted_by_l3_agent(hostb_id)
                 num_hostb_routers = len(hostb_routers['routers'])
 
@@ -944,28 +925,13 @@ class OvsAgentSchedulerTestCase(OvsAgentSchedulerTestCaseBase):
         self.assertEqual(0, num_hosta_routers)
 
     def test_router_auto_schedule_with_candidates(self):
-        l3_hosta = {
-            'binary': 'neutron-l3-agent',
-            'host': L3_HOSTA,
-            'topic': 'L3_AGENT',
-            'configurations': {'use_namespaces': False,
-                               'router_id': None,
-                               'handle_internal_only_routers':
-                               True,
-                               'gateway_external_network_id':
-                               None,
-                               'interface_driver': 'interface_driver',
-                               },
-            'agent_type': constants.AGENT_TYPE_L3}
         with contextlib.nested(self.router(),
                                self.router()) as (router1, router2):
             l3_rpc_cb = l3_rpc.L3RpcCallback()
-            l3_hosta['configurations']['router_id'] = router1['router']['id']
-            self._register_one_agent_state(l3_hosta)
-            hosta_id = self._get_agent_id(constants.AGENT_TYPE_L3,
-                                          L3_HOSTA)
+            agent = helpers.register_l3_agent(
+                host=L3_HOSTA, router_id=router1['router']['id'])
             l3_rpc_cb.sync_routers(self.adminContext, host=L3_HOSTA)
-            hosta_routers = self._list_routers_hosted_by_l3_agent(hosta_id)
+            hosta_routers = self._list_routers_hosted_by_l3_agent(agent.id)
             num_hosta_routers = len(hosta_routers['routers'])
             l3_agents_1 = self._list_l3_agents_hosting_router(
                 router1['router']['id'])
@@ -1046,19 +1012,6 @@ class OvsAgentSchedulerTestCase(OvsAgentSchedulerTestCaseBase):
             _sync_router_with_ids(router_ids, 4, 4, hosta_id)
 
     def test_router_schedule_with_candidates(self):
-        l3_hosta = {
-            'binary': 'neutron-l3-agent',
-            'host': L3_HOSTA,
-            'topic': 'L3_AGENT',
-            'configurations': {'use_namespaces': False,
-                               'router_id': None,
-                               'handle_internal_only_routers':
-                               True,
-                               'gateway_external_network_id':
-                               None,
-                               'interface_driver': 'interface_driver',
-                               },
-            'agent_type': constants.AGENT_TYPE_L3}
         with contextlib.nested(self.router(),
                                self.router(),
                                self.subnet(),
@@ -1066,10 +1019,8 @@ class OvsAgentSchedulerTestCase(OvsAgentSchedulerTestCaseBase):
                                                                     router2,
                                                                     subnet1,
                                                                     subnet2):
-            l3_hosta['configurations']['router_id'] = router1['router']['id']
-            self._register_one_agent_state(l3_hosta)
-            hosta_id = self._get_agent_id(constants.AGENT_TYPE_L3,
-                                          L3_HOSTA)
+            agent = helpers.register_l3_agent(
+                host=L3_HOSTA, router_id=router1['router']['id'])
             self._router_interface_action('add',
                                           router1['router']['id'],
                                           subnet1['subnet']['id'],
@@ -1078,7 +1029,7 @@ class OvsAgentSchedulerTestCase(OvsAgentSchedulerTestCaseBase):
                                           router2['router']['id'],
                                           subnet2['subnet']['id'],
                                           None)
-            hosta_routers = self._list_routers_hosted_by_l3_agent(hosta_id)
+            hosta_routers = self._list_routers_hosted_by_l3_agent(agent.id)
             num_hosta_routers = len(hosta_routers['routers'])
             l3_agents_1 = self._list_l3_agents_hosting_router(
                 router1['router']['id'])
index 260b910a5069020798576be6d9429740e0d55da5..93ceae59761ee6aa328f633d382f6a54d6028884 100644 (file)
@@ -27,7 +27,6 @@ from oslo_utils import timeutils
 from sqlalchemy.orm import query
 
 from neutron.common import constants
-from neutron.common import topics
 from neutron import context as q_context
 from neutron.db import agents_db
 from neutron.db import common_db_mixin
@@ -41,6 +40,7 @@ from neutron.extensions import l3agentscheduler as l3agent
 from neutron import manager
 from neutron.scheduler import l3_agent_scheduler
 from neutron.tests import base
+from neutron.tests.common import helpers
 from neutron.tests.unit.db import test_db_base_plugin_v2
 from neutron.tests.unit.extensions import test_l3
 from neutron.tests.unit import testlib_api
@@ -54,24 +54,7 @@ from neutron.tests.unit import testlib_api
 load_tests = testscenarios.load_tests_apply_scenarios
 
 HOST_DVR = 'my_l3_host_dvr'
-DVR_L3_AGENT = {
-    'binary': 'neutron-l3-agent',
-    'host': HOST_DVR,
-    'topic': topics.L3_AGENT,
-    'configurations': {'agent_mode': 'dvr'},
-    'agent_type': constants.AGENT_TYPE_L3,
-    'start_flag': True
-}
-
 HOST_DVR_SNAT = 'my_l3_host_dvr_snat'
-DVR_SNAT_L3_AGENT = {
-    'binary': 'neutron-l3-agent',
-    'host': HOST_DVR_SNAT,
-    'topic': topics.L3_AGENT,
-    'configurations': {'agent_mode': 'dvr_snat'},
-    'agent_type': constants.AGENT_TYPE_L3,
-    'start_flag': True
-}
 
 
 class FakeL3Scheduler(l3_agent_scheduler.L3Scheduler):
@@ -287,49 +270,21 @@ class L3SchedulerBaseTestCase(base.BaseTestCase):
 
 class L3SchedulerBaseMixin(object):
 
-    def _register_l3_agent(self, host, agent_mode='legacy', plugin=None):
-        if not plugin:
-            plugin = self.plugin
-
-        agent = {
-            'binary': 'neutron-l3-agent',
-            'host': host,
-            'topic': topics.L3_AGENT,
-            'configurations': {'agent_mode': agent_mode},
-            'agent_type': constants.AGENT_TYPE_L3,
-            'start_flag': True
-        }
-        callback = agents_db.AgentExtRpcCallback()
-        callback.report_state(self.adminContext,
-                              agent_state={'agent_state': agent},
-                              time=timeutils.strtime())
-        agent_db = plugin.get_agents_db(self.adminContext,
-                                        filters={'host': [agent['host']]})
-        return agent_db[0]
-
     def _register_l3_agents(self, plugin=None):
-        self.agent1 = self._register_l3_agent('host_1', plugin=plugin)
+        self.agent1 = helpers.register_l3_agent(
+            'host_1', constants.L3_AGENT_MODE_LEGACY)
         self.agent_id1 = self.agent1.id
-
-        self.agent2 = self._register_l3_agent('host_2', plugin=plugin)
+        self.agent2 = helpers.register_l3_agent(
+            'host_2', constants.L3_AGENT_MODE_LEGACY)
         self.agent_id2 = self.agent2.id
 
     def _register_l3_dvr_agents(self):
-        callback = agents_db.AgentExtRpcCallback()
-        callback.report_state(self.adminContext,
-                              agent_state={'agent_state': DVR_L3_AGENT},
-                              time=timeutils.strtime())
-        agent_db = self.plugin.get_agents_db(self.adminContext,
-                                             filters={'host': [HOST_DVR]})
-        self.l3_dvr_agent = agent_db[0]
-        self.l3_dvr_agent_id = agent_db[0].id
-        callback.report_state(self.adminContext,
-                              agent_state={'agent_state': DVR_SNAT_L3_AGENT},
-                              time=timeutils.strtime())
-        agent_db = self.plugin.get_agents_db(self.adminContext,
-                                             filters={'host': [HOST_DVR_SNAT]})
-        self.l3_dvr_snat_id = agent_db[0].id
-        self.l3_dvr_snat_agent = agent_db[0]
+        self.l3_dvr_agent = helpers.register_l3_agent(
+            HOST_DVR, constants.L3_AGENT_MODE_DVR)
+        self.l3_dvr_agent_id = self.l3_dvr_agent.id
+        self.l3_dvr_snat_agent = helpers.register_l3_agent(
+            HOST_DVR_SNAT, constants.L3_AGENT_MODE_DVR_SNAT)
+        self.l3_dvr_snat_id = self.l3_dvr_snat_agent.id
 
     def _set_l3_agent_admin_state(self, context, agent_id, state=True):
         update = {'agent': {'admin_state_up': state}}
@@ -458,7 +413,6 @@ class L3SchedulerTestBaseMixin(object):
         }
         self._register_l3_dvr_agents()
         agent_id = self.l3_dvr_snat_id
-        agent = self.l3_dvr_snat_agent
         router = self._create_router_for_l3_agent_dvr_test(
             distributed=True,
             external_gw=external_gw_info)
@@ -472,7 +426,7 @@ class L3SchedulerTestBaseMixin(object):
             self.add_router_to_l3_agent(self.adminContext, agent_id,
                                         router['router']['id'])
             rtr_agent_binding.assert_called_once_with(
-                self.adminContext, agent, router['router'])
+                self.adminContext, mock.ANY, router['router'])
 
     def test_add_router_to_l3_agent(self):
         self._test_add_router_to_l3_agent()
@@ -639,9 +593,8 @@ class L3SchedulerTestBaseMixin(object):
         agent_list = [self.agent1, self.l3_dvr_agent]
         # test dvr agent_mode case only dvr agent should be candidate
         router['distributed'] = True
-        exp_host = DVR_L3_AGENT.get('host')
         self.check_ports_exist_on_l3agent = mock.Mock(return_value=True)
-        self._check_get_l3_agent_candidates(router, agent_list, exp_host)
+        self._check_get_l3_agent_candidates(router, agent_list, HOST_DVR)
 
     def test_get_l3_agent_candidates_dvr_no_vms(self):
         self._register_l3_dvr_agents()
@@ -651,12 +604,11 @@ class L3SchedulerTestBaseMixin(object):
         router['external_gateway_info'] = None
         router['id'] = str(uuid.uuid4())
         agent_list = [self.agent1, self.l3_dvr_agent]
-        exp_host = DVR_L3_AGENT.get('host')
         router['distributed'] = True
         # Test no VMs present case
         self.check_ports_exist_on_l3agent = mock.Mock(return_value=False)
         self._check_get_l3_agent_candidates(
-            router, agent_list, exp_host, count=0)
+            router, agent_list, HOST_DVR, count=0)
 
     def test_get_l3_agent_candidates_dvr_snat(self):
         self._register_l3_dvr_agents()
@@ -668,9 +620,8 @@ class L3SchedulerTestBaseMixin(object):
         router['distributed'] = True
 
         agent_list = [self.l3_dvr_snat_agent]
-        exp_host = DVR_SNAT_L3_AGENT.get('host')
         self.check_ports_exist_on_l3agent = mock.Mock(return_value=True)
-        self._check_get_l3_agent_candidates(router, agent_list, exp_host)
+        self._check_get_l3_agent_candidates(router, agent_list, HOST_DVR_SNAT)
 
     def test_get_l3_agent_candidates_dvr_snat_no_vms(self):
         self._register_l3_dvr_agents()
@@ -682,12 +633,11 @@ class L3SchedulerTestBaseMixin(object):
         router['distributed'] = True
 
         agent_list = [self.l3_dvr_snat_agent]
-        exp_host = DVR_SNAT_L3_AGENT.get('host')
         self.check_ports_exist_on_l3agent = mock.Mock(return_value=False)
         # Test no VMs present case
         self.check_ports_exist_on_l3agent.return_value = False
         self._check_get_l3_agent_candidates(
-            router, agent_list, exp_host, count=0)
+            router, agent_list, HOST_DVR_SNAT, count=0)
 
     def test_get_l3_agent_candidates_centralized(self):
         self._register_l3_dvr_agents()
@@ -698,9 +648,8 @@ class L3SchedulerTestBaseMixin(object):
         router['id'] = str(uuid.uuid4())
         # check centralized test case
         router['distributed'] = False
-        exp_host = DVR_SNAT_L3_AGENT.get('host')
         agent_list = [self.l3_dvr_snat_agent]
-        self._check_get_l3_agent_candidates(router, agent_list, exp_host)
+        self._check_get_l3_agent_candidates(router, agent_list, HOST_DVR_SNAT)
 
     def _prepare_check_ports_exist_tests(self):
         l3_agent = agents_db.Agent()
@@ -783,7 +732,7 @@ class L3SchedulerTestBaseMixin(object):
         self.assertTrue(val)
 
     def test_get_l3_agents_hosting_routers(self):
-        agent = self._register_l3_agent('host_6')
+        agent = helpers.register_l3_agent('host_6')
         router = self._make_router(self.fmt,
                                    tenant_id=str(uuid.uuid4()),
                                    name='r1')
@@ -1482,10 +1431,10 @@ class L3_HA_scheduler_db_mixinTestCase(L3HATestCaseMixin):
         super(L3_HA_scheduler_db_mixinTestCase,
               self)._register_l3_agents(plugin=plugin)
 
-        self.agent3 = self._register_l3_agent('host_3', plugin=plugin)
+        self.agent3 = helpers.register_l3_agent(host='host_3')
         self.agent_id3 = self.agent3.id
 
-        self.agent4 = self._register_l3_agent('host_4', plugin=plugin)
+        self.agent4 = helpers.register_l3_agent(host='host_4')
         self.agent_id4 = self.agent4.id
 
     def test_get_ha_routers_l3_agents_count(self):
@@ -1620,7 +1569,7 @@ class L3HAChanceSchedulerTestCase(L3HATestCaseMixin):
         self.assertIn(self.agent_id1, agent_ids)
         self.assertIn(self.agent_id2, agent_ids)
 
-        agent = self._register_l3_agent('host_3')
+        agent = helpers.register_l3_agent(host='host_3')
         self.agent_id3 = agent.id
         routers_to_auto_schedule = [router['id']] if specific_router else []
         self.plugin.auto_schedule_routers(self.adminContext,
@@ -1665,10 +1614,10 @@ class L3HALeastRoutersSchedulerTestCase(L3HATestCaseMixin):
         super(L3HALeastRoutersSchedulerTestCase,
               self)._register_l3_agents(plugin=plugin)
 
-        agent = self._register_l3_agent('host_3', plugin=plugin)
+        agent = helpers.register_l3_agent(host='host_3')
         self.agent_id3 = agent.id
 
-        agent = self._register_l3_agent('host_4', plugin=plugin)
+        agent = helpers.register_l3_agent(host='host_4')
         self.agent_id4 = agent.id
 
     def setUp(self):
@@ -1772,7 +1721,7 @@ class TestGetL3AgentsWithAgentModeFilter(testlib_api.SqlTestCase,
         hosts = ['host_1', 'host_2', 'host_3', 'host_4', 'host_5']
         agent_modes = ['legacy', 'dvr_snat', 'dvr', 'fake_mode', 'legacy']
         for host, agent_mode in zip(hosts, agent_modes):
-            self._register_l3_agent(host, agent_mode, self.plugin)
+            helpers.register_l3_agent(host, agent_mode)
 
     def _get_agent_mode(self, agent):
         agent_conf = self.plugin.get_configuration_dict(agent)
index bd086b2230d3cd9e74512d28843ba1b5c23970dc..408ea1921b50fa9d17b1f9d1c8fc6f53dd721795 100644 (file)
 # under the License.
 
 import mock
-from oslo_utils import timeutils
 
 from neutron.api.v2 import attributes as attr
-from neutron.common import constants as n_constants
-from neutron.common import topics
 from neutron import context
 from neutron.db import agents_db
 from neutron.db import l3_agentschedulers_db
@@ -27,6 +24,7 @@ from neutron.extensions import metering as ext_metering
 from neutron import manager
 from neutron.openstack.common import uuidutils
 from neutron.plugins.common import constants
+from neutron.tests.common import helpers
 from neutron.tests.unit.db.metering import test_metering_db
 from neutron.tests.unit.db import test_db_base_plugin_v2
 from neutron.tests.unit.extensions import test_l3
@@ -428,21 +426,7 @@ class TestMeteringPluginRpcFromL3Agent(
         self.tenant_id_2 = 'tenant_id_2'
 
         self.adminContext = context.get_admin_context()
-        self._register_l3_agent('agent1')
-
-    def _register_l3_agent(self, host):
-        agent = {
-            'binary': 'neutron-l3-agent',
-            'host': host,
-            'topic': topics.L3_AGENT,
-            'configurations': {},
-            'agent_type': n_constants.AGENT_TYPE_L3,
-            'start_flag': True
-        }
-        callback = agents_db.AgentExtRpcCallback()
-        callback.report_state(self.adminContext,
-                              agent_state={'agent_state': agent},
-                              time=timeutils.strtime())
+        helpers.register_l3_agent(host='agent1')
 
     def test_get_sync_data_metering(self):
         with self.subnet() as subnet:
@@ -458,7 +442,7 @@ class TestMeteringPluginRpcFromL3Agent(
                                                             host='agent1')
                     self.assertEqual('router1', data[0]['name'])
 
-                    self._register_l3_agent('agent2')
+                    helpers.register_l3_agent(host='agent2')
                     data = callbacks.get_sync_data_metering(self.adminContext,
                                                             host='agent2')
                     self.assertFalse(data)