Compare subnet length as well when deleting DHCP entry

When searching for the DHCP subnet entry to delete, the existing code
compares only the subnet prefix and ignores subnet length.  This could
delete the wrong entry/entries if nested subnets are present.

Change-Id: Icf079c42adeca14ef84ec57dc45a5930fde8786d
Closes-Bug: #1362416
This commit is contained in:
Angus Lees 2014-08-28 14:37:34 +10:00
parent 024584df30
commit e9b4af4c6a
2 changed files with 11 additions and 6 deletions

View File

@ -212,7 +212,8 @@ class MidoClient:
raise MidonetApiException(
msg=_("Tried to delete non-existent DHCP"))
for dhcp in dhcp_subnets:
if dhcp.get_subnet_prefix() == net_addr:
if (dhcp.get_subnet_prefix() == net_addr and
dhcp.get_subnet_length() == str(net_len)):
dhcp.delete()
break

View File

@ -92,14 +92,18 @@ class MidoClientTestCase(testtools.TestCase):
def test_delete_dhcp(self):
bridge = mock.Mock()
subnet = mock.Mock()
subnet.get_subnet_prefix.return_value = "10.0.0.0"
subnets = mock.MagicMock(return_value=[subnet])
subnet1 = mock.Mock()
subnet1.get_subnet_prefix.return_value = "10.0.0.0"
subnet1.get_subnet_length.return_value = "16"
subnet2 = mock.Mock()
subnet2.get_subnet_prefix.return_value = "10.0.0.0"
subnet2.get_subnet_length.return_value = "24"
subnets = mock.MagicMock(return_value=[subnet1, subnet2])
bridge.get_dhcp_subnets.side_effect = subnets
self.client.delete_dhcp(bridge, "10.0.0.0/24")
bridge.assert_has_calls(mock.call.get_dhcp_subnets)
subnet.assert_has_calls([mock.call.get_subnet_prefix(),
mock.call.delete()])
self.assertFalse(subnet1.delete.called)
subnet2.delete.assert_called_once_with()
def test_add_dhcp_host(self):