12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019 |
- import logging
- import pytest
- from flexmock import flexmock
- from borgmatic.borg import create as module
- from ..test_verbosity import insert_logging_mock
- def test_expand_directory_with_basic_path_passes_it_through():
- flexmock(module.os.path).should_receive('expanduser').and_return('foo')
- flexmock(module.glob).should_receive('glob').and_return([])
- paths = module._expand_directory('foo')
- assert paths == ['foo']
- def test_expand_directory_with_glob_expands():
- flexmock(module.os.path).should_receive('expanduser').and_return('foo*')
- flexmock(module.glob).should_receive('glob').and_return(['foo', 'food'])
- paths = module._expand_directory('foo*')
- assert paths == ['foo', 'food']
- def test_expand_directories_flattens_expanded_directories():
- flexmock(module).should_receive('_expand_directory').with_args('~/foo').and_return(
- ['/root/foo']
- )
- flexmock(module).should_receive('_expand_directory').with_args('bar*').and_return(
- ['bar', 'barf']
- )
- paths = module._expand_directories(('~/foo', 'bar*'))
- assert paths == ('/root/foo', 'bar', 'barf')
- def test_expand_directories_considers_none_as_no_directories():
- paths = module._expand_directories(None)
- assert paths == ()
- def test_expand_home_directories_expands_tildes():
- flexmock(module.os.path).should_receive('expanduser').with_args('~/bar').and_return('/foo/bar')
- flexmock(module.os.path).should_receive('expanduser').with_args('baz').and_return('baz')
- paths = module._expand_home_directories(('~/bar', 'baz'))
- assert paths == ('/foo/bar', 'baz')
- def test_expand_home_directories_considers_none_as_no_directories():
- paths = module._expand_home_directories(None)
- assert paths == ()
- def test_write_pattern_file_does_not_raise():
- temporary_file = flexmock(name='filename', write=lambda mode: None, flush=lambda: None)
- flexmock(module.tempfile).should_receive('NamedTemporaryFile').and_return(temporary_file)
- module._write_pattern_file(['exclude'])
- def test_write_pattern_file_with_empty_exclude_patterns_does_not_raise():
- module._write_pattern_file([])
- def test_make_pattern_flags_includes_pattern_filename_when_given():
- pattern_flags = module._make_pattern_flags(
- location_config={'patterns': ['R /', '- /var']}, pattern_filename='/tmp/patterns'
- )
- assert pattern_flags == ('--patterns-from', '/tmp/patterns')
- def test_make_pattern_flags_includes_patterns_from_filenames_when_in_config():
- pattern_flags = module._make_pattern_flags(
- location_config={'patterns_from': ['patterns', 'other']}
- )
- assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', 'other')
- def test_make_pattern_flags_includes_both_filenames_when_patterns_given_and_patterns_from_in_config():
- pattern_flags = module._make_pattern_flags(
- location_config={'patterns_from': ['patterns']}, pattern_filename='/tmp/patterns'
- )
- assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', '/tmp/patterns')
- def test_make_pattern_flags_considers_none_patterns_from_filenames_as_empty():
- pattern_flags = module._make_pattern_flags(location_config={'patterns_from': None})
- assert pattern_flags == ()
- def test_make_exclude_flags_includes_exclude_patterns_filename_when_given():
- exclude_flags = module._make_exclude_flags(
- location_config={'exclude_patterns': ['*.pyc', '/var']}, exclude_filename='/tmp/excludes'
- )
- assert exclude_flags == ('--exclude-from', '/tmp/excludes')
- def test_make_exclude_flags_includes_exclude_from_filenames_when_in_config():
- exclude_flags = module._make_exclude_flags(
- location_config={'exclude_from': ['excludes', 'other']}
- )
- assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', 'other')
- def test_make_exclude_flags_includes_both_filenames_when_patterns_given_and_exclude_from_in_config():
- exclude_flags = module._make_exclude_flags(
- location_config={'exclude_from': ['excludes']}, exclude_filename='/tmp/excludes'
- )
- assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', '/tmp/excludes')
- def test_make_exclude_flags_considers_none_exclude_from_filenames_as_empty():
- exclude_flags = module._make_exclude_flags(location_config={'exclude_from': None})
- assert exclude_flags == ()
- def test_make_exclude_flags_includes_exclude_caches_when_true_in_config():
- exclude_flags = module._make_exclude_flags(location_config={'exclude_caches': True})
- assert exclude_flags == ('--exclude-caches',)
- def test_make_exclude_flags_does_not_include_exclude_caches_when_false_in_config():
- exclude_flags = module._make_exclude_flags(location_config={'exclude_caches': False})
- assert exclude_flags == ()
- def test_make_exclude_flags_includes_exclude_if_present_when_in_config():
- exclude_flags = module._make_exclude_flags(location_config={'exclude_if_present': 'exclude_me'})
- assert exclude_flags == ('--exclude-if-present', 'exclude_me')
- def test_make_exclude_flags_includes_keep_exclude_tags_when_true_in_config():
- exclude_flags = module._make_exclude_flags(location_config={'keep_exclude_tags': True})
- assert exclude_flags == ('--keep-exclude-tags',)
- def test_make_exclude_flags_does_not_include_keep_exclude_tags_when_false_in_config():
- exclude_flags = module._make_exclude_flags(location_config={'keep_exclude_tags': False})
- assert exclude_flags == ()
- def test_make_exclude_flags_includes_exclude_nodump_when_true_in_config():
- exclude_flags = module._make_exclude_flags(location_config={'exclude_nodump': True})
- assert exclude_flags == ('--exclude-nodump',)
- def test_make_exclude_flags_does_not_include_exclude_nodump_when_false_in_config():
- exclude_flags = module._make_exclude_flags(location_config={'exclude_nodump': False})
- assert exclude_flags == ()
- def test_make_exclude_flags_is_empty_when_config_has_no_excludes():
- exclude_flags = module._make_exclude_flags(location_config={})
- assert exclude_flags == ()
- def test_borgmatic_source_directories_set_when_directory_exists():
- flexmock(module.os.path).should_receive('exists').and_return(True)
- flexmock(module.os.path).should_receive('expanduser')
- assert module.borgmatic_source_directories() == [module.BORGMATIC_SOURCE_DIRECTORY]
- def test_borgmatic_source_directories_empty_when_directory_does_not_exist():
- flexmock(module.os.path).should_receive('exists').and_return(False)
- flexmock(module.os.path).should_receive('expanduser')
- assert module.borgmatic_source_directories() == []
- DEFAULT_ARCHIVE_NAME = '{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}'
- ARCHIVE_WITH_PATHS = ('repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'bar')
- def test_create_archive_calls_borg_with_parameters():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={},
- )
- def test_create_archive_with_patterns_calls_borg_with_patterns():
- pattern_flags = ('--patterns-from', 'patterns')
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(
- flexmock(name='/tmp/patterns')
- ).and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(pattern_flags)
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create') + pattern_flags + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'patterns': ['pattern'],
- },
- storage_config={},
- )
- def test_create_archive_with_exclude_patterns_calls_borg_with_excludes():
- exclude_flags = ('--exclude-from', 'excludes')
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(('exclude',))
- flexmock(module).should_receive('_write_pattern_file').and_return(None).and_return(
- flexmock(name='/tmp/excludes')
- )
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(exclude_flags)
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create') + exclude_flags + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': ['exclude'],
- },
- storage_config={},
- )
- def test_create_archive_with_log_info_calls_borg_with_info_parameter():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--list', '--filter', 'AME-', '--info', '--stats') + ARCHIVE_WITH_PATHS,
- output_log_level=logging.INFO,
- )
- insert_logging_mock(logging.INFO)
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={},
- )
- def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS, output_log_level=None
- )
- insert_logging_mock(logging.INFO)
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={},
- json=True,
- )
- def test_create_archive_with_log_debug_calls_borg_with_debug_parameter():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--list', '--filter', 'AME-', '--stats', '--debug', '--show-rc')
- + ARCHIVE_WITH_PATHS,
- output_log_level=logging.INFO,
- )
- insert_logging_mock(logging.DEBUG)
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={},
- )
- def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS, output_log_level=None
- )
- insert_logging_mock(logging.DEBUG)
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={},
- json=True,
- )
- def test_create_archive_with_dry_run_calls_borg_with_dry_run_parameter():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--dry-run') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
- )
- module.create_archive(
- dry_run=True,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={},
- )
- def test_create_archive_with_dry_run_and_log_info_calls_borg_without_stats_parameter():
- # --dry-run and --stats are mutually exclusive, see:
- # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--list', '--filter', 'AME-', '--info', '--dry-run')
- + ARCHIVE_WITH_PATHS,
- output_log_level=logging.INFO,
- )
- insert_logging_mock(logging.INFO)
- module.create_archive(
- dry_run=True,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={},
- )
- def test_create_archive_with_dry_run_and_log_debug_calls_borg_without_stats_parameter():
- # --dry-run and --stats are mutually exclusive, see:
- # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--list', '--filter', 'AME-', '--debug', '--show-rc', '--dry-run')
- + ARCHIVE_WITH_PATHS,
- output_log_level=logging.INFO,
- )
- insert_logging_mock(logging.DEBUG)
- module.create_archive(
- dry_run=True,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={},
- )
- def test_create_archive_with_checkpoint_interval_calls_borg_with_checkpoint_interval_parameters():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--checkpoint-interval', '600') + ARCHIVE_WITH_PATHS,
- output_log_level=logging.INFO,
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={'checkpoint_interval': 600},
- )
- def test_create_archive_with_chunker_params_calls_borg_with_chunker_params_parameters():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--chunker-params', '1,2,3,4') + ARCHIVE_WITH_PATHS,
- output_log_level=logging.INFO,
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={'chunker_params': '1,2,3,4'},
- )
- def test_create_archive_with_compression_calls_borg_with_compression_parameters():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--compression', 'rle') + ARCHIVE_WITH_PATHS,
- output_log_level=logging.INFO,
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={'compression': 'rle'},
- )
- def test_create_archive_with_remote_rate_limit_calls_borg_with_remote_ratelimit_parameters():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--remote-ratelimit', '100') + ARCHIVE_WITH_PATHS,
- output_log_level=logging.INFO,
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={'remote_rate_limit': 100},
- )
- def test_create_archive_with_one_file_system_calls_borg_with_one_file_system_parameter():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--one-file-system') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'one_file_system': True,
- 'exclude_patterns': None,
- },
- storage_config={},
- )
- def test_create_archive_with_numeric_owner_calls_borg_with_numeric_owner_parameter():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--numeric-owner') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'numeric_owner': True,
- 'exclude_patterns': None,
- },
- storage_config={},
- )
- def test_create_archive_with_read_special_calls_borg_with_read_special_parameter():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--read-special') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'read_special': True,
- 'exclude_patterns': None,
- },
- storage_config={},
- )
- @pytest.mark.parametrize('option_name', ('atime', 'ctime', 'birthtime', 'bsd_flags'))
- def test_create_archive_with_option_true_calls_borg_without_corresponding_parameter(option_name):
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- option_name: True,
- 'exclude_patterns': None,
- },
- storage_config={},
- )
- @pytest.mark.parametrize('option_name', ('atime', 'ctime', 'birthtime', 'bsd_flags'))
- def test_create_archive_with_option_false_calls_borg_with_corresponding_parameter(option_name):
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--no' + option_name.replace('_', '')) + ARCHIVE_WITH_PATHS,
- output_log_level=logging.INFO,
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- option_name: False,
- 'exclude_patterns': None,
- },
- storage_config={},
- )
- def test_create_archive_with_files_cache_calls_borg_with_files_cache_parameters():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--files-cache', 'ctime,size') + ARCHIVE_WITH_PATHS,
- output_log_level=logging.INFO,
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'files_cache': 'ctime,size',
- 'exclude_patterns': None,
- },
- storage_config={},
- )
- def test_create_archive_with_local_path_calls_borg_via_local_path():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg1', 'create') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={},
- local_path='borg1',
- )
- def test_create_archive_with_remote_path_calls_borg_with_remote_path_parameters():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--remote-path', 'borg1') + ARCHIVE_WITH_PATHS,
- output_log_level=logging.INFO,
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={},
- remote_path='borg1',
- )
- def test_create_archive_with_umask_calls_borg_with_umask_parameters():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--umask', '740') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={'umask': 740},
- )
- def test_create_archive_with_lock_wait_calls_borg_with_lock_wait_parameters():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--lock-wait', '5') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={'lock_wait': 5},
- )
- def test_create_archive_with_stats_calls_borg_with_stats_parameter():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--stats') + ARCHIVE_WITH_PATHS, output_log_level=logging.WARNING
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={},
- stats=True,
- )
- def test_create_archive_with_progress_calls_borg_with_progress_parameter():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command_without_capture').with_args(
- ('borg', 'create', '--progress') + ARCHIVE_WITH_PATHS
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={},
- progress=True,
- )
- def test_create_archive_with_json_calls_borg_with_json_parameter():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS, output_log_level=None
- ).and_return('[]')
- json_output = module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={},
- json=True,
- )
- assert json_output == '[]'
- def test_create_archive_with_stats_and_json_calls_borg_without_stats_parameter():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS, output_log_level=None
- ).and_return('[]')
- json_output = module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={},
- json=True,
- stats=True,
- )
- assert json_output == '[]'
- def test_create_archive_with_source_directories_glob_expands():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'food'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
- output_log_level=logging.INFO,
- )
- flexmock(module.glob).should_receive('glob').with_args('foo*').and_return(['foo', 'food'])
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo*'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={},
- )
- def test_create_archive_with_non_matching_source_directories_glob_passes_through():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo*',))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo*'),
- output_log_level=logging.INFO,
- )
- flexmock(module.glob).should_receive('glob').with_args('foo*').and_return([])
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo*'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={},
- )
- def test_create_archive_with_glob_calls_borg_with_expanded_directories():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'food'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
- output_log_level=logging.INFO,
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo*'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={},
- )
- def test_create_archive_with_archive_name_format_calls_borg_with_archive_name():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', 'repo::ARCHIVE_NAME', 'foo', 'bar'), output_log_level=logging.INFO
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={'archive_name_format': 'ARCHIVE_NAME'},
- )
- def test_create_archive_with_archive_name_format_accepts_borg_placeholders():
- flexmock(module).should_receive('borgmatic_source_directories').and_return([])
- flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
- flexmock(module).should_receive('_expand_home_directories').and_return(())
- flexmock(module).should_receive('_write_pattern_file').and_return(None)
- flexmock(module).should_receive('_make_pattern_flags').and_return(())
- flexmock(module).should_receive('_make_exclude_flags').and_return(())
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', 'repo::Documents_{hostname}-{now}', 'foo', 'bar'),
- output_log_level=logging.INFO,
- )
- module.create_archive(
- dry_run=False,
- repository='repo',
- location_config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
- )
|