| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- import os
- import pytest
- from flexmock import flexmock
- from borgmatic.actions import create as module
- def test_run_create_executes_and_calls_hooks_for_configured_repository():
- flexmock(module.logger).answer = lambda message: None
- flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
- flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
- flexmock()
- )
- flexmock(module.borgmatic.borg.create).should_receive('create_archive').once()
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return({})
- flexmock(module.borgmatic.hooks.dispatch).should_receive(
- 'call_hooks_even_if_unconfigured'
- ).and_return({})
- flexmock(module.borgmatic.actions.pattern).should_receive('collect_patterns').and_return(())
- flexmock(module.borgmatic.actions.pattern).should_receive('process_patterns').and_return([])
- flexmock(os.path).should_receive('join').and_return('/run/borgmatic/bootstrap')
- create_arguments = flexmock(
- repository=None,
- progress=flexmock(),
- statistics=flexmock(),
- json=False,
- list_details=flexmock(),
- )
- global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
- list(
- module.run_create(
- config_filename='test.yaml',
- repository={'path': 'repo'},
- config={},
- config_paths=['/tmp/test.yaml'],
- local_borg_version=None,
- create_arguments=create_arguments,
- global_arguments=global_arguments,
- dry_run_label='',
- local_path=None,
- remote_path=None,
- )
- )
- def test_run_create_runs_with_selected_repository():
- flexmock(module.logger).answer = lambda message: None
- flexmock(module.borgmatic.config.validate).should_receive(
- 'repositories_match'
- ).once().and_return(True)
- flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
- flexmock()
- )
- flexmock(module.borgmatic.borg.create).should_receive('create_archive').once()
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return({})
- flexmock(module.borgmatic.hooks.dispatch).should_receive(
- 'call_hooks_even_if_unconfigured'
- ).and_return({})
- flexmock(module.borgmatic.actions.pattern).should_receive('collect_patterns').and_return(())
- flexmock(module.borgmatic.actions.pattern).should_receive('process_patterns').and_return([])
- flexmock(os.path).should_receive('join').and_return('/run/borgmatic/bootstrap')
- create_arguments = flexmock(
- repository=flexmock(),
- progress=flexmock(),
- statistics=flexmock(),
- json=False,
- list_details=flexmock(),
- )
- global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
- list(
- module.run_create(
- config_filename='test.yaml',
- repository={'path': 'repo'},
- config={},
- config_paths=['/tmp/test.yaml'],
- local_borg_version=None,
- create_arguments=create_arguments,
- global_arguments=global_arguments,
- dry_run_label='',
- local_path=None,
- remote_path=None,
- )
- )
- def test_run_create_bails_if_repository_does_not_match():
- flexmock(module.logger).answer = lambda message: None
- flexmock(module.borgmatic.config.validate).should_receive(
- 'repositories_match'
- ).once().and_return(False)
- flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').never()
- flexmock(module.borgmatic.borg.create).should_receive('create_archive').never()
- create_arguments = flexmock(
- repository=flexmock(),
- progress=flexmock(),
- statistics=flexmock(),
- json=False,
- list_details=flexmock(),
- )
- global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
- list(
- module.run_create(
- config_filename='test.yaml',
- repository='repo',
- config={},
- config_paths=['/tmp/test.yaml'],
- local_borg_version=None,
- create_arguments=create_arguments,
- global_arguments=global_arguments,
- dry_run_label='',
- local_path=None,
- remote_path=None,
- )
- )
- def test_run_create_with_both_list_and_json_errors():
- flexmock(module.logger).answer = lambda message: None
- flexmock(module.borgmatic.config.validate).should_receive(
- 'repositories_match'
- ).once().and_return(True)
- flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').never()
- flexmock(module.borgmatic.borg.create).should_receive('create_archive').never()
- create_arguments = flexmock(
- repository=flexmock(),
- progress=flexmock(),
- statistics=flexmock(),
- json=True,
- list_details=flexmock(),
- )
- global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
- with pytest.raises(ValueError):
- list(
- module.run_create(
- config_filename='test.yaml',
- repository={'path': 'repo'},
- config={'list_details': True},
- config_paths=['/tmp/test.yaml'],
- local_borg_version=None,
- create_arguments=create_arguments,
- global_arguments=global_arguments,
- dry_run_label='',
- local_path=None,
- remote_path=None,
- )
- )
- def test_run_create_with_both_list_and_progress_errors():
- flexmock(module.logger).answer = lambda message: None
- flexmock(module.borgmatic.config.validate).should_receive(
- 'repositories_match'
- ).once().and_return(True)
- flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').never()
- flexmock(module.borgmatic.borg.create).should_receive('create_archive').never()
- create_arguments = flexmock(
- repository=flexmock(),
- progress=flexmock(),
- statistics=flexmock(),
- json=False,
- list_details=flexmock(),
- )
- global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
- with pytest.raises(ValueError):
- list(
- module.run_create(
- config_filename='test.yaml',
- repository={'path': 'repo'},
- config={'list_details': True, 'progress': True},
- config_paths=['/tmp/test.yaml'],
- local_borg_version=None,
- create_arguments=create_arguments,
- global_arguments=global_arguments,
- dry_run_label='',
- local_path=None,
- remote_path=None,
- )
- )
- def test_run_create_produces_json():
- flexmock(module.logger).answer = lambda message: None
- flexmock(module.borgmatic.config.validate).should_receive(
- 'repositories_match'
- ).once().and_return(True)
- flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
- flexmock()
- )
- flexmock(module.borgmatic.borg.create).should_receive('create_archive').once().and_return(
- flexmock()
- )
- parsed_json = flexmock()
- flexmock(module.borgmatic.actions.json).should_receive('parse_json').and_return(parsed_json)
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return({})
- flexmock(module.borgmatic.hooks.dispatch).should_receive(
- 'call_hooks_even_if_unconfigured'
- ).and_return({})
- flexmock(module.borgmatic.actions.pattern).should_receive('collect_patterns').and_return(())
- flexmock(module.borgmatic.actions.pattern).should_receive('process_patterns').and_return([])
- flexmock(os.path).should_receive('join').and_return('/run/borgmatic/bootstrap')
- create_arguments = flexmock(
- repository=flexmock(),
- progress=flexmock(),
- statistics=flexmock(),
- json=True,
- list_details=flexmock(),
- )
- global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
- assert list(
- module.run_create(
- config_filename='test.yaml',
- repository={'path': 'repo'},
- config={},
- config_paths=['/tmp/test.yaml'],
- local_borg_version=None,
- create_arguments=create_arguments,
- global_arguments=global_arguments,
- dry_run_label='',
- local_path=None,
- remote_path=None,
- )
- ) == [parsed_json]
|