test_export_tar.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic.borg import export_tar as module
  4. from ..test_verbosity import insert_logging_mock
  5. def insert_execute_command_mock(
  6. command, output_log_level=logging.INFO, borg_local_path='borg', capture=True
  7. ):
  8. flexmock(module.environment).should_receive('make_environment')
  9. flexmock(module).should_receive('execute_command').with_args(
  10. command,
  11. output_file=None if capture else module.DO_NOT_CAPTURE,
  12. output_log_level=output_log_level,
  13. borg_local_path=borg_local_path,
  14. extra_environment=None,
  15. ).once()
  16. def test_export_tar_archive_calls_borg_with_path_parameters():
  17. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  18. insert_execute_command_mock(
  19. ('borg', 'export-tar', 'repo::archive', 'test.tar', 'path1', 'path2')
  20. )
  21. module.export_tar_archive(
  22. dry_run=False,
  23. repository='repo',
  24. archive='archive',
  25. paths=['path1', 'path2'],
  26. destination_path='test.tar',
  27. storage_config={},
  28. )
  29. def test_export_tar_archive_calls_borg_with_local_path_parameters():
  30. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  31. insert_execute_command_mock(
  32. ('borg1', 'export-tar', 'repo::archive', 'test.tar'), borg_local_path='borg1'
  33. )
  34. module.export_tar_archive(
  35. dry_run=False,
  36. repository='repo',
  37. archive='archive',
  38. paths=None,
  39. destination_path='test.tar',
  40. storage_config={},
  41. local_path='borg1',
  42. )
  43. def test_export_tar_archive_calls_borg_with_remote_path_parameters():
  44. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  45. insert_execute_command_mock(
  46. ('borg', 'export-tar', '--remote-path', 'borg1', 'repo::archive', 'test.tar')
  47. )
  48. module.export_tar_archive(
  49. dry_run=False,
  50. repository='repo',
  51. archive='archive',
  52. paths=None,
  53. destination_path='test.tar',
  54. storage_config={},
  55. remote_path='borg1',
  56. )
  57. def test_export_tar_archive_calls_borg_with_umask_parameters():
  58. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  59. insert_execute_command_mock(
  60. ('borg', 'export-tar', '--umask', '0770', 'repo::archive', 'test.tar')
  61. )
  62. module.export_tar_archive(
  63. dry_run=False,
  64. repository='repo',
  65. archive='archive',
  66. paths=None,
  67. destination_path='test.tar',
  68. storage_config={'umask': '0770'},
  69. )
  70. def test_export_tar_archive_calls_borg_with_lock_wait_parameters():
  71. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  72. insert_execute_command_mock(
  73. ('borg', 'export-tar', '--lock-wait', '5', 'repo::archive', 'test.tar')
  74. )
  75. module.export_tar_archive(
  76. dry_run=False,
  77. repository='repo',
  78. archive='archive',
  79. paths=None,
  80. destination_path='test.tar',
  81. storage_config={'lock_wait': '5'},
  82. )
  83. def test_export_tar_archive_with_log_info_calls_borg_with_info_parameter():
  84. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  85. insert_execute_command_mock(('borg', 'export-tar', '--info', 'repo::archive', 'test.tar'))
  86. insert_logging_mock(logging.INFO)
  87. module.export_tar_archive(
  88. dry_run=False,
  89. repository='repo',
  90. archive='archive',
  91. paths=None,
  92. destination_path='test.tar',
  93. storage_config={},
  94. )
  95. def test_export_tar_archive_with_log_debug_calls_borg_with_debug_parameters():
  96. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  97. insert_execute_command_mock(
  98. ('borg', 'export-tar', '--debug', '--show-rc', 'repo::archive', 'test.tar')
  99. )
  100. insert_logging_mock(logging.DEBUG)
  101. module.export_tar_archive(
  102. dry_run=False,
  103. repository='repo',
  104. archive='archive',
  105. paths=None,
  106. destination_path='test.tar',
  107. storage_config={},
  108. )
  109. def test_export_tar_archive_calls_borg_with_dry_run_parameter():
  110. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  111. flexmock(module).should_receive('execute_command').never()
  112. module.export_tar_archive(
  113. dry_run=True,
  114. repository='repo',
  115. archive='archive',
  116. paths=None,
  117. destination_path='test.tar',
  118. storage_config={},
  119. )
  120. def test_export_tar_archive_calls_borg_with_tar_filter_parameters():
  121. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  122. insert_execute_command_mock(
  123. ('borg', 'export-tar', '--tar-filter', 'bzip2', 'repo::archive', 'test.tar')
  124. )
  125. module.export_tar_archive(
  126. dry_run=False,
  127. repository='repo',
  128. archive='archive',
  129. paths=None,
  130. destination_path='test.tar',
  131. storage_config={},
  132. tar_filter='bzip2',
  133. )
  134. def test_export_tar_archive_calls_borg_with_list_parameter():
  135. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  136. insert_execute_command_mock(
  137. ('borg', 'export-tar', '--list', 'repo::archive', 'test.tar'),
  138. output_log_level=logging.WARNING,
  139. )
  140. module.export_tar_archive(
  141. dry_run=False,
  142. repository='repo',
  143. archive='archive',
  144. paths=None,
  145. destination_path='test.tar',
  146. storage_config={},
  147. files=True,
  148. )
  149. def test_export_tar_archive_calls_borg_with_strip_components_parameter():
  150. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  151. insert_execute_command_mock(
  152. ('borg', 'export-tar', '--strip-components', '5', 'repo::archive', 'test.tar')
  153. )
  154. module.export_tar_archive(
  155. dry_run=False,
  156. repository='repo',
  157. archive='archive',
  158. paths=None,
  159. destination_path='test.tar',
  160. storage_config={},
  161. strip_components=5,
  162. )
  163. def test_export_tar_archive_skips_abspath_for_remote_repository_parameter():
  164. flexmock(module.os.path).should_receive('abspath').never()
  165. insert_execute_command_mock(('borg', 'export-tar', 'server:repo::archive', 'test.tar'))
  166. module.export_tar_archive(
  167. dry_run=False,
  168. repository='server:repo',
  169. archive='archive',
  170. paths=None,
  171. destination_path='test.tar',
  172. storage_config={},
  173. )
  174. def test_export_tar_archive_calls_borg_with_stdout_destination_path():
  175. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  176. insert_execute_command_mock(('borg', 'export-tar', 'repo::archive', '-'), capture=False)
  177. module.export_tar_archive(
  178. dry_run=False,
  179. repository='repo',
  180. archive='archive',
  181. paths=None,
  182. destination_path='-',
  183. storage_config={},
  184. )