From 84114e426f3973d78fc0f6766f3ad7577349da7a Mon Sep 17 00:00:00 2001 From: Maru Newby Date: Thu, 1 Mar 2012 20:55:53 -0800 Subject: Update fixed_ip_associate to not use relationships * fixed_ip_associate was using the FixedIp.instance and FixedIp.network relationships, which were removed. This change updates the method to use instance_id and network_id instead. * Adds unit tests for fixed_ip_associate * Resolves bug 943029 Change-Id: I0829d110058b8799a520ab68b5fa1b2370b9cc3e --- nova/db/sqlalchemy/api.py | 12 ++++-------- nova/tests/test_db_api.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index b824146a0..e5e277942 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -993,16 +993,12 @@ def fixed_ip_associate(context, address, instance_id, network_id=None, if fixed_ip_ref is None: raise exception.FixedIpNotFoundForNetwork(address=address, network_id=network_id) - if fixed_ip_ref.instance is not None: + if fixed_ip_ref.instance_id: raise exception.FixedIpAlreadyInUse(address=address) - if not fixed_ip_ref.network: - fixed_ip_ref.network = network_get(context, - network_id, - session=session) - fixed_ip_ref.instance = instance_get(context, - instance_id, - session=session) + if not fixed_ip_ref.network_id: + fixed_ip_ref.network_id = network_id + fixed_ip_ref.instance_id = instance_id session.add(fixed_ip_ref) return fixed_ip_ref['address'] diff --git a/nova/tests/test_db_api.py b/nova/tests/test_db_api.py index 985de8e26..9b6f2b0d0 100644 --- a/nova/tests/test_db_api.py +++ b/nova/tests/test_db_api.py @@ -679,3 +679,43 @@ class CapacityTestCase(test.TestCase): self.assertEquals(x.free_disk_gb, 2000) self.assertEquals(x.current_workload, 2) self.assertEquals(x.running_vms, 5) + + +class TestIpAllocation(test.TestCase): + + def setUp(self): + super(TestIpAllocation, self).setUp() + self.ctxt = context.get_admin_context() + self.instance = db.instance_create(self.ctxt, {}) + self.network = db.network_create_safe(self.ctxt, {}) + + def create_fixed_ip(self, **params): + default_params = {'address': '192.168.0.1'} + default_params.update(params) + return db.fixed_ip_create(self.ctxt, default_params) + + def test_fixed_ip_associate_fails_if_ip_not_in_network(self): + self.assertRaises(exception.FixedIpNotFoundForNetwork, + db.fixed_ip_associate, + self.ctxt, None, None) + + def test_fixed_ip_associate_fails_if_ip_in_use(self): + address = self.create_fixed_ip(instance_id=self.instance.id) + self.assertRaises(exception.FixedIpAlreadyInUse, + db.fixed_ip_associate, + self.ctxt, address, self.instance.id) + + def test_fixed_ip_associate_succeeds(self): + address = self.create_fixed_ip(network_id=self.network.id) + db.fixed_ip_associate(self.ctxt, address, self.instance.id, + network_id=self.network.id) + fixed_ip = db.fixed_ip_get_by_address(self.ctxt, address) + self.assertEqual(fixed_ip.instance_id, self.instance.id) + + def test_fixed_ip_associate_succeeds_and_sets_network(self): + address = self.create_fixed_ip() + db.fixed_ip_associate(self.ctxt, address, self.instance.id, + network_id=self.network.id) + fixed_ip = db.fixed_ip_get_by_address(self.ctxt, address) + self.assertEqual(fixed_ip.instance_id, self.instance.id) + self.assertEqual(fixed_ip.network_id, self.network.id) -- cgit