本文整理汇总了Python中testing_helpers.build_test_backup_info函数的典型用法代码示例。如果您正苦于以下问题:Python build_test_backup_info函数的具体用法?Python build_test_backup_info怎么用?Python build_test_backup_info使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了build_test_backup_info函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_delete_running_backup
def test_delete_running_backup(self, delete_mock, get_first_backup_mock, tmpdir, capsys):
"""
Simple test for the deletion of a running backup.
We want to test the behaviour of the server.delete_backup method
when invoked on a running backup
"""
# Test the removal of a running backup. status STARTED
server = build_real_server({"barman_home": tmpdir.strpath})
backup_info_started = build_test_backup_info(status=BackupInfo.STARTED, server_name=server.config.name)
get_first_backup_mock.return_value = backup_info_started.backup_id
with ServerBackupLock(tmpdir.strpath, server.config.name):
server.delete_backup(backup_info_started)
out, err = capsys.readouterr()
assert "Cannot delete a running backup (%s %s)" % (server.config.name, backup_info_started.backup_id) in err
# Test the removal of a running backup. status EMPTY
backup_info_empty = build_test_backup_info(status=BackupInfo.EMPTY, server_name=server.config.name)
get_first_backup_mock.return_value = backup_info_empty.backup_id
with ServerBackupLock(tmpdir.strpath, server.config.name):
server.delete_backup(backup_info_empty)
out, err = capsys.readouterr()
assert "Cannot delete a running backup (%s %s)" % (server.config.name, backup_info_started.backup_id) in err
# Test the removal of a running backup. status DONE
backup_info_done = build_test_backup_info(status=BackupInfo.DONE, server_name=server.config.name)
with ServerBackupLock(tmpdir.strpath, server.config.name):
server.delete_backup(backup_info_done)
delete_mock.assert_called_with(backup_info_done)
# Test the removal of a backup not running. status STARTED
server.delete_backup(backup_info_started)
delete_mock.assert_called_with(backup_info_started)
示例2: test_backup_copy_with_included_files
def test_backup_copy_with_included_files(self, rsync_moc, tmpdir, capsys):
backup_manager = build_backup_manager(global_conf={
'barman_home': tmpdir.mkdir('home').strpath
})
# Create a backup info with additional configuration files
backup_info = build_test_backup_info(
server=backup_manager.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",
included_files=["/tmp/config/file.conf"],
begin_offset=28)
backup_info.save()
# This is to check that all the preparation is done correctly
assert os.path.exists(backup_info.filename)
# Execute a backup
backup_manager.executor.backup_copy(backup_info)
out, err = capsys.readouterr()
# check for the presence of the warning in the stderr
assert "WARNING: The usage of include directives is not supported" in err
# check that the additional configuration file is present in the output
assert backup_info.included_files[0] in err
示例3: test_get_wal_info
def test_get_wal_info(self, get_wal_mock, tmpdir):
"""
Basic test for get_wal_info method
Test the wals per second and total time in seconds values.
:return:
"""
# Build a test server with a test path
server = build_real_server(global_conf={
'barman_home': tmpdir.strpath
})
# Mock method get_wal_until_next_backup for returning a list of
# 3 fake WAL. the first one is the start and stop WAL of the backup
wal_list = [
WalFileInfo.from_xlogdb_line(
"000000010000000000000002\t16777216\t1434450086.53\tNone\n"),
WalFileInfo.from_xlogdb_line(
"000000010000000000000003\t16777216\t1434450087.54\tNone\n"),
WalFileInfo.from_xlogdb_line(
"000000010000000000000004\t16777216\t1434450088.55\tNone\n")]
get_wal_mock.return_value = wal_list
backup_info = build_test_backup_info(
server=server,
begin_wal=wal_list[0].name,
end_wal=wal_list[0].name)
backup_info.save()
# Evaluate total time in seconds:
# last_wal_timestamp - first_wal_timestamp
wal_total_seconds = wal_list[-1].time - wal_list[0].time
# Evaluate the wals_per_second value:
# wals_in_backup + wals_until_next_backup / total_time_in_seconds
wals_per_second = len(wal_list) / wal_total_seconds
wal_info = server.get_wal_info(backup_info)
assert wal_info
assert wal_info['wal_total_seconds'] == wal_total_seconds
assert wal_info['wals_per_second'] == wals_per_second
示例4: test_backup_cache_add
def test_backup_cache_add(self, tmpdir):
"""
Check the method responsible for the registration of a BackupInfo obj
into the backups cache
"""
# build a backup_manager and setup a basic configuration
backup_manager = build_backup_manager(
name='TestServer',
global_conf={
'barman_home': tmpdir.strpath
})
# Create a BackupInfo object with status DONE
b_info = build_test_backup_info(
backup_id='fake_backup_id',
server=backup_manager.server,
)
b_info.save()
assert backup_manager._backup_cache is None
# Register the object to cache. The cache is not initialized, so it
# must load the cache from disk.
backup_manager.backup_cache_add(b_info)
# Check that the test backup is in the cache
assert backup_manager.get_backup(b_info.backup_id) is b_info
# Initialize an empty cache
backup_manager._backup_cache = {}
# Add the backup again
backup_manager.backup_cache_add(b_info)
assert backup_manager.get_backup(b_info.backup_id) is b_info
示例5: test_backup_copy
def test_backup_copy(self, rsync_mock, tmpdir):
"""
Test the execution of a rsync copy
:param rsync_mock: mock for the rsync command
:param tmpdir: temporary dir
"""
backup_manager = build_backup_manager(global_conf={
'barman_home': tmpdir.mkdir('home').strpath
})
backup_info = build_test_backup_info(
server=backup_manager.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)
backup_manager.executor.backup_copy(backup_info)
assert rsync_mock.mock_calls == [
mock.call(check=True, network_compression=False, args=[],
bwlimit=None, ssh='ssh',
ssh_options=['-c', '"arcfour"', '-p', '22',
'[email protected]', '-o',
'BatchMode=yes', '-o',
'StrictHostKeyChecking=no']),
mock.call().smart_copy(':/fake/location/',
backup_info.get_data_directory(16387),
None, None),
mock.call(check=True, network_compression=False, args=[],
bwlimit=None, ssh='ssh',
ssh_options=['-c', '"arcfour"', '-p', '22',
'[email protected]', '-o',
'BatchMode=yes', '-o',
'StrictHostKeyChecking=no']),
mock.call().smart_copy(':/another/location/',
backup_info.get_data_directory(16405),
None, None),
mock.call(network_compression=False,
exclude_and_protect=['/pg_tblspc/16387',
'/pg_tblspc/16405'],
args=[], bwlimit=None, ssh='ssh',
ssh_options=['-c', '"arcfour"', '-p', '22',
'[email protected]',
'-o', 'BatchMode=yes',
'-o', 'StrictHostKeyChecking=no']),
mock.call().smart_copy(':/pg/data/',
backup_info.get_data_directory(),
None, None),
mock.call()(
':/pg/data/global/pg_control',
'%s/global/pg_control' % backup_info.get_data_directory()),
mock.call()(':/etc/postgresql.conf',
backup_info.get_data_directory())]
示例6: test_concurrent_stop_backup
def test_concurrent_stop_backup(self, label_mock, stop_mock,):
"""
Basic test for the start_backup method
:param label_mock: mimic the response of _write_backup_label
:param stop_mock: mimic the response of _pgespresso_stop_backup
"""
# Build a backup info and configure the mocks
server = build_mocked_server(main_conf={
'backup_options':
BackupOptions.CONCURRENT_BACKUP
})
backup_manager = build_backup_manager(server=server)
# Mock executor._pgespresso_stop_backup(backup_info) call
stop_time = datetime.datetime.now()
stop_mock.return_value = ("000000060000A25700000044", stop_time)
backup_info = build_test_backup_info()
backup_manager.executor.strategy.stop_backup(backup_info)
assert backup_info.end_xlog == 'A257/45000000'
assert backup_info.end_wal == '000000060000A25700000044'
assert backup_info.end_offset == 0
assert backup_info.end_time == stop_time
示例7: test_get_backup
def test_get_backup(self, tmpdir):
"""
Check the get_backup method that uses the backups cache to retrieve
a backup using the id
"""
# Setup temp dir and server
# build a backup_manager and setup a basic configuration
backup_manager = build_backup_manager(
name='TestServer',
global_conf={
'barman_home': tmpdir.strpath
})
# Create a BackupInfo object with status DONE
b_info = build_test_backup_info(
backup_id='fake_backup_id',
server=backup_manager.server,
)
b_info.save()
assert backup_manager._backup_cache is None
# Check that the backup returned is the same
assert backup_manager.get_backup(b_info.backup_id).to_dict() == \
b_info.to_dict()
# Empty the backup manager cache
backup_manager._backup_cache = {}
# Check that the backup returned is None
assert backup_manager.get_backup(b_info.backup_id) is None
示例8: test_backup_cache_remove
def test_backup_cache_remove(self, tmpdir):
"""
Check the method responsible for the removal of a BackupInfo object from
the backups cache
"""
# build a backup_manager and setup a basic configuration
backup_manager = build_backup_manager(
name='TestServer',
global_conf={
'barman_home': tmpdir.strpath
})
assert backup_manager._backup_cache is None
# Create a BackupInfo object with status DONE
b_info = build_test_backup_info(
backup_id='fake_backup_id',
server=backup_manager.server,
)
# Remove the backup from the uninitialized cache
backup_manager.backup_cache_remove(b_info)
# Check that the test backup is still not initialized
assert backup_manager._backup_cache is None
# Initialize the cache
backup_manager._backup_cache = {b_info.backup_id: b_info}
# Remove the backup from the cache
backup_manager.backup_cache_remove(b_info)
assert b_info.backup_id not in backup_manager._backup_cache
示例9: test_recover_basebackup_copy
def test_recover_basebackup_copy(self, rsync_pg_mock, tmpdir):
"""
Test the copy of a content of a backup during a recovery
:param rsync_pg_mock: Mock rsync object for the purpose if this test
"""
# Build basic folder/files structure
dest = tmpdir.mkdir('destination')
server = testing_helpers.build_real_server()
backup_info = testing_helpers.build_test_backup_info(
server=server,
tablespaces=[('tbs1', 16387, '/fake/location')])
# Build a executor
executor = RecoveryExecutor(server.backup_manager)
executor.config.tablespace_bandwidth_limit = {'tbs1': ''}
executor.config.bandwidth_limit = 10
executor.basebackup_copy(
backup_info, dest.strpath, tablespaces=None)
rsync_pg_mock.assert_called_with(
network_compression=False, bwlimit=10, ssh=None, path=None,
exclude_and_protect=['/pg_tblspc/16387'])
rsync_pg_mock.assert_any_call(
network_compression=False, bwlimit='', ssh=None, path=None,
check=True)
rsync_pg_mock.return_value.smart_copy.assert_any_call(
'/some/barman/home/main/base/1234567890/16387/',
'/fake/location', None)
rsync_pg_mock.return_value.smart_copy.assert_called_with(
'/some/barman/home/main/base/1234567890/data/',
dest.strpath, None)
示例10: test_map_temporary_config_files
def test_map_temporary_config_files(self, tmpdir):
"""
Test the method that prepares configuration files
for the final steps of a recovery
"""
# Build directory/files structure for testing
tempdir = tmpdir.mkdir("tempdir")
recovery_info = {
"configuration_files": ["postgresql.conf", "postgresql.auto.conf"],
"tempdir": tempdir.strpath,
"temporary_configuration_files": [],
"results": {"changes": [], "warnings": [], "missing_files": []},
}
backup_info = testing_helpers.build_test_backup_info()
backup_info.config.basebackups_directory = tmpdir.strpath
datadir = tmpdir.mkdir(backup_info.backup_id).mkdir("data")
postgresql_conf_local = datadir.join("postgresql.conf")
postgresql_auto_local = datadir.join("postgresql.auto.conf")
postgresql_conf_local.write("archive_command = something\n" "data_directory = something")
postgresql_auto_local.write("archive_command = something\n" "data_directory = something")
# Build a RecoveryExecutor object (using a mock as server and backup
# manager.
backup_manager = testing_helpers.build_backup_manager()
executor = RecoveryExecutor(backup_manager)
executor._map_temporary_config_files(recovery_info, backup_info, "[email protected]")
# check that configuration files have been moved by the method
assert tempdir.join("postgresql.conf").check()
assert tempdir.join("postgresql.conf").computehash() == postgresql_conf_local.computehash()
assert tempdir.join("postgresql.auto.conf").check()
assert tempdir.join("postgresql.auto.conf").computehash() == postgresql_auto_local.computehash()
assert recovery_info["results"]["missing_files"] == ["pg_hba.conf", "pg_ident.conf"]
示例11: test_setup
def test_setup(self, rsync_mock):
"""
Test the method that set up a recovery
"""
backup_info = testing_helpers.build_test_backup_info()
backup_manager = testing_helpers.build_backup_manager()
executor = RecoveryExecutor(backup_manager)
backup_info.version = 90300
# setup should create a temporary directory
# and teardown should delete it
ret = executor._setup(backup_info, None, "/tmp")
assert os.path.exists(ret["tempdir"])
executor._teardown(ret)
assert not os.path.exists(ret["tempdir"])
# no postgresql.auto.conf on version 9.3
ret = executor._setup(backup_info, None, "/tmp")
executor._teardown(ret)
assert "postgresql.auto.conf" not in ret["configuration_files"]
# Check the present for postgresql.auto.conf on version 9.4
backup_info.version = 90400
ret = executor._setup(backup_info, None, "/tmp")
executor._teardown(ret)
assert "postgresql.auto.conf" in ret["configuration_files"]
# Receive a error if the remote command is invalid
with pytest.raises(SystemExit):
executor.server.path = None
executor._setup(backup_info, "invalid", "/tmp")
示例12: test_set_pitr_targets
def test_set_pitr_targets(self, tmpdir):
"""
Evaluate targets for point in time recovery
"""
# Build basic folder/files structure
tempdir = tmpdir.mkdir("temp_dir")
dest = tmpdir.mkdir("dest")
wal_dest = tmpdir.mkdir("wal_dest")
recovery_info = {
"configuration_files": ["postgresql.conf", "postgresql.auto.conf"],
"tempdir": tempdir.strpath,
"results": {"changes": [], "warnings": []},
"is_pitr": False,
"wal_dest": wal_dest.strpath,
"get_wal": False,
}
backup_info = testing_helpers.build_test_backup_info()
backup_manager = testing_helpers.build_backup_manager()
# Build a recovery executor
executor = RecoveryExecutor(backup_manager)
executor._set_pitr_targets(recovery_info, backup_info, dest.strpath, "", "", "", "")
# Test with empty values (no PITR)
assert recovery_info["target_epoch"] is None
assert recovery_info["target_datetime"] is None
assert recovery_info["wal_dest"] == wal_dest.strpath
# Test for PITR targets
executor._set_pitr_targets(
recovery_info, backup_info, dest.strpath, "target_name", "2015-06-03 16:11:03.71038+02", "2", None
)
target_datetime = dateutil.parser.parse("2015-06-03 16:11:03.710380+02:00")
target_epoch = time.mktime(target_datetime.timetuple()) + (target_datetime.microsecond / 1000000.0)
assert recovery_info["target_datetime"] == target_datetime
assert recovery_info["target_epoch"] == target_epoch
assert recovery_info["wal_dest"] == dest.join("barman_xlog").strpath
示例13: test_prepare_tablespaces
def test_prepare_tablespaces(self, tmpdir):
"""
Test tablespaces preparation for recovery
"""
# Prepare basic directory/files structure
dest = tmpdir.mkdir('destination')
wals = tmpdir.mkdir('wals')
backup_info = testing_helpers.build_test_backup_info(
tablespaces=[('tbs1', 16387, '/fake/location')])
# build an executor
server = testing_helpers.build_real_server(
main_conf={'wals_directory': wals.strpath})
executor = RecoveryExecutor(server.backup_manager)
# use a mock as cmd obj
cmd_mock = Mock()
executor.prepare_tablespaces(backup_info, cmd_mock, dest.strpath, {})
cmd_mock.create_dir_if_not_exists.assert_any_call(
dest.join('pg_tblspc').strpath)
cmd_mock.create_dir_if_not_exists.assert_any_call(
'/fake/location')
cmd_mock.delete_if_exists.assert_called_once_with(
dest.join('pg_tblspc').join('16387').strpath)
cmd_mock.create_symbolic_link.assert_called_once_with(
'/fake/location',
dest.join('pg_tblspc').join('16387').strpath)
示例14: test_pgespresso_stop_backup
def test_pgespresso_stop_backup(self, tbs_map_mock, label_mock):
"""
Basic test for the pgespresso_stop_backup method
"""
# Build a backup info and configure the mocks
server = build_mocked_server(main_conf={
'backup_options':
BackupOptions.CONCURRENT_BACKUP
})
backup_manager = build_backup_manager(server=server)
# Mock executor._pgespresso_stop_backup(backup_info) call
stop_time = datetime.datetime.now()
server.postgres.server_version = 90500
server.postgres.pgespresso_stop_backup.return_value = {
'end_wal': "000000060000A25700000044",
'timestamp': stop_time
}
backup_info = build_test_backup_info(timeline=6)
backup_manager.executor.strategy.stop_backup(backup_info)
assert backup_info.end_xlog == 'A257/44FFFFFF'
assert backup_info.end_wal == '000000060000A25700000044'
assert backup_info.end_offset == 0xFFFFFF
assert backup_info.end_time == stop_time
示例15: test_load_backup_cache
def test_load_backup_cache(self, tmpdir):
"""
Check the loading of backups inside the backup_cache
"""
# build a backup_manager and setup a basic configuration
backup_manager = build_backup_manager(
name='TestServer',
global_conf={
'barman_home': tmpdir.strpath
})
# Make sure the cache is uninitialized
assert backup_manager._backup_cache is None
# Create a BackupInfo object with status DONE
b_info = build_test_backup_info(
backup_id='fake_backup_id',
server=backup_manager.server,
)
b_info.save()
# Load backups inside the cache
backup_manager._load_backup_cache()
# Check that the test backup is inside the backups_cache
assert backup_manager._backup_cache[b_info.backup_id].to_dict() == \
b_info.to_dict()