def _shelve_offload(self, mock_notify, mock_notify_instance_usage, mock_get_power_state, mock_update_resource_tracker, mock_delete_alloc, mock_terminate, mock_get_bdms, clean_shutdown=True): host = 'fake-mini' instance = self._create_fake_instance_obj(params={'host': host}) instance.task_state = task_states.SHELVING instance.save() self.useFixture(utils_fixture.TimeFixture()) fake_bdms = objects.BlockDeviceMappingList() mock_get_bdms.return_value = fake_bdms with mock.patch.object(instance, 'save'): self.compute.shelve_offload_instance(self.context, instance, clean_shutdown=clean_shutdown) mock_notify.assert_has_calls([ mock.call(self.context, instance, 'fake-mini', action='shelve_offload', phase='start', bdms=fake_bdms), mock.call(self.context, instance, 'fake-mini', action='shelve_offload', phase='end', bdms=fake_bdms)]) self.assertEqual(vm_states.SHELVED_OFFLOADED, instance.vm_state) self.assertIsNone(instance.task_state) self.assertTrue(mock_terminate.called) # prepare expect call lists mock_notify_instance_usage_call_list = [ mock.call(self.context, instance, 'shelve_offload.start'), mock.call(self.context, instance, 'shelve_offload.end')] mock_notify_instance_usage.assert_has_calls( mock_notify_instance_usage_call_list) # instance.host is replaced with host because # original instance.host is clear after # ComputeManager.shelve_offload_instance execute mock_get_power_state.assert_called_once_with( self.context, instance) mock_update_resource_tracker.assert_called_once_with(self.context, instance) mock_delete_alloc.assert_called_once_with(instance) return instance
def fake_db_req(**updates): ctxt = context.RequestContext('fake-user', 'fake-project') instance_uuid = uuidutils.generate_uuid() instance = fake_instance.fake_instance_obj(ctxt, objects.Instance, uuid=instance_uuid) # This will always be set this way for an instance at build time instance.host = None block_devices = objects.BlockDeviceMappingList( objects=[fake_block_device.fake_bdm_object( context, fake_block_device.FakeDbBlockDeviceDict( source_type='blank', destination_type='local', guest_format='foo', device_type='disk', disk_bus='', boot_index=1, device_name='xvda', delete_on_termination=False, snapshot_id=None, volume_id=None, volume_size=0, image_id='bar', no_device=False, connection_info=None, tag='', instance_uuid=uuids.instance))]) db_build_request = { 'id': 1, 'project_id': 'fake-project', 'instance_uuid': instance_uuid, 'instance': jsonutils.dumps(instance.obj_to_primitive()), 'block_device_mappings': jsonutils.dumps( block_devices.obj_to_primitive()), 'created_at': datetime.datetime(2016, 1, 16), 'updated_at': datetime.datetime(2016, 1, 16), } for name, field in objects.BuildRequest.fields.items(): if name in db_build_request: continue if field.nullable: db_build_request[name] = None elif field.default != fields.UnspecifiedDefault: db_build_request[name] = field.default else: raise Exception('fake_db_req needs help with %s' % name) if updates: db_build_request.update(updates) return db_build_request
def test_rebuild_instance(self, _record_action_start, _checks_for_create_and_rebuild, _check_auto_disk_config, _get_image, bdm_get_by_instance_uuid, get_flavor, instance_save, _req_spec_get_by_inst_uuid): orig_system_metadata = {} instance = fake_instance.fake_instance_obj( self.context, vm_state=vm_states.ACTIVE, cell_name='fake-cell', launched_at=timeutils.utcnow(), image_ref=uuids.image_id, system_metadata=orig_system_metadata, expected_attrs=['system_metadata']) get_flavor.return_value = '' # The API request schema validates that a UUID is passed for the # imageRef parameter so we need to provide an image. image_href = uuids.image_id image = { "min_ram": 10, "min_disk": 1, "properties": { 'architecture': 'x86_64' }, "id": uuids.image_id } admin_pass = '' files_to_inject = [] bdms = objects.BlockDeviceMappingList() _get_image.return_value = (None, image) bdm_get_by_instance_uuid.return_value = bdms self.compute_api.rebuild(self.context, instance, image_href, admin_pass, files_to_inject) self.assertTrue(self.cells_rpcapi.rebuild_instance.called)
def _shelve_instance(self, shelved_offload_time, mock_notify, mock_notify_instance_usage, mock_get_power_state, mock_snapshot, mock_power_off, mock_terminate, mock_get_bdms, clean_shutdown=True): mock_get_power_state.return_value = 123 CONF.set_override('shelved_offload_time', shelved_offload_time) host = 'fake-mini' instance = self._create_fake_instance_obj(params={'host': host}) image_id = 'fake_image_id' host = 'fake-mini' self.useFixture(utils_fixture.TimeFixture()) instance.task_state = task_states.SHELVING instance.save() fake_bdms = None if shelved_offload_time == 0: fake_bdms = objects.BlockDeviceMappingList() mock_get_bdms.return_value = fake_bdms tracking = {'last_state': instance.vm_state} def check_save(expected_task_state=None): self.assertEqual(123, instance.power_state) if tracking['last_state'] == vm_states.ACTIVE: if CONF.shelved_offload_time == 0: self.assertEqual(task_states.SHELVING_OFFLOADING, instance.task_state) else: self.assertIsNone(instance.task_state) self.assertEqual(vm_states.SHELVED, instance.vm_state) self.assertEqual([task_states.SHELVING, task_states.SHELVING_IMAGE_UPLOADING], expected_task_state) self.assertIn('shelved_at', instance.system_metadata) self.assertEqual(image_id, instance.system_metadata['shelved_image_id']) self.assertEqual(host, instance.system_metadata['shelved_host']) tracking['last_state'] = instance.vm_state elif (tracking['last_state'] == vm_states.SHELVED and CONF.shelved_offload_time == 0): self.assertIsNone(instance.task_state) self.assertEqual(vm_states.SHELVED_OFFLOADED, instance.vm_state) self.assertEqual([task_states.SHELVING, task_states.SHELVING_OFFLOADING], expected_task_state) tracking['last_state'] = instance.vm_state elif (tracking['last_state'] == vm_states.SHELVED_OFFLOADED and CONF.shelved_offload_time == 0): self.assertIsNone(instance.host) self.assertIsNone(instance.node) self.assertIsNone(expected_task_state) else: self.fail('Unexpected save!') with test.nested( mock.patch.object(instance, 'save'), mock.patch.object(self.compute.network_api, 'cleanup_instance_network_on_host')) as ( mock_save, mock_cleanup ): mock_save.side_effect = check_save self.compute.shelve_instance(self.context, instance, image_id=image_id, clean_shutdown=clean_shutdown) mock_notify.assert_has_calls([ mock.call(self.context, instance, 'fake-mini', action='shelve', phase='start', bdms=fake_bdms), mock.call(self.context, instance, 'fake-mini', action='shelve', phase='end', bdms=fake_bdms)]) # prepare expect call lists mock_notify_instance_usage_call_list = [ mock.call(self.context, instance, 'shelve.start'), mock.call(self.context, instance, 'shelve.end')] mock_power_off_call_list = [] mock_get_power_state_call_list = [ mock.call(self.context, instance)] mock_cleanup_call_list = [] if clean_shutdown: mock_power_off_call_list.append( mock.call(instance, CONF.shutdown_timeout, self.compute.SHUTDOWN_RETRY_INTERVAL)) else: mock_power_off_call_list.append(mock.call(instance, 0, 0)) if CONF.shelved_offload_time == 0: mock_notify_instance_usage_call_list.extend([ mock.call(self.context, instance, 'shelve_offload.start'), mock.call(self.context, instance, 'shelve_offload.end')]) mock_power_off_call_list.append(mock.call(instance, 0, 0)) mock_get_power_state_call_list.append(mock.call(self.context, instance)) # instance.host is replaced with host because # original instance.host is clear after # ComputeManager.shelve_instance execute with # shelved_offload_time == 0 mock_cleanup_call_list.append(mock.call(self.context, instance, host)) mock_notify_instance_usage.assert_has_calls( mock_notify_instance_usage_call_list) mock_power_off.assert_has_calls(mock_power_off_call_list) mock_cleanup.assert_has_calls(mock_cleanup_call_list) mock_snapshot.assert_called_once_with(self.context, instance, 'fake_image_id', mock.ANY) mock_get_power_state.assert_has_calls(mock_get_power_state_call_list) if CONF.shelved_offload_time == 0: self.assertTrue(mock_terminate.called)
def _shelve_offload(self, mock_notify, mock_notify_instance_usage, mock_get_power_state, mock_update_resource_tracker, mock_delete_alloc, mock_terminate, mock_get_bdms, mock_event, clean_shutdown=True): host = 'fake-mini' instance = self._create_fake_instance_obj(params={'host': host}) instance.task_state = task_states.SHELVING instance.save() self.useFixture(utils_fixture.TimeFixture()) fake_bdms = objects.BlockDeviceMappingList() mock_get_bdms.return_value = fake_bdms def stub_instance_save(inst, *args, **kwargs): # If the vm_state is changed to SHELVED_OFFLOADED make sure we # have already freed up allocations in placement. if inst.vm_state == vm_states.SHELVED_OFFLOADED: self.assertTrue( mock_delete_alloc.called, 'Allocations must be deleted before the ' 'vm_status can change to shelved_offloaded.') self.stub_out('nova.objects.Instance.save', stub_instance_save) self.compute.shelve_offload_instance(self.context, instance, clean_shutdown=clean_shutdown) mock_notify.assert_has_calls([ mock.call(self.context, instance, 'fake-mini', action='shelve_offload', phase='start', bdms=fake_bdms), mock.call(self.context, instance, 'fake-mini', action='shelve_offload', phase='end', bdms=fake_bdms) ]) self.assertEqual(vm_states.SHELVED_OFFLOADED, instance.vm_state) self.assertIsNone(instance.task_state) self.assertTrue(mock_terminate.called) # prepare expect call lists mock_notify_instance_usage_call_list = [ mock.call(self.context, instance, 'shelve_offload.start'), mock.call(self.context, instance, 'shelve_offload.end') ] mock_notify_instance_usage.assert_has_calls( mock_notify_instance_usage_call_list) # instance.host is replaced with host because # original instance.host is clear after # ComputeManager.shelve_offload_instance execute mock_get_power_state.assert_called_once_with(self.context, instance) mock_update_resource_tracker.assert_called_once_with( self.context, instance) mock_delete_alloc.assert_called_once_with(self.context, instance) mock_event.assert_called_once_with(self.context, 'compute_shelve_offload_instance', CONF.host, instance.uuid) return instance
def block_device_make_list(context, db_list, **extra_args): return base.obj_make_list(context, objects.BlockDeviceMappingList(context), objects.BlockDeviceMapping, db_list, **extra_args)
def _validate_device(self, device=None): bdms = base.obj_make_list(self.context, objects.BlockDeviceMappingList(), objects.BlockDeviceMapping, self.data) return compute_utils.get_device_name_for_instance( self.instance, bdms, device)
def test_convert_block_devices(self): bdms = objects.BlockDeviceMappingList( objects=[self.volume_bdm, self.ephemeral_bdm]) converted = driver_block_device._convert_block_devices( self.driver_classes['volume'], bdms) self.assertEqual(converted, [self.volume_driver_bdm])
def test_list_with_additional_filter(self, mock_get): fake_bdms = objects.BlockDeviceMappingList() mock_get.return_value = fake_bdms req = fakes.HTTPRequest.blank( '/servers/id/os-volume_attachments?limit=1&additional=something') self.attachments.index(req, FAKE_UUID)