Browse Source

Merge "Adds standard file operation functions"

master
Zuul 1 week ago
parent
commit
fbb6cedfbe
2 changed files with 57 additions and 0 deletions
  1. +32
    -0
      oslo_vmware/rw_handles.py
  2. +25
    -0
      oslo_vmware/tests/test_rw_handles.py

+ 32
- 0
oslo_vmware/rw_handles.py View File

@@ -177,6 +177,32 @@ class FileHandle(object):
"""
raise NotImplementedError()

def tell(self):
"""Get the position of File Handle

:return: position
"""
raise NotImplementedError()

def fileno(self):
"""returns the integer file descriptor
by default this is not supported and raises IOError
"""
raise IOError()

def seek(self, offset):
"""sets the file's current position at the offset

:param offset: offset
"""
pass

def flush(self):
"""flushes the internal buffer

"""
pass

def get_size(self):
"""Get size of the file to be read.

@@ -484,6 +510,9 @@ class VmdkWriteHandle(VmdkHandle):
""""Get managed object reference of the VM created for import."""
return self._vm_ref

def tell(self):
return self._bytes_written

def write(self, data):
"""Write data to the file.

@@ -585,6 +614,9 @@ class VmdkReadHandle(VmdkHandle):
LOG.exception(excep_msg)
raise exceptions.VimException(excep_msg, excep)

def tell(self):
return self._bytes_read

def close(self):
"""Releases the lease and close the connection.


+ 25
- 0
oslo_vmware/tests/test_rw_handles.py View File

@@ -168,6 +168,12 @@ class VmdkHandleTest(base.TestCase):

self.assertRaises(exceptions.VimException, handle.update_progress)

def test_fileno(self):
session = mock.Mock()
handle = rw_handles.VmdkHandle(session, None, 'fake-url', None)

self.assertRaises(IOError, handle.fileno)


class VmdkWriteHandleTest(base.TestCase):
"""Tests for VmdkWriteHandle."""
@@ -228,6 +234,16 @@ class VmdkWriteHandleTest(base.TestCase):
self._conn.putrequest.assert_called_once_with('PUT', '/ds/disk1.vmdk')
self._conn.send.assert_called_once_with(data)

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_post(self):
session = self._create_mock_session()
handle = rw_handles.VmdkWriteHandle(session, '10.1.2.3', 443,
@@ -326,6 +342,15 @@ class VmdkReadHandleTest(base.TestCase):
data = handle.read(chunk_size)
self.assertEqual('fake-data', data)

def test_tell(self):
chunk_size = rw_handles.READ_CHUNKSIZE
session = self._create_mock_session()
handle = rw_handles.VmdkReadHandle(session, '10.1.2.3', 443,
'vm-1', '[ds] disk1.vmdk',
chunk_size * 10)
data = handle.read(chunk_size)
self.assertEqual(len(data), handle.tell())

def test_update_progress(self):
chunk_size = len('fake-data')
vmdk_size = chunk_size * 10

Loading…
Cancel
Save