From 6290af9cf97d4ee323ba988733e026a27695e9fa Mon Sep 17 00:00:00 2001 From: Brandon Logan Date: Wed, 1 Jun 2016 11:10:08 -0500 Subject: [PATCH] Fix getting dhcp agents for multiple networks The method to get dhcp agents for networks takes as a an argument the ability to send a list of network_ids. However, this method is really only being used to send in a list of one network_id, but there don't seem to be any usages where it is being used for many network_ids. Since its not being used, this hasn't been caught yet. Change-Id: I6f07ed50b29448d279a4fd5f9d392af3a8490340 Closes-Bug: #1587973 --- neutron/db/agentschedulers_db.py | 7 ++----- .../unit/scheduler/test_dhcp_agent_scheduler.py | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/neutron/db/agentschedulers_db.py b/neutron/db/agentschedulers_db.py index 2f90bf9f5fb..8fcc2f36d1d 100644 --- a/neutron/db/agentschedulers_db.py +++ b/neutron/db/agentschedulers_db.py @@ -335,12 +335,9 @@ class DhcpAgentSchedulerDbMixin(dhcpagentscheduler query = query.options(orm.contains_eager( NetworkDhcpAgentBinding.dhcp_agent)) query = query.join(NetworkDhcpAgentBinding.dhcp_agent) - if len(network_ids) == 1: + if network_ids: query = query.filter( - NetworkDhcpAgentBinding.network_id == network_ids[0]) - elif network_ids: - query = query.filter( - NetworkDhcpAgentBinding.network_id in network_ids) + NetworkDhcpAgentBinding.network_id.in_(network_ids)) if admin_state_up is not None: query = query.filter(agents_db.Agent.admin_state_up == admin_state_up) diff --git a/neutron/tests/unit/scheduler/test_dhcp_agent_scheduler.py b/neutron/tests/unit/scheduler/test_dhcp_agent_scheduler.py index 699cc78f1ea..91677aafa9a 100644 --- a/neutron/tests/unit/scheduler/test_dhcp_agent_scheduler.py +++ b/neutron/tests/unit/scheduler/test_dhcp_agent_scheduler.py @@ -472,9 +472,11 @@ class TestDhcpSchedulerFilter(TestDhcpSchedulerBaseTestCase, agents = self._create_and_set_agents_down(['host-a', 'host-b'], 1) agents += self._create_and_set_agents_down(['host-c', 'host-d'], 1, admin_state_up=False) - self._test_schedule_bind_network(agents, self.network_id) + networks = kwargs.pop('networks', [self.network_id]) + for network in networks: + self._test_schedule_bind_network(agents, network) agents = self.get_dhcp_agents_hosting_networks(self.ctx, - [self.network_id], + networks, **kwargs) host_ids = set(a['host'] for a in agents) self.assertEqual(expected, host_ids) @@ -505,6 +507,14 @@ class TestDhcpSchedulerFilter(TestDhcpSchedulerBaseTestCase, active=True, admin_state_up=False) + def test_get_dhcp_agents_hosting_many_networks(self): + net_id = 'another-net-id' + self._save_networks([net_id]) + networks = [net_id, self.network_id] + self._test_get_dhcp_agents_hosting_networks({'host-a', 'host-b', + 'host-c', 'host-d'}, + networks=networks) + class DHCPAgentAZAwareWeightSchedulerTestCase(TestDhcpSchedulerBaseTestCase):