diff --git a/neutron/common/utils.py b/neutron/common/utils.py index f890f6ee4..8fde56629 100644 --- a/neutron/common/utils.py +++ b/neutron/common/utils.py @@ -353,6 +353,7 @@ def is_dvr_serviced(device_owner): if they are required for DVR or any service directly or indirectly associated with DVR. """ - if (device_owner.startswith('compute:') or ( - q_const.DEVICE_OWNER_LOADBALANCER == device_owner)): - return True + dvr_serviced_device_owners = (q_const.DEVICE_OWNER_LOADBALANCER, + q_const.DEVICE_OWNER_DHCP) + return (device_owner.startswith('compute:') or + device_owner in dvr_serviced_device_owners) diff --git a/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py b/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py index 91ed43d9e..de2724437 100644 --- a/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py +++ b/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py @@ -291,6 +291,10 @@ class TestOvsNeutronAgent(base.BaseTestCase): self._test_port_bound_for_dvr( device_owner=n_const.DEVICE_OWNER_LOADBALANCER) + def test_port_bound_for_dvr_with_dhcp_ports(self): + self._test_port_bound_for_dvr( + device_owner=n_const.DEVICE_OWNER_DHCP) + def test_port_bound_for_dvr_with_csnat_ports(self, ofport=10): self._setup_for_dvr_test() with mock.patch('neutron.agent.linux.ovs_lib.OVSBridge.' @@ -434,6 +438,10 @@ class TestOvsNeutronAgent(base.BaseTestCase): self._test_treat_devices_removed_for_dvr( device_owner=n_const.DEVICE_OWNER_LOADBALANCER) + def test_treat_devices_removed_for_dvr_with_dhcp_ports(self): + self._test_treat_devices_removed_for_dvr( + device_owner=n_const.DEVICE_OWNER_DHCP) + def test_treat_devices_removed_for_dvr_csnat_port(self, ofport=10): self._setup_for_dvr_test() with mock.patch('neutron.agent.linux.ovs_lib.OVSBridge.' diff --git a/neutron/tests/unit/test_common_utils.py b/neutron/tests/unit/test_common_utils.py index 96b9f170d..5534e380d 100644 --- a/neutron/tests/unit/test_common_utils.py +++ b/neutron/tests/unit/test_common_utils.py @@ -528,3 +528,18 @@ class TestExceptionLogger(base.BaseTestCase): calls.assert_has_calls([mock.call(0), mock.call(1), mock.call(3)], any_order=True) self.assertTrue(logger.called) + + +class TestDvrServices(base.BaseTestCase): + + def _test_is_dvr_serviced(self, device_owner, expected): + self.assertEqual(expected, utils.is_dvr_serviced(device_owner)) + + def test_is_dvr_serviced_with_lb_port(self): + self._test_is_dvr_serviced(constants.DEVICE_OWNER_LOADBALANCER, True) + + def test_is_dvr_serviced_with_dhcp_port(self): + self._test_is_dvr_serviced(constants.DEVICE_OWNER_DHCP, True) + + def test_is_dvr_serviced_with_vm_port(self): + self._test_is_dvr_serviced('compute:', True) diff --git a/neutron/tests/unit/test_l3_schedulers.py b/neutron/tests/unit/test_l3_schedulers.py index 97d04152b..cef537afb 100644 --- a/neutron/tests/unit/test_l3_schedulers.py +++ b/neutron/tests/unit/test_l3_schedulers.py @@ -911,7 +911,17 @@ class L3DvrSchedulerTestCase(testlib_api.SqlTestCase, sub_ids) self.assertFalse(result) - def test_check_dvr_serviced_port_exists_on_subnet(self): + def _test_dvr_serviced_port_exists_on_subnet(self, port): + with mock.patch('neutron.db.db_base_plugin_v2.NeutronDbPluginV2.' + 'get_ports', return_value=[port]): + result = self.dut.check_ports_active_on_host_and_subnet( + self.adminContext, + 'thisHost', + 'dvr1-intf-id', + 'my-subnet-id') + self.assertTrue(result) + + def test_dvr_serviced_vip_port_exists_on_subnet(self): vip_port = { 'id': 'lbaas-vip-port1', 'device_id': 'vip-pool-id', @@ -925,19 +935,23 @@ class L3DvrSchedulerTestCase(testlib_api.SqlTestCase, } ] } + self._test_dvr_serviced_port_exists_on_subnet(port=vip_port) - with contextlib.nested( - mock.patch('neutron.db.db_base_plugin_v2.NeutronDbPluginV2' - '.get_ports', return_value=[vip_port]), - mock.patch('neutron.common.utils.is_dvr_serviced', - return_value=True)) as (get_ports_fn, dvr_serv_fn): - result = self.dut.check_ports_active_on_host_and_subnet( - self.adminContext, - 'thisHost', - 'dvr1-intf-id', - 'my-subnet-id') - self.assertTrue(result) - self.assertEqual(dvr_serv_fn.call_count, 1) + def test_dvr_serviced_dhcp_port_exists_on_subnet(self): + dhcp_port = { + 'id': 'dhcp-port1', + 'device_id': 'dhcp-net-id', + 'status': 'ACTIVE', + 'binding:host_id': 'thisHost', + 'device_owner': constants.DEVICE_OWNER_DHCP, + 'fixed_ips': [ + { + 'subnet_id': 'my-subnet-id', + 'ip_address': '10.10.10.2' + } + ] + } + self._test_dvr_serviced_port_exists_on_subnet(port=dhcp_port) def _prepare_schedule_snat_tests(self): agent = agents_db.Agent()