def test_protect_ssh_invocation(self, popen, pipe_processor_loop):
        ret = 0
        out = 'out'
        err = 'err'

        pipe = _mock_pipe(popen, pipe_processor_loop, ret, out, err)

        cmd = command_wrappers.Rsync(exclude_and_protect=['foo', 'bar'])
        result = cmd('src', 'dst')

        popen.assert_called_with([
            'rsync', '--exclude=foo', '--filter=P_foo', '--exclude=bar',
            '--filter=P_bar', 'src', 'dst'
        ],
                                 shell=False,
                                 env=mock.ANY,
                                 stdout=PIPE,
                                 stderr=PIPE,
                                 stdin=PIPE,
                                 preexec_fn=mock.ANY,
                                 close_fds=True)
        assert not pipe.stdin.write.called
        pipe.stdin.close.assert_called_once_with()
        assert result == ret
        assert cmd.ret == ret
        assert cmd.out == out
        assert cmd.err == err
    def test_custom_ssh_invocation(self, popen, pipe_processor_loop, which):
        ret = 0
        out = 'out'
        err = 'err'

        pipe = _mock_pipe(popen, pipe_processor_loop, ret, out, err)
        cmd = command_wrappers.Rsync('/custom/rsync',
                                     ssh='/custom/ssh',
                                     ssh_options=['-c', 'arcfour'])
        result = cmd('src', 'dst')

        which.assert_called_with('/custom/rsync', None)
        popen.assert_called_with([
            '/custom/rsync', '-e', "/custom/ssh '-c' 'arcfour'", 'src', 'dst'
        ],
                                 shell=False,
                                 env=None,
                                 stdout=PIPE,
                                 stderr=PIPE,
                                 stdin=PIPE,
                                 preexec_fn=mock.ANY,
                                 close_fds=True)
        assert not pipe.stdin.write.called
        pipe.stdin.close.assert_called_once_with()
        assert result == ret
        assert cmd.ret == ret
        assert cmd.out == out
        assert cmd.err == err
 def test_rsync_build_failure(self, popen, pipe_processor_loop):
     """
     Simple test that checks if a CommandFailedException is raised
     when Rsync object is build with an invalid path or rsync
     is not in system path
     """
     # Pass an invalid path to Rsync class constructor.
     # Expect a CommandFailedException
     with pytest.raises(barman.exceptions.CommandFailedException):
         command_wrappers.Rsync('/invalid/path/rsync')
     # Force the which method to return false, simulating rsync command not
     # present in system PATH. Expect a CommandFailedExceptiomn
     with mock.patch("barman.utils.which") as mock_which:
         mock_which.return_value = False
         with pytest.raises(barman.exceptions.CommandFailedException):
             command_wrappers.Rsync(ssh_options=['-c', 'arcfour'])
    def test_invocation_list_file(self, popen, pipe_processor_loop):
        """
        Unit test for dateutil package in list_file

        This test cover all list_file's code with correct parameters

        :param tmpdir: temporary folder
        :param popen: mock popen
        """
        # variables to be tested
        ret = 0
        out = 'drwxrwxrwt       69632 2015/02/09 15:01:00 tmp\n' \
              'drwxrwxrwt       69612 2015/02/19 15:01:22 tmp2'
        err = 'err'
        # created mock pipe
        pipe = _mock_pipe(popen, pipe_processor_loop, ret, out, err)
        # created rsync and launched list_files
        cmd = command_wrappers.Rsync()
        return_values = list(cmd.list_files('some/path'))

        # returned list must contain two elements
        assert len(return_values) == 2

        # assert call
        popen.assert_called_with(
            ['rsync', '--no-human-readable', '--list-only', '-r', 'some/path'],
            shell=False, env=None,
            stdout=PIPE, stderr=PIPE, stdin=PIPE,
            preexec_fn=mock.ANY, close_fds=True
        )

        # Rsync pipe must be called with no input
        assert not pipe.stdin.write.called
        pipe.stdin.close.assert_called_once_with()

        # assert tmp and tmp2 in test_list
        assert return_values[0] == cmd.FileItem(
            'drwxrwxrwt',
            69632,
            datetime(year=2015, month=2, day=9,
                     hour=15, minute=1, second=0,
                     tzinfo=dateutil.tz.tzlocal()),
            'tmp')
        assert return_values[1] == cmd.FileItem(
            'drwxrwxrwt',
            69612,
            datetime(year=2015, month=2, day=19,
                     hour=15, minute=1, second=22,
                     tzinfo=dateutil.tz.tzlocal()),
            'tmp2')
    def test_args_invocation(self, popen, pipe_processor_loop):
        ret = 0
        out = 'out'
        err = 'err'

        pipe = _mock_pipe(popen, pipe_processor_loop, ret, out, err)

        cmd = command_wrappers.Rsync(args=['a', 'b'])
        result = cmd('src', 'dst')

        popen.assert_called_with(
            ['rsync', 'a', 'b', 'src', 'dst'], shell=False, env=None,
            stdout=PIPE, stderr=PIPE, stdin=PIPE,
            preexec_fn=mock.ANY, close_fds=True
        )
        assert not pipe.stdin.write.called
        pipe.stdin.close.assert_called_once_with()
        assert result == ret
        assert cmd.ret == ret
        assert cmd.out == out
        assert cmd.err == err
    def test_from_file_list_ssh_invocation(self, popen, pipe_processor_loop):
        ret = 0
        out = 'out'
        err = 'err'

        pipe = _mock_pipe(popen, pipe_processor_loop, ret, out, err)

        cmd = command_wrappers.Rsync()
        result = cmd.from_file_list(['a', 'b', 'c'], 'src', 'dst')

        popen.assert_called_with(
            ['rsync', '--files-from=-', 'src', 'dst'],
            shell=False, env=None,
            stdout=PIPE, stderr=PIPE, stdin=PIPE,
            preexec_fn=mock.ANY, close_fds=True
        )
        pipe.stdin.write.assert_called_with('a\nb\nc'.encode('UTF-8'))
        pipe.stdin.close.assert_called_once_with()
        assert result == ret
        assert cmd.ret == ret
        assert cmd.out == out
        assert cmd.err == err