Example #1
0
    def test_list_files(self):
        """
        Unit test for RsyncCopyController._list_file's code
        """
        # Mock rsync invocation
        rsync_mock = mock.Mock(name='Rsync()')
        rsync_mock.ret = 0
        rsync_mock.out = 'drwxrwxrwt       69632 2015/02/09 15:01:00 tmp\n' \
                         'drwxrwxrwt       69612 Thu Feb 19 15:01:22 2015 tmp2'
        rsync_mock.err = 'err'

        # Test the _list_files internal method
        rcc = RsyncCopyController()
        return_values = list(rcc._list_files(rsync_mock, 'some/path'))

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

        # Check rsync.get_output has called correctly
        rsync_mock.get_output.assert_called_with('--no-human-readable',
                                                 '--list-only',
                                                 '-r',
                                                 'some/path',
                                                 check=True)

        # Check the result
        assert return_values[0] == _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] == _FileItem(
            'drwxrwxrwt', 69612,
            datetime(year=2015,
                     month=2,
                     day=19,
                     hour=15,
                     minute=1,
                     second=22,
                     tzinfo=dateutil.tz.tzlocal()), 'tmp2')

        # Test the _list_files internal method with a wrong output (added TZ)
        rsync_mock.out = (
            'drwxrwxrwt       69612 Thu Feb 19 15:01:22 CET 2015 tmp2\n')

        rcc = RsyncCopyController()
        with pytest.raises(RsyncListFilesFailure):
            # The list() call is needed to consume the generator
            list(rcc._list_files(rsync_mock, 'some/path'))

        # Check rsync.get_output has called correctly
        rsync_mock.get_output.assert_called_with('--no-human-readable',
                                                 '--list-only',
                                                 '-r',
                                                 'some/path',
                                                 check=True)
    def test_list_files(self):
        """
        Unit test for RsyncCopyController_list_file's code
        """
        # Mock rsync invocation
        rsync_mock = mock.Mock(name='Rsync()')
        rsync_mock.ret = 0
        rsync_mock.out = 'drwxrwxrwt       69632 2015/02/09 15:01:00 tmp\n' \
                         'drwxrwxrwt       69612 2015/02/19 15:01:22 tmp2'
        rsync_mock.err = 'err'

        # Test the _list_files internal method
        rcc = RsyncCopyController()
        return_values = list(rcc._list_files(rsync_mock, 'some/path'))

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

        # Check rsync.get_output has called correctly
        rsync_mock.get_output.assert_called_with(
            '--no-human-readable', '--list-only', '-r', 'some/path',
            check=True)

        # Check the result
        assert return_values[0] == rcc._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] == rcc._FileItem(
            'drwxrwxrwt',
            69612,
            datetime(year=2015, month=2, day=19,
                     hour=15, minute=1, second=22,
                     tzinfo=dateutil.tz.tzlocal()),
            'tmp2')
    def test_list_files(self, rsync_factory_mock):
        """
        Unit test for RsyncCopyController._list_file's code
        """
        # Mock rsync invocation
        rsync_mock = mock.Mock(name="Rsync()")
        rsync_mock.ret = 0
        rsync_mock.out = (
            "drwxrwxrwt       69632 2015/02/09 15:01:00 tmp\n"
            "drwxrwxrwt       69612 Thu Feb 19 15:01:22 2015 tmp2")
        rsync_mock.err = "err"

        # Mock _rsync_factory() invocation
        rsync_factory_mock.return_value = rsync_mock

        # Create an item to inspect
        item = _RsyncCopyItem(
            label="pgdata",
            src=":/pg/data/",
            dst="/some/dir",
            is_directory=True,
            item_class=RsyncCopyController.PGDATA_CLASS,
            optional=False,
        )

        # Test the _list_files internal method
        rcc = RsyncCopyController()
        return_values = list(rcc._list_files(item, "some/path"))

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

        # Verify that _rsync_factory has been called correctly
        assert rsync_factory_mock.mock_calls == [
            mock.call(item),
        ]

        # Check rsync.get_output has called correctly
        rsync_mock.get_output.assert_called_with("--no-human-readable",
                                                 "--list-only",
                                                 "-r",
                                                 "some/path",
                                                 check=True)

        # Check the result
        assert return_values[0] == _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] == _FileItem(
            "drwxrwxrwt",
            69612,
            datetime(
                year=2015,
                month=2,
                day=19,
                hour=15,
                minute=1,
                second=22,
                tzinfo=dateutil.tz.tzlocal(),
            ),
            "tmp2",
        )

        # Test the _list_files internal method with a wrong output (added TZ)
        rsync_mock.out = "drwxrwxrwt       69612 Thu Feb 19 15:01:22 CET 2015 tmp2\n"

        rcc = RsyncCopyController()
        with pytest.raises(RsyncListFilesFailure):
            # The list() call is needed to consume the generator
            list(rcc._list_files(rsync_mock, "some/path"))

        # Check rsync.get_output has called correctly
        rsync_mock.get_output.assert_called_with("--no-human-readable",
                                                 "--list-only",
                                                 "-r",
                                                 "some/path",
                                                 check=True)