def __init__(self): self.new_pci_tracker() def abort_instance_claim(self, *args, **kwargs): self.icalled = True def drop_move_claim(self, *args, **kwargs): self.rcalled = True def new_pci_tracker(self): ctxt = context.RequestContext('testuser', 'testproject') self.pci_tracker = pci_manager.PciDevTracker(ctxt) @mock.patch('nova.objects.InstancePCIRequests.get_by_instance_uuid', return_value=objects.InstancePCIRequests(requests=[])) class ClaimTestCase(test.NoDBTestCase): def setUp(self): super(ClaimTestCase, self).setUp() self.context = context.RequestContext('fake-user', 'fake-project') self.resources = self._fake_resources() self.tracker = DummyTracker() def _claim(self, limits=None, overhead=None, **kwargs): numa_topology = kwargs.pop('numa_topology', None) instance = self._fake_instance(**kwargs) if numa_topology: db_numa_topology = { 'id': 1, 'created_at': None, 'updated_at': None,
class MoveClaimTestCase(ClaimTestCase): def setUp(self): super(MoveClaimTestCase, self).setUp() self.instance = self._fake_instance() self.get_numa_constraint_patch = None def _claim(self, limits=None, overhead=None, **kwargs): numa_constraint = kwargs.pop('numa_topology', None) instance_type = self._fake_instance_type(**kwargs) if overhead is None: overhead = {'memory_mb': 0} with mock.patch('nova.virt.hardware.numa_get_constraints', return_value=numa_constraint): return claims.MoveClaim(self.context, self.instance, instance_type, {}, self.tracker, self.resources, overhead=overhead, limits=limits) def _set_pci_request(self, claim): request = [{ 'count': 1, 'spec': [{ 'vendor_id': 'v', 'product_id': 'p' }], }] claim.instance.update( system_metadata={'new_pci_requests': jsonutils.dumps(request)}) @mock.patch('nova.objects.InstancePCIRequests.get_by_instance_uuid', return_value=objects.InstancePCIRequests(requests=[])) def test_ext_resources(self, mock_get): self._claim() self.assertTrue(self.tracker.ext_resources_handler.test_called) self.assertTrue(self.tracker.ext_resources_handler.usage_is_itype) @mock.patch('nova.objects.InstancePCIRequests.get_by_instance_uuid', return_value=objects.InstancePCIRequests(requests=[])) def test_abort(self, mock_get): claim = self._abort() self.assertTrue(claim.tracker.rcalled) @mock.patch('nova.objects.InstancePCIRequests.get_by_instance_uuid', return_value=objects.InstancePCIRequests(requests=[])) def test_create_migration_context(self, mock_get): numa_topology = objects.InstanceNUMATopology(cells=[ objects.InstanceNUMACell(id=1, cpuset=set([1, 2]), memory=512) ]) self.instance.numa_topology = None claim = self._claim(numa_topology=numa_topology) migration = objects.Migration(context=self.context, id=42) claim.migration = migration fake_mig_context = mock.Mock(spec=objects.MigrationContext) with mock.patch('nova.objects.MigrationContext', return_value=fake_mig_context) as ctxt_mock: claim.create_migration_context() ctxt_mock.assert_called_once_with(context=self.context, instance_uuid=self.instance.uuid, migration_id=42, old_numa_topology=None, new_numa_topology=mock.ANY) self.assertIsInstance(ctxt_mock.call_args[1]['new_numa_topology'], objects.InstanceNUMATopology) self.assertEqual(migration, claim.migration)
from nova import objects from nova.tests.unit.objects import test_instance_numa_topology from nova.tests.unit.objects import test_objects from nova.tests import uuidsentinel as uuids fake_instance_uuid = uuids.fake fake_migration_context_obj = objects.MigrationContext() fake_migration_context_obj.instance_uuid = fake_instance_uuid fake_migration_context_obj.migration_id = 42 fake_migration_context_obj.new_numa_topology = ( test_instance_numa_topology.fake_obj_numa_topology.obj_clone()) fake_migration_context_obj.old_numa_topology = None fake_migration_context_obj.new_pci_devices = objects.PciDeviceList() fake_migration_context_obj.old_pci_devices = None fake_migration_context_obj.new_pci_requests = (objects.InstancePCIRequests( requests=[objects.InstancePCIRequest(count=123, spec=[])])) fake_migration_context_obj.old_pci_requests = None fake_db_context = { 'created_at': None, 'updated_at': None, 'deleted_at': None, 'deleted': 0, 'instance_uuid': fake_instance_uuid, 'migration_context': jsonutils.dumps(fake_migration_context_obj.obj_to_primitive()),
def fake_claim(context, instance, node, limits): instance.host = self.compute.host requests = objects.InstancePCIRequests(requests=[]) return claims.Claim(context, instance, test_compute.NODENAME, self.rt, _fake_resources(), requests)
def _create_instance_data(self): """Creates an instance record and associated data like BDMs, VIFs, migrations, etc in the source cell and returns the Instance object. The idea is to create as many things from the Instance.INSTANCE_OPTIONAL_ATTRS list as possible. :returns: The created Instance and Migration objects """ # Create the nova-compute services record first. fake_service = test_service._fake_service() fake_service.pop('version', None) # version field is immutable fake_service.pop('id', None) # cannot create with an id set service = objects.Service(self.source_context, **fake_service) service.create() # Create the compute node using the service. fake_compute_node = copy.copy(test_compute_node.fake_compute_node) fake_compute_node['host'] = service.host fake_compute_node['hypervisor_hostname'] = service.host fake_compute_node['stats'] = {} # the object requires a dict fake_compute_node['service_id'] = service.id fake_compute_node.pop('id', None) # cannot create with an id set compute_node = objects.ComputeNode( self.source_context, **fake_compute_node) compute_node.create() # Build an Instance object with basic fields set. updates = { 'metadata': {'foo': 'bar'}, 'system_metadata': {'roles': ['member']}, 'host': compute_node.host, 'node': compute_node.hypervisor_hostname } inst = fake_instance.fake_instance_obj(self.source_context, **updates) delattr(inst, 'id') # cannot create an instance with an id set # Now we have to dirty all of the fields because fake_instance_obj # uses Instance._from_db_object to create the Instance object we have # but _from_db_object calls obj_reset_changes() which resets all of # the fields that were on the object, including the basic stuff like # the 'host' field, which means those fields don't get set in the DB. # TODO(mriedem): This should live in fake_instance_obj with a # make_creatable kwarg. for field in inst.obj_fields: if field in inst: setattr(inst, field, getattr(inst, field)) # Make sure at least one expected basic field is dirty on the Instance. self.assertIn('host', inst.obj_what_changed()) # Set the optional fields on the instance before creating it. inst.pci_requests = objects.InstancePCIRequests(requests=[ objects.InstancePCIRequest( **test_instance_pci_requests.fake_pci_requests[0])]) inst.numa_topology = objects.InstanceNUMATopology( cells=test_instance_numa.fake_obj_numa_topology.cells) inst.trusted_certs = objects.TrustedCerts(ids=[uuids.cert]) inst.vcpu_model = test_vcpu_model.fake_vcpumodel inst.keypairs = objects.KeyPairList(objects=[ objects.KeyPair(**test_keypair.fake_keypair)]) inst.device_metadata = ( test_instance_device_metadata.get_fake_obj_device_metadata( self.source_context)) # FIXME(mriedem): db.instance_create does not handle tags inst.obj_reset_changes(['tags']) inst.create() bdm = { 'instance_uuid': inst.uuid, 'source_type': 'volume', 'destination_type': 'volume', 'volume_id': uuids.volume_id, 'volume_size': 1, 'device_name': '/dev/vda', } bdm = objects.BlockDeviceMapping( self.source_context, **fake_block_device.FakeDbBlockDeviceDict(bdm_dict=bdm)) delattr(bdm, 'id') # cannot create a bdm with an id set bdm.obj_reset_changes(['id']) bdm.create() vif = objects.VirtualInterface( self.source_context, address='de:ad:be:ef:ca:fe', uuid=uuids.port, instance_uuid=inst.uuid) vif.create() info_cache = objects.InstanceInfoCache().new( self.source_context, inst.uuid) info_cache.network_info = network_model.NetworkInfo([ network_model.VIF(id=vif.uuid, address=vif.address)]) info_cache.save(update_cells=False) objects.TagList.create(self.source_context, inst.uuid, ['test']) try: raise test.TestingException('test-fault') except test.TestingException as fault: compute_utils.add_instance_fault_from_exc( self.source_context, inst, fault) objects.InstanceAction().action_start( self.source_context, inst.uuid, 'resize', want_result=False) objects.InstanceActionEvent().event_start( self.source_context, inst.uuid, 'migrate_server', want_result=False) # Create a fake migration for the cross-cell resize operation. migration = objects.Migration( self.source_context, **test_migration.fake_db_migration( instance_uuid=inst.uuid, cross_cell_move=True, migration_type='resize')) delattr(migration, 'id') # cannot create a migration with an id set migration.obj_reset_changes(['id']) migration.create() # Create an old non-resize migration to make sure it is copied to the # target cell database properly. old_migration = objects.Migration( self.source_context, **test_migration.fake_db_migration( instance_uuid=inst.uuid, migration_type='live-migration', status='completed', uuid=uuids.old_migration)) delattr(old_migration, 'id') # cannot create a migration with an id old_migration.obj_reset_changes(['id']) old_migration.create() fake_pci_device = copy.copy(test_pci_device.fake_db_dev) fake_pci_device['extra_info'] = {} # the object requires a dict fake_pci_device['compute_node_id'] = compute_node.id pci_device = objects.PciDevice.create( self.source_context, fake_pci_device) pci_device.allocate(inst) # sets the status and instance_uuid fields pci_device.save() # Return a fresh copy of the instance from the DB with as many joined # fields loaded as possible. expected_attrs = copy.copy(instance_obj.INSTANCE_OPTIONAL_ATTRS) # Cannot load fault from get_by_uuid. expected_attrs.remove('fault') inst = objects.Instance.get_by_uuid( self.source_context, inst.uuid, expected_attrs=expected_attrs) return inst, migration
213581824, 'name': 'f16-x86_64-openstack-sda', 'checksum': '755122332caeb9f661d5c978adb8b45f', 'created_at': '2014-12-10T16:23:14.000000', 'disk_format': 'qcow2', 'id': 'c8b1790e-a07d-4971-b137-44f2432936cd', }) IMAGE_META.obj_reset_changes(recursive=True) PCI_REQUESTS = objects.InstancePCIRequests(requests=[ objects.InstancePCIRequest(count=1), objects.InstancePCIRequest(count=2) ]) PCI_REQUESTS.obj_reset_changes(recursive=True) def fake_db_spec(): req_obj = fake_spec_obj() db_request_spec = { 'id': 1, 'instance_uuid': req_obj.instance_uuid, 'spec': jsonutils.dumps(req_obj.obj_to_primitive()), } return db_request_spec
def test_pci_passthrough_empty_pci_request_obj(self): requests = objects.InstancePCIRequests(requests=[]) spec_obj = objects.RequestSpec(pci_requests=requests) host = fakes.FakeHostState('h1', 'n1', {}) self.assertTrue(self.filt_cls.host_passes(host, spec_obj))