Exemplo n.º 1
0
    def test_statistics(self, signal_mock, tempfile_mock, copy_mock,
                        create_and_purge_mock, analyse_mock, rsync_mock,
                        tmpdir, workers):
        """
        Unit test for RsyncCopyController.statistics's code
        """

        # Do a fake copy run to populate the start/stop timestamps.
        # The steps are the same of the full run test
        tempdir = tmpdir.mkdir('tmp')
        tempfile_mock.return_value = tempdir.strpath
        server = build_real_server(
            global_conf={'barman_home': tmpdir.mkdir('home').strpath})
        config = server.config
        executor = server.backup_manager.executor

        rcc = RsyncCopyController(
            path=server.path,
            ssh_command=executor.ssh_command,
            ssh_options=executor.ssh_options,
            network_compression=config.network_compression,
            reuse_backup=None,
            safe_horizon=None,
            workers=workers)

        backup_info = build_test_backup_info(
            server=server,
            pgdata="/pg/data",
            config_file="/etc/postgresql.conf",
            hba_file="/pg/data/pg_hba.conf",
            ident_file="/pg/data/pg_ident.conf",
            begin_xlog="0/2000028",
            begin_wal="000000010000000000000002",
            begin_offset=28)
        backup_info.save()
        # This is to check that all the preparation is done correctly
        assert os.path.exists(backup_info.filename)

        # Silence the access to result properties
        rsync_mock.return_value.out = ''
        rsync_mock.return_value.err = ''
        rsync_mock.return_value.ret = 0

        # Mock analyze directory
        def analyse_func(item):
            l = item.label
            item.dir_file = l + '_dir_file'
            item.exclude_and_protect_file = l + '_exclude_and_protect_file'
            item.safe_list = [_FileItem('mode', 1, 'date', 'path')]
            item.check_list = [_FileItem('mode', 1, 'date', 'path')]

        analyse_mock.side_effect = analyse_func

        rcc.add_directory(label='tbs1',
                          src=':/fake/location/',
                          dst=backup_info.get_data_directory(16387),
                          reuse=None,
                          bwlimit=None,
                          item_class=rcc.TABLESPACE_CLASS)
        rcc.add_directory(label='tbs2',
                          src=':/another/location/',
                          dst=backup_info.get_data_directory(16405),
                          reuse=None,
                          bwlimit=None,
                          item_class=rcc.TABLESPACE_CLASS)
        rcc.add_directory(
            label='pgdata',
            src=':/pg/data/',
            dst=backup_info.get_data_directory(),
            reuse=None,
            bwlimit=None,
            item_class=rcc.PGDATA_CLASS,
            exclude=[
                '/pg_xlog/*', '/pg_log/*', '/recovery.conf', '/postmaster.pid'
            ],
            exclude_and_protect=['pg_tblspc/16387', 'pg_tblspc/16405'])
        rcc.add_file(label='pg_control',
                     src=':/pg/data/global/pg_control',
                     dst='%s/global/pg_control' %
                     backup_info.get_data_directory(),
                     item_class=rcc.PGCONTROL_CLASS)
        rcc.add_file(label='config_file',
                     src=':/etc/postgresql.conf',
                     dst=backup_info.get_data_directory(),
                     item_class=rcc.CONFIG_CLASS,
                     optional=False)
        # Do the fake run
        rcc.copy()

        # Calculate statistics
        result = rcc.statistics()

        # We cannot check the actual result because it is not predictable,
        # so we check that every value is present and is a number and it is
        # greather than 0
        assert result.get('analysis_time') > 0
        assert 'analysis_time_per_item' in result
        for tbs in ('pgdata', 'tbs1', 'tbs2'):
            assert result['analysis_time_per_item'][tbs] > 0

        assert result.get('copy_time') > 0
        assert 'copy_time_per_item' in result
        assert 'serialized_copy_time_per_item' in result
        for tbs in ('pgdata', 'tbs1', 'tbs2', 'config_file', 'pg_control'):
            assert result['copy_time_per_item'][tbs] > 0
            assert result['serialized_copy_time_per_item'][tbs] > 0

        assert result.get('number_of_workers') == rcc.workers
        assert result.get('total_time') > 0
Exemplo n.º 2
0
    def test_full_copy(self, signal_mock, tempfile_mock, copy_mock,
                       create_and_purge_mock, analyse_mock, rsync_mock,
                       tmpdir):
        """
        Test the execution of a full copy
        """

        # Build the prerequisites
        tempdir = tmpdir.mkdir('tmp')
        tempfile_mock.return_value = tempdir.strpath
        server = build_real_server(
            global_conf={'barman_home': tmpdir.mkdir('home').strpath})
        config = server.config
        executor = server.backup_manager.executor

        rcc = RsyncCopyController(
            path=server.path,
            ssh_command=executor.ssh_command,
            ssh_options=executor.ssh_options,
            network_compression=config.network_compression,
            reuse_backup=None,
            safe_horizon=None)

        backup_info = build_test_backup_info(
            server=server,
            pgdata="/pg/data",
            config_file="/etc/postgresql.conf",
            hba_file="/pg/data/pg_hba.conf",
            ident_file="/pg/data/pg_ident.conf",
            begin_xlog="0/2000028",
            begin_wal="000000010000000000000002",
            begin_offset=28)
        backup_info.save()
        # This is to check that all the preparation is done correctly
        assert os.path.exists(backup_info.filename)

        # Silence the access to result properties
        rsync_mock.return_value.out = ''
        rsync_mock.return_value.err = ''
        rsync_mock.return_value.ret = 0

        # Mock analyze directory
        def analyse_func(item):
            l = item.label
            item.dir_file = l + '_dir_file'
            item.exclude_and_protect_file = l + '_exclude_and_protect_file'
            item.safe_list = [_FileItem('mode', 1, 'date', 'path')]
            item.check_list = [_FileItem('mode', 1, 'date', 'path')]

        analyse_mock.side_effect = analyse_func

        rcc.add_directory(label='tbs1',
                          src=':/fake/location/',
                          dst=backup_info.get_data_directory(16387),
                          reuse=None,
                          bwlimit=None,
                          item_class=rcc.TABLESPACE_CLASS)
        rcc.add_directory(label='tbs2',
                          src=':/another/location/',
                          dst=backup_info.get_data_directory(16405),
                          reuse=None,
                          bwlimit=None,
                          item_class=rcc.TABLESPACE_CLASS)
        rcc.add_directory(
            label='pgdata',
            src=':/pg/data/',
            dst=backup_info.get_data_directory(),
            reuse=None,
            bwlimit=None,
            item_class=rcc.PGDATA_CLASS,
            exclude=[
                '/pg_xlog/*', '/pg_log/*', '/recovery.conf', '/postmaster.pid'
            ],
            exclude_and_protect=['pg_tblspc/16387', 'pg_tblspc/16405'])
        rcc.add_file(label='pg_control',
                     src=':/pg/data/global/pg_control',
                     dst='%s/global/pg_control' %
                     backup_info.get_data_directory(),
                     item_class=rcc.PGCONTROL_CLASS)
        rcc.add_file(label='config_file',
                     src=':/etc/postgresql.conf',
                     dst=backup_info.get_data_directory(),
                     item_class=rcc.CONFIG_CLASS,
                     optional=False)
        rcc.copy()

        # Check the order of calls to the Rsync mock
        assert rsync_mock.mock_calls == [
            mock.call(network_compression=False,
                      args=['--itemize-changes', '--itemize-changes'],
                      bwlimit=None,
                      ssh='ssh',
                      path=None,
                      ssh_options=[
                          '-c', '"arcfour"', '-p', '22',
                          '*****@*****.**', '-o', 'BatchMode=yes', '-o',
                          'StrictHostKeyChecking=no'
                      ],
                      exclude=None,
                      exclude_and_protect=None,
                      include=None,
                      retry_sleep=0,
                      retry_times=0,
                      retry_handler=mock.ANY),
            mock.call(network_compression=False,
                      args=['--itemize-changes', '--itemize-changes'],
                      bwlimit=None,
                      ssh='ssh',
                      path=None,
                      ssh_options=[
                          '-c', '"arcfour"', '-p', '22',
                          '*****@*****.**', '-o', 'BatchMode=yes', '-o',
                          'StrictHostKeyChecking=no'
                      ],
                      exclude=None,
                      exclude_and_protect=None,
                      include=None,
                      retry_sleep=0,
                      retry_times=0,
                      retry_handler=mock.ANY),
            mock.call(
                network_compression=False,
                args=['--itemize-changes', '--itemize-changes'],
                bwlimit=None,
                ssh='ssh',
                path=None,
                ssh_options=[
                    '-c', '"arcfour"', '-p', '22', '*****@*****.**',
                    '-o', 'BatchMode=yes', '-o', 'StrictHostKeyChecking=no'
                ],
                exclude=[
                    '/pg_xlog/*', '/pg_log/*', '/recovery.conf',
                    '/postmaster.pid'
                ],
                exclude_and_protect=['pg_tblspc/16387', 'pg_tblspc/16405'],
                include=None,
                retry_sleep=0,
                retry_times=0,
                retry_handler=mock.ANY),
            mock.call(network_compression=False,
                      args=['--itemize-changes', '--itemize-changes'],
                      bwlimit=None,
                      ssh='ssh',
                      path=None,
                      ssh_options=[
                          '-c', '"arcfour"', '-p', '22',
                          '*****@*****.**', '-o', 'BatchMode=yes', '-o',
                          'StrictHostKeyChecking=no'
                      ],
                      exclude=None,
                      exclude_and_protect=None,
                      include=None,
                      retry_sleep=0,
                      retry_times=0,
                      retry_handler=mock.ANY),
            mock.call()(':/etc/postgresql.conf',
                        backup_info.get_data_directory(),
                        allowed_retval=(0, 23, 24)),
            mock.call(network_compression=False,
                      args=['--itemize-changes', '--itemize-changes'],
                      bwlimit=None,
                      ssh='ssh',
                      path=None,
                      ssh_options=[
                          '-c', '"arcfour"', '-p', '22',
                          '*****@*****.**', '-o', 'BatchMode=yes', '-o',
                          'StrictHostKeyChecking=no'
                      ],
                      exclude=None,
                      exclude_and_protect=None,
                      include=None,
                      retry_sleep=0,
                      retry_times=0,
                      retry_handler=mock.ANY),
            mock.call()(':/pg/data/global/pg_control',
                        '%s/global/pg_control' %
                        backup_info.get_data_directory(),
                        allowed_retval=(0, 23, 24)),
        ]

        # Check calls to _analyse_directory method
        assert analyse_mock.mock_calls == [
            mock.call(item) for item in rcc.item_list if item.is_directory
        ]

        # Check calls to _create_dir_and_purge method
        assert create_and_purge_mock.mock_calls == [
            mock.call(item) for item in rcc.item_list if item.is_directory
        ]

        # Utility function to build the file_list name
        def file_list_name(label, kind):
            return '%s/%s_%s_%s.list' % (tempdir.strpath, label, kind,
                                         os.getpid())

        # Check the order of calls to the copy method
        # All the file_list arguments are None because the analyze part
        # has not really been executed
        assert copy_mock.mock_calls == [
            mock.call(mock.ANY,
                      ':/fake/location/',
                      backup_info.get_data_directory(16387),
                      checksum=False,
                      file_list=file_list_name('tbs1', 'safe')),
            mock.call(mock.ANY,
                      ':/fake/location/',
                      backup_info.get_data_directory(16387),
                      checksum=True,
                      file_list=file_list_name('tbs1', 'check')),
            mock.call(mock.ANY,
                      ':/another/location/',
                      backup_info.get_data_directory(16405),
                      checksum=False,
                      file_list=file_list_name('tbs2', 'safe')),
            mock.call(mock.ANY,
                      ':/another/location/',
                      backup_info.get_data_directory(16405),
                      checksum=True,
                      file_list=file_list_name('tbs2', 'check')),
            mock.call(mock.ANY,
                      ':/pg/data/',
                      backup_info.get_data_directory(),
                      checksum=False,
                      file_list=file_list_name('pgdata', 'safe')),
            mock.call(mock.ANY,
                      ':/pg/data/',
                      backup_info.get_data_directory(),
                      checksum=True,
                      file_list=file_list_name('pgdata', 'check')),
        ]
Exemplo n.º 3
0
    def test_full_copy(self, tempfile_mock, copy_mock, create_and_purge_mock,
                       analyse_mock, rsync_mock, tmpdir):
        """
        Test the execution of a full copy
        """

        # Build the prerequisites
        tempdir = tmpdir.mkdir('tmp')
        tempfile_mock.return_value = tempdir.strpath
        server = build_real_server(global_conf={
            'barman_home': tmpdir.mkdir('home').strpath
        })
        config = server.config
        executor = server.backup_manager.executor

        rcc = RsyncCopyController(
            path=server.path,
            ssh_command=executor.ssh_command,
            ssh_options=executor.ssh_options,
            network_compression=config.network_compression,
            reuse_backup=None,
            safe_horizon=None)

        backup_info = build_test_backup_info(
            server=server,
            pgdata="/pg/data",
            config_file="/etc/postgresql.conf",
            hba_file="/pg/data/pg_hba.conf",
            ident_file="/pg/data/pg_ident.conf",
            begin_xlog="0/2000028",
            begin_wal="000000010000000000000002",
            begin_offset=28)
        backup_info.save()
        # This is to check that all the preparation is done correctly
        assert os.path.exists(backup_info.filename)

        # Silence the access to result properties
        rsync_mock.return_value.out = ''
        rsync_mock.return_value.err = ''
        rsync_mock.return_value.ret = 0

        rcc.add_directory(
            label='tbs1',
            src=':/fake/location/',
            dst=backup_info.get_data_directory(16387),
            reuse=None,
            bwlimit=None,
            item_class=rcc.TABLESPACE_CLASS),
        rcc.add_directory(
            label='tbs2',
            src=':/another/location/',
            dst=backup_info.get_data_directory(16405),
            reuse=None,
            bwlimit=None,
            item_class=rcc.TABLESPACE_CLASS),
        rcc.add_directory(
            label='pgdata',
            src=':/pg/data/',
            dst=backup_info.get_data_directory(),
            reuse=None,
            bwlimit=None,
            item_class=rcc.PGDATA_CLASS,
            exclude=['/pg_xlog/*',
                     '/pg_log/*',
                     '/recovery.conf',
                     '/postmaster.pid'],
            exclude_and_protect=['pg_tblspc/16387', 'pg_tblspc/16405']),
        rcc.add_file(
            label='pg_control',
            src=':/pg/data/global/pg_control',
            dst='%s/global/pg_control' % backup_info.get_data_directory(),
            item_class=rcc.PGCONTROL_CLASS),
        rcc.add_file(
            label='config_file',
            src=':/etc/postgresql.conf',
            dst=backup_info.get_data_directory(),
            item_class=rcc.CONFIG_CLASS,
            optional=False),
        rcc.copy(),

        # Check the order of calls to the Rsync mock
        assert rsync_mock.mock_calls == [
            mock.call(network_compression=False,
                      args=['--itemize-changes',
                            '--itemize-changes'],
                      bwlimit=None, ssh='ssh', path=None,
                      ssh_options=['-c', '"arcfour"', '-p', '22',
                                   '*****@*****.**', '-o',
                                   'BatchMode=yes', '-o',
                                   'StrictHostKeyChecking=no'],
                      exclude=None, exclude_and_protect=None,
                      retry_sleep=0, retry_times=0, retry_handler=mock.ANY),
            mock.call(network_compression=False,
                      args=['--itemize-changes',
                            '--itemize-changes'],
                      bwlimit=None, ssh='ssh', path=None,
                      ssh_options=['-c', '"arcfour"', '-p', '22',
                                   '*****@*****.**', '-o',
                                   'BatchMode=yes', '-o',
                                   'StrictHostKeyChecking=no'],
                      exclude=None, exclude_and_protect=None,
                      retry_sleep=0, retry_times=0, retry_handler=mock.ANY),
            mock.call(network_compression=False,
                      args=['--itemize-changes',
                            '--itemize-changes'],
                      bwlimit=None, ssh='ssh', path=None,
                      ssh_options=['-c', '"arcfour"', '-p', '22',
                                   '*****@*****.**', '-o',
                                   'BatchMode=yes', '-o',
                                   'StrictHostKeyChecking=no'],
                      exclude=[
                          '/pg_xlog/*',
                          '/pg_log/*',
                          '/recovery.conf',
                          '/postmaster.pid'],
                      exclude_and_protect=[
                          'pg_tblspc/16387',
                          'pg_tblspc/16405'],
                      retry_sleep=0, retry_times=0, retry_handler=mock.ANY),
            mock.call(network_compression=False,
                      args=['--itemize-changes',
                            '--itemize-changes'],
                      bwlimit=None, ssh='ssh', path=None,
                      ssh_options=['-c', '"arcfour"', '-p', '22',
                                   '*****@*****.**', '-o',
                                   'BatchMode=yes', '-o',
                                   'StrictHostKeyChecking=no'],
                      exclude=None, exclude_and_protect=None,
                      retry_sleep=0, retry_times=0, retry_handler=mock.ANY),
            mock.call()(
                ':/pg/data/global/pg_control',
                '%s/global/pg_control' % backup_info.get_data_directory(),
                allowed_retval=(0, 23, 24)),
            mock.call(network_compression=False,
                      args=['--itemize-changes',
                            '--itemize-changes'],
                      bwlimit=None, ssh='ssh', path=None,
                      ssh_options=['-c', '"arcfour"', '-p', '22',
                                   '*****@*****.**', '-o',
                                   'BatchMode=yes', '-o',
                                   'StrictHostKeyChecking=no'],
                      exclude=None, exclude_and_protect=None,
                      retry_sleep=0, retry_times=0, retry_handler=mock.ANY),
            mock.call()(
                ':/etc/postgresql.conf',
                backup_info.get_data_directory(),
                allowed_retval=(0, 23, 24)),
        ]

        # Check calls to _analyse_directory method
        assert analyse_mock.mock_calls == [
            mock.call(item, tempdir.strpath) for item in rcc.item_list
            if item.is_directory
        ]

        # Check calls to _create_dir_and_purge method
        assert create_and_purge_mock.mock_calls == [
            mock.call(item) for item in rcc.item_list
            if item.is_directory
        ]

        # Check the order of calls to the copy method
        # All the file_list arguments are None because the analyze part
        # has not really been executed
        assert copy_mock.mock_calls == [
            mock.call(
                mock.ANY, ':/fake/location/',
                backup_info.get_data_directory(16387), checksum=False,
                file_list=None),
            mock.call(
                mock.ANY, ':/fake/location/',
                backup_info.get_data_directory(16387), checksum=True,
                file_list=None),
            mock.call(
                mock.ANY, ':/another/location/',
                backup_info.get_data_directory(16405), checksum=False,
                file_list=None),
            mock.call(
                mock.ANY, ':/another/location/',
                backup_info.get_data_directory(16405), checksum=True,
                file_list=None),
            mock.call(mock.ANY, ':/pg/data/',
                      backup_info.get_data_directory(), checksum=False,
                      file_list=None),
            mock.call(mock.ANY, ':/pg/data/',
                      backup_info.get_data_directory(), checksum=True,
                      file_list=None),
        ]
Exemplo n.º 4
0
    def test_full_copy(self, smart_copy_mock, rsync_mock, tmpdir):
        """
        Test the execution of a rsync copy

        :param rsync_mock: mock for the rsync command
        :param tmpdir: temporary dir
        """

        # Build the prerequisites
        server = build_real_server(
            global_conf={'barman_home': tmpdir.mkdir('home').strpath})
        config = server.config
        executor = server.backup_manager.executor

        rcc = RsyncCopyController(
            path=server.path,
            ssh_command=executor.ssh_command,
            ssh_options=executor.ssh_options,
            network_compression=config.network_compression,
            reuse_backup=None,
            safe_horizon=None)

        backup_info = build_test_backup_info(
            server=server,
            pgdata="/pg/data",
            config_file="/etc/postgresql.conf",
            hba_file="/pg/data/pg_hba.conf",
            ident_file="/pg/data/pg_ident.conf",
            begin_xlog="0/2000028",
            begin_wal="000000010000000000000002",
            begin_offset=28)
        backup_info.save()
        # This is to check that all the preparation is done correctly
        assert os.path.exists(backup_info.filename)

        # Silence the access to result properties
        rsync_mock.return_value.out = ''
        rsync_mock.return_value.err = ''
        rsync_mock.return_value.ret = 0

        rcc.add_directory(label='tbs1',
                          src=':/fake/location/',
                          dst=backup_info.get_data_directory(16387),
                          reuse=None,
                          bwlimit=None,
                          item_class=rcc.TABLESPACE_CLASS),
        rcc.add_directory(label='tbs2',
                          src=':/another/location/',
                          dst=backup_info.get_data_directory(16405),
                          reuse=None,
                          bwlimit=None,
                          item_class=rcc.TABLESPACE_CLASS),
        rcc.add_directory(
            label='pgdata',
            src=':/pg/data/',
            dst=backup_info.get_data_directory(),
            reuse=None,
            bwlimit=None,
            item_class=rcc.PGDATA_CLASS,
            exclude=[
                '/pg_xlog/*', '/pg_log/*', '/recovery.conf', '/postmaster.pid'
            ],
            exclude_and_protect=['pg_tblspc/16387', 'pg_tblspc/16405']),
        rcc.add_file(label='pg_control',
                     src=':/pg/data/global/pg_control',
                     dst='%s/global/pg_control' %
                     backup_info.get_data_directory(),
                     item_class=rcc.PGCONTROL_CLASS),
        rcc.add_file(label='config_file',
                     src=':/etc/postgresql.conf',
                     dst=backup_info.get_data_directory(),
                     item_class=rcc.CONFIG_CLASS,
                     optional=False),
        rcc.copy(),

        assert rsync_mock.mock_calls == [
            mock.call(network_compression=False,
                      args=['--itemize-changes', '--itemize-changes'],
                      bwlimit=None,
                      ssh='ssh',
                      path=None,
                      ssh_options=[
                          '-c', '"arcfour"', '-p', '22',
                          '*****@*****.**', '-o', 'BatchMode=yes', '-o',
                          'StrictHostKeyChecking=no'
                      ],
                      exclude=None,
                      exclude_and_protect=None,
                      retry_sleep=0,
                      retry_times=0,
                      retry_handler=mock.ANY),
            mock.call(network_compression=False,
                      args=['--itemize-changes', '--itemize-changes'],
                      bwlimit=None,
                      ssh='ssh',
                      path=None,
                      ssh_options=[
                          '-c', '"arcfour"', '-p', '22',
                          '*****@*****.**', '-o', 'BatchMode=yes', '-o',
                          'StrictHostKeyChecking=no'
                      ],
                      exclude=None,
                      exclude_and_protect=None,
                      retry_sleep=0,
                      retry_times=0,
                      retry_handler=mock.ANY),
            mock.call(
                network_compression=False,
                args=['--itemize-changes', '--itemize-changes'],
                bwlimit=None,
                ssh='ssh',
                path=None,
                ssh_options=[
                    '-c', '"arcfour"', '-p', '22', '*****@*****.**',
                    '-o', 'BatchMode=yes', '-o', 'StrictHostKeyChecking=no'
                ],
                exclude=[
                    '/pg_xlog/*', '/pg_log/*', '/recovery.conf',
                    '/postmaster.pid'
                ],
                exclude_and_protect=['pg_tblspc/16387', 'pg_tblspc/16405'],
                retry_sleep=0,
                retry_times=0,
                retry_handler=mock.ANY),
            mock.call(network_compression=False,
                      args=['--itemize-changes', '--itemize-changes'],
                      bwlimit=None,
                      ssh='ssh',
                      path=None,
                      ssh_options=[
                          '-c', '"arcfour"', '-p', '22',
                          '*****@*****.**', '-o', 'BatchMode=yes', '-o',
                          'StrictHostKeyChecking=no'
                      ],
                      exclude=None,
                      exclude_and_protect=None,
                      retry_sleep=0,
                      retry_times=0,
                      retry_handler=mock.ANY),
            mock.call()(':/pg/data/global/pg_control',
                        '%s/global/pg_control' %
                        backup_info.get_data_directory(),
                        allowed_retval=(0, 23, 24)),
            mock.call(network_compression=False,
                      args=['--itemize-changes', '--itemize-changes'],
                      bwlimit=None,
                      ssh='ssh',
                      path=None,
                      ssh_options=[
                          '-c', '"arcfour"', '-p', '22',
                          '*****@*****.**', '-o', 'BatchMode=yes', '-o',
                          'StrictHostKeyChecking=no'
                      ],
                      exclude=None,
                      exclude_and_protect=None,
                      retry_sleep=0,
                      retry_times=0,
                      retry_handler=mock.ANY),
            mock.call()(':/etc/postgresql.conf',
                        backup_info.get_data_directory(),
                        allowed_retval=(0, 23, 24)),
        ]

        assert smart_copy_mock.mock_calls == [
            mock.call(mock.ANY, ':/fake/location/',
                      backup_info.get_data_directory(16387), None, None),
            mock.call(mock.ANY, ':/another/location/',
                      backup_info.get_data_directory(16405), None, None),
            mock.call(mock.ANY, ':/pg/data/', backup_info.get_data_directory(),
                      None, None),
        ]
Exemplo n.º 5
0
    def test_statistics(
        self,
        signal_mock,
        tempfile_mock,
        copy_mock,
        create_and_purge_mock,
        analyse_mock,
        rsync_mock,
        tmpdir,
        workers,
    ):
        """
        Unit test for RsyncCopyController.statistics's code
        """

        # Do a fake copy run to populate the start/stop timestamps.
        # The steps are the same of the full run test
        tempdir = tmpdir.mkdir("tmp")
        tempfile_mock.return_value = tempdir.strpath
        server = build_real_server(
            global_conf={"barman_home": tmpdir.mkdir("home").strpath})
        config = server.config
        executor = server.backup_manager.executor

        rcc = RsyncCopyController(
            path=server.path,
            ssh_command=executor.ssh_command,
            ssh_options=executor.ssh_options,
            network_compression=config.network_compression,
            reuse_backup=None,
            safe_horizon=None,
            workers=workers,
        )

        backup_info = build_test_backup_info(
            server=server,
            pgdata="/pg/data",
            config_file="/etc/postgresql.conf",
            hba_file="/pg/data/pg_hba.conf",
            ident_file="/pg/data/pg_ident.conf",
            begin_xlog="0/2000028",
            begin_wal="000000010000000000000002",
            begin_offset=28,
        )
        backup_info.save()
        # This is to check that all the preparation is done correctly
        assert os.path.exists(backup_info.filename)

        # Silence the access to result properties
        rsync_mock.return_value.out = ""
        rsync_mock.return_value.err = ""
        rsync_mock.return_value.ret = 0

        # Mock analyze directory
        def analyse_func(item):
            label = item.label
            item.dir_file = label + "_dir_file"
            item.exclude_and_protect_file = label + "_exclude_and_protect_file"
            item.safe_list = [_FileItem("mode", 1, "date", "path")]
            item.check_list = [_FileItem("mode", 1, "date", "path")]

        analyse_mock.side_effect = analyse_func

        rcc.add_directory(
            label="tbs1",
            src=":/fake/location/",
            dst=backup_info.get_data_directory(16387),
            reuse=None,
            bwlimit=None,
            item_class=rcc.TABLESPACE_CLASS,
        )
        rcc.add_directory(
            label="tbs2",
            src=":/another/location/",
            dst=backup_info.get_data_directory(16405),
            reuse=None,
            bwlimit=None,
            item_class=rcc.TABLESPACE_CLASS,
        )
        rcc.add_directory(
            label="pgdata",
            src=":/pg/data/",
            dst=backup_info.get_data_directory(),
            reuse=None,
            bwlimit=None,
            item_class=rcc.PGDATA_CLASS,
            exclude=[
                "/pg_xlog/*",
                "/pg_log/*",
                "/log/*",
                "/recovery.conf",
                "/postmaster.pid",
            ],
            exclude_and_protect=["pg_tblspc/16387", "pg_tblspc/16405"],
        )
        rcc.add_file(
            label="pg_control",
            src=":/pg/data/global/pg_control",
            dst="%s/global/pg_control" % backup_info.get_data_directory(),
            item_class=rcc.PGCONTROL_CLASS,
        )
        rcc.add_file(
            label="config_file",
            src=":/etc/postgresql.conf",
            dst=backup_info.get_data_directory(),
            item_class=rcc.CONFIG_CLASS,
            optional=False,
        )
        # Do the fake run
        rcc.copy()

        # Calculate statistics
        result = rcc.statistics()

        # We cannot check the actual result because it is not predictable,
        # so we check that every value is present and is a number and it is
        # greater than 0
        assert result.get("analysis_time") > 0
        assert "analysis_time_per_item" in result
        for tbs in ("pgdata", "tbs1", "tbs2"):
            assert result["analysis_time_per_item"][tbs] > 0

        assert result.get("copy_time") > 0
        assert "copy_time_per_item" in result
        assert "serialized_copy_time_per_item" in result
        for tbs in ("pgdata", "tbs1", "tbs2", "config_file", "pg_control"):
            assert result["copy_time_per_item"][tbs] > 0
            assert result["serialized_copy_time_per_item"][tbs] > 0

        assert result.get("number_of_workers") == rcc.workers
        assert result.get("total_time") > 0
Exemplo n.º 6
0
    def test_full_copy(
        self,
        signal_mock,
        tempfile_mock,
        copy_mock,
        create_and_purge_mock,
        analyse_mock,
        rsync_mock,
        tmpdir,
    ):
        """
        Test the execution of a full copy
        """

        # Build the prerequisites
        tempdir = tmpdir.mkdir("tmp")
        tempfile_mock.return_value = tempdir.strpath
        server = build_real_server(
            global_conf={"barman_home": tmpdir.mkdir("home").strpath})
        config = server.config
        executor = server.backup_manager.executor

        rcc = RsyncCopyController(
            path=server.path,
            ssh_command=executor.ssh_command,
            ssh_options=executor.ssh_options,
            network_compression=config.network_compression,
            reuse_backup=None,
            safe_horizon=None,
        )

        backup_info = build_test_backup_info(
            server=server,
            pgdata="/pg/data",
            config_file="/etc/postgresql.conf",
            hba_file="/pg/data/pg_hba.conf",
            ident_file="/pg/data/pg_ident.conf",
            begin_xlog="0/2000028",
            begin_wal="000000010000000000000002",
            begin_offset=28,
        )
        backup_info.save()
        # This is to check that all the preparation is done correctly
        assert os.path.exists(backup_info.filename)

        # Silence the access to result properties
        rsync_mock.return_value.out = ""
        rsync_mock.return_value.err = ""
        rsync_mock.return_value.ret = 0

        # Mock analyze directory
        def analyse_func(item):
            label = item.label
            item.dir_file = label + "_dir_file"
            item.exclude_and_protect_file = label + "_exclude_and_protect_file"
            item.safe_list = [_FileItem("mode", 1, "date", "path")]
            item.check_list = [_FileItem("mode", 1, "date", "path")]

        analyse_mock.side_effect = analyse_func

        rcc.add_directory(
            label="tbs1",
            src=":/fake/location/",
            dst=backup_info.get_data_directory(16387),
            reuse=None,
            bwlimit=None,
            item_class=rcc.TABLESPACE_CLASS,
        )
        rcc.add_directory(
            label="tbs2",
            src=":/another/location/",
            dst=backup_info.get_data_directory(16405),
            reuse=None,
            bwlimit=None,
            item_class=rcc.TABLESPACE_CLASS,
        )
        rcc.add_directory(
            label="pgdata",
            src=":/pg/data/",
            dst=backup_info.get_data_directory(),
            reuse=None,
            bwlimit=None,
            item_class=rcc.PGDATA_CLASS,
            exclude=[
                "/pg_xlog/*",
                "/pg_log/*",
                "/log/*",
                "/recovery.conf",
                "/postmaster.pid",
            ],
            exclude_and_protect=["pg_tblspc/16387", "pg_tblspc/16405"],
        )
        rcc.add_file(
            label="pg_control",
            src=":/pg/data/global/pg_control",
            dst="%s/global/pg_control" % backup_info.get_data_directory(),
            item_class=rcc.PGCONTROL_CLASS,
        )
        rcc.add_file(
            label="config_file",
            src=":/etc/postgresql.conf",
            dst=backup_info.get_data_directory(),
            item_class=rcc.CONFIG_CLASS,
            optional=False,
        )
        rcc.copy()

        # Check the order of calls to the Rsync mock
        assert rsync_mock.mock_calls == [
            mock.call(
                network_compression=False,
                args=[
                    "--ignore-missing-args",
                    "--itemize-changes",
                    "--itemize-changes",
                ],
                bwlimit=None,
                ssh="ssh",
                path=None,
                ssh_options=[
                    "-c",
                    '"arcfour"',
                    "-p",
                    "22",
                    "*****@*****.**",
                    "-o",
                    "BatchMode=yes",
                    "-o",
                    "StrictHostKeyChecking=no",
                ],
                exclude=None,
                exclude_and_protect=None,
                include=None,
                retry_sleep=0,
                retry_times=0,
                retry_handler=mock.ANY,
            ),
            mock.call(
                network_compression=False,
                args=[
                    "--ignore-missing-args",
                    "--itemize-changes",
                    "--itemize-changes",
                ],
                bwlimit=None,
                ssh="ssh",
                path=None,
                ssh_options=[
                    "-c",
                    '"arcfour"',
                    "-p",
                    "22",
                    "*****@*****.**",
                    "-o",
                    "BatchMode=yes",
                    "-o",
                    "StrictHostKeyChecking=no",
                ],
                exclude=None,
                exclude_and_protect=None,
                include=None,
                retry_sleep=0,
                retry_times=0,
                retry_handler=mock.ANY,
            ),
            mock.call(
                network_compression=False,
                args=[
                    "--ignore-missing-args",
                    "--itemize-changes",
                    "--itemize-changes",
                ],
                bwlimit=None,
                ssh="ssh",
                path=None,
                ssh_options=[
                    "-c",
                    '"arcfour"',
                    "-p",
                    "22",
                    "*****@*****.**",
                    "-o",
                    "BatchMode=yes",
                    "-o",
                    "StrictHostKeyChecking=no",
                ],
                exclude=[
                    "/pg_xlog/*",
                    "/pg_log/*",
                    "/log/*",
                    "/recovery.conf",
                    "/postmaster.pid",
                ],
                exclude_and_protect=["pg_tblspc/16387", "pg_tblspc/16405"],
                include=None,
                retry_sleep=0,
                retry_times=0,
                retry_handler=mock.ANY,
            ),
            mock.call(
                network_compression=False,
                args=[
                    "--ignore-missing-args",
                    "--itemize-changes",
                    "--itemize-changes",
                ],
                bwlimit=None,
                ssh="ssh",
                path=None,
                ssh_options=[
                    "-c",
                    '"arcfour"',
                    "-p",
                    "22",
                    "*****@*****.**",
                    "-o",
                    "BatchMode=yes",
                    "-o",
                    "StrictHostKeyChecking=no",
                ],
                exclude=None,
                exclude_and_protect=None,
                include=None,
                retry_sleep=0,
                retry_times=0,
                retry_handler=mock.ANY,
            ),
            mock.call()(
                ":/etc/postgresql.conf",
                backup_info.get_data_directory(),
                allowed_retval=(0, 23, 24),
            ),
            mock.call(
                network_compression=False,
                args=[
                    "--ignore-missing-args",
                    "--itemize-changes",
                    "--itemize-changes",
                ],
                bwlimit=None,
                ssh="ssh",
                path=None,
                ssh_options=[
                    "-c",
                    '"arcfour"',
                    "-p",
                    "22",
                    "*****@*****.**",
                    "-o",
                    "BatchMode=yes",
                    "-o",
                    "StrictHostKeyChecking=no",
                ],
                exclude=None,
                exclude_and_protect=None,
                include=None,
                retry_sleep=0,
                retry_times=0,
                retry_handler=mock.ANY,
            ),
            mock.call()(
                ":/pg/data/global/pg_control",
                "%s/global/pg_control" % backup_info.get_data_directory(),
                allowed_retval=(0, 23, 24),
            ),
        ]

        # Check calls to _analyse_directory method
        assert analyse_mock.mock_calls == [
            mock.call(item) for item in rcc.item_list if item.is_directory
        ]

        # Check calls to _create_dir_and_purge method
        assert create_and_purge_mock.mock_calls == [
            mock.call(item) for item in rcc.item_list if item.is_directory
        ]

        # Utility function to build the file_list name
        def file_list_name(label, kind):
            return "%s/%s_%s_%s.list" % (tempdir.strpath, label, kind,
                                         os.getpid())

        # Check the order of calls to the copy method
        # All the file_list arguments are None because the analyze part
        # has not really been executed
        assert copy_mock.mock_calls == [
            mock.call(
                mock.ANY,
                ":/fake/location/",
                backup_info.get_data_directory(16387),
                checksum=False,
                file_list=file_list_name("tbs1", "safe"),
            ),
            mock.call(
                mock.ANY,
                ":/fake/location/",
                backup_info.get_data_directory(16387),
                checksum=True,
                file_list=file_list_name("tbs1", "check"),
            ),
            mock.call(
                mock.ANY,
                ":/another/location/",
                backup_info.get_data_directory(16405),
                checksum=False,
                file_list=file_list_name("tbs2", "safe"),
            ),
            mock.call(
                mock.ANY,
                ":/another/location/",
                backup_info.get_data_directory(16405),
                checksum=True,
                file_list=file_list_name("tbs2", "check"),
            ),
            mock.call(
                mock.ANY,
                ":/pg/data/",
                backup_info.get_data_directory(),
                checksum=False,
                file_list=file_list_name("pgdata", "safe"),
            ),
            mock.call(
                mock.ANY,
                ":/pg/data/",
                backup_info.get_data_directory(),
                checksum=True,
                file_list=file_list_name("pgdata", "check"),
            ),
        ]