2
0

test_extract.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic.borg import extract as module
  4. from ..test_verbosity import insert_logging_mock
  5. def insert_execute_command_mock(command):
  6. flexmock(module).should_receive('execute_command').with_args(command).once()
  7. def insert_execute_command_output_mock(command, result):
  8. flexmock(module).should_receive('execute_command').with_args(
  9. command, output_log_level=None
  10. ).and_return(result).once()
  11. def test_extract_last_archive_dry_run_calls_borg_with_last_archive():
  12. insert_execute_command_output_mock(
  13. ('borg', 'list', '--short', 'repo'), result='archive1\narchive2\n'
  14. )
  15. insert_execute_command_mock(('borg', 'extract', '--dry-run', 'repo::archive2'))
  16. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  17. def test_extract_last_archive_dry_run_without_any_archives_should_not_raise():
  18. insert_execute_command_output_mock(('borg', 'list', '--short', 'repo'), result='\n')
  19. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  20. def test_extract_last_archive_dry_run_with_log_info_calls_borg_with_info_parameter():
  21. insert_execute_command_output_mock(
  22. ('borg', 'list', '--short', 'repo', '--info'), result='archive1\narchive2\n'
  23. )
  24. insert_execute_command_mock(('borg', 'extract', '--dry-run', 'repo::archive2', '--info'))
  25. insert_logging_mock(logging.INFO)
  26. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  27. def test_extract_last_archive_dry_run_with_log_debug_calls_borg_with_debug_parameter():
  28. insert_execute_command_output_mock(
  29. ('borg', 'list', '--short', 'repo', '--debug', '--show-rc'), result='archive1\narchive2\n'
  30. )
  31. insert_execute_command_mock(
  32. ('borg', 'extract', '--dry-run', 'repo::archive2', '--debug', '--show-rc', '--list')
  33. )
  34. insert_logging_mock(logging.DEBUG)
  35. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  36. def test_extract_last_archive_dry_run_calls_borg_via_local_path():
  37. insert_execute_command_output_mock(
  38. ('borg1', 'list', '--short', 'repo'), result='archive1\narchive2\n'
  39. )
  40. insert_execute_command_mock(('borg1', 'extract', '--dry-run', 'repo::archive2'))
  41. module.extract_last_archive_dry_run(repository='repo', lock_wait=None, local_path='borg1')
  42. def test_extract_last_archive_dry_run_calls_borg_with_remote_path_parameters():
  43. insert_execute_command_output_mock(
  44. ('borg', 'list', '--short', 'repo', '--remote-path', 'borg1'), result='archive1\narchive2\n'
  45. )
  46. insert_execute_command_mock(
  47. ('borg', 'extract', '--dry-run', 'repo::archive2', '--remote-path', 'borg1')
  48. )
  49. module.extract_last_archive_dry_run(repository='repo', lock_wait=None, remote_path='borg1')
  50. def test_extract_last_archive_dry_run_calls_borg_with_lock_wait_parameters():
  51. insert_execute_command_output_mock(
  52. ('borg', 'list', '--short', 'repo', '--lock-wait', '5'), result='archive1\narchive2\n'
  53. )
  54. insert_execute_command_mock(
  55. ('borg', 'extract', '--dry-run', 'repo::archive2', '--lock-wait', '5')
  56. )
  57. module.extract_last_archive_dry_run(repository='repo', lock_wait=5)
  58. def test_extract_archive_calls_borg_with_restore_path_parameters():
  59. insert_execute_command_mock(('borg', 'extract', 'repo::archive', 'path1', 'path2'))
  60. module.extract_archive(
  61. dry_run=False,
  62. repository='repo',
  63. archive='archive',
  64. restore_paths=['path1', 'path2'],
  65. location_config={},
  66. storage_config={},
  67. )
  68. def test_extract_archive_calls_borg_with_remote_path_parameters():
  69. insert_execute_command_mock(('borg', 'extract', 'repo::archive', '--remote-path', 'borg1'))
  70. module.extract_archive(
  71. dry_run=False,
  72. repository='repo',
  73. archive='archive',
  74. restore_paths=None,
  75. location_config={},
  76. storage_config={},
  77. remote_path='borg1',
  78. )
  79. def test_extract_archive_calls_borg_with_numeric_owner_parameter():
  80. insert_execute_command_mock(('borg', 'extract', 'repo::archive', '--numeric-owner'))
  81. module.extract_archive(
  82. dry_run=False,
  83. repository='repo',
  84. archive='archive',
  85. restore_paths=None,
  86. location_config={'numeric_owner': True},
  87. storage_config={},
  88. )
  89. def test_extract_archive_calls_borg_with_umask_parameters():
  90. insert_execute_command_mock(('borg', 'extract', 'repo::archive', '--umask', '0770'))
  91. module.extract_archive(
  92. dry_run=False,
  93. repository='repo',
  94. archive='archive',
  95. restore_paths=None,
  96. location_config={},
  97. storage_config={'umask': '0770'},
  98. )
  99. def test_extract_archive_calls_borg_with_lock_wait_parameters():
  100. insert_execute_command_mock(('borg', 'extract', 'repo::archive', '--lock-wait', '5'))
  101. module.extract_archive(
  102. dry_run=False,
  103. repository='repo',
  104. archive='archive',
  105. restore_paths=None,
  106. location_config={},
  107. storage_config={'lock_wait': '5'},
  108. )
  109. def test_extract_archive_with_log_info_calls_borg_with_info_parameter():
  110. insert_execute_command_mock(('borg', 'extract', 'repo::archive', '--info'))
  111. insert_logging_mock(logging.INFO)
  112. module.extract_archive(
  113. dry_run=False,
  114. repository='repo',
  115. archive='archive',
  116. restore_paths=None,
  117. location_config={},
  118. storage_config={},
  119. )
  120. def test_extract_archive_with_log_debug_calls_borg_with_debug_parameters():
  121. insert_execute_command_mock(
  122. ('borg', 'extract', 'repo::archive', '--debug', '--list', '--show-rc')
  123. )
  124. insert_logging_mock(logging.DEBUG)
  125. module.extract_archive(
  126. dry_run=False,
  127. repository='repo',
  128. archive='archive',
  129. restore_paths=None,
  130. location_config={},
  131. storage_config={},
  132. )
  133. def test_extract_archive_calls_borg_with_dry_run_parameter():
  134. insert_execute_command_mock(('borg', 'extract', 'repo::archive', '--dry-run'))
  135. module.extract_archive(
  136. dry_run=True,
  137. repository='repo',
  138. archive='archive',
  139. restore_paths=None,
  140. location_config={},
  141. storage_config={},
  142. )
  143. def test_extract_archive_calls_borg_with_progress_parameter():
  144. insert_execute_command_mock(('borg', 'extract', 'repo::archive', '--progress'))
  145. module.extract_archive(
  146. dry_run=False,
  147. repository='repo',
  148. archive='archive',
  149. restore_paths=None,
  150. location_config={},
  151. storage_config={},
  152. progress=True,
  153. )