def test_server_backup_lock(self, tmpdir): """ Tests for ServerBackupLock class """ lock = ServerBackupLock(tmpdir.strpath, 'server_name') assert lock.filename == tmpdir.join('.server_name-backup.lock') assert lock.raise_if_fail assert not lock.wait
def test_delete_running_backup(self, delete_mock, get_first_backup_mock, tmpdir, capsys): """ Simple test for the deletion of a running backup. We want to test the behaviour of the server.delete_backup method when invoked on a running backup """ # Test the removal of a running backup. status STARTED server = build_real_server({'barman_home': tmpdir.strpath}) backup_info_started = build_test_backup_info( status=BackupInfo.STARTED, server_name=server.config.name) get_first_backup_mock.return_value = backup_info_started.backup_id with ServerBackupLock(tmpdir.strpath, server.config.name): server.delete_backup(backup_info_started) out, err = capsys.readouterr() assert "Cannot delete a running backup (%s %s)" % ( server.config.name, backup_info_started.backup_id) in err # Test the removal of a running backup. status EMPTY backup_info_empty = build_test_backup_info( status=BackupInfo.EMPTY, server_name=server.config.name) get_first_backup_mock.return_value = backup_info_empty.backup_id with ServerBackupLock(tmpdir.strpath, server.config.name): server.delete_backup(backup_info_empty) out, err = capsys.readouterr() assert "Cannot delete a running backup (%s %s)" % ( server.config.name, backup_info_started.backup_id) in err # Test the removal of a running backup. status DONE backup_info_done = build_test_backup_info( status=BackupInfo.DONE, server_name=server.config.name) with ServerBackupLock(tmpdir.strpath, server.config.name): server.delete_backup(backup_info_done) delete_mock.assert_called_with(backup_info_done) # Test the removal of a backup not running. status STARTED server.delete_backup(backup_info_started) delete_mock.assert_called_with(backup_info_started)
def delete_backup(self, backup): """Deletes a backup :param backup: the backup to delete """ try: # Lock acquisition: if you can acquire a ServerBackupLock # it means that no backup process is running on that server, # so there is no need to check the backup status. # Simply proceed with the normal delete process. server_backup_lock = ServerBackupLock( self.config.barman_lock_directory, self.config.name) server_backup_lock.acquire(server_backup_lock.raise_if_fail, server_backup_lock.wait) server_backup_lock.release() return self.backup_manager.delete_backup(backup) except LockFileBusy: # Otherwise if the lockfile is busy, a backup process is actually # running on that server. To be sure that it's safe # to delete the backup, we must check its status and its position # in the catalogue. # If it is the first and it is STARTED or EMPTY, we are trying to # remove a running backup. This operation must be forbidden. # Otherwise, normally delete the backup. first_backup = self.get_first_backup(BackupInfo.STATUS_ALL) if backup.backup_id == first_backup.backup_id \ and backup.status in (BackupInfo.STARTED, BackupInfo.EMPTY): output.error("Cannot delete a running backup (%s %s)" % (self.config.name, backup.backup_id)) else: return self.backup_manager.delete_backup(backup) except LockFilePermissionDenied, e: # We cannot access the lockfile. # Exit without removing the backup. output.error("Permission denied, unable to access '%s'" % e)