Ejemplo n.º 1
0
    def test_identify_compression(self, tmpdir):
        bz2_tmp_file = tmpdir.join("test_file")
        # "test" in bz2 compression
        bz2_tmp_file.write(base64.b64decode(
            b"QlpoOTFBWSZTWczDcdQAAAJBgAAQAgAMACAAIZpoM00Zl4u5IpwoSGZhuOoA"),
                           mode='wb')

        compression_bz2 = identify_compression(bz2_tmp_file.strpath)
        assert compression_bz2 == "bzip2"

        zip_tmp_file = tmpdir.join("test_file")
        # "test" in bz2 compression
        zip_tmp_file.write(
            base64.b64decode(b"H4sIAF0ssFIAAytJLS7hAgDGNbk7BQAAAA=="),
            mode='wb')

        # check custom compression method creation
        compression_zip = identify_compression(zip_tmp_file.strpath)
        assert compression_zip == "gzip"
Ejemplo n.º 2
0
    def test_identify_compression(self, tmpdir):
        bz2_tmp_file = tmpdir.join("test_file")
        # "test" in bz2 compression
        bz2_tmp_file.write(base64.b64decode(
            b"QlpoOTFBWSZTWczDcdQAAAJBgAAQAgAMACAAIZpoM00Zl4u5IpwoSGZhuOoA"),
            mode='wb')

        compression_bz2 = identify_compression(bz2_tmp_file.strpath)
        assert compression_bz2 == "bzip2"

        zip_tmp_file = tmpdir.join("test_file")
        # "test" in bz2 compression
        zip_tmp_file.write(base64.b64decode(
            b"H4sIAF0ssFIAAytJLS7hAgDGNbk7BQAAAA=="),
            mode='wb')

        # check custom compression method creation
        compression_zip = identify_compression(zip_tmp_file.strpath)
        assert compression_zip == "gzip"
Ejemplo n.º 3
0
    def test_bzip2(self, tmpdir):

        config_mock = mock.Mock()

        compressor = PyBZip2Compressor(config=config_mock, compression='pybzip2')

        src = tmpdir.join('sourcefile')
        src.write('content')

        compressor.compress(src.strpath, '%s/bzipfile.bz2' % tmpdir.strpath)
        assert os.path.exists('%s/bzipfile.bz2' % tmpdir.strpath)
        compression_zip = identify_compression('%s/bzipfile.bz2' % tmpdir.strpath)
        assert compression_zip == "bzip2"

        compressor.decompress('%s/bzipfile.bz2' % tmpdir.strpath,
                              '%s/bzipfile.uncompressed' % tmpdir.strpath)

        f = open('%s/bzipfile.uncompressed' % tmpdir.strpath).read()
        assert f == 'content'
Ejemplo n.º 4
0
    def test_gzip(self, tmpdir):

        config_mock = mock.Mock()

        compressor = GZipCompressor(config=config_mock, compression='gzip')

        src = tmpdir.join('sourcefile')
        src.write('content')

        compressor.compress(src.strpath, '%s/zipfile.zip' % tmpdir.strpath)
        assert os.path.exists('%s/zipfile.zip' % tmpdir.strpath)
        compression_zip = identify_compression('%s/zipfile.zip' % tmpdir.strpath)
        assert compression_zip == "gzip"

        compressor.decompress('%s/zipfile.zip' % tmpdir.strpath,
                              '%s/zipfile.uncompressed' % tmpdir.strpath)

        f = open('%s/zipfile.uncompressed' % tmpdir.strpath).read()
        assert f == 'content'
Ejemplo n.º 5
0
    def test_gzip(self, tmpdir):

        config_mock = mock.Mock()

        compressor = PyGZipCompressor(config=config_mock, compression='pygzip')

        src = tmpdir.join('sourcefile')
        src.write('content')

        compressor.compress(src.strpath, '%s/zipfile.zip' % tmpdir.strpath)
        assert os.path.exists('%s/zipfile.zip' % tmpdir.strpath)
        compression_zip = identify_compression('%s/zipfile.zip' %
                                               tmpdir.strpath)
        assert compression_zip == "gzip"

        compressor.decompress('%s/zipfile.zip' % tmpdir.strpath,
                              '%s/zipfile.uncompressed' % tmpdir.strpath)

        f = open('%s/zipfile.uncompressed' % tmpdir.strpath).read()
        assert f == 'content'
Ejemplo n.º 6
0
    def test_bzip2(self, tmpdir):

        config_mock = mock.Mock()

        compressor = BZip2Compressor(config=config_mock, compression='bzip2')

        src = tmpdir.join('sourcefile')
        src.write('content')

        compressor.compress(src.strpath, '%s/bzipfile.bz2' % tmpdir.strpath)
        assert os.path.exists('%s/bzipfile.bz2' % tmpdir.strpath)
        compression_zip = identify_compression('%s/bzipfile.bz2' %
                                               tmpdir.strpath)
        assert compression_zip == "bzip2"

        compressor.decompress('%s/bzipfile.bz2' % tmpdir.strpath,
                              '%s/bzipfile.uncompressed' % tmpdir.strpath)

        f = open('%s/bzipfile.uncompressed' % tmpdir.strpath).read()
        assert f == 'content'
Ejemplo n.º 7
0
    def test_gzip(self, tmpdir):

        config_mock = mock.Mock()

        compressor = PyGZipCompressor(config=config_mock, compression="pygzip")

        src = tmpdir.join("sourcefile")
        src.write("content")

        compressor.compress(src.strpath, "%s/zipfile.zip" % tmpdir.strpath)
        assert os.path.exists("%s/zipfile.zip" % tmpdir.strpath)
        compression_zip = identify_compression("%s/zipfile.zip" %
                                               tmpdir.strpath)
        assert compression_zip == "gzip"

        compressor.decompress(
            "%s/zipfile.zip" % tmpdir.strpath,
            "%s/zipfile.uncompressed" % tmpdir.strpath,
        )

        f = open("%s/zipfile.uncompressed" % tmpdir.strpath).read()
        assert f == "content"
Ejemplo n.º 8
0
    def from_file(cls, filename, default_compression=None, **kwargs):
        """
        Factory method to generate a WalFileInfo from a WAL file.

        Every keyword argument will override any attribute from the provided
        file. If a keyword argument doesn't has a corresponding attribute
        an AttributeError exception is raised.

        :param str filename: the file to inspect
        :param str default_compression: the compression to set if
            the current schema is not identifiable.
        """
        stat = os.stat(filename)
        kwargs.setdefault('name', os.path.basename(filename))
        kwargs.setdefault('size', stat.st_size)
        kwargs.setdefault('time', stat.st_mtime)
        if 'compression' not in kwargs:
            kwargs['compression'] = identify_compression(filename) \
                or default_compression
        obj = cls(**kwargs)
        obj.filename = "%s.meta" % filename
        return obj
Ejemplo n.º 9
0
    def from_file(cls, filename, unidentified_compression=None, **kwargs):
        """
        Factory method to generate a WalFileInfo from a WAL file.

        Every keyword argument will override any attribute from the provided
        file. If a keyword argument doesn't has a corresponding attribute
        an AttributeError exception is raised.

        :param str filename: the file to inspect
        :param str unidentified_compression: the compression to set if
            the current schema is not identifiable.
        """
        stat = os.stat(filename)
        kwargs.setdefault('name', os.path.basename(filename))
        kwargs.setdefault('size', stat.st_size)
        kwargs.setdefault('time', stat.st_mtime)
        if 'compression' not in kwargs:
            kwargs['compression'] = identify_compression(filename) \
                or unidentified_compression
        obj = cls(**kwargs)
        obj.filename = "%s.meta" % filename
        obj.orig_filename = filename
        return obj
Ejemplo n.º 10
0
    def test_archive_wal(self, tmpdir, capsys):
        """
        Test WalArchiver.archive_wal behaviour when the WAL file already
        exists in the archive
        """

        # Setup the test environment
        backup_manager = build_backup_manager(
            name="TestServer", global_conf={"barman_home": tmpdir.strpath}
        )
        backup_manager.compression_manager.get_compressor.return_value = None
        backup_manager.server.get_backup.return_value = None

        basedir = tmpdir.join("main")
        incoming_dir = basedir.join("incoming")
        archive_dir = basedir.join("wals")
        xlog_db = archive_dir.join("xlog.db")
        wal_name = "000000010000000000000001"
        wal_file = incoming_dir.join(wal_name)
        wal_file.ensure()
        archive_dir.ensure(dir=True)
        xlog_db.ensure()
        backup_manager.server.xlogdb.return_value.__enter__.return_value = xlog_db.open(
            mode="a"
        )
        archiver = FileWalArchiver(backup_manager)
        backup_manager.server.archivers = [archiver]

        # Tests a basic archival process
        wal_info = WalFileInfo.from_file(wal_file.strpath)
        archiver.archive_wal(None, wal_info)

        assert not os.path.exists(wal_file.strpath)
        assert os.path.exists(wal_info.fullpath(backup_manager.server))

        # Tests the archiver behaviour for duplicate WAL files, as the
        # wal file named '000000010000000000000001' was already archived
        # in the previous test
        wal_file.ensure()
        wal_info = WalFileInfo.from_file(wal_file.strpath)

        with pytest.raises(MatchingDuplicateWalFile):
            archiver.archive_wal(None, wal_info)

        # Tests the archiver behaviour for duplicated WAL files with
        # different contents
        wal_file.write("test")
        wal_info = WalFileInfo.from_file(wal_file.strpath)

        with pytest.raises(DuplicateWalFile):
            archiver.archive_wal(None, wal_info)

        # Tests the archiver behaviour for duplicate WAL files, as the
        # wal file named '000000010000000000000001' was already archived
        # in the previous test and the input file uses compression
        compressor = PyGZipCompressor(backup_manager.config, "pygzip")
        compressor.compress(wal_file.strpath, wal_file.strpath)
        wal_info = WalFileInfo.from_file(wal_file.strpath)
        assert os.path.exists(wal_file.strpath)
        backup_manager.compression_manager.get_compressor.return_value = compressor

        with pytest.raises(MatchingDuplicateWalFile):
            archiver.archive_wal(None, wal_info)

        # Test the archiver behaviour when the incoming file is compressed
        # and it has been already archived and compressed.
        compressor.compress(
            wal_info.fullpath(backup_manager.server),
            wal_info.fullpath(backup_manager.server),
        )

        wal_info = WalFileInfo.from_file(wal_file.strpath)
        with pytest.raises(MatchingDuplicateWalFile):
            archiver.archive_wal(None, wal_info)

        # Reset the status of the incoming and WALs directory
        # removing the files archived during the preceding tests.
        os.unlink(wal_info.fullpath(backup_manager.server))
        os.unlink(wal_file.strpath)

        # Test the archival of a WAL file using compression.
        wal_file.write("test")
        wal_info = WalFileInfo.from_file(wal_file.strpath)
        archiver.archive_wal(compressor, wal_info)
        assert os.path.exists(wal_info.fullpath(backup_manager.server))
        assert not os.path.exists(wal_file.strpath)
        assert "gzip" == identify_compression(wal_info.fullpath(backup_manager.server))
Ejemplo n.º 11
0
    def test_archive_wal(self, tmpdir, capsys):
        """
        Test WalArchiver.archive_wal behaviour when the WAL file already
        exists in the archive
        """

        # Setup the test environment
        backup_manager = build_backup_manager(
            name='TestServer',
            global_conf={
                'barman_home': tmpdir.strpath
            })
        backup_manager.compression_manager.get_compressor.return_value = None
        backup_manager.server.get_backup.return_value = None

        basedir = tmpdir.join('main')
        incoming_dir = basedir.join('incoming')
        archive_dir = basedir.join('wals')
        xlog_db = archive_dir.join('xlog.db')
        wal_name = '000000010000000000000001'
        wal_file = incoming_dir.join(wal_name)
        wal_file.ensure()
        archive_dir.ensure(dir=True)
        xlog_db.ensure()
        backup_manager.server.xlogdb.return_value.__enter__.return_value = (
            xlog_db.open(mode='a'))
        archiver = FileWalArchiver(backup_manager)
        backup_manager.server.archivers = [archiver]

        # Tests a basic archival process
        wal_info = WalFileInfo.from_file(wal_file.strpath)
        archiver.archive_wal(None, wal_info)

        assert not os.path.exists(wal_file.strpath)
        assert os.path.exists(wal_info.fullpath(backup_manager.server))

        # Tests the archiver behaviour for duplicate WAL files, as the
        # wal file named '000000010000000000000001' was already archived
        # in the previous test
        wal_file.ensure()
        wal_info = WalFileInfo.from_file(wal_file.strpath)

        with pytest.raises(MatchingDuplicateWalFile):
            archiver.archive_wal(None, wal_info)

        # Tests the archiver behaviour for duplicated WAL files with
        # different contents
        wal_file.write('test')
        wal_info = WalFileInfo.from_file(wal_file.strpath)

        with pytest.raises(DuplicateWalFile):
            archiver.archive_wal(None, wal_info)

        # Tests the archiver behaviour for duplicate WAL files, as the
        # wal file named '000000010000000000000001' was already archived
        # in the previous test and the input file uses compression
        compressor = PyGZipCompressor(backup_manager.config, 'pygzip')
        compressor.compress(wal_file.strpath, wal_file.strpath)
        wal_info = WalFileInfo.from_file(wal_file.strpath)
        assert os.path.exists(wal_file.strpath)
        backup_manager.compression_manager.get_compressor.return_value = (
            compressor)

        with pytest.raises(MatchingDuplicateWalFile):
            archiver.archive_wal(None, wal_info)

        # Test the archiver behaviour when the incoming file is compressed
        # and it has been already archived and compressed.
        compressor.compress(wal_info.fullpath(backup_manager.server),
                            wal_info.fullpath(backup_manager.server))

        wal_info = WalFileInfo.from_file(wal_file.strpath)
        with pytest.raises(MatchingDuplicateWalFile):
            archiver.archive_wal(None, wal_info)

        # Reset the status of the incoming and WALs directory
        # removing the files archived during the preceding tests.
        os.unlink(wal_info.fullpath(backup_manager.server))
        os.unlink(wal_file.strpath)

        # Test the archival of a WAL file using compression.
        wal_file.write('test')
        wal_info = WalFileInfo.from_file(wal_file.strpath)
        archiver.archive_wal(compressor, wal_info)
        assert os.path.exists(wal_info.fullpath(backup_manager.server))
        assert not os.path.exists(wal_file.strpath)
        assert 'gzip' == identify_compression(
            wal_info.fullpath(backup_manager.server)
        )