diff --git a/nova/tests/test_compute_utils.py b/nova/tests/test_compute_utils.py index b452ec66..7ba03063 100644 --- a/nova/tests/test_compute_utils.py +++ b/nova/tests/test_compute_utils.py @@ -86,7 +86,7 @@ class UsageInfoTestCase(test.TestCase): type_id = instance_types.get_instance_type_by_name('m1.tiny')['id'] self.assertEquals(str(payload['instance_type_id']), str(type_id)) for attr in ('display_name', 'created_at', 'launched_at', - 'state', 'state_description', 'fixed_ips', + 'state', 'state_description', 'bandwidth', 'audit_period_beginning', 'audit_period_ending'): self.assertTrue(attr in payload, diff --git a/nova/tests/test_db_api.py b/nova/tests/test_db_api.py index 524a2811..356be7a5 100644 --- a/nova/tests/test_db_api.py +++ b/nova/tests/test_db_api.py @@ -67,19 +67,6 @@ class DbApiTestCase(test.TestCase): self.project_id) self.assertEqual(instance['id'], result['id']) - def test_instance_get_project_vpn_joins(self): - values = {'instance_type_id': FLAGS.default_instance_type, - 'image_ref': FLAGS.vpn_image_id, - 'project_id': self.project_id, - } - instance = db.instance_create(self.context, values) - _setup_networking(instance['id']) - result = db.instance_get_project_vpn(self.context.elevated(), - self.project_id) - self.assertEqual(instance['id'], result['id']) - self.assertEqual(result['fixed_ips'][0]['floating_ips'][0].address, - '1.2.1.2') - def test_instance_get_all_by_filters(self): args = {'reservation_id': 'a', 'image_ref': 1, 'host': 'host1'} inst1 = db.instance_create(self.context, args) diff --git a/nova/tests/test_libvirt.py b/nova/tests/test_libvirt.py index 8334e6f9..1490f917 100644 --- a/nova/tests/test_libvirt.py +++ b/nova/tests/test_libvirt.py @@ -1328,17 +1328,22 @@ class IptablesFirewallTestCase(test.TestCase): return '', '' print cmd, kwargs + network_info = _fake_network_info(self.stubs, 1) + def get_fixed_ips(*args, **kwargs): ips = [] for network, info in network_info: ips.extend(info['ips']) - return [ip['ip'] for ip in ips] + return [ip['ip'] for ip in ips] + + def nw_info(*args, **kwargs): + return network_info from nova.network import linux_net linux_net.iptables_manager.execute = fake_iptables_execute - network_info = _fake_network_info(self.stubs, 1) - self.stubs.Set(db, 'instance_get_fixed_addresses', get_fixed_ips) + fake_network.stub_out_nw_api_get_instance_nw_info(self.stubs, + nw_info) self.fw.prepare_instance_filter(instance_ref, network_info) self.fw.apply_instance_filter(instance_ref, network_info) diff --git a/nova/tests/test_linux_net.py b/nova/tests/test_linux_net.py index c8f8cbe0..21c5284a 100644 --- a/nova/tests/test_linux_net.py +++ b/nova/tests/test_linux_net.py @@ -19,6 +19,7 @@ import os import mox +from nova import context from nova import db from nova import flags from nova import log as logging @@ -102,8 +103,7 @@ fixed_ips = [{'id': 0, 'instance_id': 0, 'allocated': True, 'virtual_interface_id': 0, - 'virtual_interface': addresses[0], - 'instance': instances[0], + 'instance_id': 0, 'floating_ips': []}, {'id': 1, 'network_id': 1, @@ -111,8 +111,7 @@ fixed_ips = [{'id': 0, 'instance_id': 0, 'allocated': True, 'virtual_interface_id': 1, - 'virtual_interface': addresses[1], - 'instance': instances[0], + 'instance_id': 0, 'floating_ips': []}, {'id': 2, 'network_id': 1, @@ -120,8 +119,7 @@ fixed_ips = [{'id': 0, 'instance_id': 1, 'allocated': True, 'virtual_interface_id': 2, - 'virtual_interface': addresses[2], - 'instance': instances[1], + 'instance_id': 1, 'floating_ips': []}, {'id': 3, 'network_id': 0, @@ -129,8 +127,7 @@ fixed_ips = [{'id': 0, 'instance_id': 1, 'allocated': True, 'virtual_interface_id': 3, - 'virtual_interface': addresses[3], - 'instance': instances[1], + 'instance_id': 1, 'floating_ips': []}, {'id': 4, 'network_id': 0, @@ -138,8 +135,7 @@ fixed_ips = [{'id': 0, 'instance_id': 0, 'allocated': True, 'virtual_interface_id': 4, - 'virtual_interface': addresses[4], - 'instance': instances[0], + 'instance_id': 0, 'floating_ips': []}, {'id': 5, 'network_id': 1, @@ -147,8 +143,7 @@ fixed_ips = [{'id': 0, 'instance_id': 1, 'allocated': True, 'virtual_interface_id': 5, - 'virtual_interface': addresses[5], - 'instance': instances[1], + 'instance_id': 1, 'floating_ips': []}] @@ -156,37 +151,31 @@ vifs = [{'id': 0, 'address': 'DE:AD:BE:EF:00:00', 'uuid': '00000000-0000-0000-0000-0000000000000000', 'network_id': 0, - 'network': networks[0], 'instance_id': 0}, {'id': 1, 'address': 'DE:AD:BE:EF:00:01', 'uuid': '00000000-0000-0000-0000-0000000000000001', 'network_id': 1, - 'network': networks[1], 'instance_id': 0}, {'id': 2, 'address': 'DE:AD:BE:EF:00:02', 'uuid': '00000000-0000-0000-0000-0000000000000002', 'network_id': 1, - 'network': networks[1], 'instance_id': 1}, {'id': 3, 'address': 'DE:AD:BE:EF:00:03', 'uuid': '00000000-0000-0000-0000-0000000000000003', 'network_id': 0, - 'network': networks[0], 'instance_id': 1}, {'id': 4, 'address': 'DE:AD:BE:EF:00:04', 'uuid': '00000000-0000-0000-0000-0000000000000004', 'network_id': 0, - 'network': networks[0], 'instance_id': 0}, {'id': 5, 'address': 'DE:AD:BE:EF:00:05', 'uuid': '00000000-0000-0000-0000-0000000000000005', 'network_id': 1, - 'network': networks[1], 'instance_id': 1}] @@ -197,10 +186,20 @@ class LinuxNetworkTestCase(test.TestCase): network_driver = FLAGS.network_driver self.driver = utils.import_object(network_driver) self.driver.db = db + self.context = context.RequestContext('testuser', 'testproject', + is_admin=True) def test_update_dhcp_for_nw00(self): self.flags(use_single_default_gateway=True) + def get_vif(_context, vif_id): + return vifs[vif_id] + + def get_instance(_context, instance_id): + return instances[instance_id] + + self.stubs.Set(db, 'virtual_interface_get', get_vif) + self.stubs.Set(db, 'instance_get', get_instance) self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') self.mox.StubOutWithMock(self.driver, 'write_to_file') @@ -236,10 +235,19 @@ class LinuxNetworkTestCase(test.TestCase): self.mox.ReplayAll() - self.driver.update_dhcp(None, "eth0", networks[0]) + self.driver.update_dhcp(self.context, "eth0", networks[0]) def test_update_dhcp_for_nw01(self): self.flags(use_single_default_gateway=True) + + def get_vif(_context, vif_id): + return vifs[vif_id] + + def get_instance(_context, instance_id): + return instances[instance_id] + + self.stubs.Set(db, 'virtual_interface_get', get_vif) + self.stubs.Set(db, 'instance_get', get_instance) self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') self.mox.StubOutWithMock(self.driver, 'write_to_file') @@ -275,10 +283,19 @@ class LinuxNetworkTestCase(test.TestCase): self.mox.ReplayAll() - self.driver.update_dhcp(None, "eth0", networks[0]) + self.driver.update_dhcp(self.context, "eth0", networks[0]) def test_get_dhcp_hosts_for_nw00(self): self.flags(use_single_default_gateway=True) + + def get_vif(_context, vif_id): + return vifs[vif_id] + + def get_instance(_context, instance_id): + return instances[instance_id] + + self.stubs.Set(db, 'virtual_interface_get', get_vif) + self.stubs.Set(db, 'instance_get', get_instance) self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') db.network_get_associated_fixed_ips(mox.IgnoreArg(), @@ -288,16 +305,25 @@ class LinuxNetworkTestCase(test.TestCase): self.mox.ReplayAll() expected = \ - "10.0.0.1,fake_instance00.novalocal,"\ + "DE:AD:BE:EF:00:00,fake_instance00.novalocal,"\ "192.168.0.100,net:NW-i00000000-0\n"\ - "10.0.0.4,fake_instance01.novalocal,"\ + "DE:AD:BE:EF:00:03,fake_instance01.novalocal,"\ "192.168.1.101,net:NW-i00000001-0" - actual_hosts = self.driver.get_dhcp_hosts(None, networks[1]) + actual_hosts = self.driver.get_dhcp_hosts(self.context, networks[1]) self.assertEquals(actual_hosts, expected) def test_get_dhcp_hosts_for_nw01(self): self.flags(use_single_default_gateway=True) + + def get_vif(_context, vif_id): + return vifs[vif_id] + + def get_instance(_context, instance_id): + return instances[instance_id] + + self.stubs.Set(db, 'virtual_interface_get', get_vif) + self.stubs.Set(db, 'instance_get', get_instance) self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') db.network_get_associated_fixed_ips(mox.IgnoreArg(), @@ -307,15 +333,19 @@ class LinuxNetworkTestCase(test.TestCase): self.mox.ReplayAll() expected = \ - "10.0.0.2,fake_instance00.novalocal,"\ + "DE:AD:BE:EF:00:01,fake_instance00.novalocal,"\ "192.168.1.100,net:NW-i00000000-1\n"\ - "10.0.0.3,fake_instance01.novalocal,"\ + "DE:AD:BE:EF:00:02,fake_instance01.novalocal,"\ "192.168.0.101,net:NW-i00000001-1" - actual_hosts = self.driver.get_dhcp_hosts(None, networks[0]) + actual_hosts = self.driver.get_dhcp_hosts(self.context, networks[0]) self.assertEquals(actual_hosts, expected) def test_get_dhcp_opts_for_nw00(self): + def get_instance(_context, instance_id): + return instances[instance_id] + + self.stubs.Set(db, 'instance_get', get_instance) self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') @@ -337,11 +367,16 @@ class LinuxNetworkTestCase(test.TestCase): self.mox.ReplayAll() expected_opts = 'NW-i00000001-0,3' - actual_opts = self.driver.get_dhcp_opts(None, networks[0]) + actual_opts = self.driver.get_dhcp_opts(self.context, networks[0]) self.assertEquals(actual_opts, expected_opts) def test_get_dhcp_opts_for_nw01(self): + def get_instance(_context, instance_id): + print instance_id + return instances[instance_id] + + self.stubs.Set(db, 'instance_get', get_instance) self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') @@ -363,18 +398,20 @@ class LinuxNetworkTestCase(test.TestCase): self.mox.ReplayAll() expected_opts = "NW-i00000000-1,3" - actual_opts = self.driver.get_dhcp_opts(None, networks[1]) + actual_opts = self.driver.get_dhcp_opts(self.context, networks[1]) self.assertEquals(actual_opts, expected_opts) def test_dhcp_opts_not_default_gateway_network(self): expected = "NW-i00000000-0,3" - actual = self.driver._host_dhcp_opts(fixed_ips[0]) + actual = self.driver._host_dhcp_opts(fixed_ips[0], instances[0]) self.assertEquals(actual, expected) def test_host_dhcp_without_default_gateway_network(self): - expected = ("10.0.0.1,fake_instance00.novalocal,192.168.0.100") - actual = self.driver._host_dhcp(fixed_ips[0]) + expected = ','.join(['DE:AD:BE:EF:00:00', + 'fake_instance00.novalocal', + '192.168.0.100']) + actual = self.driver._host_dhcp(fixed_ips[0], vifs[0], instances[0]) self.assertEquals(actual, expected) def test_linux_bridge_driver_plug(self): diff --git a/nova/tests/test_network.py b/nova/tests/test_network.py index 2776f366..8f8efc99 100644 --- a/nova/tests/test_network.py +++ b/nova/tests/test_network.py @@ -35,15 +35,6 @@ LOG = logging.getLogger('nova.tests.network') HOST = "testhost" -class FakeModel(dict): - """Represent a model from the db""" - def __init__(self, *args, **kwargs): - self.update(kwargs) - - def __getattr__(self, name): - return self[name] - - networks = [{'id': 0, 'uuid': "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 'label': 'test0', @@ -85,7 +76,6 @@ networks = [{'id': 0, 'project_id': 'fake_project', 'vpn_public_address': '192.168.1.2'}] - fixed_ips = [{'id': 0, 'network_id': 0, 'address': '192.168.0.100', @@ -118,19 +108,16 @@ vifs = [{'id': 0, 'address': 'DE:AD:BE:EF:00:00', 'uuid': '00000000-0000-0000-0000-0000000000000000', 'network_id': 0, - 'network': FakeModel(**networks[0]), 'instance_id': 0}, {'id': 1, 'address': 'DE:AD:BE:EF:00:01', 'uuid': '00000000-0000-0000-0000-0000000000000001', 'network_id': 1, - 'network': FakeModel(**networks[1]), 'instance_id': 0}, {'id': 2, 'address': 'DE:AD:BE:EF:00:02', 'uuid': '00000000-0000-0000-0000-0000000000000002', 'network_id': 2, - 'network': None, 'instance_id': 0}] @@ -196,6 +183,7 @@ class FlatNetworkTestCase(test.TestCase): self.assertDictListMatch(info['ips'], check) def test_validate_networks(self): + self.mox.StubOutWithMock(db, 'network_get') self.mox.StubOutWithMock(db, 'network_get_all_by_uuids') self.mox.StubOutWithMock(db, "fixed_ip_get_by_address") @@ -203,11 +191,13 @@ class FlatNetworkTestCase(test.TestCase): "192.168.1.100")] db.network_get_all_by_uuids(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(networks) + db.network_get(mox.IgnoreArg(), + mox.IgnoreArg()).AndReturn(networks[1]) - fixed_ips[1]['network'] = FakeModel(**networks[1]) - fixed_ips[1]['instance'] = None + ip = fixed_ips[1].copy() + ip['instance_id'] = None db.fixed_ip_get_by_address(mox.IgnoreArg(), - mox.IgnoreArg()).AndReturn(fixed_ips[1]) + mox.IgnoreArg()).AndReturn(ip) self.mox.ReplayAll() self.network.validate_networks(self.context, requested_networks) @@ -448,6 +438,10 @@ class VlanNetworkTestCase(test.TestCase): cidr='192.168.0.1/24', network_size=100) def test_validate_networks(self): + def network_get(_context, network_id): + return networks[network_id] + + self.stubs.Set(db, 'network_get', network_get) self.mox.StubOutWithMock(db, 'network_get_all_by_uuids') self.mox.StubOutWithMock(db, "fixed_ip_get_by_address") @@ -457,8 +451,8 @@ class VlanNetworkTestCase(test.TestCase): mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(networks) - fixed_ips[1]['network'] = FakeModel(**networks[1]) - fixed_ips[1]['instance'] = None + fixed_ips[1]['network_id'] = networks[1]['id'] + fixed_ips[1]['instance_id'] = None db.fixed_ip_get_by_address(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(fixed_ips[1]) @@ -613,14 +607,20 @@ class VlanNetworkTestCase(test.TestCase): return {'address': '10.0.0.1', 'pool': 'nova', 'interface': 'eth0', - 'network': {'multi_host': False, 'host': 'jibberjabber'}} + 'network_id': 'blah'} + + def fake4_network(*args, **kwargs): + return {'multi_host': False, 'host': 'jibberjabber'} # fixed ip with local host def fake5(*args, **kwargs): return {'address': '10.0.0.1', 'pool': 'nova', 'interface': 'eth0', - 'network': {'multi_host': False, 'host': 'testhost'}} + 'network_id': 'blahblah'} + + def fake5_network(*args, **kwargs): + return {'multi_host': False, 'host': 'testhost'} def fake6(*args, **kwargs): self.local = False @@ -643,6 +643,7 @@ class VlanNetworkTestCase(test.TestCase): # does not raise and makes call remotely self.local = True self.stubs.Set(self.network.db, 'fixed_ip_get_by_address', fake4) + self.stubs.Set(self.network.db, 'network_get', fake4_network) self.stubs.Set(rpc, 'cast', fake6) self.network.associate_floating_ip(ctxt, mox.IgnoreArg(), mox.IgnoreArg()) @@ -651,6 +652,7 @@ class VlanNetworkTestCase(test.TestCase): # does not raise and makes call locally self.local = False self.stubs.Set(self.network.db, 'fixed_ip_get_by_address', fake5) + self.stubs.Set(self.network.db, 'network_get', fake5_network) self.stubs.Set(self.network, '_associate_floating_ip', fake7) self.network.associate_floating_ip(ctxt, mox.IgnoreArg(), mox.IgnoreArg()) @@ -682,14 +684,21 @@ class VlanNetworkTestCase(test.TestCase): return {'address': '10.0.0.1', 'pool': 'nova', 'interface': 'eth0', - 'network': {'multi_host': False, 'host': 'jibberjabber'}} + 'network_id': 'blah'} + + def fake4_network(*args, **kwargs): + return {'multi_host': False, + 'host': 'jibberjabber'} # fixed ip with local host def fake5(*args, **kwargs): return {'address': '10.0.0.1', 'pool': 'nova', 'interface': 'eth0', - 'network': {'multi_host': False, 'host': 'testhost'}} + 'network_id': 'blahblah'} + + def fake5_network(*args, **kwargs): + return {'multi_host': False, 'host': 'testhost'} def fake6(*args, **kwargs): self.local = False @@ -711,6 +720,7 @@ class VlanNetworkTestCase(test.TestCase): # does not raise and makes call remotely self.local = True self.stubs.Set(self.network.db, 'fixed_ip_get', fake4) + self.stubs.Set(self.network.db, 'network_get', fake4_network) self.stubs.Set(rpc, 'cast', fake6) self.network.disassociate_floating_ip(ctxt, mox.IgnoreArg()) self.assertFalse(self.local) @@ -718,6 +728,7 @@ class VlanNetworkTestCase(test.TestCase): # does not raise and makes call locally self.local = False self.stubs.Set(self.network.db, 'fixed_ip_get', fake5) + self.stubs.Set(self.network.db, 'network_get', fake5_network) self.stubs.Set(self.network, '_disassociate_floating_ip', fake7) self.network.disassociate_floating_ip(ctxt, mox.IgnoreArg()) self.assertTrue(self.local) @@ -752,6 +763,11 @@ class VlanNetworkTestCase(test.TestCase): """Makes sure that we cannot deallocaate or disassociate a public ip of other project""" + def network_get(_context, network_id): + return networks[network_id] + + self.stubs.Set(db, 'network_get', network_get) + context1 = context.RequestContext('user', 'project1') context2 = context.RequestContext('user', 'project2') @@ -789,6 +805,7 @@ class VlanNetworkTestCase(test.TestCase): float_addr) # Clean up the ip addresses + self.network.disassociate_floating_ip(context1, float_addr) self.network.deallocate_floating_ip(context1, float_addr) self.network.deallocate_fixed_ip(context1, fix_addr) db.floating_ip_destroy(context1.elevated(), float_addr) diff --git a/nova/tests/test_xenapi.py b/nova/tests/test_xenapi.py index 77cde2a0..180400af 100644 --- a/nova/tests/test_xenapi.py +++ b/nova/tests/test_xenapi.py @@ -1411,14 +1411,19 @@ class XenAPIDom0IptablesFirewallTestCase(test.TestCase): instance_ref = db.instance_get(admin_ctxt, instance_ref['id']) src_instance_ref = db.instance_get(admin_ctxt, src_instance_ref['id']) + network_info = fake_network.fake_get_instance_nw_info(self.stubs, 1) + def get_fixed_ips(*args, **kwargs): ips = [] for _n, info in network_info: ips.extend(info['ips']) return [ip['ip'] for ip in ips] - network_info = fake_network.fake_get_instance_nw_info(self.stubs, 1) - self.stubs.Set(db, 'instance_get_fixed_addresses', get_fixed_ips) + def nw_info(*args, **kwargs): + return network_info + + fake_network.stub_out_nw_api_get_instance_nw_info(self.stubs, + nw_info) self.fw.prepare_instance_filter(instance_ref, network_info) self.fw.apply_instance_filter(instance_ref, network_info) diff --git a/nova/tests/vmwareapi/db_fakes.py b/nova/tests/vmwareapi/db_fakes.py index c745944c..599ba271 100644 --- a/nova/tests/vmwareapi/db_fakes.py +++ b/nova/tests/vmwareapi/db_fakes.py @@ -96,10 +96,6 @@ def stub_out_db_instance_api(stubs): """Stubs out the db.instance_action_create method.""" pass - def fake_instance_get_fixed_addresses(context, instance_id): - """Stubs out the db.instance_get_fixed_address method.""" - return '10.10.10.10' - def fake_instance_type_get_all(context, inactive=0, filters=None): return INSTANCE_TYPES.values() @@ -109,7 +105,5 @@ def stub_out_db_instance_api(stubs): stubs.Set(db, 'instance_create', fake_instance_create) stubs.Set(db, 'network_get_by_instance', fake_network_get_by_instance) stubs.Set(db, 'instance_action_create', fake_instance_action_create) - stubs.Set(db, 'instance_get_fixed_addresses', - fake_instance_get_fixed_addresses) stubs.Set(db, 'instance_type_get_all', fake_instance_type_get_all) stubs.Set(db, 'instance_type_get_by_name', fake_instance_type_get_by_name)