Merge "RBD: Improve IOWrapper's close & flush methods"

This commit is contained in:
Zuul 2022-08-25 13:57:27 +00:00 committed by Gerrit Code Review
commit 0e3ab3bd74
2 changed files with 21 additions and 1 deletions

View File

@ -226,6 +226,8 @@ class RBDVolumeIOWrapper(io.RawIOBase):
return self._offset
def flush(self):
# Raise ValueError if already closed
super().flush()
try:
self._rbd_volume.image.flush()
except AttributeError:
@ -240,4 +242,8 @@ class RBDVolumeIOWrapper(io.RawIOBase):
raise IOError(_("fileno() not supported by RBD()"))
def close(self):
self.rbd_image.close()
if not self.closed:
# Can't set closed attribute ourselves, call parent to flush and
# change it.
super().close()
self.rbd_image.close()

View File

@ -194,6 +194,14 @@ class RBDVolumeIOWrapperTestCase(base.TestCase):
self.assertEqual(1, self.mock_volume.image.flush.call_count)
self.assertEqual(1, mock_logger.warning.call_count)
def test_flush_on_closed(self):
self.mock_volume_wrapper.close()
self.mock_volume.image.flush.assert_called_once_with()
self.assertTrue(self.mock_volume_wrapper.closed)
self.mock_volume.image.flush.reset_mock()
self.assertRaises(ValueError, self.mock_volume_wrapper.flush)
self.mock_volume.image.flush.assert_not_called()
def test_fileno(self):
self.assertRaises(IOError, self.mock_volume_wrapper.fileno)
@ -207,6 +215,12 @@ class RBDVolumeIOWrapperTestCase(base.TestCase):
linuxrbd.RBDImageMetadata(rbd_volume, 'pool', 'user', None))
rbd_handle.close()
self.assertEqual(1, rbd_disconnect.call_count)
# Confirm the handle now reports that is closed (this attribute cannot
# be modified directly)
self.assertTrue(rbd_handle.closed)
# New call to close shouldn't create additional calls
rbd_handle.close()
self.assertEqual(1, rbd_disconnect.call_count)
class RBDVolumeTestCase(base.TestCase):