diff options
author | Radoslav Gerganov <rgerganov@vmware.com> | 2017-10-04 14:37:21 +0300 |
---|---|---|
committer | Radoslav Gerganov <rgerganov@vmware.com> | 2017-11-06 10:30:21 +0200 |
commit | b2f06b84d518bc5af95344b1cbe8fd184f164628 (patch) | |
tree | 67956e7c747eecb442af3ee56f3e8205c3a7e4ef | |
parent | df0a80220d7ce6fe2152b62db9c04ed1148fb7a8 (diff) | |
download | nova-b2f06b84d518bc5af95344b1cbe8fd184f164628.tar.gz |
Move last_bytes into the path module
The last_bytes function is not libvirt specific and this patch moves it
into the path module. Now other virt drivers can use it without assuming
they depend on libvirt.
Change-Id: Ieb7fef9d054efef6bdb44b1d438ec703752be8b4
-rw-r--r-- | nova/privsep/libvirt.py | 34 | ||||
-rw-r--r-- | nova/privsep/path.py | 34 | ||||
-rw-r--r-- | nova/tests/unit/privsep/test_path.py (renamed from nova/tests/unit/privsep/test_libvirt.py) | 8 | ||||
-rwxr-xr-x | nova/tests/unit/virt/libvirt/test_driver.py | 8 | ||||
-rw-r--r-- | nova/virt/libvirt/driver.py | 2 |
5 files changed, 43 insertions, 43 deletions
diff --git a/nova/privsep/libvirt.py b/nova/privsep/libvirt.py index 05bd124518..8d87012c84 100644 --- a/nova/privsep/libvirt.py +++ b/nova/privsep/libvirt.py @@ -18,7 +18,6 @@ libvirt specific routines. """ import binascii -import errno import os import stat @@ -34,39 +33,6 @@ LOG = logging.getLogger(__name__) @nova.privsep.sys_admin_pctxt.entrypoint -def last_bytes(path, num): - # NOTE(mikal): this is implemented in this contrived manner because you - # can't mock a decorator in python (they're loaded at file parse time, - # and the mock happens later). - with open(path, 'rb') as f: - return _last_bytes_inner(f, num) - - -def _last_bytes_inner(file_like_object, num): - """Return num bytes from the end of the file, and remaining byte count. - - :param file_like_object: The file to read - :param num: The number of bytes to return - - :returns: (data, remaining) - """ - - try: - file_like_object.seek(-num, os.SEEK_END) - except IOError as e: - # seek() fails with EINVAL when trying to go before the start of - # the file. It means that num is larger than the file size, so - # just go to the start. - if e.errno == errno.EINVAL: - file_like_object.seek(0, os.SEEK_SET) - else: - raise - - remaining = file_like_object.tell() - return (file_like_object.read(), remaining) - - -@nova.privsep.sys_admin_pctxt.entrypoint def dmcrypt_create_volume(target, device, cipher, key_size, key): """Sets up a dmcrypt mapping diff --git a/nova/privsep/path.py b/nova/privsep/path.py index e2864b2faa..c35e32ce54 100644 --- a/nova/privsep/path.py +++ b/nova/privsep/path.py @@ -15,6 +15,7 @@ """Routines that bypass file-system checks.""" +import errno import os from oslo_utils import fileutils @@ -89,3 +90,36 @@ class path(object): @nova.privsep.sys_admin_pctxt.entrypoint def exists(path): return os.path.exists(path) + + +@nova.privsep.sys_admin_pctxt.entrypoint +def last_bytes(path, num): + # NOTE(mikal): this is implemented in this contrived manner because you + # can't mock a decorator in python (they're loaded at file parse time, + # and the mock happens later). + with open(path, 'rb') as f: + return _last_bytes_inner(f, num) + + +def _last_bytes_inner(file_like_object, num): + """Return num bytes from the end of the file, and remaining byte count. + + :param file_like_object: The file to read + :param num: The number of bytes to return + + :returns: (data, remaining) + """ + + try: + file_like_object.seek(-num, os.SEEK_END) + except IOError as e: + # seek() fails with EINVAL when trying to go before the start of + # the file. It means that num is larger than the file size, so + # just go to the start. + if e.errno == errno.EINVAL: + file_like_object.seek(0, os.SEEK_SET) + else: + raise + + remaining = file_like_object.tell() + return (file_like_object.read(), remaining) diff --git a/nova/tests/unit/privsep/test_libvirt.py b/nova/tests/unit/privsep/test_path.py index af8bb5a7ce..cacbebd0e9 100644 --- a/nova/tests/unit/privsep/test_libvirt.py +++ b/nova/tests/unit/privsep/test_path.py @@ -17,7 +17,7 @@ import os import six import tempfile -import nova.privsep.libvirt +import nova.privsep.path from nova import test @@ -30,13 +30,13 @@ class LastBytesTestCase(test.NoDBTestCase): def test_truncated(self): self.f.seek(0, os.SEEK_SET) - out, remaining = nova.privsep.libvirt._last_bytes_inner(self.f, 5) + out, remaining = nova.privsep.path._last_bytes_inner(self.f, 5) self.assertEqual(out, b'67890') self.assertGreater(remaining, 0) def test_read_all(self): self.f.seek(0, os.SEEK_SET) - out, remaining = nova.privsep.libvirt._last_bytes_inner(self.f, 1000) + out, remaining = nova.privsep.path._last_bytes_inner(self.f, 1000) self.assertEqual(out, b'1234567890') self.assertFalse(remaining > 0) @@ -47,4 +47,4 @@ class LastBytesTestCase(test.NoDBTestCase): flo.write(content) self.assertEqual( (content, 0), - nova.privsep.libvirt._last_bytes_inner(flo, 1000)) + nova.privsep.path._last_bytes_inner(flo, 1000)) diff --git a/nova/tests/unit/virt/libvirt/test_driver.py b/nova/tests/unit/virt/libvirt/test_driver.py index 6415e8cbbb..526d01704e 100755 --- a/nova/tests/unit/virt/libvirt/test_driver.py +++ b/nova/tests/unit/virt/libvirt/test_driver.py @@ -11493,7 +11493,7 @@ class LibvirtConnTestCase(test.NoDBTestCase, drvr._ensure_console_log_for_instance, mock.ANY) - @mock.patch('nova.privsep.libvirt.last_bytes', + @mock.patch('nova.privsep.path.last_bytes', return_value=(b'67890', 0)) def test_get_console_output_file(self, mock_last_bytes): with utils.tempdir() as tmpdir: @@ -11575,7 +11575,7 @@ class LibvirtConnTestCase(test.NoDBTestCase, self.assertEqual('', output) @mock.patch('os.path.exists', return_value=True) - @mock.patch('nova.privsep.libvirt.last_bytes', + @mock.patch('nova.privsep.path.last_bytes', return_value=(b'67890', 0)) @mock.patch('nova.privsep.path.writefile') @mock.patch('nova.privsep.libvirt.readpty') @@ -11682,7 +11682,7 @@ class LibvirtConnTestCase(test.NoDBTestCase, def mock_last_bytes(path, count): with fake_libvirt_utils.file_open(path) as flo: - return nova.privsep.libvirt._last_bytes_inner(flo, count) + return nova.privsep.path._last_bytes_inner(flo, count) xml = """ <domain type='kvm'> @@ -11712,7 +11712,7 @@ class LibvirtConnTestCase(test.NoDBTestCase, libvirt_driver.MAX_CONSOLE_BYTES = bytes_to_read with mock.patch('os.path.exists', side_effect=mock_path_exists): - with mock.patch('nova.privsep.libvirt.last_bytes', + with mock.patch('nova.privsep.path.last_bytes', side_effect=mock_last_bytes): log_data = drvr.get_console_output(self.context, instance) diff --git a/nova/virt/libvirt/driver.py b/nova/virt/libvirt/driver.py index 99a30059a8..eb9aa23b11 100644 --- a/nova/virt/libvirt/driver.py +++ b/nova/virt/libvirt/driver.py @@ -2839,7 +2839,7 @@ class LibvirtDriver(driver.ComputeDriver): path = console_log while bytes_to_read > 0 and os.path.exists(path): - read_log_data, remaining = nova.privsep.libvirt.last_bytes( + read_log_data, remaining = nova.privsep.path.last_bytes( path, bytes_to_read) # We need the log file content in chronological order, # that's why we *prepend* the log data. |