def test_cmd_path(self, command_mock): command_instance = command_mock.return_value ulc = UnixLocalCommand(path="/a:/b") result = ulc.cmd("test command") assert result == command_instance.return_value command_mock.assert_called_once_with(cmd="sh", args=["-c"], path="/a:/b") command_instance.assert_called_once_with("test command")
def test_get_last_output(self, command_mock): command_instance = command_mock.return_value command_instance.out = "out" command_instance.err = "err" ulc = UnixLocalCommand() out, err = ulc.get_last_output() assert out == 'out' assert err == 'err'
def test_cmd(self, command_mock): command_instance = command_mock.return_value ulc = UnixLocalCommand() result = ulc.cmd('test command') assert result == command_instance.return_value command_mock.assert_called_once_with(cmd='sh', args=['-c'], path=None) command_instance.assert_called_once_with('test command')
def test_dir_if_not_exists(self, command_mock): command_instance = command_mock.return_value ulc = UnixLocalCommand() # Path exists command_mock.reset_mock() command_instance.side_effect = [ 0, # exists 0, # is dir ] result = ulc.create_dir_if_not_exists("test dir") assert not result # dir not created assert command_instance.mock_calls == [ call("test '-e' 'test dir'"), call("test '-d' 'test dir'"), ] # Path does not exist command_mock.reset_mock() command_instance.side_effect = [ 1, # exists 0, # mkdir ] result = ulc.create_dir_if_not_exists("test dir") assert result # dir created assert command_instance.mock_calls == [ call("test '-e' 'test dir'"), call("mkdir '-p' 'test dir'"), ] # Path exists and is a file command_mock.reset_mock() command_instance.side_effect = [ 0, # exists 1, # is dir ] with pytest.raises(FsOperationFailed): ulc.create_dir_if_not_exists("test dir") assert command_instance.mock_calls == [ call("test '-e' 'test dir'"), call("test '-d' 'test dir'"), ] # Path does not exist, but fail creation command_mock.reset_mock() command_instance.side_effect = [ 1, # exists 1, # mkdir ] with pytest.raises(FsOperationFailed): ulc.create_dir_if_not_exists("test dir") assert command_instance.mock_calls == [ call("test '-e' 'test dir'"), call("mkdir '-p' 'test dir'"), ]
def test_list_dir_content(self, command_mock): command_instance = command_mock.return_value ulc = UnixLocalCommand() # List directory command_mock.reset_mock() command_instance.out = "command output" result = ulc.list_dir_content("test path") assert result == "command output" assert command_instance.mock_calls == [ call("ls 'test path'"), ] # List directory with options command_mock.reset_mock() command_instance.out = "command output" result = ulc.list_dir_content("test path", ["-la"]) assert result == "command output" assert command_instance.mock_calls == [ call("ls '-la' 'test path'"), ]
def test_check_directory_exists(self, command_mock): command_instance = command_mock.return_value ulc = UnixLocalCommand() # Path does not exist command_mock.reset_mock() command_instance.side_effect = [ 1, # exists ] result = ulc.check_directory_exists("test dir") assert not result # path does not exists assert command_instance.mock_calls == [ call("test '-e' 'test dir'"), ] # Path exists and is a directory command_mock.reset_mock() command_instance.side_effect = [ 0, # exists 0, # is dir ] result = ulc.check_directory_exists("test dir") assert result # path exists and is a directory assert command_instance.mock_calls == [ call("test '-e' 'test dir'"), call("test '-d' 'test dir'"), ] # Path exists, but is not a directory command_mock.reset_mock() command_instance.side_effect = [ 0, # exists 1, # is dir ] with pytest.raises(FsOperationFailed): ulc.check_directory_exists("test dir") assert command_instance.mock_calls == [ call("test '-e' 'test dir'"), call("test '-d' 'test dir'"), ]
def test_delete_if_exists(self, command_mock): command_instance = command_mock.return_value ulc = UnixLocalCommand() # Path does not exist command_mock.reset_mock() command_instance.side_effect = [ 1, # exists ] result = ulc.delete_if_exists("test dir") assert not result # path not deleted assert command_instance.mock_calls == [ call("test '-e' 'test dir' '-o' '-L' 'test dir'"), ] # Path exists command_mock.reset_mock() command_instance.side_effect = [ 0, # exists 0, # rm ] result = ulc.delete_if_exists("test dir") assert result # path deleted assert command_instance.mock_calls == [ call("test '-e' 'test dir' '-o' '-L' 'test dir'"), call("rm '-fr' 'test dir'"), ] # Path exists, but fail deletion command_mock.reset_mock() command_instance.side_effect = [ 0, # exists 1, # rm ] with pytest.raises(FsOperationFailed): ulc.delete_if_exists("test dir") assert command_instance.mock_calls == [ call("test '-e' 'test dir' '-o' '-L' 'test dir'"), call("rm '-fr' 'test dir'"), ]
def test_get_file_content(self, command_mock): command_instance = command_mock.return_value ulc = UnixLocalCommand() # Path does not exist command_mock.reset_mock() command_instance.side_effect = [ 1, # exists ] with pytest.raises(FsOperationFailed): ulc.get_file_content("test path") assert command_instance.mock_calls == [ call("test '-e' 'test path'"), ] # Path exists but is not readable command_mock.reset_mock() command_instance.side_effect = [ 0, # exists 1, # readable ] with pytest.raises(FsOperationFailed): ulc.get_file_content("test path") assert command_instance.mock_calls == [ call("test '-e' 'test path'"), call("test '-r' 'test path'"), ] # Path exists, is readable, but cat fails command_mock.reset_mock() command_instance.side_effect = [ 0, # exists 0, # readable 1, # cat ] with pytest.raises(FsOperationFailed): ulc.get_file_content("test path") assert command_instance.mock_calls == [ call("test '-e' 'test path'"), call("test '-r' 'test path'"), call("cat 'test path'"), ] # Path exists, is readable and cat works command_mock.reset_mock() command_instance.side_effect = [ 0, # exists 0, # readable 0, # cat ] command_instance.out = "content" result = ulc.get_file_content("test path") assert result == "content" assert command_instance.mock_calls == [ call("test '-e' 'test path'"), call("test '-r' 'test path'"), call("cat 'test path'"), ]
def test_create_symbolic_link(self, command_mock): command_instance = command_mock.return_value ulc = UnixLocalCommand() # Src does not exist command_mock.reset_mock() command_instance.side_effect = [ 1, # exists ] with pytest.raises(FsOperationFailed): ulc.create_symbolic_link("test src", "test dst") assert command_instance.mock_calls == [ call("test '-e' 'test src'"), ] # Src exists but also dst command_mock.reset_mock() command_instance.side_effect = [ 0, # exists src 0, # exists dst ] with pytest.raises(FsOperationFailed): ulc.create_symbolic_link("test src", "test dst") assert command_instance.mock_calls == [ call("test '-e' 'test src'"), call("test '-e' 'test dst'"), ] # Path exists, dst does not exist, link creation failed command_mock.reset_mock() command_instance.side_effect = [ 0, # exists src 1, # exists dst 1, # link ] with pytest.raises(FsOperationFailed): ulc.create_symbolic_link("test src", "test dst") assert command_instance.mock_calls == [ call("test '-e' 'test src'"), call("test '-e' 'test dst'"), call("ln '-s' 'test src' 'test dst'"), ] # Path exists, dst does not exist, link created command_mock.reset_mock() command_instance.side_effect = [ 0, # exists src 1, # exists dst 0, # link ] result = ulc.create_symbolic_link("test src", "test dst") assert result assert command_instance.mock_calls == [ call("test '-e' 'test src'"), call("test '-e' 'test dst'"), call("ln '-s' 'test src' 'test dst'"), ]
def test_check_write_permission(self, command_mock): command_instance = command_mock.return_value ulc = UnixLocalCommand() # Path does not exist command_mock.reset_mock() command_instance.side_effect = [ 1, # exists ] with pytest.raises(FsOperationFailed): ulc.check_write_permission("test dir") assert command_instance.mock_calls == [ call("test '-e' 'test dir'"), ] # Path exists but is not a directory command_mock.reset_mock() command_instance.side_effect = [ 0, # exists 1, # is dir ] with pytest.raises(FsOperationFailed): ulc.check_write_permission("test dir") assert command_instance.mock_calls == [ call("test '-e' 'test dir'"), call("test '-d' 'test dir'"), ] # Path exists, is a directory, but is not writable command_mock.reset_mock() command_instance.side_effect = [ 0, # exists 0, # is dir 1, # can write ] with pytest.raises(FsOperationFailed): ulc.check_write_permission("test dir") assert command_instance.mock_calls == [ call("test '-e' 'test dir'"), call("test '-d' 'test dir'"), call("touch 'test dir/.barman_write_check'"), ] # Path exists, is a directory, is writable, but remove failure command_mock.reset_mock() command_instance.side_effect = [ 0, # exists 0, # is dir 0, # can write 1, # can remove ] with pytest.raises(FsOperationFailed): ulc.check_write_permission("test dir") assert command_instance.mock_calls == [ call("test '-e' 'test dir'"), call("test '-d' 'test dir'"), call("touch 'test dir/.barman_write_check'"), call("rm 'test dir/.barman_write_check'"), ] # Path exists, is a directory, is writable, and can remove command_mock.reset_mock() command_instance.side_effect = [ 0, # exists 0, # is dir 0, # can write 0, # can remove ] result = ulc.check_write_permission("test dir") assert result assert command_instance.mock_calls == [ call("test '-e' 'test dir'"), call("test '-d' 'test dir'"), call("touch 'test dir/.barman_write_check'"), call("rm 'test dir/.barman_write_check'"), ]
def _setup(self, backup_info, remote_command, dest): """ Prepare the recovery_info dictionary for the recovery, as well as temporary working directory :param barman.infofile.BackupInfo backup_info: representation of a backup :param str remote_command: ssh command for remote connection :return dict: recovery_info dictionary, holding the basic values for a recovery """ # Calculate the name of the WAL directory if backup_info.version < 100000: wal_dest = os.path.join(dest, 'pg_xlog') else: wal_dest = os.path.join(dest, 'pg_wal') recovery_info = { 'cmd': None, 'recovery_dest': 'local', 'rsync': None, 'configuration_files': [], 'destination_path': dest, 'temporary_configuration_files': [], 'tempdir': tempfile.mkdtemp(prefix='barman_recovery-'), 'is_pitr': False, 'wal_dest': wal_dest, 'get_wal': RecoveryOptions.GET_WAL in self.config.recovery_options, } # A map that will keep track of the results of the recovery. # Used for output generation results = { 'changes': [], 'warnings': [], 'delete_barman_xlog': False, 'missing_files': [], 'get_wal': False, } recovery_info['results'] = results # Set up a list of configuration files recovery_info['configuration_files'].append('postgresql.conf') if backup_info.version >= 90400: recovery_info['configuration_files'].append('postgresql.auto.conf') # Handle remote recovery options if remote_command: recovery_info['recovery_dest'] = 'remote' try: recovery_info['rsync'] = RsyncPgData( path=self.server.path, ssh=remote_command, bwlimit=self.config.bandwidth_limit, network_compression=self.config.network_compression) except CommandFailedException: self._teardown(recovery_info) raise try: # create a UnixRemoteCommand obj if is a remote recovery recovery_info['cmd'] = UnixRemoteCommand(remote_command, path=self.server.path) except FsOperationFailed: self._teardown(recovery_info) output.error( "Unable to connect to the target host using the command " "'%s'", remote_command) output.close_and_exit() else: # if is a local recovery create a UnixLocalCommand recovery_info['cmd'] = UnixLocalCommand() return recovery_info
def test_get_file_mode(self, tmpdir): incoming_dir = tmpdir.mkdir("some_dir") print(incoming_dir.strpath) cm = UnixLocalCommand() print(cm.get_file_mode(incoming_dir.strpath)) assert "755" == cm.get_file_mode(incoming_dir.strpath)