def test_posix_backend_failed_read(self): """Test reading to a failed file.""" backend = PosixBackendArchive('/tmp/') backend.open('1234', 'w') backend.close() # test failed write backend.open('1234', 'r') # easiest way to unit test is look at class variable # pylint: disable=protected-access backend._file.read = mock.MagicMock( side_effect=IOError('Unable to Read!')) # pylint: enable=protected-access with self.assertRaises(ArchiveInterfaceError) as exc: backend.read(1024) self.assertTrue( "Can't read posix file with error" in str(exc.exception)) backend.close()
def test_posix_backend_failed_fd(self): """Test reading to a failed file fd.""" backend = PosixBackendArchive('/tmp/') # test failed write backend.open('1234', 'w') # easiest way to unit test is look at class variable # pylint: disable=protected-access backend._file = None # pylint: enable=protected-access with self.assertRaises(ArchiveInterfaceError) as exc: backend.write('Something to write') self.assertTrue('Internal file handle invalid' in str(exc.exception)) with self.assertRaises(ArchiveInterfaceError) as exc: backend.read(1024) self.assertTrue('Internal file handle invalid' in str(exc.exception)) with self.assertRaises(ArchiveInterfaceError) as exc: backend.stage() self.assertTrue('Internal file handle invalid' in str(exc.exception)) with self.assertRaises(ArchiveInterfaceError) as exc: backend.status() self.assertTrue('Internal file handle invalid' in str(exc.exception))