From ecb68bf77565cb01ec0ea4d28c7f1315e10b21c4 Mon Sep 17 00:00:00 2001 From: John Tran Date: Tue, 19 Jul 2011 13:49:05 -0700 Subject: network api release_floating_ip method checks if an instance associated to the floating prior to releasing. added test --- nova/network/api.py | 3 +++ nova/tests/test_cloud.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/nova/network/api.py b/nova/network/api.py index 70b1099f0..f03081be4 100644 --- a/nova/network/api.py +++ b/nova/network/api.py @@ -61,6 +61,9 @@ class API(base.Base): affect_auto_assigned=False): """Removes floating ip with address from a project.""" floating_ip = self.db.floating_ip_get_by_address(context, address) + if floating_ip['fixed_ip']: + raise exception.ApiError(_('Floating ip is in use. ' + 'Disassociate it before releasing.')) if not affect_auto_assigned and floating_ip.get('auto_assigned'): return # NOTE(vish): We don't know which network host should get the ip diff --git a/nova/tests/test_cloud.py b/nova/tests/test_cloud.py index d71a03aff..557e3f89b 100644 --- a/nova/tests/test_cloud.py +++ b/nova/tests/test_cloud.py @@ -15,6 +15,7 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. +import mox from base64 import b64decode from M2Crypto import BIO @@ -131,6 +132,34 @@ class CloudTestCase(test.TestCase): allocate, self.context) + def test_release_address(self): + address = "10.10.10.10" + allocate = self.cloud.allocate_address + db.floating_ip_create(self.context, + {'address': address, + 'host': self.network.host}) + result = self.cloud.release_address(self.context, address) + self.assertEqual(result['releaseResponse'], ['Address released.']) + + def test_release_address_still_associated(self): + address = "10.10.10.10" + fixed_ip = {'instance': {'id': 1}} + floating_ip = {'id': 0, + 'address': address, + 'fixed_ip_id': 0, + 'fixed_ip': fixed_ip, + 'project_id': None, + 'auto_assigned': False} + from nova import network + network_api = network.api.API() + self.mox.StubOutWithMock(network_api.db, 'floating_ip_get_by_address') + network_api.db.floating_ip_get_by_address(mox.IgnoreArg(), + mox.IgnoreArg()).AndReturn(floating_ip) + self.mox.ReplayAll() + release = self.cloud.release_address + # ApiError: Floating ip is in use. Disassociate it before releasing. + self.assertRaises(exception.ApiError, release, self.context, address) + @test.skip_test("Skipping this pending future merge") def test_associate_disassociate_address(self): """Verifies associate runs cleanly without raising an exception""" -- cgit From 1d4a789ed370fe0cc00c292f89b96b0ffaf115ff Mon Sep 17 00:00:00 2001 From: John Tran Date: Tue, 19 Jul 2011 14:16:14 -0700 Subject: move import network to the top --- nova/tests/test_cloud.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/tests/test_cloud.py b/nova/tests/test_cloud.py index 46e0ec68c..8cdc73a66 100644 --- a/nova/tests/test_cloud.py +++ b/nova/tests/test_cloud.py @@ -30,6 +30,7 @@ from nova import db from nova import exception from nova import flags from nova import log as logging +from nova import network from nova import rpc from nova import test from nova import utils @@ -151,7 +152,6 @@ class CloudTestCase(test.TestCase): 'fixed_ip': fixed_ip, 'project_id': None, 'auto_assigned': False} - from nova import network network_api = network.api.API() self.mox.StubOutWithMock(network_api.db, 'floating_ip_get_by_address') network_api.db.floating_ip_get_by_address(mox.IgnoreArg(), -- cgit