def test_posix_backend_error(self): """Test opening a file from posix backend.""" with self.assertRaises(ArchiveInterfaceError): backend = PosixBackendArchive('/tmp') # easiest way to unit test is look at class variable # pylint: disable=protected-access backend._file = 'none file object' backend.open('1234', 'w')
def test_patch(self): """Test patching file.""" old_path = '/tmp/1234' backend = PosixBackendArchive('/tmp') my_file = backend.open('1234', 'w') my_file.close() backend.patch('5678', '/tmp/1234') # Error would be thrown on patch so nothing to assert self.assertEqual(old_path, '/tmp/1234')
def test_posix_backend_read(self): """Test reading a file from posix backend.""" self.test_posix_backend_write() filepath = '1234' mode = 'r' backend = PosixBackendArchive('/tmp/') my_file = backend.open(filepath, mode) buf = my_file.read(-1) self.assertEqual(buf, bytes_type('i am a test string')) my_file.close()
def test_posix_file_permissions(self): """Test the correct setting of a file mod time.""" filepath = '12345' mode = 'w' backend = PosixBackendArchive('/tmp/') my_file = backend.open(filepath, mode) my_file.close() my_file.set_file_permissions() statinfo = oct(os.stat('/tmp/12345')[ST_MODE])[-3:] self.assertEqual(statinfo, '444')
def test_seek(self): """Test patching file.""" backend = PosixBackendArchive('/tmp') my_file = backend.open('1234', 'w') my_file.write('something') my_file.close() my_file = backend.open('1234', 'r') my_file.seek(4) data = my_file.read(-1).decode('utf8') self.assertEqual(data, 'thing')
def test_posix_file_perms_failed(self, mock_chmod): """Test the correct setting of a file mod time.""" backend = PosixBackendArchive('/tmp/') my_file = backend.open('1235', 'w') my_file.close() mock_chmod.side_effect = OSError('Unable to chmod.') with self.assertRaises(ArchiveInterfaceError) as exc: my_file.set_file_permissions() self.assertTrue("Can't set posix file permissions with error" in str( exc.exception))
def test_posix_file_mod_time_failed(self, mock_utime): """Test the correct setting of a file mod time.""" backend = PosixBackendArchive('/tmp/') my_file = backend.open('1234', 'w') my_file.close() mock_utime.side_effect = OSError('Unable to set utime.') with self.assertRaises(ArchiveInterfaceError) as exc: my_file.set_mod_time(1000000) self.assertTrue( "Can't set posix file mod time with error" in str(exc.exception))
def test_delete_failed(self, mock_unlink): """Test patching file.""" mock_unlink.side_effect = OSError('Unable to delete') backend = PosixBackendArchive('/tmp') my_file = backend.open('1234', 'w') my_file.close() with self.assertRaises(ArchiveInterfaceError) as exc: backend.remove() self.assertTrue( 'Can\'t remove posix file with error' in str(exc.exception))
def test_patch_failed(self, mock_move): """Test patching file.""" mock_move.side_effect = OSError('Unable to move') backend = PosixBackendArchive('/tmp') my_file = backend.open('1234', 'w') my_file.close() with self.assertRaises(ArchiveInterfaceError) as exc: backend.patch('5678', '/tmp/1234') self.assertTrue( 'Can\'t move posix file with error' in str(exc.exception))
def test_posix_backend_stage(self): """Test staging a file from posix backend.""" filepath = '1234' mode = 'w' backend = PosixBackendArchive('/tmp') my_file = backend.open(filepath, mode) my_file.stage() # pylint: disable=protected-access self.assertTrue(my_file._file._staged) # pylint: enable=protected-access my_file.close()
def test_posix_backend_close(self): """Test closing a file from posix backend.""" filepath = '1234' mode = 'w' backend = PosixBackendArchive('/tmp/') my_file = backend.open(filepath, mode) # easiest way to unit test is look at class variable # pylint: disable=protected-access self.assertEqual(backend._file.__class__.__name__, 'ExtendedFile') my_file.close() self.assertEqual(backend._file, None)
def test_posix_file_mod_time(self): """Test the correct setting of a file mod time.""" filepath = '1234' mode = 'w' backend = PosixBackendArchive('/tmp/') my_file = backend.open(filepath, mode) my_file.close() my_file.set_mod_time(1000000) my_file = backend.open(filepath, 'r') status = my_file.status() my_file.close() self.assertEqual(status.mtime, 1000000)
def test_posix_backend_write(self): """Test writing a file from posix backend.""" filepath = '1234' mode = 'w' backend = PosixBackendArchive('/tmp/') my_file = backend.open(filepath, mode) error = my_file.write('i am a test string') if PY2: self.assertEqual(error, None) else: self.assertEqual(error, 18) my_file.close()
def test_posix_backend_open_twice(self): """Test opening a file from posix backend twice.""" filepath = '1234' mode = 'w' backend = PosixBackendArchive('/tmp') my_file = backend.open(filepath, mode) my_file = backend.open(filepath, mode) self.assertTrue(isinstance(my_file, PosixBackendArchive)) # easiest way to unit test is look at class variable # pylint: disable=protected-access self.assertEqual(backend._file.__class__.__name__, 'ExtendedFile') # pylint: enable=protected-access my_file.close()
def test_posix_backend_create(self): """Test creating a posix backend.""" backend = PosixBackendArchive('/tmp') self.assertTrue(isinstance(backend, PosixBackendArchive)) # easiest way to unit test is look at class variable # pylint: disable=protected-access self.assertEqual(backend._prefix, '/tmp')
def test_posix_backend_failed_write(self): """Test writing to a failed file.""" 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.write = mock.MagicMock( side_effect=IOError('Unable to Write!')) # pylint: enable=protected-access with self.assertRaises(ArchiveInterfaceError) as exc: backend.write('write stuff') self.assertTrue( "Can't write posix file with error" in str(exc.exception)) backend.close()
def test_posix_backend_open_id2f(self): """Test opening a file from posix backend twice.""" backend = PosixBackendArchive('/tmp') mode = 'w' my_file = backend.open('/a/b/d', mode) temp_cfg_file = pa_config.CONFIG_FILE pa_config.CONFIG_FILE = 'test_configs/posix-id2filename.cfg' backend = PosixBackendArchive('/tmp') my_file = backend.open(12345, mode) my_file.write('this is file 12345') my_file.close() # pylint: disable=protected-access my_file.patch(123456789, '/tmp{}'.format(my_file._id2filename(12345))) # pylint: enable=protected-access my_file = backend.open(123456789, 'r') text = my_file.read(-1) pa_config.CONFIG_FILE = temp_cfg_file self.assertTrue(isinstance(my_file, PosixBackendArchive)) self.assertEqual(bytes_type('this is file 12345'), text) my_file.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))
def test_posix_backend_failed_stage(self): """Test reading a file from posix backend.""" backend = PosixBackendArchive('/tmp/') backend.open('1234', 'w') backend.close() backend.open('1234', 'r') # easiest way to unit test is look at class variable # pylint: disable=protected-access backend._file.stage = mock.MagicMock( side_effect=IOError('Unable to Stage!')) # pylint: enable=protected-access with self.assertRaises(ArchiveInterfaceError) as exc: backend.stage() self.assertTrue( 'Can\'t stage posix file with error' in str(exc.exception)) backend.close()