12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592 |
- import logging
- import pytest
- from flexmock import flexmock
- from borgmatic.borg import create as module
- from borgmatic.borg.pattern import Pattern, Pattern_source, Pattern_style, Pattern_type
- from ..test_verbosity import insert_logging_mock
- def test_write_patterns_file_writes_pattern_lines():
- temporary_file = flexmock(name='filename', flush=lambda: None)
- temporary_file.should_receive('write').with_args('R /foo\n+ sh:/foo/bar')
- flexmock(module.tempfile).should_receive('NamedTemporaryFile').and_return(temporary_file)
- module.write_patterns_file(
- [Pattern('/foo'), Pattern('/foo/bar', Pattern_type.INCLUDE, Pattern_style.SHELL)],
- borgmatic_runtime_directory='/run/user/0',
- )
- def test_write_patterns_file_with_empty_exclude_patterns_does_not_raise():
- module.write_patterns_file([], borgmatic_runtime_directory='/run/user/0')
- def test_write_patterns_file_appends_to_existing():
- patterns_file = flexmock(name='filename', flush=lambda: None)
- patterns_file.should_receive('write').with_args('\n')
- patterns_file.should_receive('write').with_args('R /foo\n+ /foo/bar')
- flexmock(module.tempfile).should_receive('NamedTemporaryFile').never()
- module.write_patterns_file(
- [Pattern('/foo'), Pattern('/foo/bar', Pattern_type.INCLUDE)],
- borgmatic_runtime_directory='/run/user/0',
- patterns_file=patterns_file,
- )
- def test_make_exclude_flags_includes_exclude_caches_when_true_in_config():
- exclude_flags = module.make_exclude_flags(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(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(
- config={'exclude_if_present': ['exclude_me', 'also_me']}
- )
- assert exclude_flags == (
- '--exclude-if-present',
- 'exclude_me',
- '--exclude-if-present',
- 'also_me',
- )
- def test_make_exclude_flags_includes_keep_exclude_tags_when_true_in_config():
- exclude_flags = module.make_exclude_flags(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(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(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(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(config={})
- assert exclude_flags == ()
- def test_make_list_filter_flags_with_debug_and_feature_available_includes_plus_and_minus():
- flexmock(module.logger).should_receive('isEnabledFor').and_return(True)
- flexmock(module.feature).should_receive('available').and_return(True)
- assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=False) == 'AME+-'
- def test_make_list_filter_flags_with_info_and_feature_available_omits_plus_and_minus():
- flexmock(module.logger).should_receive('isEnabledFor').and_return(False)
- flexmock(module.feature).should_receive('available').and_return(True)
- assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=False) == 'AME'
- def test_make_list_filter_flags_with_debug_and_feature_available_and_dry_run_includes_plus_and_minus():
- flexmock(module.logger).should_receive('isEnabledFor').and_return(True)
- flexmock(module.feature).should_receive('available').and_return(True)
- assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=True) == 'AME+-'
- def test_make_list_filter_flags_with_info_and_feature_available_and_dry_run_includes_plus_and_minus():
- flexmock(module.logger).should_receive('isEnabledFor').and_return(False)
- flexmock(module.feature).should_receive('available').and_return(True)
- assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=True) == 'AME+-'
- def test_make_list_filter_flags_with_debug_and_feature_not_available_includes_x():
- flexmock(module.logger).should_receive('isEnabledFor').and_return(True)
- flexmock(module.feature).should_receive('available').and_return(False)
- assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=False) == 'AMEx-'
- def test_make_list_filter_flags_with_info_and_feature_not_available_omits_x():
- flexmock(module.logger).should_receive('isEnabledFor').and_return(False)
- flexmock(module.feature).should_receive('available').and_return(False)
- assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=False) == 'AME-'
- @pytest.mark.parametrize(
- 'character_device,block_device,fifo,expected_result',
- (
- (False, False, False, False),
- (True, False, False, True),
- (False, True, False, True),
- (True, True, False, True),
- (False, False, True, True),
- (False, True, True, True),
- (True, False, True, True),
- ),
- )
- def test_special_file_looks_at_file_type(character_device, block_device, fifo, expected_result):
- flexmock(module.os).should_receive('stat').and_return(flexmock(st_mode=flexmock()))
- flexmock(module.stat).should_receive('S_ISCHR').and_return(character_device)
- flexmock(module.stat).should_receive('S_ISBLK').and_return(block_device)
- flexmock(module.stat).should_receive('S_ISFIFO').and_return(fifo)
- assert module.special_file('/dev/special') == expected_result
- def test_special_file_treats_broken_symlink_as_non_special():
- flexmock(module.os).should_receive('stat').and_raise(FileNotFoundError)
- assert module.special_file('/broken/symlink') is False
- def test_special_file_prepends_relative_path_with_working_directory():
- flexmock(module.os).should_receive('stat').with_args('/working/dir/relative').and_return(
- flexmock(st_mode=flexmock())
- )
- flexmock(module.stat).should_receive('S_ISCHR').and_return(False)
- flexmock(module.stat).should_receive('S_ISBLK').and_return(False)
- flexmock(module.stat).should_receive('S_ISFIFO').and_return(False)
- assert module.special_file('relative', '/working/dir') is False
- def test_any_parent_directories_treats_parents_as_match():
- module.any_parent_directories('/foo/bar.txt', ('/foo', '/etc'))
- def test_any_parent_directories_treats_grandparents_as_match():
- module.any_parent_directories('/foo/bar/baz.txt', ('/foo', '/etc'))
- def test_any_parent_directories_treats_unrelated_paths_as_non_match():
- module.any_parent_directories('/foo/bar.txt', ('/usr', '/etc'))
- def test_collect_special_file_paths_parses_special_files_from_borg_dry_run_file_list():
- flexmock(module.flags).should_receive('omit_flag').replace_with(
- lambda arguments, flag: arguments
- )
- flexmock(module.flags).should_receive('omit_flag_and_value').replace_with(
- lambda arguments, flag: arguments
- )
- flexmock(module.environment).should_receive('make_environment').and_return(None)
- flexmock(module).should_receive('execute_command_and_capture_output').and_return(
- 'Processing files ...\n- /foo\n+ /bar\n- /baz'
- )
- flexmock(module).should_receive('special_file').and_return(True)
- flexmock(module.os.path).should_receive('exists').and_return(False)
- flexmock(module).should_receive('any_parent_directories').never()
- assert module.collect_special_file_paths(
- dry_run=False,
- create_command=('borg', 'create'),
- config={},
- local_path=None,
- working_directory=None,
- borgmatic_runtime_directory='/run/borgmatic',
- ) == ('/foo', '/bar', '/baz')
- def test_collect_special_file_paths_skips_borgmatic_runtime_directory():
- flexmock(module.flags).should_receive('omit_flag').replace_with(
- lambda arguments, flag: arguments
- )
- flexmock(module.flags).should_receive('omit_flag_and_value').replace_with(
- lambda arguments, flag: arguments
- )
- flexmock(module.environment).should_receive('make_environment').and_return(None)
- flexmock(module).should_receive('execute_command_and_capture_output').and_return(
- '+ /foo\n- /run/borgmatic/bar\n- /baz'
- )
- flexmock(module).should_receive('special_file').and_return(True)
- flexmock(module.os.path).should_receive('exists').and_return(True)
- flexmock(module).should_receive('any_parent_directories').with_args(
- '/foo', ('/run/borgmatic',)
- ).and_return(False)
- flexmock(module).should_receive('any_parent_directories').with_args(
- '/run/borgmatic/bar', ('/run/borgmatic',)
- ).and_return(True)
- flexmock(module).should_receive('any_parent_directories').with_args(
- '/baz', ('/run/borgmatic',)
- ).and_return(False)
- assert module.collect_special_file_paths(
- dry_run=False,
- create_command=('borg', 'create'),
- config={},
- local_path=None,
- working_directory=None,
- borgmatic_runtime_directory='/run/borgmatic',
- ) == ('/foo', '/baz')
- def test_collect_special_file_paths_with_borgmatic_runtime_directory_missing_from_paths_output_errors():
- flexmock(module.flags).should_receive('omit_flag').replace_with(
- lambda arguments, flag: arguments
- )
- flexmock(module.flags).should_receive('omit_flag_and_value').replace_with(
- lambda arguments, flag: arguments
- )
- flexmock(module.environment).should_receive('make_environment').and_return(None)
- flexmock(module).should_receive('execute_command_and_capture_output').and_return(
- '+ /foo\n- /bar\n- /baz'
- )
- flexmock(module).should_receive('special_file').and_return(True)
- flexmock(module.os.path).should_receive('exists').and_return(True)
- flexmock(module).should_receive('any_parent_directories').and_return(False)
- with pytest.raises(ValueError):
- module.collect_special_file_paths(
- dry_run=False,
- create_command=('borg', 'create'),
- config={},
- local_path=None,
- working_directory=None,
- borgmatic_runtime_directory='/run/borgmatic',
- )
- def test_collect_special_file_paths_with_dry_run_and_borgmatic_runtime_directory_missing_from_paths_output_does_not_raise():
- flexmock(module.flags).should_receive('omit_flag').replace_with(
- lambda arguments, flag: arguments
- )
- flexmock(module.flags).should_receive('omit_flag_and_value').replace_with(
- lambda arguments, flag: arguments
- )
- flexmock(module.environment).should_receive('make_environment').and_return(None)
- flexmock(module).should_receive('execute_command_and_capture_output').and_return(
- '+ /foo\n- /bar\n- /baz'
- )
- flexmock(module).should_receive('special_file').and_return(True)
- flexmock(module.os.path).should_receive('exists').and_return(True)
- flexmock(module).should_receive('any_parent_directories').and_return(False)
- assert module.collect_special_file_paths(
- dry_run=True,
- create_command=('borg', 'create'),
- config={},
- local_path=None,
- working_directory=None,
- borgmatic_runtime_directory='/run/borgmatic',
- ) == ('/foo', '/bar', '/baz')
- def test_collect_special_file_paths_excludes_non_special_files():
- flexmock(module.flags).should_receive('omit_flag').replace_with(
- lambda arguments, flag: arguments
- )
- flexmock(module.flags).should_receive('omit_flag_and_value').replace_with(
- lambda arguments, flag: arguments
- )
- flexmock(module.environment).should_receive('make_environment').and_return(None)
- flexmock(module).should_receive('execute_command_and_capture_output').and_return(
- '+ /foo\n+ /bar\n+ /baz'
- )
- flexmock(module).should_receive('special_file').and_return(True).and_return(False).and_return(
- True
- )
- flexmock(module.os.path).should_receive('exists').and_return(False)
- flexmock(module).should_receive('any_parent_directories').never()
- assert module.collect_special_file_paths(
- dry_run=False,
- create_command=('borg', 'create'),
- config={},
- local_path=None,
- working_directory=None,
- borgmatic_runtime_directory='/run/borgmatic',
- ) == ('/foo', '/baz')
- DEFAULT_ARCHIVE_NAME = '{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}' # noqa: FS003
- REPO_ARCHIVE = (f'repo::{DEFAULT_ARCHIVE_NAME}',)
- def test_make_base_create_produces_borg_command():
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('write_patterns_file').and_return(None)
- flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
- flexmock(module.feature).should_receive('available').and_return(True)
- flexmock(module).should_receive('make_exclude_flags').and_return(())
- flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
- (f'repo::{DEFAULT_ARCHIVE_NAME}',)
- )
- (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- assert create_flags == ('borg', 'create')
- assert create_positional_arguments == REPO_ARCHIVE
- assert not pattern_file
- def test_make_base_create_command_includes_patterns_file_in_borg_command():
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- mock_pattern_file = flexmock(name='/tmp/patterns')
- flexmock(module).should_receive('write_patterns_file').and_return(mock_pattern_file).and_return(
- None
- )
- flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
- flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
- '{hostname}'
- )
- flexmock(module.feature).should_receive('available').and_return(True)
- pattern_flags = ('--patterns-from', mock_pattern_file.name)
- flexmock(module).should_receive('make_exclude_flags').and_return(())
- flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
- (f'repo::{DEFAULT_ARCHIVE_NAME}',)
- )
- (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'patterns': ['pattern'],
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- assert create_flags == ('borg', 'create') + pattern_flags
- assert create_positional_arguments == (f'repo::{DEFAULT_ARCHIVE_NAME}',)
- assert pattern_file == mock_pattern_file
- def test_make_base_create_command_with_store_config_false_omits_config_files():
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('write_patterns_file').and_return(None)
- flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
- flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
- '{hostname}'
- )
- flexmock(module.feature).should_receive('available').and_return(True)
- flexmock(module).should_receive('make_exclude_flags').and_return(())
- flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
- (f'repo::{DEFAULT_ARCHIVE_NAME}',)
- )
- (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'store_config_files': False,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- assert create_flags == ('borg', 'create')
- assert create_positional_arguments == REPO_ARCHIVE
- assert not pattern_file
- @pytest.mark.parametrize(
- 'option_name,option_value,feature_available,option_flags',
- (
- ('checkpoint_interval', 600, True, ('--checkpoint-interval', '600')),
- ('checkpoint_volume', 1024, True, ('--checkpoint-volume', '1024')),
- ('chunker_params', '1,2,3,4', True, ('--chunker-params', '1,2,3,4')),
- ('compression', 'rle', True, ('--compression', 'rle')),
- ('one_file_system', True, True, ('--one-file-system',)),
- ('upload_rate_limit', 100, True, ('--upload-ratelimit', '100')),
- ('upload_rate_limit', 100, False, ('--remote-ratelimit', '100')),
- ('upload_buffer_size', 160, True, ('--upload-buffer', '160')),
- ('numeric_ids', True, True, ('--numeric-ids',)),
- ('numeric_ids', True, False, ('--numeric-owner',)),
- ('read_special', True, True, ('--read-special',)),
- ('ctime', True, True, ()),
- ('ctime', False, True, ('--noctime',)),
- ('birthtime', True, True, ()),
- ('birthtime', False, True, ('--nobirthtime',)),
- ('atime', True, True, ('--atime',)),
- ('atime', True, False, ()),
- ('atime', False, True, ()),
- ('atime', False, False, ('--noatime',)),
- ('flags', True, True, ()),
- ('flags', True, False, ()),
- ('flags', False, True, ('--noflags',)),
- ('flags', False, False, ('--nobsdflags',)),
- ('files_cache', 'ctime,size', True, ('--files-cache', 'ctime,size')),
- ('umask', 740, True, ('--umask', '740')),
- ('lock_wait', 5, True, ('--lock-wait', '5')),
- ),
- )
- def test_make_base_create_command_includes_configuration_option_as_command_flag(
- option_name, option_value, feature_available, option_flags
- ):
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('write_patterns_file').and_return(None)
- flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
- flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
- '{hostname}'
- )
- flexmock(module.feature).should_receive('available').and_return(feature_available)
- flexmock(module).should_receive('make_exclude_flags').and_return(())
- flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
- (f'repo::{DEFAULT_ARCHIVE_NAME}',)
- )
- (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- option_name: option_value,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- assert create_flags == ('borg', 'create') + option_flags
- assert create_positional_arguments == REPO_ARCHIVE
- assert not pattern_file
- def test_make_base_create_command_includes_dry_run_in_borg_command():
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('write_patterns_file').and_return(None)
- flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
- flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
- '{hostname}'
- )
- flexmock(module.feature).should_receive('available').and_return(True)
- flexmock(module).should_receive('make_exclude_flags').and_return(())
- flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
- (f'repo::{DEFAULT_ARCHIVE_NAME}',)
- )
- (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
- dry_run=True,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': ['exclude'],
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- assert create_flags == ('borg', 'create', '--dry-run')
- assert create_positional_arguments == REPO_ARCHIVE
- assert not pattern_file
- def test_make_base_create_command_includes_local_path_in_borg_command():
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('write_patterns_file').and_return(None)
- flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
- flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
- '{hostname}'
- )
- flexmock(module.feature).should_receive('available').and_return(True)
- flexmock(module).should_receive('make_exclude_flags').and_return(())
- flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
- (f'repo::{DEFAULT_ARCHIVE_NAME}',)
- )
- (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- local_path='borg1',
- )
- assert create_flags == ('borg1', 'create')
- assert create_positional_arguments == REPO_ARCHIVE
- assert not pattern_file
- def test_make_base_create_command_includes_remote_path_in_borg_command():
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('write_patterns_file').and_return(None)
- flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
- flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
- '{hostname}'
- )
- flexmock(module.feature).should_receive('available').and_return(True)
- flexmock(module).should_receive('make_exclude_flags').and_return(())
- flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
- (f'repo::{DEFAULT_ARCHIVE_NAME}',)
- )
- (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- remote_path='borg1',
- )
- assert create_flags == ('borg', 'create', '--remote-path', 'borg1')
- assert create_positional_arguments == REPO_ARCHIVE
- assert not pattern_file
- def test_make_base_create_command_includes_log_json_in_borg_command():
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('write_patterns_file').and_return(None)
- flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
- flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
- '{hostname}'
- )
- flexmock(module.feature).should_receive('available').and_return(True)
- flexmock(module).should_receive('make_exclude_flags').and_return(())
- flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
- (f'repo::{DEFAULT_ARCHIVE_NAME}',)
- )
- (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'log_json': True,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- assert create_flags == ('borg', 'create', '--log-json')
- assert create_positional_arguments == REPO_ARCHIVE
- assert not pattern_file
- def test_make_base_create_command_includes_list_flags_in_borg_command():
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('write_patterns_file').and_return(None)
- flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
- flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
- '{hostname}'
- )
- flexmock(module.feature).should_receive('available').and_return(True)
- flexmock(module).should_receive('make_exclude_flags').and_return(())
- flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
- (f'repo::{DEFAULT_ARCHIVE_NAME}',)
- )
- (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'list_details': True,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- assert create_flags == ('borg', 'create', '--list', '--filter', 'FOO')
- assert create_positional_arguments == REPO_ARCHIVE
- assert not pattern_file
- def test_make_base_create_command_with_stream_processes_ignores_read_special_false_and_excludes_special_files():
- patterns = [Pattern('foo'), Pattern('bar')]
- patterns_file = flexmock(name='patterns')
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('write_patterns_file').with_args(
- patterns, '/run/borgmatic'
- ).and_return(patterns_file)
- flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
- flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
- '{hostname}'
- )
- flexmock(module.feature).should_receive('available').and_return(True)
- flexmock(module).should_receive('make_exclude_flags').and_return(())
- flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
- (f'repo::{DEFAULT_ARCHIVE_NAME}',)
- )
- flexmock(module.logger).should_receive('warning').twice()
- flexmock(module.environment).should_receive('make_environment')
- flexmock(module).should_receive('collect_special_file_paths').and_return(('/dev/null',)).once()
- flexmock(module).should_receive('write_patterns_file').with_args(
- (
- Pattern(
- '/dev/null',
- Pattern_type.NO_RECURSE,
- Pattern_style.FNMATCH,
- source=Pattern_source.INTERNAL,
- ),
- ),
- '/run/borgmatic',
- patterns_file=patterns_file,
- ).and_return(patterns_file).once()
- flexmock(module).should_receive('make_exclude_flags').and_return(())
- (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'read_special': False,
- },
- patterns=patterns,
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- stream_processes=flexmock(),
- )
- assert create_flags == ('borg', 'create', '--patterns-from', 'patterns', '--read-special')
- assert create_positional_arguments == REPO_ARCHIVE
- assert pattern_file
- def test_make_base_create_command_without_patterns_and_with_stream_processes_ignores_read_special_false_and_excludes_special_files():
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('write_patterns_file').with_args(
- [], '/run/borgmatic'
- ).and_return(None)
- flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
- flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
- '{hostname}'
- )
- flexmock(module.feature).should_receive('available').and_return(True)
- flexmock(module).should_receive('make_exclude_flags').and_return(())
- flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
- (f'repo::{DEFAULT_ARCHIVE_NAME}',)
- )
- flexmock(module.logger).should_receive('warning').twice()
- flexmock(module.environment).should_receive('make_environment')
- flexmock(module).should_receive('collect_special_file_paths').and_return(('/dev/null',)).once()
- flexmock(module).should_receive('write_patterns_file').with_args(
- (
- Pattern(
- '/dev/null',
- Pattern_type.NO_RECURSE,
- Pattern_style.FNMATCH,
- source=Pattern_source.INTERNAL,
- ),
- ),
- '/run/borgmatic',
- patterns_file=None,
- ).and_return(flexmock(name='patterns')).once()
- flexmock(module).should_receive('make_exclude_flags').and_return(())
- (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': [],
- 'repositories': ['repo'],
- 'read_special': False,
- },
- patterns=[],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- stream_processes=flexmock(),
- )
- assert create_flags == ('borg', 'create', '--read-special', '--patterns-from', 'patterns')
- assert create_positional_arguments == REPO_ARCHIVE
- assert pattern_file
- def test_make_base_create_command_with_stream_processes_and_read_special_true_skips_special_files_excludes():
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('write_patterns_file').and_return(None)
- flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
- flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
- '{hostname}'
- )
- flexmock(module.feature).should_receive('available').and_return(True)
- flexmock(module).should_receive('make_exclude_flags').and_return(())
- flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
- (f'repo::{DEFAULT_ARCHIVE_NAME}',)
- )
- flexmock(module.logger).should_receive('warning').never()
- flexmock(module).should_receive('collect_special_file_paths').never()
- (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'read_special': True,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- stream_processes=flexmock(),
- )
- assert create_flags == ('borg', 'create', '--read-special')
- assert create_positional_arguments == REPO_ARCHIVE
- assert not pattern_file
- def test_make_base_create_command_includes_archive_name_format_in_borg_command():
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('write_patterns_file').and_return(None)
- flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
- flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
- '{hostname}'
- )
- flexmock(module.feature).should_receive('available').and_return(True)
- flexmock(module).should_receive('make_exclude_flags').and_return(())
- flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
- ('repo::ARCHIVE_NAME',)
- )
- (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'archive_name_format': 'ARCHIVE_NAME',
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- assert create_flags == ('borg', 'create')
- assert create_positional_arguments == ('repo::ARCHIVE_NAME',)
- assert not pattern_file
- def test_make_base_create_command_includes_default_archive_name_format_in_borg_command():
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('write_patterns_file').and_return(None)
- flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
- flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
- '{hostname}'
- )
- flexmock(module.feature).should_receive('available').and_return(True)
- flexmock(module).should_receive('make_exclude_flags').and_return(())
- flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
- ('repo::{hostname}',)
- )
- (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- assert create_flags == ('borg', 'create')
- assert create_positional_arguments == ('repo::{hostname}',)
- assert not pattern_file
- def test_make_base_create_command_includes_archive_name_format_with_placeholders_in_borg_command():
- repository_archive_pattern = 'repo::Documents_{hostname}-{now}' # noqa: FS003
- flexmock(module).should_receive('write_patterns_file').and_return(None)
- flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
- flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
- '{hostname}'
- )
- flexmock(module.feature).should_receive('available').and_return(True)
- flexmock(module).should_receive('make_exclude_flags').and_return(())
- flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
- (repository_archive_pattern,)
- )
- (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'archive_name_format': 'Documents_{hostname}-{now}', # noqa: FS003
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- assert create_flags == ('borg', 'create')
- assert create_positional_arguments == (repository_archive_pattern,)
- assert not pattern_file
- def test_make_base_create_command_includes_repository_and_archive_name_format_with_placeholders_in_borg_command():
- repository_archive_pattern = '{fqdn}::Documents_{hostname}-{now}' # noqa: FS003
- flexmock(module).should_receive('write_patterns_file').and_return(None)
- flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
- flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
- '{hostname}'
- )
- flexmock(module.feature).should_receive('available').and_return(True)
- flexmock(module).should_receive('make_exclude_flags').and_return(())
- flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
- (repository_archive_pattern,)
- )
- (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
- dry_run=False,
- repository_path='{fqdn}', # noqa: FS003
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['{fqdn}'], # noqa: FS003
- 'archive_name_format': 'Documents_{hostname}-{now}', # noqa: FS003
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- assert create_flags == ('borg', 'create')
- assert create_positional_arguments == (repository_archive_pattern,)
- assert not pattern_file
- def test_make_base_create_command_includes_extra_borg_options_in_borg_command():
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('write_patterns_file').and_return(None)
- flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
- flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
- '{hostname}'
- )
- flexmock(module.feature).should_receive('available').and_return(True)
- flexmock(module).should_receive('make_exclude_flags').and_return(())
- flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
- (f'repo::{DEFAULT_ARCHIVE_NAME}',)
- )
- (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'extra_borg_options': {'create': '--extra --options'},
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- assert create_flags == ('borg', 'create', '--extra', '--options')
- assert create_positional_arguments == REPO_ARCHIVE
- assert not pattern_file
- def test_make_base_create_command_with_non_existent_directory_and_source_directories_must_exist_raises():
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('check_all_root_patterns_exist').and_raise(ValueError)
- with pytest.raises(ValueError):
- module.make_base_create_command(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'source_directories_must_exist': True,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- def test_create_archive_calls_borg_with_flags():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
- flexmock(module).should_receive('make_base_create_command').and_return(
- (('borg', 'create'), REPO_ARCHIVE, flexmock())
- )
- flexmock(module.environment).should_receive('make_environment')
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create') + REPO_ARCHIVE,
- output_log_level=logging.INFO,
- output_file=None,
- borg_local_path='borg',
- borg_exit_codes=None,
- working_directory=None,
- environment=None,
- )
- module.create_archive(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/borgmatic/run',
- )
- def test_create_archive_calls_borg_with_environment():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
- flexmock(module).should_receive('make_base_create_command').and_return(
- (('borg', 'create'), REPO_ARCHIVE, flexmock())
- )
- environment = {'BORG_THINGY': 'YUP'}
- flexmock(module.environment).should_receive('make_environment').and_return(environment)
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create') + REPO_ARCHIVE,
- output_log_level=logging.INFO,
- output_file=None,
- borg_local_path='borg',
- borg_exit_codes=None,
- working_directory=None,
- environment=environment,
- )
- module.create_archive(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/borgmatic/run',
- )
- def test_create_archive_with_log_info_calls_borg_with_info_flag():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
- flexmock(module).should_receive('make_base_create_command').and_return(
- (('borg', 'create'), REPO_ARCHIVE, flexmock())
- )
- flexmock(module.environment).should_receive('make_environment')
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--info') + REPO_ARCHIVE,
- output_log_level=logging.INFO,
- output_file=None,
- borg_local_path='borg',
- borg_exit_codes=None,
- working_directory=None,
- environment=None,
- )
- insert_logging_mock(logging.INFO)
- module.create_archive(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/borgmatic/run',
- )
- def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
- flexmock(module).should_receive('make_base_create_command').and_return(
- (('borg', 'create'), REPO_ARCHIVE, flexmock())
- )
- flexmock(module.environment).should_receive('make_environment')
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('execute_command_and_capture_output').with_args(
- ('borg', 'create', '--json') + REPO_ARCHIVE,
- working_directory=None,
- environment=None,
- borg_local_path='borg',
- borg_exit_codes=None,
- )
- insert_logging_mock(logging.INFO)
- module.create_archive(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/borgmatic/run',
- json=True,
- )
- def test_create_archive_with_log_debug_calls_borg_with_debug_flag():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
- flexmock(module).should_receive('make_base_create_command').and_return(
- (('borg', 'create'), REPO_ARCHIVE, flexmock())
- )
- flexmock(module.environment).should_receive('make_environment')
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--debug', '--show-rc') + REPO_ARCHIVE,
- output_log_level=logging.INFO,
- output_file=None,
- borg_local_path='borg',
- borg_exit_codes=None,
- working_directory=None,
- environment=None,
- )
- insert_logging_mock(logging.DEBUG)
- module.create_archive(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/borgmatic/run',
- )
- def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
- flexmock(module).should_receive('make_base_create_command').and_return(
- (('borg', 'create'), REPO_ARCHIVE, flexmock())
- )
- flexmock(module.environment).should_receive('make_environment')
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('execute_command_and_capture_output').with_args(
- ('borg', 'create', '--json') + REPO_ARCHIVE,
- working_directory=None,
- environment=None,
- borg_local_path='borg',
- borg_exit_codes=None,
- )
- insert_logging_mock(logging.DEBUG)
- module.create_archive(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/borgmatic/run',
- json=True,
- )
- def test_create_archive_with_stats_and_dry_run_calls_borg_without_stats():
- # --dry-run and --stats are mutually exclusive, see:
- # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
- flexmock(module).should_receive('make_base_create_command').and_return(
- (('borg', 'create', '--dry-run'), REPO_ARCHIVE, flexmock())
- )
- flexmock(module.environment).should_receive('make_environment')
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--dry-run', '--info') + REPO_ARCHIVE,
- output_log_level=logging.INFO,
- output_file=None,
- borg_local_path='borg',
- borg_exit_codes=None,
- working_directory=None,
- environment=None,
- )
- insert_logging_mock(logging.INFO)
- module.create_archive(
- dry_run=True,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/borgmatic/run',
- )
- def test_create_archive_with_working_directory_calls_borg_with_working_directory():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
- flexmock(module).should_receive('make_base_create_command').and_return(
- (('borg', 'create'), REPO_ARCHIVE, flexmock())
- )
- flexmock(module.environment).should_receive('make_environment')
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
- '/working/dir'
- )
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create') + REPO_ARCHIVE,
- output_log_level=logging.INFO,
- output_file=None,
- borg_local_path='borg',
- borg_exit_codes=None,
- working_directory='/working/dir',
- environment=None,
- )
- module.create_archive(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'working_directory': '/working/dir',
- 'exclude_patterns': None,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/borgmatic/run',
- )
- def test_create_archive_with_exit_codes_calls_borg_using_them():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
- flexmock(module).should_receive('make_base_create_command').and_return(
- (('borg', 'create'), REPO_ARCHIVE, flexmock())
- )
- flexmock(module.environment).should_receive('make_environment')
- borg_exit_codes = flexmock()
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create') + REPO_ARCHIVE,
- output_log_level=logging.INFO,
- output_file=None,
- borg_local_path='borg',
- borg_exit_codes=borg_exit_codes,
- working_directory=None,
- environment=None,
- )
- module.create_archive(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- 'borg_exit_codes': borg_exit_codes,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/borgmatic/run',
- )
- def test_create_archive_with_stats_calls_borg_with_stats_flag_and_answer_output_log_level():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
- flexmock(module).should_receive('make_base_create_command').and_return(
- (('borg', 'create'), REPO_ARCHIVE, flexmock())
- )
- flexmock(module.environment).should_receive('make_environment')
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--stats') + REPO_ARCHIVE,
- output_log_level=module.borgmatic.logger.ANSWER,
- output_file=None,
- borg_local_path='borg',
- borg_exit_codes=None,
- working_directory=None,
- environment=None,
- )
- module.create_archive(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- 'statistics': True,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/borgmatic/run',
- )
- def test_create_archive_with_files_calls_borg_with_answer_output_log_level():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
- flexmock(module).should_receive('make_base_create_command').and_return(
- (
- ('borg', 'create', '--list', '--filter', 'FOO'),
- REPO_ARCHIVE,
- flexmock(),
- )
- )
- flexmock(module.environment).should_receive('make_environment')
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--list', '--filter', 'FOO') + REPO_ARCHIVE,
- output_log_level=module.borgmatic.logger.ANSWER,
- output_file=None,
- borg_local_path='borg',
- borg_exit_codes=None,
- working_directory=None,
- environment=None,
- )
- module.create_archive(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- 'list_details': True,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/borgmatic/run',
- )
- def test_create_archive_with_progress_and_log_info_calls_borg_with_progress_flag_and_no_list():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
- flexmock(module).should_receive('make_base_create_command').and_return(
- (('borg', 'create'), REPO_ARCHIVE, flexmock())
- )
- flexmock(module.environment).should_receive('make_environment')
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--info', '--progress') + REPO_ARCHIVE,
- output_log_level=logging.INFO,
- output_file=module.DO_NOT_CAPTURE,
- borg_local_path='borg',
- borg_exit_codes=None,
- working_directory=None,
- environment=None,
- )
- insert_logging_mock(logging.INFO)
- module.create_archive(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- 'progress': True,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/borgmatic/run',
- )
- def test_create_archive_with_progress_calls_borg_with_progress_flag():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
- flexmock(module).should_receive('make_base_create_command').and_return(
- (('borg', 'create'), REPO_ARCHIVE, flexmock())
- )
- flexmock(module.environment).should_receive('make_environment')
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create', '--progress') + REPO_ARCHIVE,
- output_log_level=logging.INFO,
- output_file=module.DO_NOT_CAPTURE,
- borg_local_path='borg',
- borg_exit_codes=None,
- working_directory=None,
- environment=None,
- )
- module.create_archive(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- 'progress': True,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/borgmatic/run',
- )
- def test_create_archive_with_progress_and_stream_processes_calls_borg_with_progress_flag():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
- processes = flexmock()
- flexmock(module).should_receive('make_base_create_command').and_return(
- (
- ('borg', 'create', '--read-special'),
- REPO_ARCHIVE,
- flexmock(),
- )
- )
- flexmock(module.environment).should_receive('make_environment')
- create_command = (
- 'borg',
- 'create',
- '--read-special',
- '--progress',
- ) + REPO_ARCHIVE
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('execute_command_with_processes').with_args(
- create_command + ('--dry-run', '--list'),
- processes=processes,
- output_log_level=logging.INFO,
- output_file=module.DO_NOT_CAPTURE,
- borg_local_path='borg',
- borg_exit_codes=None,
- working_directory=None,
- environment=None,
- )
- flexmock(module).should_receive('execute_command_with_processes').with_args(
- create_command,
- processes=processes,
- output_log_level=logging.INFO,
- output_file=module.DO_NOT_CAPTURE,
- borg_local_path='borg',
- borg_exit_codes=None,
- working_directory=None,
- environment=None,
- )
- module.create_archive(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- 'progress': True,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/borgmatic/run',
- stream_processes=processes,
- )
- def test_create_archive_with_json_calls_borg_with_json_flag():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
- flexmock(module).should_receive('make_base_create_command').and_return(
- (('borg', 'create'), REPO_ARCHIVE, flexmock())
- )
- flexmock(module.environment).should_receive('make_environment')
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('execute_command_and_capture_output').with_args(
- ('borg', 'create', '--json') + REPO_ARCHIVE,
- working_directory=None,
- environment=None,
- borg_local_path='borg',
- borg_exit_codes=None,
- ).and_return('[]')
- json_output = module.create_archive(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/borgmatic/run',
- json=True,
- )
- assert json_output == '[]'
- def test_create_archive_with_stats_and_json_calls_borg_without_stats_flag():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
- flexmock(module).should_receive('make_base_create_command').and_return(
- (('borg', 'create'), REPO_ARCHIVE, flexmock())
- )
- flexmock(module.environment).should_receive('make_environment')
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
- flexmock(module).should_receive('execute_command_and_capture_output').with_args(
- ('borg', 'create', '--json') + REPO_ARCHIVE,
- working_directory=None,
- environment=None,
- borg_local_path='borg',
- borg_exit_codes=None,
- ).and_return('[]')
- json_output = module.create_archive(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo*'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/borgmatic/run',
- json=True,
- )
- assert json_output == '[]'
- def test_create_archive_calls_borg_with_working_directory():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
- flexmock(module).should_receive('make_base_create_command').and_return(
- (('borg', 'create'), REPO_ARCHIVE, flexmock())
- )
- flexmock(module.environment).should_receive('make_environment')
- flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
- '/working/dir'
- )
- flexmock(module).should_receive('execute_command').with_args(
- ('borg', 'create') + REPO_ARCHIVE,
- output_log_level=logging.INFO,
- output_file=None,
- borg_local_path='borg',
- borg_exit_codes=None,
- working_directory='/working/dir',
- environment=None,
- )
- module.create_archive(
- dry_run=False,
- repository_path='repo',
- config={
- 'source_directories': ['foo', 'bar'],
- 'repositories': ['repo'],
- 'exclude_patterns': None,
- 'working_directory': '/working/dir',
- },
- patterns=[Pattern('foo'), Pattern('bar')],
- local_borg_version='1.2.3',
- global_arguments=flexmock(),
- borgmatic_runtime_directory='/borgmatic/run',
- )
- def test_check_all_root_patterns_exist_with_existent_pattern_path_does_not_raise():
- flexmock(module.os.path).should_receive('exists').and_return(True)
- module.check_all_root_patterns_exist([Pattern('foo')])
- def test_check_all_root_patterns_exist_with_non_root_pattern_skips_existence_check():
- flexmock(module.os.path).should_receive('exists').never()
- module.check_all_root_patterns_exist([Pattern('foo', Pattern_type.INCLUDE)])
- def test_check_all_root_patterns_exist_with_non_existent_pattern_path_raises():
- flexmock(module.os.path).should_receive('exists').and_return(False)
- with pytest.raises(ValueError):
- module.check_all_root_patterns_exist([Pattern('foo')])
|