From c63c42146704fdf19f2f163ade62033313e27dc9 Mon Sep 17 00:00:00 2001 From: John Griffith Date: Wed, 7 Mar 2012 17:45:12 -0700 Subject: Update floating auto assignment to use the model * addresses bug 928819 * previously submitted changes (#change,4236) * unit tests added that don't use fakes for everything * added testAssert to check assignment process * added call to deallocate as well Change-Id: I46503e6e88031a6e1ab3ac76163402091168c6b0 --- nova/api/openstack/common.py | 3 ++- nova/network/manager.py | 6 ++---- nova/tests/network/test_manager.py | 40 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/nova/api/openstack/common.py b/nova/api/openstack/common.py index 3fefa6718..719a74bb9 100644 --- a/nova/api/openstack/common.py +++ b/nova/api/openstack/common.py @@ -288,7 +288,7 @@ def dict_to_query_str(params): def get_networks_for_instance_from_nw_info(nw_info): networks = {} - + LOG.debug(_('Converting nw_info: %s') % nw_info) for vif in nw_info: ips = vif.fixed_ips() floaters = vif.floating_ips() @@ -298,6 +298,7 @@ def get_networks_for_instance_from_nw_info(nw_info): networks[label]['ips'].extend(ips) networks[label]['floating_ips'].extend(floaters) + LOG.debug(_('Converted networks: %s') % networks) return networks diff --git a/nova/network/manager.py b/nova/network/manager.py index 41ddf57c5..e80396bdd 100644 --- a/nova/network/manager.py +++ b/nova/network/manager.py @@ -320,10 +320,8 @@ class FloatingIP(object): self.db.floating_ip_set_auto_assigned(context, floating_address) # get the first fixed address belonging to the instance - for nw, info in nw_info: - if info.get('ips'): - fixed_address = info['ips'][0]['ip'] - break + fixed_ips = nw_info.fixed_ips() + fixed_address = fixed_ips[0]['address'] # associate the floating ip to fixed_ip self.associate_floating_ip(context, diff --git a/nova/tests/network/test_manager.py b/nova/tests/network/test_manager.py index 2e50587f9..589aab067 100644 --- a/nova/tests/network/test_manager.py +++ b/nova/tests/network/test_manager.py @@ -1268,6 +1268,46 @@ class TestFloatingIPManager(network_manager.FloatingIP, """Dummy manager that implements FloatingIP""" +class AllocateTestCase(test.TestCase): + def test_allocate_for_instance(self): + address = "10.10.10.10" + self.flags(auto_assign_floating_ip=True) + self.compute = self.start_service('compute') + self.network = self.start_service('network') + + self.user_id = 'fake' + self.project_id = 'fake' + self.context = context.RequestContext(self.user_id, + self.project_id, + is_admin=True) + + db.floating_ip_create(self.context, + {'address': address, + 'pool': 'nova'}) + inst = db.instance_create(self.context, {'host': self.compute.host, + 'instance_type_id': 1}) + networks = db.network_get_all(self.context) + for network in networks: + db.network_update(self.context, network['id'], + {'host': self.network.host}) + project_id = self.context.project_id + nw_info = self.network.allocate_for_instance(self.context, + instance_id=inst['id'], + instance_uuid='', + host=inst['host'], + vpn=None, + rxtx_factor=3, + project_id=project_id) + self.assertEquals(1, len(nw_info)) + fixed_ip = nw_info.fixed_ips()[0]['address'] + self.assertTrue(utils.is_valid_ipv4(fixed_ip)) + self.network.deallocate_for_instance(self.context, + instance_id=inst['id'], + fixed_ips=fixed_ip, + host=self.network.host, + project_id=project_id) + + class FloatingIPTestCase(test.TestCase): """Tests nova.network.manager.FloatingIP""" def setUp(self): -- cgit