123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512 |
- import pytest
- from flexmock import flexmock
- from borgmatic.actions import check as module
- def test_parse_checks_returns_them_as_tuple():
- checks = module.parse_checks({'checks': [{'name': 'foo'}, {'name': 'bar'}]})
- assert checks == ('foo', 'bar')
- def test_parse_checks_with_missing_value_returns_defaults():
- checks = module.parse_checks({})
- assert checks == ('repository', 'archives')
- def test_parse_checks_with_empty_list_returns_defaults():
- checks = module.parse_checks({'checks': []})
- assert checks == ('repository', 'archives')
- def test_parse_checks_with_none_value_returns_defaults():
- checks = module.parse_checks({'checks': None})
- assert checks == ('repository', 'archives')
- def test_parse_checks_with_disabled_returns_no_checks():
- checks = module.parse_checks({'checks': [{'name': 'foo'}, {'name': 'disabled'}]})
- assert checks == ()
- def test_parse_checks_prefers_override_checks_to_configured_checks():
- checks = module.parse_checks(
- {'checks': [{'name': 'archives'}]}, only_checks=['repository', 'extract']
- )
- assert checks == ('repository', 'extract')
- @pytest.mark.parametrize(
- 'frequency,expected_result',
- (
- (None, None),
- ('always', None),
- ('1 hour', module.datetime.timedelta(hours=1)),
- ('2 hours', module.datetime.timedelta(hours=2)),
- ('1 day', module.datetime.timedelta(days=1)),
- ('2 days', module.datetime.timedelta(days=2)),
- ('1 week', module.datetime.timedelta(weeks=1)),
- ('2 weeks', module.datetime.timedelta(weeks=2)),
- ('1 month', module.datetime.timedelta(days=30)),
- ('2 months', module.datetime.timedelta(days=60)),
- ('1 year', module.datetime.timedelta(days=365)),
- ('2 years', module.datetime.timedelta(days=365 * 2)),
- ),
- )
- def test_parse_frequency_parses_into_timedeltas(frequency, expected_result):
- assert module.parse_frequency(frequency) == expected_result
- @pytest.mark.parametrize(
- 'frequency',
- (
- 'sometime',
- 'x days',
- '3 decades',
- ),
- )
- def test_parse_frequency_raises_on_parse_error(frequency):
- with pytest.raises(ValueError):
- module.parse_frequency(frequency)
- def test_filter_checks_on_frequency_without_config_uses_default_checks():
- flexmock(module).should_receive('parse_frequency').and_return(
- module.datetime.timedelta(weeks=4)
- )
- flexmock(module).should_receive('make_check_time_path')
- flexmock(module).should_receive('probe_for_check_time').and_return(None)
- assert module.filter_checks_on_frequency(
- config={},
- borg_repository_id='repo',
- checks=('repository', 'archives'),
- force=False,
- archives_check_id='1234',
- ) == ('repository', 'archives')
- def test_filter_checks_on_frequency_retains_unconfigured_check():
- assert module.filter_checks_on_frequency(
- config={},
- borg_repository_id='repo',
- checks=('data',),
- force=False,
- ) == ('data',)
- def test_filter_checks_on_frequency_retains_check_without_frequency():
- flexmock(module).should_receive('parse_frequency').and_return(None)
- assert module.filter_checks_on_frequency(
- config={'checks': [{'name': 'archives'}]},
- borg_repository_id='repo',
- checks=('archives',),
- force=False,
- archives_check_id='1234',
- ) == ('archives',)
- def test_filter_checks_on_frequency_retains_check_with_empty_only_run_on():
- flexmock(module).should_receive('parse_frequency').and_return(None)
- assert module.filter_checks_on_frequency(
- config={'checks': [{'name': 'archives', 'only_run_on': []}]},
- borg_repository_id='repo',
- checks=('archives',),
- force=False,
- archives_check_id='1234',
- datetime_now=flexmock(weekday=lambda: 0),
- ) == ('archives',)
- def test_filter_checks_on_frequency_retains_check_with_only_run_on_matching_today():
- flexmock(module).should_receive('parse_frequency').and_return(None)
- assert module.filter_checks_on_frequency(
- config={'checks': [{'name': 'archives', 'only_run_on': [module.calendar.day_name[0]]}]},
- borg_repository_id='repo',
- checks=('archives',),
- force=False,
- archives_check_id='1234',
- datetime_now=flexmock(weekday=lambda: 0),
- ) == ('archives',)
- def test_filter_checks_on_frequency_retains_check_with_only_run_on_matching_today_via_weekday_value():
- flexmock(module).should_receive('parse_frequency').and_return(None)
- assert module.filter_checks_on_frequency(
- config={'checks': [{'name': 'archives', 'only_run_on': ['weekday']}]},
- borg_repository_id='repo',
- checks=('archives',),
- force=False,
- archives_check_id='1234',
- datetime_now=flexmock(weekday=lambda: 0),
- ) == ('archives',)
- def test_filter_checks_on_frequency_retains_check_with_only_run_on_matching_today_via_weekend_value():
- flexmock(module).should_receive('parse_frequency').and_return(None)
- assert module.filter_checks_on_frequency(
- config={'checks': [{'name': 'archives', 'only_run_on': ['weekend']}]},
- borg_repository_id='repo',
- checks=('archives',),
- force=False,
- archives_check_id='1234',
- datetime_now=flexmock(weekday=lambda: 6),
- ) == ('archives',)
- def test_filter_checks_on_frequency_skips_check_with_only_run_on_not_matching_today():
- flexmock(module).should_receive('parse_frequency').and_return(None)
- assert (
- module.filter_checks_on_frequency(
- config={'checks': [{'name': 'archives', 'only_run_on': [module.calendar.day_name[5]]}]},
- borg_repository_id='repo',
- checks=('archives',),
- force=False,
- archives_check_id='1234',
- datetime_now=flexmock(weekday=lambda: 0),
- )
- == ()
- )
- def test_filter_checks_on_frequency_retains_check_with_elapsed_frequency():
- flexmock(module).should_receive('parse_frequency').and_return(
- module.datetime.timedelta(hours=1)
- )
- flexmock(module).should_receive('make_check_time_path')
- flexmock(module).should_receive('probe_for_check_time').and_return(
- module.datetime.datetime(year=module.datetime.MINYEAR, month=1, day=1)
- )
- assert module.filter_checks_on_frequency(
- config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
- borg_repository_id='repo',
- checks=('archives',),
- force=False,
- archives_check_id='1234',
- ) == ('archives',)
- def test_filter_checks_on_frequency_retains_check_with_missing_check_time_file():
- flexmock(module).should_receive('parse_frequency').and_return(
- module.datetime.timedelta(hours=1)
- )
- flexmock(module).should_receive('make_check_time_path')
- flexmock(module).should_receive('probe_for_check_time').and_return(None)
- assert module.filter_checks_on_frequency(
- config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
- borg_repository_id='repo',
- checks=('archives',),
- force=False,
- archives_check_id='1234',
- ) == ('archives',)
- def test_filter_checks_on_frequency_skips_check_with_unelapsed_frequency():
- flexmock(module).should_receive('parse_frequency').and_return(
- module.datetime.timedelta(hours=1)
- )
- flexmock(module).should_receive('make_check_time_path')
- flexmock(module).should_receive('probe_for_check_time').and_return(
- module.datetime.datetime.now()
- )
- assert (
- module.filter_checks_on_frequency(
- config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
- borg_repository_id='repo',
- checks=('archives',),
- force=False,
- archives_check_id='1234',
- )
- == ()
- )
- def test_filter_checks_on_frequency_retains_check_with_unelapsed_frequency_and_force():
- assert module.filter_checks_on_frequency(
- config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
- borg_repository_id='repo',
- checks=('archives',),
- force=True,
- archives_check_id='1234',
- ) == ('archives',)
- def test_filter_checks_on_frequency_passes_through_empty_checks():
- assert (
- module.filter_checks_on_frequency(
- config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
- borg_repository_id='repo',
- checks=(),
- force=False,
- archives_check_id='1234',
- )
- == ()
- )
- def test_make_archives_check_id_with_flags_returns_a_value_and_does_not_raise():
- assert module.make_archives_check_id(('--match-archives', 'sh:foo-*'))
- def test_make_archives_check_id_with_empty_flags_returns_none():
- assert module.make_archives_check_id(()) is None
- def test_make_check_time_path_with_borgmatic_source_directory_includes_it():
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_state_directory'
- ).and_return(
- '/home/user/.local/state/borgmatic',
- )
- assert (
- module.make_check_time_path({}, '1234', 'archives', '5678')
- == '/home/user/.local/state/borgmatic/checks/1234/archives/5678'
- )
- def test_make_check_time_path_with_archives_check_and_no_archives_check_id_defaults_to_all():
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_state_directory'
- ).and_return(
- '/home/user/.local/state/borgmatic',
- )
- assert (
- module.make_check_time_path(
- {},
- '1234',
- 'archives',
- )
- == '/home/user/.local/state/borgmatic/checks/1234/archives/all'
- )
- def test_make_check_time_path_with_repositories_check_ignores_archives_check_id():
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_state_directory'
- ).and_return(
- '/home/user/.local/state/borgmatic',
- )
- assert (
- module.make_check_time_path({}, '1234', 'repository', '5678')
- == '/home/user/.local/state/borgmatic/checks/1234/repository'
- )
- def test_read_check_time_does_not_raise():
- flexmock(module.os).should_receive('stat').and_return(flexmock(st_mtime=123))
- assert module.read_check_time('/path')
- def test_read_check_time_on_missing_file_does_not_raise():
- flexmock(module.os).should_receive('stat').and_raise(FileNotFoundError)
- assert module.read_check_time('/path') is None
- def test_probe_for_check_time_uses_maximum_of_multiple_check_times():
- flexmock(module).should_receive('make_check_time_path').and_return(
- '~/.borgmatic/checks/1234/archives/5678'
- ).and_return('~/.borgmatic/checks/1234/archives/all')
- flexmock(module).should_receive('read_check_time').and_return(1).and_return(2)
- assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) == 2
- def test_probe_for_check_time_deduplicates_identical_check_time_paths():
- flexmock(module).should_receive('make_check_time_path').and_return(
- '~/.borgmatic/checks/1234/archives/5678'
- ).and_return('~/.borgmatic/checks/1234/archives/5678')
- flexmock(module).should_receive('read_check_time').and_return(1).once()
- assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) == 1
- def test_probe_for_check_time_skips_none_check_time():
- flexmock(module).should_receive('make_check_time_path').and_return(
- '~/.borgmatic/checks/1234/archives/5678'
- ).and_return('~/.borgmatic/checks/1234/archives/all')
- flexmock(module).should_receive('read_check_time').and_return(None).and_return(2)
- assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) == 2
- def test_probe_for_check_time_uses_single_check_time():
- flexmock(module).should_receive('make_check_time_path').and_return(
- '~/.borgmatic/checks/1234/archives/5678'
- ).and_return('~/.borgmatic/checks/1234/archives/all')
- flexmock(module).should_receive('read_check_time').and_return(1).and_return(None)
- assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) == 1
- def test_probe_for_check_time_returns_none_when_no_check_time_found():
- flexmock(module).should_receive('make_check_time_path').and_return(
- '~/.borgmatic/checks/1234/archives/5678'
- ).and_return('~/.borgmatic/checks/1234/archives/all')
- flexmock(module).should_receive('read_check_time').and_return(None).and_return(None)
- assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) is None
- def test_upgrade_check_times_moves_checks_from_borgmatic_source_directory_to_state_directory():
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_source_directory'
- ).and_return('/home/user/.borgmatic')
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_state_directory'
- ).and_return('/home/user/.local/state/borgmatic')
- flexmock(module.os.path).should_receive('exists').with_args(
- '/home/user/.borgmatic/checks'
- ).and_return(True)
- flexmock(module.os.path).should_receive('exists').with_args(
- '/home/user/.local/state/borgmatic/checks'
- ).and_return(False)
- flexmock(module.os).should_receive('makedirs')
- flexmock(module.shutil).should_receive('move').with_args(
- '/home/user/.borgmatic/checks', '/home/user/.local/state/borgmatic/checks'
- ).once()
- flexmock(module).should_receive('make_check_time_path').and_return(
- '/home/user/.local/state/borgmatic/checks/1234/archives/all'
- )
- flexmock(module.os.path).should_receive('isfile').and_return(False)
- flexmock(module.os).should_receive('mkdir').never()
- module.upgrade_check_times(flexmock(), flexmock())
- def test_upgrade_check_times_with_checks_already_in_borgmatic_state_directory_does_not_move_anything():
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_source_directory'
- ).and_return('/home/user/.borgmatic')
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_state_directory'
- ).and_return('/home/user/.local/state/borgmatic')
- flexmock(module.os.path).should_receive('exists').with_args(
- '/home/user/.borgmatic/checks'
- ).and_return(True)
- flexmock(module.os.path).should_receive('exists').with_args(
- '/home/user/.local/state/borgmatic/checks'
- ).and_return(True)
- flexmock(module.os).should_receive('makedirs').never()
- flexmock(module.shutil).should_receive('move').never()
- flexmock(module).should_receive('make_check_time_path').and_return(
- '/home/user/.local/state/borgmatic/checks/1234/archives/all'
- )
- flexmock(module.os.path).should_receive('isfile').and_return(False)
- flexmock(module.shutil).should_receive('move').never()
- flexmock(module.os).should_receive('mkdir').never()
- module.upgrade_check_times(flexmock(), flexmock())
- def test_upgrade_check_times_renames_old_check_paths_to_all():
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_source_directory'
- ).and_return('/home/user/.borgmatic')
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_state_directory'
- ).and_return('/home/user/.local/state/borgmatic')
- flexmock(module.os.path).should_receive('exists').and_return(False)
- base_path = '/home/user/.local/state/borgmatic/checks/1234'
- flexmock(module).should_receive('make_check_time_path').with_args(
- object, object, 'archives', 'all'
- ).and_return(f'{base_path}/archives/all')
- flexmock(module).should_receive('make_check_time_path').with_args(
- object, object, 'data', 'all'
- ).and_return(f'{base_path}/data/all')
- flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/archives').and_return(
- True
- )
- flexmock(module.os.path).should_receive('isfile').with_args(
- f'{base_path}/archives.temp'
- ).and_return(False)
- flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/data').and_return(
- False
- )
- flexmock(module.os.path).should_receive('isfile').with_args(
- f'{base_path}/data.temp'
- ).and_return(False)
- flexmock(module.shutil).should_receive('move').with_args(
- f'{base_path}/archives', f'{base_path}/archives.temp'
- ).once()
- flexmock(module.os).should_receive('mkdir').with_args(f'{base_path}/archives').once()
- flexmock(module.shutil).should_receive('move').with_args(
- f'{base_path}/archives.temp', f'{base_path}/archives/all'
- ).once()
- module.upgrade_check_times(flexmock(), flexmock())
- def test_upgrade_check_times_renames_data_check_paths_when_archives_paths_are_already_upgraded():
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_source_directory'
- ).and_return('/home/user/.borgmatic')
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_state_directory'
- ).and_return('/home/user/.local/state/borgmatic')
- flexmock(module.os.path).should_receive('exists').and_return(False)
- base_path = '/home/user/.local/state/borgmatic/checks/1234'
- flexmock(module).should_receive('make_check_time_path').with_args(
- object, object, 'archives', 'all'
- ).and_return(f'{base_path}/archives/all')
- flexmock(module).should_receive('make_check_time_path').with_args(
- object, object, 'data', 'all'
- ).and_return(f'{base_path}/data/all')
- flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/archives').and_return(
- False
- )
- flexmock(module.os.path).should_receive('isfile').with_args(
- f'{base_path}/archives.temp'
- ).and_return(False)
- flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/data').and_return(
- True
- )
- flexmock(module.shutil).should_receive('move').with_args(
- f'{base_path}/data', f'{base_path}/data.temp'
- ).once()
- flexmock(module.os).should_receive('mkdir').with_args(f'{base_path}/data').once()
- flexmock(module.shutil).should_receive('move').with_args(
- f'{base_path}/data.temp', f'{base_path}/data/all'
- ).once()
- module.upgrade_check_times(flexmock(), flexmock())
- def test_upgrade_check_times_skips_already_upgraded_check_paths():
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_source_directory'
- ).and_return('/home/user/.borgmatic')
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_state_directory'
- ).and_return('/home/user/.local/state/borgmatic')
- flexmock(module.os.path).should_receive('exists').and_return(False)
- flexmock(module).should_receive('make_check_time_path').and_return(
- '/home/user/.local/state/borgmatic/checks/1234/archives/all'
- )
- flexmock(module.os.path).should_receive('isfile').and_return(False)
- flexmock(module.shutil).should_receive('move').never()
- flexmock(module.os).should_receive('mkdir').never()
- module.upgrade_check_times(flexmock(), flexmock())
- def test_upgrade_check_times_renames_stale_temporary_check_path():
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_source_directory'
- ).and_return('/home/user/.borgmatic')
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_state_directory'
- ).and_return('/home/user/.local/state/borgmatic')
- flexmock(module.os.path).should_receive('exists').and_return(False)
- base_path = '/home/borgmatic/.local/state/checks/1234'
- flexmock(module).should_receive('make_check_time_path').with_args(
- object, object, 'archives', 'all'
- ).and_return(f'{base_path}/archives/all')
- flexmock(module).should_receive('make_check_time_path').with_args(
- object, object, 'data', 'all'
- ).and_return(f'{base_path}/data/all')
- flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/archives').and_return(
- False
- )
- flexmock(module.os.path).should_receive('isfile').with_args(
- f'{base_path}/archives.temp'
- ).and_return(True)
- flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/data').and_return(
- False
- )
- flexmock(module.os.path).should_receive('isfile').with_args(
- f'{base_path}/data.temp'
- ).and_return(False)
- flexmock(module.shutil).should_receive('move').with_args(
- f'{base_path}/archives', f'{base_path}/archives.temp'
- ).and_raise(FileNotFoundError)
- flexmock(module.os).should_receive('mkdir').with_args(f'{base_path}/archives').once()
- flexmock(module.shutil).should_receive('move').with_args(
- f'{base_path}/archives.temp', f'{base_path}/archives/all'
- ).once()
- module.upgrade_check_times(flexmock(), flexmock())
- def test_collect_spot_check_source_paths_parses_borg_output():
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return(
- {'hook1': False, 'hook2': True}
- )
- flexmock(module.borgmatic.actions.create).should_receive(
- 'process_source_directories'
- ).and_return(['foo', 'bar'])
- flexmock(module.borgmatic.borg.create).should_receive('make_base_create_command').with_args(
- dry_run=True,
- repository_path='repo',
- config=object,
- source_directories=['foo', 'bar'],
- local_borg_version=object,
- global_arguments=object,
- borgmatic_runtime_directory='/run/borgmatic',
- local_path=object,
- remote_path=object,
- list_files=True,
- stream_processes=True,
- ).and_return((('borg', 'create'), ('repo::archive',), flexmock(), flexmock()))
- flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
- flexmock()
- )
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).and_return(
- 'warning: stuff\n- /etc/path\n+ /etc/other\n? /nope',
- )
- flexmock(module.os.path).should_receive('isfile').and_return(True)
- assert module.collect_spot_check_source_paths(
- repository={'path': 'repo'},
- config={'working_directory': '/'},
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- ) == ('/etc/path', '/etc/other')
- def test_collect_spot_check_source_paths_passes_through_stream_processes_false():
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return(
- {'hook1': False, 'hook2': False}
- )
- flexmock(module.borgmatic.actions.create).should_receive(
- 'process_source_directories'
- ).and_return(['foo', 'bar'])
- flexmock(module.borgmatic.borg.create).should_receive('make_base_create_command').with_args(
- dry_run=True,
- repository_path='repo',
- config=object,
- source_directories=['foo', 'bar'],
- local_borg_version=object,
- global_arguments=object,
- borgmatic_runtime_directory='/run/borgmatic',
- local_path=object,
- remote_path=object,
- list_files=True,
- stream_processes=False,
- ).and_return((('borg', 'create'), ('repo::archive',), flexmock(), flexmock()))
- flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
- flexmock()
- )
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).and_return(
- 'warning: stuff\n- /etc/path\n+ /etc/other\n? /nope',
- )
- flexmock(module.os.path).should_receive('isfile').and_return(True)
- assert module.collect_spot_check_source_paths(
- repository={'path': 'repo'},
- config={'working_directory': '/'},
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- ) == ('/etc/path', '/etc/other')
- def test_collect_spot_check_source_paths_without_working_directory_parses_borg_output():
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return(
- {'hook1': False, 'hook2': True}
- )
- flexmock(module.borgmatic.actions.create).should_receive(
- 'process_source_directories'
- ).and_return(['foo', 'bar'])
- flexmock(module.borgmatic.borg.create).should_receive('make_base_create_command').with_args(
- dry_run=True,
- repository_path='repo',
- config=object,
- source_directories=['foo', 'bar'],
- local_borg_version=object,
- global_arguments=object,
- borgmatic_runtime_directory='/run/borgmatic',
- local_path=object,
- remote_path=object,
- list_files=True,
- stream_processes=True,
- ).and_return((('borg', 'create'), ('repo::archive',), flexmock(), flexmock()))
- flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
- flexmock()
- )
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).and_return(
- 'warning: stuff\n- /etc/path\n+ /etc/other\n? /nope',
- )
- flexmock(module.os.path).should_receive('isfile').and_return(True)
- assert module.collect_spot_check_source_paths(
- repository={'path': 'repo'},
- config={},
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- ) == ('/etc/path', '/etc/other')
- def test_collect_spot_check_source_paths_skips_directories():
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return(
- {'hook1': False, 'hook2': True}
- )
- flexmock(module.borgmatic.actions.create).should_receive(
- 'process_source_directories'
- ).and_return(['foo', 'bar'])
- flexmock(module.borgmatic.borg.create).should_receive('make_base_create_command').with_args(
- dry_run=True,
- repository_path='repo',
- config=object,
- source_directories=['foo', 'bar'],
- local_borg_version=object,
- global_arguments=object,
- borgmatic_runtime_directory='/run/borgmatic',
- local_path=object,
- remote_path=object,
- list_files=True,
- stream_processes=True,
- ).and_return((('borg', 'create'), ('repo::archive',), flexmock(), flexmock()))
- flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
- flexmock()
- )
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).and_return(
- 'warning: stuff\n- /etc/path\n+ /etc/dir\n? /nope',
- )
- flexmock(module.os.path).should_receive('isfile').with_args('/etc/path').and_return(False)
- flexmock(module.os.path).should_receive('isfile').with_args('/etc/dir').and_return(False)
- assert (
- module.collect_spot_check_source_paths(
- repository={'path': 'repo'},
- config={'working_directory': '/'},
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- == ()
- )
- def test_collect_spot_check_archive_paths_excludes_directories():
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_source_directory'
- ).and_return('/home/user/.borgmatic')
- flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
- (
- 'f /etc/path',
- 'f /etc/other',
- 'd /etc/dir',
- )
- )
- assert module.collect_spot_check_archive_paths(
- repository={'path': 'repo'},
- archive='archive',
- config={},
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- borgmatic_runtime_directory='/run/user/1001/borgmatic',
- ) == ('/etc/path', '/etc/other')
- def test_collect_spot_check_archive_paths_excludes_file_in_borgmatic_runtime_directory_as_stored_with_prefix_truncation():
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_source_directory'
- ).and_return('/root/.borgmatic')
- flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
- (
- 'f /etc/path',
- 'f /borgmatic/some/thing',
- )
- )
- assert module.collect_spot_check_archive_paths(
- repository={'path': 'repo'},
- archive='archive',
- config={},
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- borgmatic_runtime_directory='/run/user/0/borgmatic',
- ) == ('/etc/path',)
- def test_collect_spot_check_archive_paths_excludes_file_in_borgmatic_source_directory():
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_source_directory'
- ).and_return('/root/.borgmatic')
- flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
- (
- 'f /etc/path',
- 'f /root/.borgmatic/some/thing',
- )
- )
- assert module.collect_spot_check_archive_paths(
- repository={'path': 'repo'},
- archive='archive',
- config={},
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- borgmatic_runtime_directory='/run/user/0/borgmatic',
- ) == ('/etc/path',)
- def test_collect_spot_check_archive_paths_excludes_file_in_borgmatic_runtime_directory():
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_source_directory'
- ).and_return('/root.borgmatic')
- flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
- (
- 'f /etc/path',
- 'f /run/user/0/borgmatic/some/thing',
- )
- )
- assert module.collect_spot_check_archive_paths(
- repository={'path': 'repo'},
- archive='archive',
- config={},
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- borgmatic_runtime_directory='/run/user/0/borgmatic',
- ) == ('/etc/path',)
- def test_collect_spot_check_source_paths_uses_working_directory():
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return(
- {'hook1': False, 'hook2': True}
- )
- flexmock(module.borgmatic.actions.create).should_receive(
- 'process_source_directories'
- ).and_return(['foo', 'bar'])
- flexmock(module.borgmatic.borg.create).should_receive('make_base_create_command').with_args(
- dry_run=True,
- repository_path='repo',
- config=object,
- source_directories=['foo', 'bar'],
- local_borg_version=object,
- global_arguments=object,
- borgmatic_runtime_directory='/run/borgmatic',
- local_path=object,
- remote_path=object,
- list_files=True,
- stream_processes=True,
- ).and_return((('borg', 'create'), ('repo::archive',), flexmock(), flexmock()))
- flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
- flexmock()
- )
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
- '/working/dir'
- )
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).and_return(
- 'warning: stuff\n- foo\n+ bar\n? /nope',
- )
- flexmock(module.os.path).should_receive('isfile').with_args('/working/dir/foo').and_return(True)
- flexmock(module.os.path).should_receive('isfile').with_args('/working/dir/bar').and_return(True)
- assert module.collect_spot_check_source_paths(
- repository={'path': 'repo'},
- config={'working_directory': '/working/dir'},
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- ) == ('foo', 'bar')
- def test_compare_spot_check_hashes_returns_paths_having_failing_hashes():
- flexmock(module.random).should_receive('sample').replace_with(
- lambda population, count: population[:count]
- )
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
- None,
- )
- flexmock(module.os.path).should_receive('exists').and_return(True)
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).with_args(('xxh64sum', '/foo', '/bar'), working_directory=None).and_return(
- 'hash1 /foo\nhash2 /bar'
- )
- flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
- ['hash1 /foo', 'nothash2 /bar']
- )
- assert module.compare_spot_check_hashes(
- repository={'path': 'repo'},
- archive='archive',
- config={
- 'checks': [
- {
- 'name': 'archives',
- 'frequency': '2 weeks',
- },
- {
- 'name': 'spot',
- 'data_sample_percentage': 50,
- },
- ]
- },
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- log_prefix='repo',
- source_paths=('/foo', '/bar', '/baz', '/quux'),
- ) == ('/bar',)
- def test_compare_spot_check_hashes_handles_data_sample_percentage_above_100():
- flexmock(module.random).should_receive('sample').replace_with(
- lambda population, count: population[:count]
- )
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
- None,
- )
- flexmock(module.os.path).should_receive('exists').and_return(True)
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).with_args(('xxh64sum', '/foo', '/bar'), working_directory=None).and_return(
- 'hash1 /foo\nhash2 /bar'
- )
- flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
- ['nothash1 /foo', 'nothash2 /bar']
- )
- assert module.compare_spot_check_hashes(
- repository={'path': 'repo'},
- archive='archive',
- config={
- 'checks': [
- {
- 'name': 'archives',
- 'frequency': '2 weeks',
- },
- {
- 'name': 'spot',
- 'data_sample_percentage': 1000,
- },
- ]
- },
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- log_prefix='repo',
- source_paths=('/foo', '/bar'),
- ) == ('/foo', '/bar')
- def test_compare_spot_check_hashes_uses_xxh64sum_command_option():
- flexmock(module.random).should_receive('sample').replace_with(
- lambda population, count: population[:count]
- )
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
- None,
- )
- flexmock(module.os.path).should_receive('exists').and_return(True)
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).with_args(('/usr/local/bin/xxh64sum', '/foo', '/bar'), working_directory=None).and_return(
- 'hash1 /foo\nhash2 /bar'
- )
- flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
- ['hash1 /foo', 'nothash2 /bar']
- )
- assert module.compare_spot_check_hashes(
- repository={'path': 'repo'},
- archive='archive',
- config={
- 'checks': [
- {
- 'name': 'spot',
- 'data_sample_percentage': 50,
- 'xxh64sum_command': '/usr/local/bin/xxh64sum',
- },
- ]
- },
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- log_prefix='repo',
- source_paths=('/foo', '/bar', '/baz', '/quux'),
- ) == ('/bar',)
- def test_compare_spot_check_hashes_considers_path_missing_from_archive_as_not_matching():
- flexmock(module.random).should_receive('sample').replace_with(
- lambda population, count: population[:count]
- )
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
- None,
- )
- flexmock(module.os.path).should_receive('exists').and_return(True)
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).with_args(('xxh64sum', '/foo', '/bar'), working_directory=None).and_return(
- 'hash1 /foo\nhash2 /bar'
- )
- flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
- ['hash1 /foo']
- )
- assert module.compare_spot_check_hashes(
- repository={'path': 'repo'},
- archive='archive',
- config={
- 'checks': [
- {
- 'name': 'spot',
- 'data_sample_percentage': 50,
- },
- ]
- },
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- log_prefix='repo',
- source_paths=('/foo', '/bar', '/baz', '/quux'),
- ) == ('/bar',)
- def test_compare_spot_check_hashes_considers_non_existent_path_as_not_matching():
- flexmock(module.random).should_receive('sample').replace_with(
- lambda population, count: population[:count]
- )
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
- None,
- )
- flexmock(module.os.path).should_receive('exists').with_args('/foo').and_return(True)
- flexmock(module.os.path).should_receive('exists').with_args('/bar').and_return(False)
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).with_args(('xxh64sum', '/foo'), working_directory=None).and_return('hash1 /foo')
- flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
- ['hash1 /foo', 'hash2 /bar']
- )
- assert module.compare_spot_check_hashes(
- repository={'path': 'repo'},
- archive='archive',
- config={
- 'checks': [
- {
- 'name': 'spot',
- 'data_sample_percentage': 50,
- },
- ]
- },
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- log_prefix='repo',
- source_paths=('/foo', '/bar', '/baz', '/quux'),
- ) == ('/bar',)
- def test_compare_spot_check_hashes_with_too_many_paths_feeds_them_to_commands_in_chunks():
- flexmock(module).SAMPLE_PATHS_SUBSET_COUNT = 2
- flexmock(module.random).should_receive('sample').replace_with(
- lambda population, count: population[:count]
- )
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
- None,
- )
- flexmock(module.os.path).should_receive('exists').and_return(True)
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).with_args(('xxh64sum', '/foo', '/bar'), working_directory=None).and_return(
- 'hash1 /foo\nhash2 /bar'
- )
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).with_args(('xxh64sum', '/baz', '/quux'), working_directory=None).and_return(
- 'hash3 /baz\nhash4 /quux'
- )
- flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
- ['hash1 /foo', 'hash2 /bar']
- ).and_return(['hash3 /baz', 'nothash4 /quux'])
- assert module.compare_spot_check_hashes(
- repository={'path': 'repo'},
- archive='archive',
- config={
- 'checks': [
- {
- 'name': 'archives',
- 'frequency': '2 weeks',
- },
- {
- 'name': 'spot',
- 'data_sample_percentage': 100,
- },
- ]
- },
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- log_prefix='repo',
- source_paths=('/foo', '/bar', '/baz', '/quux'),
- ) == ('/quux',)
- def test_compare_spot_check_hashes_uses_working_directory_to_access_source_paths():
- flexmock(module.random).should_receive('sample').replace_with(
- lambda population, count: population[:count]
- )
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
- '/working/dir',
- )
- flexmock(module.os.path).should_receive('exists').with_args('/working/dir/foo').and_return(True)
- flexmock(module.os.path).should_receive('exists').with_args('/working/dir/bar').and_return(True)
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).with_args(('xxh64sum', 'foo', 'bar'), working_directory='/working/dir').and_return(
- 'hash1 foo\nhash2 bar'
- )
- flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
- ['hash1 foo', 'nothash2 bar']
- )
- assert module.compare_spot_check_hashes(
- repository={'path': 'repo'},
- archive='archive',
- config={
- 'checks': [
- {
- 'name': 'archives',
- 'frequency': '2 weeks',
- },
- {
- 'name': 'spot',
- 'data_sample_percentage': 50,
- },
- ],
- 'working_directory': '/working/dir',
- },
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- log_prefix='repo',
- source_paths=('foo', 'bar', 'baz', 'quux'),
- ) == ('bar',)
- def test_spot_check_without_spot_configuration_errors():
- with pytest.raises(ValueError):
- module.spot_check(
- repository={'path': 'repo'},
- config={
- 'checks': [
- {
- 'name': 'archives',
- },
- ]
- },
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- def test_spot_check_without_any_configuration_errors():
- with pytest.raises(ValueError):
- module.spot_check(
- repository={'path': 'repo'},
- config={},
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- def test_spot_check_data_tolerance_percenatge_greater_than_data_sample_percentage_errors():
- with pytest.raises(ValueError):
- module.spot_check(
- repository={'path': 'repo'},
- config={
- 'checks': [
- {
- 'name': 'spot',
- 'data_tolerance_percentage': 7,
- 'data_sample_percentage': 5,
- },
- ]
- },
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- def test_spot_check_with_count_delta_greater_than_count_tolerance_percentage_errors():
- flexmock(module).should_receive('collect_spot_check_source_paths').and_return(
- ('/foo', '/bar', '/baz', '/quux')
- )
- flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
- 'archive'
- )
- flexmock(module).should_receive('collect_spot_check_archive_paths').and_return(
- ('/foo', '/bar')
- ).once()
- with pytest.raises(ValueError):
- module.spot_check(
- repository={'path': 'repo'},
- config={
- 'checks': [
- {
- 'name': 'spot',
- 'count_tolerance_percentage': 1,
- 'data_tolerance_percentage': 4,
- 'data_sample_percentage': 5,
- },
- ]
- },
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- def test_spot_check_with_failing_percentage_greater_than_data_tolerance_percentage_errors():
- flexmock(module).should_receive('collect_spot_check_source_paths').and_return(
- ('/foo', '/bar', '/baz', '/quux')
- )
- flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
- 'archive'
- )
- flexmock(module).should_receive('collect_spot_check_archive_paths').and_return(('/foo', '/bar'))
- flexmock(module).should_receive('compare_spot_check_hashes').and_return(
- ('/bar', '/baz', '/quux')
- ).once()
- with pytest.raises(ValueError):
- module.spot_check(
- repository={'path': 'repo'},
- config={
- 'checks': [
- {
- 'name': 'spot',
- 'count_tolerance_percentage': 55,
- 'data_tolerance_percentage': 4,
- 'data_sample_percentage': 5,
- },
- ]
- },
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- def test_spot_check_with_high_enough_tolerances_does_not_raise():
- flexmock(module).should_receive('collect_spot_check_source_paths').and_return(
- ('/foo', '/bar', '/baz', '/quux')
- )
- flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
- 'archive'
- )
- flexmock(module).should_receive('collect_spot_check_archive_paths').and_return(('/foo', '/bar'))
- flexmock(module).should_receive('compare_spot_check_hashes').and_return(
- ('/bar', '/baz', '/quux')
- ).once()
- module.spot_check(
- repository={'path': 'repo'},
- config={
- 'checks': [
- {
- 'name': 'spot',
- 'count_tolerance_percentage': 55,
- 'data_tolerance_percentage': 80,
- 'data_sample_percentage': 80,
- },
- ]
- },
- local_borg_version=flexmock(),
- global_arguments=flexmock(),
- local_path=flexmock(),
- remote_path=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- def test_run_check_checks_archives_for_configured_repository():
- flexmock(module.logger).answer = lambda message: None
- flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
- flexmock(module.borgmatic.borg.check).should_receive('get_repository_id').and_return(flexmock())
- flexmock(module).should_receive('upgrade_check_times')
- flexmock(module).should_receive('parse_checks')
- flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
- flexmock(module).should_receive('make_archives_check_id').and_return(None)
- flexmock(module).should_receive('filter_checks_on_frequency').and_return(
- {'repository', 'archives'}
- )
- flexmock(module.borgmatic.borg.check).should_receive('check_archives').once()
- flexmock(module).should_receive('make_check_time_path')
- flexmock(module).should_receive('write_check_time')
- flexmock(module.borgmatic.borg.extract).should_receive('extract_last_archive_dry_run').never()
- flexmock(module.borgmatic.hooks.command).should_receive('execute_hook').times(2)
- check_arguments = flexmock(
- repository=None,
- progress=flexmock(),
- repair=flexmock(),
- only_checks=flexmock(),
- force=flexmock(),
- )
- global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
- module.run_check(
- config_filename='test.yaml',
- repository={'path': 'repo'},
- config={'repositories': ['repo']},
- hook_context={},
- local_borg_version=None,
- check_arguments=check_arguments,
- global_arguments=global_arguments,
- local_path=None,
- remote_path=None,
- )
- def test_run_check_runs_configured_extract_check():
- flexmock(module.logger).answer = lambda message: None
- flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
- flexmock(module.borgmatic.borg.check).should_receive('get_repository_id').and_return(flexmock())
- flexmock(module).should_receive('upgrade_check_times')
- flexmock(module).should_receive('parse_checks')
- flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
- flexmock(module).should_receive('make_archives_check_id').and_return(None)
- flexmock(module).should_receive('filter_checks_on_frequency').and_return({'extract'})
- flexmock(module.borgmatic.borg.check).should_receive('check_archives').never()
- flexmock(module.borgmatic.borg.extract).should_receive('extract_last_archive_dry_run').once()
- flexmock(module).should_receive('make_check_time_path')
- flexmock(module).should_receive('write_check_time')
- flexmock(module.borgmatic.hooks.command).should_receive('execute_hook').times(2)
- check_arguments = flexmock(
- repository=None,
- progress=flexmock(),
- repair=flexmock(),
- only_checks=flexmock(),
- force=flexmock(),
- )
- global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
- module.run_check(
- config_filename='test.yaml',
- repository={'path': 'repo'},
- config={'repositories': ['repo']},
- hook_context={},
- local_borg_version=None,
- check_arguments=check_arguments,
- global_arguments=global_arguments,
- local_path=None,
- remote_path=None,
- )
- def test_run_check_runs_configured_spot_check():
- flexmock(module.logger).answer = lambda message: None
- flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
- flexmock(module.borgmatic.borg.check).should_receive('get_repository_id').and_return(flexmock())
- flexmock(module).should_receive('upgrade_check_times')
- flexmock(module).should_receive('parse_checks')
- flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
- flexmock(module).should_receive('make_archives_check_id').and_return(None)
- flexmock(module).should_receive('filter_checks_on_frequency').and_return({'spot'})
- flexmock(module.borgmatic.borg.check).should_receive('check_archives').never()
- flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
- flexmock()
- )
- flexmock(module.borgmatic.actions.check).should_receive('spot_check').once()
- flexmock(module).should_receive('make_check_time_path')
- flexmock(module).should_receive('write_check_time')
- flexmock(module.borgmatic.hooks.command).should_receive('execute_hook').times(2)
- check_arguments = flexmock(
- repository=None,
- progress=flexmock(),
- repair=flexmock(),
- only_checks=flexmock(),
- force=flexmock(),
- )
- global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
- module.run_check(
- config_filename='test.yaml',
- repository={'path': 'repo'},
- config={'repositories': ['repo']},
- hook_context={},
- local_borg_version=None,
- check_arguments=check_arguments,
- global_arguments=global_arguments,
- local_path=None,
- remote_path=None,
- )
- def test_run_check_without_checks_runs_nothing_except_hooks():
- flexmock(module.logger).answer = lambda message: None
- flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
- flexmock(module.borgmatic.borg.check).should_receive('get_repository_id').and_return(flexmock())
- flexmock(module).should_receive('upgrade_check_times')
- flexmock(module).should_receive('parse_checks')
- flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
- flexmock(module).should_receive('make_archives_check_id').and_return(None)
- flexmock(module).should_receive('filter_checks_on_frequency').and_return({})
- flexmock(module.borgmatic.borg.check).should_receive('check_archives').never()
- flexmock(module).should_receive('make_check_time_path')
- flexmock(module).should_receive('write_check_time').never()
- flexmock(module.borgmatic.borg.extract).should_receive('extract_last_archive_dry_run').never()
- flexmock(module.borgmatic.hooks.command).should_receive('execute_hook').times(2)
- check_arguments = flexmock(
- repository=None,
- progress=flexmock(),
- repair=flexmock(),
- only_checks=flexmock(),
- force=flexmock(),
- )
- global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
- module.run_check(
- config_filename='test.yaml',
- repository={'path': 'repo'},
- config={'repositories': ['repo']},
- hook_context={},
- local_borg_version=None,
- check_arguments=check_arguments,
- global_arguments=global_arguments,
- local_path=None,
- remote_path=None,
- )
- def test_run_check_checks_archives_in_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.borg.check).should_receive('get_repository_id').and_return(flexmock())
- flexmock(module).should_receive('upgrade_check_times')
- flexmock(module).should_receive('parse_checks')
- flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
- flexmock(module).should_receive('make_archives_check_id').and_return(None)
- flexmock(module).should_receive('filter_checks_on_frequency').and_return(
- {'repository', 'archives'}
- )
- flexmock(module.borgmatic.borg.check).should_receive('check_archives').once()
- flexmock(module).should_receive('make_check_time_path')
- flexmock(module).should_receive('write_check_time')
- flexmock(module.borgmatic.borg.extract).should_receive('extract_last_archive_dry_run').never()
- check_arguments = flexmock(
- repository=flexmock(),
- progress=flexmock(),
- repair=flexmock(),
- only_checks=flexmock(),
- force=flexmock(),
- )
- global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
- module.run_check(
- config_filename='test.yaml',
- repository={'path': 'repo'},
- config={'repositories': ['repo']},
- hook_context={},
- local_borg_version=None,
- check_arguments=check_arguments,
- global_arguments=global_arguments,
- local_path=None,
- remote_path=None,
- )
- def test_run_check_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.borg.check).should_receive('check_archives').never()
- check_arguments = flexmock(
- repository=flexmock(),
- progress=flexmock(),
- repair=flexmock(),
- only_checks=flexmock(),
- force=flexmock(),
- )
- global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
- module.run_check(
- config_filename='test.yaml',
- repository={'path': 'repo'},
- config={'repositories': ['repo']},
- hook_context={},
- local_borg_version=None,
- check_arguments=check_arguments,
- global_arguments=global_arguments,
- local_path=None,
- remote_path=None,
- )
|