def test_save_instance_changed(self): bdm_object = block_device_obj.BlockDeviceMapping() bdm_object.instance = instance_obj.Instance() self.assertRaises(exception.ObjectActionError, bdm_object.save, self.context)
def __init__(self, instance, address=None, content=None, extra_md=None, conductor_api=None, network_info=None, vd_driver=None): """Creation of this object should basically cover all time consuming collection. Methods after that should not cause time delays due to network operations or lengthy cpu operations. The user should then get a single instance and make multiple method calls on it. """ if not content: content = [] ctxt = context.get_admin_context() # NOTE(danms): This should be removed after bp:compute-manager-objects if not isinstance(instance, instance_obj.Instance): expected = ['metadata', 'system_metadata'] if 'info_cache' in instance: expected.append('info_cache') instance = instance_obj.Instance._from_db_object( ctxt, instance_obj.Instance(), instance, expected_attrs=expected) self.instance = instance self.extra_md = extra_md if conductor_api: capi = conductor_api else: capi = conductor.API() self.availability_zone = ec2utils.get_availability_zone_by_host( instance['host'], capi) self.security_groups = secgroup_obj.SecurityGroupList.get_by_instance( ctxt, instance) self.mappings = _format_instance_mapping(ctxt, instance) if instance.get('user_data', None) is not None: self.userdata_raw = base64.b64decode(instance['user_data']) else: self.userdata_raw = None self.ec2_ids = capi.get_ec2_ids(ctxt, obj_base.obj_to_primitive(instance)) self.address = address # expose instance metadata. self.launch_metadata = utils.instance_meta(instance) self.password = password.extract_password(instance) self.uuid = instance.get('uuid') self.content = {} self.files = [] # get network info, and the rendered network template if network_info is None: network_info = instance.info_cache.network_info self.ip_info = \ ec2utils.get_ip_info_for_instance_from_nw_info(network_info) self.network_config = None cfg = netutils.get_injected_network_template(network_info) if cfg: key = "%04i" % len(self.content) self.content[key] = cfg self.network_config = {"name": "network_config", 'content_path': "/%s/%s" % (CONTENT_DIR, key)} # 'content' is passed in from the configdrive code in # nova/virt/libvirt/driver.py. Thats how we get the injected files # (personalities) in. AFAIK they're not stored in the db at all, # so are not available later (web service metadata time). for (path, contents) in content: key = "%04i" % len(self.content) self.files.append({'path': path, 'content_path': "/%s/%s" % (CONTENT_DIR, key)}) self.content[key] = contents if vd_driver is None: vdclass = importutils.import_class(CONF.vendordata_driver) else: vdclass = vd_driver self.vddriver = vdclass(instance=instance, address=address, extra_md=extra_md, network_info=network_info) self.route_configuration = None
def setUp(self): super(TestNovaNotifier, self).setUp() nova_CONF.compute_driver = 'nova.virt.fake.FakeDriver' nova_CONF.notification_driver = [ nova_notifier.__name__, 'nova.openstack.common.notifier.rpc_notifier', ] nova_CONF.rpc_backend = 'nova.openstack.common.rpc.impl_fake' nova_CONF.vnc_enabled = False nova_CONF.spice.enabled = False self.compute = importutils.import_object(nova_CONF.compute_manager) self.context = context.get_admin_context() self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs fake_network.set_stub_network_methods(self.stubs) self.instance_data = { "display_name": "instance-1", "id": 1, "image_ref": "FAKE", "user_id": "FAKE", "project_id": "FAKE", "display_name": "FAKE NAME", "hostname": "abcdef", "reservation_id": "FAKE RID", "instance_type_id": 1, "architecture": "x86", "memory_mb": "1024", "root_gb": "20", "ephemeral_gb": "0", "vcpus": 1, 'node': "fakenode", "host": "fakehost", "availability_zone": "1e3ce043029547f1a61c1996d1a531a4", "created_at": '2012-05-08 20:23:41', "launched_at": '2012-05-08 20:25:45', "terminated_at": '2012-05-09 20:23:41', "os_type": "linux", "kernel_id": "kernelid", "ramdisk_id": "ramdiskid", "vm_state": vm_states.ACTIVE, "task_state": None, "access_ip_v4": "192.168.5.4", "access_ip_v6": "2001:DB8::0", "metadata": {}, "uuid": "144e08f4-00cb-11e2-888e-5453ed1bbb5f", "system_metadata": {}, "user_data": None, "cleaned": 0, "deleted": None, "vm_mode": None, "deleted_at": None, "disable_terminate": False, "root_device_name": None, "default_swap_device": None, "launched_on": None, "display_description": None, "key_data": None, "key_name": None, "config_drive": None, "power_state": None, "default_ephemeral_device": None, "progress": 0, "scheduled_at": None, "updated_at": None, "shutdown_terminate": False, "cell_name": 'cell', "locked": False, "locked_by": None, "launch_index": 0, "auto_disk_config": False, "ephemeral_key_uuid": None } self.instance = nova_instance.Instance() self.instance = nova_instance.Instance._from_db_object( context, self.instance, self.instance_data, expected_attrs=['metadata', 'system_metadata']) self.stubs.Set(db, 'instance_info_cache_delete', self.do_nothing) self.stubs.Set(db, 'instance_destroy', self.do_nothing) self.stubs.Set(db, 'instance_system_metadata_get', self.fake_db_instance_system_metadata_get) self.stubs.Set(db, 'block_device_mapping_get_all_by_instance', lambda context, instance: {}) self.stubs.Set(db, 'instance_update_and_get_original', lambda *args, **kwargs: (self.instance, self.instance)) self.stubs.Set(flavors, 'extract_flavor', self.fake_extract_flavor) # Set up to capture the notification messages generated by the # plugin and to invoke our notifier plugin. self.notifications = [] ext_mgr = extension.ExtensionManager.make_test_instance(extensions=[ extension.Extension( 'test', None, None, self.Pollster(), ), ], ) self.ext_mgr = ext_mgr self.gatherer = nova_notifier.DeletedInstanceStatsGatherer(ext_mgr) # Initialize the global _gatherer in nova_notifier to use the # gatherer in this test instead of the gatherer in nova_notifier. nova_notifier.initialize_gatherer(self.gatherer) # Terminate the instance to trigger the notification. with contextlib.nested( # Under Grizzly, Nova has moved to no-db access on the # compute node. The compute manager uses RPC to talk to # the conductor. We need to disable communication between # the nova manager and the remote system since we can't # expect the message bus to be available, or the remote # controller to be there if the message bus is online. mock.patch.object(self.compute, 'conductor_api'), # The code that looks up the instance uses a global # reference to the API, so we also have to patch that to # return our fake data. mock.patch.object(nova_notifier.conductor_api, 'instance_get_by_uuid', self.fake_instance_ref_get), mock.patch( 'nova.openstack.common.notifier.rpc_notifier.notify', self.notify)): with mock.patch.object(self.compute.conductor_api, 'instance_destroy', return_value=self.instance): self.compute.terminate_instance(self.context, instance=self.instance, bdms=[], reservations=[])
def test_populate_queued_for_delete(self): cells = [] celldbs = fixtures.CellDatabases() # Create two cell databases and map them for uuid in (uuidsentinel.cell1, uuidsentinel.cell2): cm = cell_mapping.CellMapping(context=self.context, uuid=uuid, database_connection=uuid, transport_url='fake://') cm.create() cells.append(cm) celldbs.add_cell_database(uuid) self.useFixture(celldbs) # Create 5 instances per cell, two deleted, one with matching # queued_for_delete in the instance mapping for cell in cells: for i in range(0, 5): # Instance 4 should be SOFT_DELETED vm_state = (vm_states.SOFT_DELETED if i == 4 else vm_states.ACTIVE) # Instance 2 should already be marked as queued_for_delete qfd = True if i == 2 else None with context.target_cell(self.context, cell) as cctxt: inst = instance.Instance( cctxt, vm_state=vm_state, project_id=self.context.project_id, user_id=self.context.user_id) inst.create() if i in (2, 3): # Instances 2 and 3 are hard-deleted inst.destroy() instance_mapping.InstanceMapping._create_in_db( self.context, { 'project_id': self.context.project_id, 'cell_id': cell.id, 'queued_for_delete': qfd, 'instance_uuid': inst.uuid }) done, total = instance_mapping.populate_queued_for_delete( self.context, 2) # First two needed fixing, and honored the limit self.assertEqual(2, done) self.assertEqual(2, total) done, total = instance_mapping.populate_queued_for_delete( self.context, 1000) # Last six included two that were already done, and spanned to the # next cell self.assertEqual(6, done) self.assertEqual(6, total) mappings = instance_mapping.InstanceMappingList.get_by_project_id( self.context, self.context.project_id) # Check that we have only the expected number of records with # True/False (which implies no NULL records). # Six deleted instances self.assertEqual( 6, len([im for im in mappings if im.queued_for_delete is True])) # Four non-deleted instances self.assertEqual( 4, len([im for im in mappings if im.queued_for_delete is False]))
def fake_instance_obj(context, **updates): expected_attrs = updates.pop('expected_attrs', None) return instance_obj.Instance._from_db_object(context, instance_obj.Instance(), fake_db_instance(**updates), expected_attrs=expected_attrs)
def test_load_invalid(self): inst = instance.Instance() inst.uuid = 'fake-uuid' self.assertRaises(exception.ObjectActionError, inst.obj_load_attr, 'foo')
'display_name': 'Fake Instance', 'root_gb': 10, 'ephemeral_gb': 0, 'instance_type_id': '5', 'system_metadata': { 'image_os_distro': 'rhel' }, 'host': 'host1', 'flavor': TEST_FLAVOR, 'task_state': None, 'power_state': power_state.SHUTDOWN, } TEST_INST_SPAWNING = dict(TEST_INSTANCE, task_state=task_states.SPAWNING) TEST_INST1 = instance.Instance(**TEST_INSTANCE) TEST_INST2 = instance.Instance(**TEST_INST_SPAWNING) TEST_MIGRATION = { 'id': 1, 'source_compute': 'host1', 'dest_compute': 'host2', 'migration_type': 'resize', 'old_instance_type_id': 1, 'new_instance_type_id': 2, } TEST_MIGRATION_SAME_HOST = dict(TEST_MIGRATION, dest_compute='host1') IMAGE1 = { 'id': 1, 'name': 'image1',
def _create_fake_instance(self): self.inst = instance.Instance() self.inst.uuid = uuids.instance self.inst.pci_devices = pci_device.PciDeviceList()
def test_allocacte_device_fail_owner(self): inst_2 = instance.Instance() inst_2.uuid = 'fake-inst-uuid-2' pci_device.claim(self.devobj, self.inst) self.assertRaises(exception.PciDeviceInvalidOwner, pci_device.allocate, self.devobj, inst_2)
def instance_rules(self, instance, network_info): ctxt = context.get_admin_context() if isinstance(instance, dict): # NOTE(danms): allow old-world instance objects from # unconverted callers; all we need is instance.uuid below instance = instance_obj.Instance._from_db_object( ctxt, instance_obj.Instance(), instance, []) ipv4_rules = [] ipv6_rules = [] # Initialize with basic rules self._do_basic_rules(ipv4_rules, ipv6_rules, network_info) # Set up rules to allow traffic to/from DHCP server self._do_dhcp_rules(ipv4_rules, network_info) #Allow project network traffic if CONF.allow_same_net_traffic: self._do_project_network_rules(ipv4_rules, ipv6_rules, network_info) # We wrap these in CONF.use_ipv6 because they might cause # a DB lookup. The other ones are just list operations, so # they're not worth the clutter. if CONF.use_ipv6: # Allow RA responses self._do_ra_rules(ipv6_rules, network_info) security_groups = security_group_obj.SecurityGroupList.get_by_instance( ctxt, instance) # then, security group chains and rules for security_group in security_groups: rules_cls = security_group_rule_obj.SecurityGroupRuleList rules = rules_cls.get_by_security_group(ctxt, security_group) for rule in rules: LOG.debug(_('Adding security group rule: %r'), rule, instance=instance) if not rule['cidr']: version = 4 else: version = netutils.get_ip_version(rule['cidr']) if version == 4: fw_rules = ipv4_rules else: fw_rules = ipv6_rules protocol = rule['protocol'] if protocol: protocol = rule['protocol'].lower() if version == 6 and protocol == 'icmp': protocol = 'icmpv6' args = ['-j ACCEPT'] if protocol: args += ['-p', protocol] if protocol in ['udp', 'tcp']: args += self._build_tcp_udp_rule(rule, version) elif protocol == 'icmp': args += self._build_icmp_rule(rule, version) if rule['cidr']: LOG.debug('Using cidr %r', rule['cidr'], instance=instance) args += ['-s', str(rule['cidr'])] fw_rules += [' '.join(args)] else: if rule['grantee_group']: insts = ( instance_obj.InstanceList.get_by_security_group( ctxt, rule['grantee_group'])) for instance in insts: if instance['info_cache']['deleted']: LOG.debug('ignoring deleted cache') continue nw_info = compute_utils.get_nw_info_for_instance( instance) ips = [ ip['address'] for ip in nw_info.fixed_ips() if ip['version'] == version ] LOG.debug('ips: %r', ips, instance=instance) for ip in ips: subrule = args + ['-s %s' % ip] fw_rules += [' '.join(subrule)] LOG.debug('Using fw_rules: %r', fw_rules, instance=instance) ipv4_rules += ['-j $sg-fallback'] ipv6_rules += ['-j $sg-fallback'] return ipv4_rules, ipv6_rules
def _create_fake_instance(self): self.inst = instance.Instance() self.inst.uuid = 'fake-inst-uuid' self.inst.pci_devices = pci_device.PciDeviceList() self.inst.vm_state = vm_states.ACTIVE self.inst.task_state = None
def test_delete_flavor_no_namespace_fails(self): inst = instance.Instance(system_metadata={}) self.assertRaises(KeyError, inst.delete_flavor, None) self.assertRaises(KeyError, inst.delete_flavor, '')
def test_compat_pci_devices(self): inst = instance.Instance() inst.pci_devices = pci_device.PciDeviceList() primitive = inst.obj_to_primitive(target_version='1.5') self.assertNotIn('pci_devices', primitive)
def instance_obj(self): return instance_obj.Instance._from_db_object( self.ctx, instance_obj.Instance(), self.instance, expected_attrs=instance_obj.INSTANCE_DEFAULT_FIELDS)
def test_format_instance_mapping(self): # Make sure that _format_instance_mappings works. ctxt = None instance_ref0 = instance_obj.Instance( **{ 'id': 0, 'uuid': 'e5fe5518-0288-4fa3-b0c4-c79764101b85', 'root_device_name': None, 'default_ephemeral_device': None, 'default_swap_device': None }) instance_ref1 = instance_obj.Instance( **{ 'id': 0, 'uuid': 'b65cee2f-8c69-4aeb-be2f-f79742548fc2', 'root_device_name': '/dev/sda1', 'default_ephemeral_device': None, 'default_swap_device': None }) def fake_bdm_get(ctxt, uuid, use_slave=False): return [ fake_block_device.FakeDbBlockDeviceDict({ 'volume_id': 87654321, 'snapshot_id': None, 'no_device': None, 'source_type': 'volume', 'destination_type': 'volume', 'delete_on_termination': True, 'device_name': '/dev/sdh' }), fake_block_device.FakeDbBlockDeviceDict({ 'volume_id': None, 'snapshot_id': None, 'no_device': None, 'source_type': 'blank', 'destination_type': 'local', 'guest_format': 'swap', 'delete_on_termination': None, 'device_name': '/dev/sdc' }), fake_block_device.FakeDbBlockDeviceDict({ 'volume_id': None, 'snapshot_id': None, 'no_device': None, 'source_type': 'blank', 'destination_type': 'local', 'guest_format': None, 'delete_on_termination': None, 'device_name': '/dev/sdb' }) ] self.stubs.Set(db, 'block_device_mapping_get_all_by_instance', fake_bdm_get) expected = { 'ami': 'sda1', 'root': '/dev/sda1', 'ephemeral0': '/dev/sdb', 'swap': '/dev/sdc', 'ebs0': '/dev/sdh' } capi = conductor_api.LocalAPI() self.assertEqual(base._format_instance_mapping(ctxt, instance_ref0), block_device._DEFAULT_MAPPINGS) self.assertEqual(base._format_instance_mapping(ctxt, instance_ref1), expected)
def test_bdms_are_skipped_by_default(self, mock_get_bdms): instance = instance_obj.Instance(uuid=uuids.instance_uuid) bmds = instance_notification.BlockDevicePayload.from_instance(instance) self.assertIsNone(bmds)
def fake_inst_obj(context): return instance_obj.Instance._from_db_object( context, instance_obj.Instance(), INSTANCE, expected_attrs=['metadata', 'system_metadata'])
# under the License. import json import mock import webob from nova.api.openstack.compute.contrib import server_external_events from nova import context from nova import exception from nova.objects import instance as instance_obj from nova import test fake_instances = { '00000000-0000-0000-0000-000000000001': instance_obj.Instance(uuid='00000000-0000-0000-0000-000000000001', host='host1'), '00000000-0000-0000-0000-000000000002': instance_obj.Instance(uuid='00000000-0000-0000-0000-000000000002', host='host1'), '00000000-0000-0000-0000-000000000003': instance_obj.Instance(uuid='00000000-0000-0000-0000-000000000003', host='host2'), '00000000-0000-0000-0000-000000000004': instance_obj.Instance(uuid='00000000-0000-0000-0000-000000000004', host=None), } fake_instance_uuids = sorted(fake_instances.keys()) MISSING_UUID = '00000000-0000-0000-0000-000000000005' @classmethod
def _create_node(self, node_info=None, nic_info=None, ephemeral=True): result = {} if node_info is None: node_info = bm_db_utils.new_bm_node( id=123, service_host='test_host', cpus=2, memory_mb=2048, ) if nic_info is None: nic_info = [ {'address': '01:23:45:67:89:01', 'datapath_id': '0x1', 'port_no': 1}, {'address': '01:23:45:67:89:02', 'datapath_id': '0x2', 'port_no': 2}, ] result['node_info'] = node_info result['nic_info'] = nic_info result['node'] = db.bm_node_create(self.context, node_info) for nic in nic_info: db.bm_interface_create( self.context, result['node']['id'], nic['address'], nic['datapath_id'], nic['port_no'], ) if ephemeral: result['instance'] = utils.get_test_instance() else: flavor = utils.get_test_flavor(options={'ephemeral_gb': 0}) result['instance'] = utils.get_test_instance(flavor=flavor) result['instance']['node'] = result['node']['uuid'] result['spawn_params'] = dict( admin_password='******', block_device_info=None, context=self.context, image_meta=utils.get_test_image_info( None, result['instance']), injected_files=[('/fake/path', 'hello world')], instance=result['instance'], network_info=utils.get_test_network_info(), ) result['destroy_params'] = dict( context=self.context, instance=result['instance'], network_info=result['spawn_params']['network_info'], block_device_info=result['spawn_params']['block_device_info'], ) instance = instance_obj.Instance._from_db_object( self.context, instance_obj.Instance(), result['instance']) instance.node = result['node']['uuid'] result['rebuild_params'] = dict( context=self.context, instance=instance, image_meta=utils.get_test_image_info(None, result['instance']), injected_files=[('/fake/path', 'hello world')], admin_password='******', bdms={}, detach_block_devices=self.mox.CreateMockAnything(), attach_block_devices=self.mox.CreateMockAnything(), network_info=result['spawn_params']['network_info'], block_device_info=result['spawn_params']['block_device_info'], ) return result