From 5cced7a31482d014fe31f815f38fd00e079cc491 Mon Sep 17 00:00:00 2001 From: Michael Still Date: Wed, 3 Apr 2013 16:22:12 +1100 Subject: Allow listing fixed_ips for a given compute host. nova.db.fixed_ip_get_all_by_instance_host was removed in commit bb867ce3948ddc23cf928ca3dda100a1a977896a, but was still used in nova-manage. This patch works around the removal of the foreign key that led to this method being removed. Resolves bug 1163660. Change-Id: Ieccd37c752e245558827615c098604d11ad3d945 --- nova/tests/db/fakes.py | 33 ++++++++++++++++++++------------ nova/tests/test_db_api.py | 43 ++++++++++++++++++++++++++++++++++++++++++ nova/tests/test_nova_manage.py | 19 +++++++++++++++++++ 3 files changed, 83 insertions(+), 12 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/db/fakes.py b/nova/tests/db/fakes.py index 455751809..5556b6eb5 100644 --- a/nova/tests/db/fakes.py +++ b/nova/tests/db/fakes.py @@ -39,12 +39,27 @@ class FakeModel(object): def __repr__(self): return '' % self.values + def get(self, name): + return self.values[name] + def stub_out(stubs, funcs): """Set the stubs in mapping in the db api.""" for func in funcs: func_name = '_'.join(func.__name__.split('_')[1:]) stubs.Set(db, func_name, func) + stubs.Set(db.sqlalchemy.api, func_name, func) + + +fixed_ip_fields = {'id': 0, + 'network_id': 0, + 'address': '192.168.0.100', + 'instance': False, + 'instance_uuid': 'eb57d790-fc60-4119-a51a-f2b0913bdc93', + 'allocated': False, + 'virtual_interface_id': 0, + 'virtual_interface': None, + 'floating_ips': []} def stub_out_db_network_api(stubs): @@ -66,16 +81,6 @@ def stub_out_db_network_api(stubs): 'injected': False, 'vpn_public_address': '192.168.0.2'} - fixed_ip_fields = {'id': 0, - 'network_id': 0, - 'address': '192.168.0.100', - 'instance': False, - 'instance_id': 0, - 'allocated': False, - 'virtual_interface_id': 0, - 'virtual_interface': None, - 'floating_ips': []} - flavor_fields = {'id': 0, 'rxtx_cap': 3} @@ -196,8 +201,11 @@ def stub_out_db_network_api(stubs): def fake_fixed_ip_disassociate_all_by_timeout(context, host, time): return 0 - def fake_fixed_ip_get_by_instance(context, instance_id): - ips = filter(lambda i: i['instance_id'] == instance_id, + def fake_fixed_ip_get_all(context): + return [FakeModel(i) for i in fixed_ips] + + def fake_fixed_ip_get_by_instance(context, instance_uuid): + ips = filter(lambda i: i['instance_uuid'] == instance_uuid, fixed_ips) return [FakeModel(i) for i in ips] @@ -306,6 +314,7 @@ def stub_out_db_network_api(stubs): fake_fixed_ip_create, fake_fixed_ip_disassociate, fake_fixed_ip_disassociate_all_by_timeout, + fake_fixed_ip_get_all, fake_fixed_ip_get_by_instance, fake_fixed_ip_get_by_address, fake_fixed_ip_update, diff --git a/nova/tests/test_db_api.py b/nova/tests/test_db_api.py index 93685c359..422b6583f 100644 --- a/nova/tests/test_db_api.py +++ b/nova/tests/test_db_api.py @@ -20,6 +20,7 @@ """Unit tests for the DB API.""" import datetime +import types import uuid as stdlib_uuid from oslo.config import cfg @@ -30,6 +31,7 @@ from sqlalchemy.sql.expression import select from nova import context from nova import db +from nova.db.sqlalchemy import api as sqlalchemy_api from nova import exception from nova.openstack.common.db.sqlalchemy import session as db_session from nova.openstack.common import timeutils @@ -320,6 +322,27 @@ class DbApiTestCase(DbTestCase): fixed_ip_ref = db.fixed_ip_get_by_floating_address(ctxt, floating) self.assertEqual(fixed, fixed_ip_ref['address']) + def test_fixed_ip_get_by_host(self): + ctxt = context.get_admin_context() + + values = {'address': 'fixed1'} + fixed1 = db.fixed_ip_create(ctxt, values) + instance1 = self.create_instances_with_args() + db.fixed_ip_associate(ctxt, 'fixed1', instance1['uuid']) + + values = {'address': 'fixed2'} + fixed2 = db.fixed_ip_create(ctxt, values) + instance2 = self.create_instances_with_args() + db.fixed_ip_associate(ctxt, 'fixed2', instance2['uuid']) + + values = {'address': 'fixed3'} + fixed3 = db.fixed_ip_create(ctxt, values) + instance3 = self.create_instances_with_args(host='host2') + db.fixed_ip_associate(ctxt, 'fixed3', instance3['uuid']) + + result = db.fixed_ip_get_by_host(ctxt, 'host1') + self.assertEqual(2, len(result)) + def test_floating_ip_get_by_fixed_address(self): ctxt = context.get_admin_context() values = {'address': 'fixed'} @@ -1616,6 +1639,26 @@ class AggregateDBApiTestCase(test.TestCase): ctxt, result['id'], _get_fake_aggr_hosts()[0]) +class SqlAlchemyDbApiTestCase(DbTestCase): + def test_instance_get_all_by_host(self): + ctxt = context.get_admin_context() + + self.create_instances_with_args() + self.create_instances_with_args() + self.create_instances_with_args(host='host2') + result = sqlalchemy_api._instance_get_all_uuids_by_host(ctxt, 'host1') + self.assertEqual(2, len(result)) + + def test_instance_get_all_uuids_by_host(self): + ctxt = context.get_admin_context() + self.create_instances_with_args() + self.create_instances_with_args() + self.create_instances_with_args(host='host2') + result = sqlalchemy_api._instance_get_all_uuids_by_host(ctxt, 'host1') + self.assertEqual(2, len(result)) + self.assertEqual(types.UnicodeType, type(result[0])) + + class CapacityTestCase(test.TestCase): def setUp(self): super(CapacityTestCase, self).setUp() diff --git a/nova/tests/test_nova_manage.py b/nova/tests/test_nova_manage.py index 3dc5cc5c2..654e68bfc 100644 --- a/nova/tests/test_nova_manage.py +++ b/nova/tests/test_nova_manage.py @@ -15,6 +15,7 @@ # License for the specific language governing permissions and limitations # under the License. +import fixtures import StringIO import sys @@ -50,6 +51,24 @@ class FixedIpCommandsTestCase(test.TestCase): def test_unreserve_nonexistent_address(self): self.assertEqual(2, self.commands.unreserve('55.55.55.55')) + def test_list(self): + self.useFixture(fixtures.MonkeyPatch('sys.stdout', + StringIO.StringIO())) + self.commands.list() + self.assertTrue(sys.stdout.getvalue().find('192.168.0.100') != -1) + + def test_list_just_one_host(self): + def fake_fixed_ip_get_by_host(*args, **kwargs): + return [db_fakes.fixed_ip_fields] + + self.useFixture(fixtures.MonkeyPatch( + 'nova.db.fixed_ip_get_by_host', + fake_fixed_ip_get_by_host)) + self.useFixture(fixtures.MonkeyPatch('sys.stdout', + StringIO.StringIO())) + self.commands.list('banana') + self.assertTrue(sys.stdout.getvalue().find('192.168.0.100') != -1) + class FloatingIpCommandsTestCase(test.TestCase): def setUp(self): -- cgit