def test_update_progress_with_error(self): session = self._create_mock_session(True, 10) handle = rw_handles.VmdkWriteHandle(session, '10.1.2.3', 443, 'rp-1', 'folder-1', None, 100) session.invoke_api.side_effect = exceptions.VimException(None) self.assertRaises(exceptions.VimException, handle.update_progress)
def fetch_image_ova(context, instance, session, vm_name, ds_name, vm_folder_ref, res_pool_ref): """Download the OVA image from the glance image server to the Nova compute node. """ image_ref = instance.image_ref LOG.debug( "Downloading OVA image file %(image_ref)s to the ESX " "as VM named '%(vm_name)s'", { 'image_ref': image_ref, 'vm_name': vm_name }, instance=instance) metadata = IMAGE_API.get(context, image_ref) file_size = int(metadata['size']) vm_import_spec = _build_import_spec_for_import_vapp( session, vm_name, ds_name) read_iter = IMAGE_API.download(context, image_ref) ova_fd, ova_path = tempfile.mkstemp() try: # NOTE(arnaud): Look to eliminate first writing OVA to file system with os.fdopen(ova_fd, 'w') as fp: for chunk in read_iter: fp.write(chunk) with tarfile.open(ova_path, mode="r") as tar: vmdk_name = None for tar_info in tar: if tar_info and tar_info.name.endswith(".ovf"): extracted = tar.extractfile(tar_info.name) xmlstr = extracted.read() vmdk_name = get_vmdk_name_from_ovf(xmlstr) elif vmdk_name and tar_info.name.startswith(vmdk_name): # Actual file name is <vmdk_name>.XXXXXXX extracted = tar.extractfile(tar_info.name) write_handle = rw_handles.VmdkWriteHandle( session, session._host, session._port, res_pool_ref, vm_folder_ref, vm_import_spec, file_size) start_transfer(context, extracted, file_size, write_file_handle=write_handle) extracted.close() LOG.info(_LI("Downloaded OVA image file %(image_ref)s"), {'image_ref': instance.image_ref}, instance=instance) imported_vm_ref = write_handle.get_imported_vm() session._call_method(session.vim, "UnregisterVM", imported_vm_ref) LOG.info(_LI("The imported VM was unregistered"), instance=instance) return raise exception.ImageUnacceptable( reason=_("Extracting vmdk from OVA failed."), image_id=image_ref) finally: os.unlink(ova_path)
def download_stream_optimized_data(context, timeout_secs, read_handle, **kwargs): """Download stream optimized data to VMware server. :param context: image service write context :param timeout_secs: time in seconds to wait for the download to complete :param read_handle: handle from which to read the image data :param kwargs: keyword arguments to configure the destination VMDK write handle :returns: managed object reference of the VM created for import to VMware server :raises: VimException, VimFaultException, VimAttributeException, VimSessionOverLoadException, VimConnectionException, ImageTransferException, ValueError """ file_size = int(kwargs.get('image_size')) write_handle = rw_handles.VmdkWriteHandle(kwargs.get('session'), kwargs.get('host'), kwargs.get('port'), kwargs.get('resource_pool'), kwargs.get('vm_folder'), kwargs.get('vm_import_spec'), file_size, kwargs.get('http_method', 'PUT')) _start_transfer(read_handle, write_handle, timeout_secs) return write_handle.get_imported_vm()
def fetch_image_stream_optimized(context, instance, session, vm_name, ds_name, vm_folder_ref, res_pool_ref): """Fetch image from Glance to ESX datastore.""" image_ref = instance.image_ref LOG.debug("Downloading image file data %(image_ref)s to the ESX " "as VM named '%(vm_name)s'", {'image_ref': image_ref, 'vm_name': vm_name}, instance=instance) metadata = IMAGE_API.get(context, image_ref) file_size = int(metadata['size']) vm_import_spec = _build_import_spec_for_import_vapp( session, vm_name, ds_name) read_iter = IMAGE_API.download(context, image_ref) read_handle = rw_handles.ImageReadHandle(read_iter) write_handle = rw_handles.VmdkWriteHandle(session, session._host, session._port, res_pool_ref, vm_folder_ref, vm_import_spec, file_size) image_transfer(read_handle, write_handle) imported_vm_ref = write_handle.get_imported_vm() LOG.info("Downloaded image file data %(image_ref)s", {'image_ref': instance.image_ref}, instance=instance) vmdk = vm_util.get_vmdk_info(session, imported_vm_ref, vm_name) session._call_method(session.vim, "UnregisterVM", imported_vm_ref) LOG.info("The imported VM was unregistered", instance=instance) return vmdk.capacity_in_bytes
def test_update_progress(self): vmdk_size = 100 data_size = 10 session = self._create_mock_session(True, 10) handle = rw_handles.VmdkWriteHandle(session, '10.1.2.3', 443, 'rp-1', 'folder-1', None, vmdk_size) handle.write([1] * data_size) handle.update_progress()
def test_tell(self): session = self._create_mock_session() handle = rw_handles.VmdkWriteHandle(session, '10.1.2.3', 443, 'rp-1', 'folder-1', None, 100) data = [1] * 10 handle.write(data) self.assertEqual(len(data), handle._bytes_written) self.assertEqual(len(data), handle.tell())
def test_write(self): session = self._create_mock_session() handle = rw_handles.VmdkWriteHandle(session, '10.1.2.3', 443, 'rp-1', 'folder-1', None, 100) data = [1] * 10 handle.write(data) self.assertEqual(len(data), handle._bytes_written) self._conn.putrequest.assert_called_once_with('PUT', '/ds/disk1.vmdk') self._conn.send.assert_called_once_with(data)
def test_get_vm_incomplete_transfer(self): session = self._create_mock_session() handle = rw_handles.VmdkWriteHandle(session, '10.1.2.3', 443, 'rp-1', 'folder-1', None, 100) handle._get_progress = mock.Mock(return_value=99) session.invoke_api = mock.Mock() self.assertRaises(exceptions.ImageTransferException, handle.get_imported_vm)
def fetch_image_ova(context, instance, session, vm_name, ds_name, vm_folder_ref, res_pool_ref): """Download the OVA image from the glance image server to the Nova compute node. """ image_ref = instance.image_ref LOG.debug( "Downloading OVA image file %(image_ref)s to the ESX " "as VM named '%(vm_name)s'", { 'image_ref': image_ref, 'vm_name': vm_name }, instance=instance) metadata = IMAGE_API.get(context, image_ref) file_size = int(metadata['size']) vm_import_spec = _build_import_spec_for_import_vapp( session, vm_name, ds_name) read_iter = IMAGE_API.download(context, image_ref) read_handle = rw_handles.ImageReadHandle(read_iter) with tarfile.open(mode="r|", fileobj=read_handle) as tar: vmdk_name = None for tar_info in tar: if tar_info and tar_info.name.endswith(".ovf"): extracted = tar.extractfile(tar_info) xmlstr = extracted.read() vmdk_name = get_vmdk_name_from_ovf(xmlstr) elif vmdk_name and tar_info.name.startswith(vmdk_name): # Actual file name is <vmdk_name>.XXXXXXX extracted = tar.extractfile(tar_info) write_handle = rw_handles.VmdkWriteHandle( session, session._host, session._port, res_pool_ref, vm_folder_ref, vm_import_spec, file_size) start_transfer(context, extracted, file_size, write_file_handle=write_handle) extracted.close() LOG.info(_LI("Downloaded OVA image file %(image_ref)s"), {'image_ref': instance.image_ref}, instance=instance) imported_vm_ref = write_handle.get_imported_vm() vmdk = vm_util.get_vmdk_info(session, imported_vm_ref, vm_name) session._call_method(session.vim, "UnregisterVM", imported_vm_ref) LOG.info(_LI("The imported VM was unregistered"), instance=instance) return vmdk.capacity_in_bytes raise exception.ImageUnacceptable( reason=_("Extracting vmdk from OVA failed."), image_id=image_ref)
def test_close(self): session = self._create_mock_session() handle = rw_handles.VmdkWriteHandle(session, '10.1.2.3', 443, 'rp-1', 'folder-1', None, 100) def session_invoke_api_side_effect(module, method, *args, **kwargs): if module == vim_util and method == 'get_object_property': return 'ready' self.assertEqual(session.vim, module) self.assertEqual('HttpNfcLeaseComplete', method) session.invoke_api = mock.Mock( side_effect=session_invoke_api_side_effect) handle.close() self.assertEqual(2, session.invoke_api.call_count)