From b6d454762d7fdf9b202d8a580dd9bfdf069a5e80 Mon Sep 17 00:00:00 2001 From: Anthony Young Date: Wed, 7 Sep 2011 16:38:41 -0700 Subject: fix for lp844364: improve check for fixed_ip association --- nova/api/openstack/contrib/floating_ips.py | 4 +- .../api/openstack/contrib/test_floating_ips.py | 78 +++++++++++++++++++++- 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/nova/api/openstack/contrib/floating_ips.py b/nova/api/openstack/contrib/floating_ips.py index d1add8f83..9ceb5858f 100644 --- a/nova/api/openstack/contrib/floating_ips.py +++ b/nova/api/openstack/contrib/floating_ips.py @@ -107,7 +107,7 @@ class FloatingIPController(object): context = req.environ['nova.context'] floating_ip = self.network_api.get_floating_ip(context, id) - if 'fixed_ip' in floating_ip: + if floating_ip['fixed_ip']: self.network_api.disassociate_floating_ip(context, floating_ip['address']) @@ -161,7 +161,7 @@ class Floating_ips(extensions.ExtensionDescriptor): raise webob.exc.HTTPBadRequest(explanation=msg) floating_ip = self.network_api.get_floating_ip_by_ip(context, address) - if 'fixed_ip' in floating_ip: + if floating_ip['fixed_ip']: self.network_api.disassociate_floating_ip(context, address) return webob.Response(status_int=202) diff --git a/nova/tests/api/openstack/contrib/test_floating_ips.py b/nova/tests/api/openstack/contrib/test_floating_ips.py index 642f2b841..0744f0a11 100644 --- a/nova/tests/api/openstack/contrib/test_floating_ips.py +++ b/nova/tests/api/openstack/contrib/test_floating_ips.py @@ -212,11 +212,45 @@ class FloatingIpTest(test.TestCase): "fixed_ip": None} self.assertEqual(ip, expected) - def test_floating_ip_release(self): + def test_floating_ip_release_associated(self): + self.disassociated = False + + def get_floating_ip(ignore, context, id): + return {'id': 1, 'address': '10.10.10.10', + 'fixed_ip': {'id': 1}} + + def disassociate(ignore, context, floating_address): + self.disassociated = True + + self.stubs.Set(network.api.API, "get_floating_ip", + get_floating_ip) + self.stubs.Set(network.api.API, "disassociate_floating_ip", + disassociate) + req = webob.Request.blank('/v1.1/123/os-floating-ips/1') + req.method = 'DELETE' + res = req.get_response(fakes.wsgi_app()) + self.assertEqual(res.status_int, 202) + self.assertTrue(self.disassociated) + + def test_floating_ip_release_disassociated(self): + self.disassociated = False + + def fake_get_floating_ip(ignore, context, id): + return {'id': 1, 'address': '10.10.10.10', + 'fixed_ip': None} + + def fake_disassociate(ignore, context, floating_address): + self.disassociated = True + + self.stubs.Set(network.api.API, "get_floating_ip", + fake_get_floating_ip) + self.stubs.Set(network.api.API, "disassociate_floating_ip", + fake_disassociate) req = webob.Request.blank('/v1.1/123/os-floating-ips/1') req.method = 'DELETE' res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 202) + self.assertFalse(self.disassociated) def test_add_floating_ip_to_instance(self): self.stubs.Set(network.api.API, "associate_floating_ip", @@ -289,8 +323,45 @@ class FloatingIpTest(test.TestCase): self.assertEqual(resp.status_int, 202) self.assertTrue(self.disassociated) - def test_remove_floating_ip_from_instance(self): - body = dict(removeFloatingIp=dict(address='11.0.0.1')) + def test_remove_associated_floating_ip_from_instance(self): + self.disassociated = False + + def fake_get_floating_ip_by_ip(ignore, context, ip): + return {'id': 1, 'address': '10.10.10.10', + 'fixed_ip': {'id': 1}} + + def fake_disassociate(ignore, context, floating_address): + self.disassociated = True + + self.stubs.Set(network.api.API, "get_floating_ip_by_ip", + fake_get_floating_ip_by_ip) + self.stubs.Set(network.api.API, "disassociate_floating_ip", + fake_disassociate) + body = dict(removeFloatingIp=dict(address='10.10.10.10')) + req = webob.Request.blank('/v1.1/123/servers/test_inst/action') + req.method = "POST" + req.body = json.dumps(body) + req.headers["content-type"] = "application/json" + + resp = req.get_response(fakes.wsgi_app()) + self.assertEqual(resp.status_int, 202) + self.assertTrue(self.disassociated) + + def test_remove_disassociated_floating_ip_from_instance(self): + self.disassociated = False + + def fake_get_floating_ip_by_ip(ignore, context, ip): + return {'id': 1, 'address': '10.10.10.10', + 'fixed_ip': None} + + def fake_disassociate(ignore, context, floating_address): + self.disassociated = True + + self.stubs.Set(network.api.API, "get_floating_ip_by_ip", + fake_get_floating_ip_by_ip) + self.stubs.Set(network.api.API, "disassociate_floating_ip", + fake_disassociate) + body = dict(removeFloatingIp=dict(address='10.10.10.10')) req = webob.Request.blank('/v1.1/123/servers/test_inst/action') req.method = "POST" req.body = json.dumps(body) @@ -298,6 +369,7 @@ class FloatingIpTest(test.TestCase): resp = req.get_response(fakes.wsgi_app()) self.assertEqual(resp.status_int, 202) + self.assertFalse(self.disassociated) def test_bad_address_param_in_remove_floating_ip(self): body = dict(removeFloatingIp=dict(badparam='11.0.0.1')) -- cgit