From d24a8689dceaae1145d0cc0aa12e60bfdabbe2b2 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Thu, 7 Jul 2011 06:25:03 -0700 Subject: unit tests --- nova/compute/api.py | 3 ++- nova/network/manager.py | 4 ++-- nova/tests/test_network.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/nova/compute/api.py b/nova/compute/api.py index e4992bbfa..8507c5871 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -890,7 +890,8 @@ class API(base.Base): """Add fixed_ip from specified network to given instance.""" self._cast_compute_message('add_fixed_ip_to_instance', context, instance_id, network_id) - + + @scheduler_api.reroute_compute("remove_fixed_ip") def remove_fixed_ip(self, context, instance_id, address): """Remove fixed_ip from specified network to given instance.""" diff --git a/nova/network/manager.py b/nova/network/manager.py index 4b9fb98f4..ea946a1f4 100644 --- a/nova/network/manager.py +++ b/nova/network/manager.py @@ -486,10 +486,10 @@ class NetworkManager(manager.SchedulerDependentManager): """Removes a fixed ip from an instance from specified network.""" addresses = self.db.fixed_ip_get_by_instance(context, instance_id) for addr in addresses: - if addr == address: + if addr['address'] == address: self.deallocate_fixed_ip(context, address) return - raise exception.NoSuchAddress(address=address) + raise exception.FixedIpNotFound(id=address) def allocate_fixed_ip(self, context, instance_id, network, **kwargs): diff --git a/nova/tests/test_network.py b/nova/tests/test_network.py index 6d5166019..d5cab47cf 100644 --- a/nova/tests/test_network.py +++ b/nova/tests/test_network.py @@ -16,6 +16,7 @@ # under the License. from nova import db +from nova import exception from nova import flags from nova import log as logging from nova import test @@ -238,3 +239,35 @@ class VlanNetworkTestCase(test.TestCase): self.assertRaises(ValueError, self.network.create_networks, None, num_networks=100, vlan_start=1, cidr='192.168.0.1/24', network_size=100) + + +class CommonNetworkTestCase(test.TestCase): + + class FakeNetworkManager(network_manager.NetworkManager): + """This NetworkManager doesn't call the base class so we can bypass all + inherited service cruft and just perform unit tests. + """ + + class FakeDB: + def fixed_ip_get_by_instance(self, context, instance_id): + return [dict(address='10.0.0.0'), dict(address='10.0.0.1'), + dict(address='10.0.0.2')] + + def __init__(self): + self.db = self.FakeDB() + self.deallocate_called = None + + def deallocate_fixed_ip(self, context, address): + self.deallocate_called = address + + def test_remove_fixed_ip_from_instance(self): + manager = self.FakeNetworkManager() + manager.remove_fixed_ip_from_instance(None, 99, '10.0.0.1') + + self.assertEquals(manager.deallocate_called, '10.0.0.1') + + def test_remove_fixed_ip_from_instance_bad_input(self): + manager = self.FakeNetworkManager() + self.assertRaises(exception.FixedIpNotFound, + manager.remove_fixed_ip_from_instance, + None, 99, 'bad input') -- cgit