test_export_tar.py 6.5 KB

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