test_list.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.borg import list as module
  5. from ..test_verbosity import insert_logging_mock
  6. def test_list_archives_calls_borg_with_parameters():
  7. flexmock(module).should_receive('execute_command').with_args(
  8. ('borg', 'list', 'repo'), output_log_level=logging.WARNING, error_on_warnings=False
  9. )
  10. module.list_archives(
  11. repository='repo',
  12. storage_config={},
  13. list_arguments=flexmock(archive=None, json=False, successful=False),
  14. )
  15. def test_list_archives_with_log_info_calls_borg_with_info_parameter():
  16. flexmock(module).should_receive('execute_command').with_args(
  17. ('borg', 'list', '--info', 'repo'),
  18. output_log_level=logging.WARNING,
  19. error_on_warnings=False,
  20. )
  21. insert_logging_mock(logging.INFO)
  22. module.list_archives(
  23. repository='repo',
  24. storage_config={},
  25. list_arguments=flexmock(archive=None, json=False, successful=False),
  26. )
  27. def test_list_archives_with_log_info_and_json_suppresses_most_borg_output():
  28. flexmock(module).should_receive('execute_command').with_args(
  29. ('borg', 'list', '--json', 'repo'), output_log_level=None, error_on_warnings=False
  30. )
  31. insert_logging_mock(logging.INFO)
  32. module.list_archives(
  33. repository='repo',
  34. storage_config={},
  35. list_arguments=flexmock(archive=None, json=True, successful=False),
  36. )
  37. def test_list_archives_with_log_debug_calls_borg_with_debug_parameter():
  38. flexmock(module).should_receive('execute_command').with_args(
  39. ('borg', 'list', '--debug', '--show-rc', 'repo'),
  40. output_log_level=logging.WARNING,
  41. error_on_warnings=False,
  42. )
  43. insert_logging_mock(logging.DEBUG)
  44. module.list_archives(
  45. repository='repo',
  46. storage_config={},
  47. list_arguments=flexmock(archive=None, json=False, successful=False),
  48. )
  49. def test_list_archives_with_log_debug_and_json_suppresses_most_borg_output():
  50. flexmock(module).should_receive('execute_command').with_args(
  51. ('borg', 'list', '--json', 'repo'), output_log_level=None, error_on_warnings=False
  52. )
  53. insert_logging_mock(logging.DEBUG)
  54. module.list_archives(
  55. repository='repo',
  56. storage_config={},
  57. list_arguments=flexmock(archive=None, json=True, successful=False),
  58. )
  59. def test_list_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  60. storage_config = {'lock_wait': 5}
  61. flexmock(module).should_receive('execute_command').with_args(
  62. ('borg', 'list', '--lock-wait', '5', 'repo'),
  63. output_log_level=logging.WARNING,
  64. error_on_warnings=False,
  65. )
  66. module.list_archives(
  67. repository='repo',
  68. storage_config=storage_config,
  69. list_arguments=flexmock(archive=None, json=False, successful=False),
  70. )
  71. def test_list_archives_with_archive_calls_borg_with_archive_parameter():
  72. storage_config = {}
  73. flexmock(module).should_receive('execute_command').with_args(
  74. ('borg', 'list', 'repo::archive'), output_log_level=logging.WARNING, error_on_warnings=False
  75. )
  76. module.list_archives(
  77. repository='repo',
  78. storage_config=storage_config,
  79. list_arguments=flexmock(archive='archive', json=False, successful=False),
  80. )
  81. def test_list_archives_with_local_path_calls_borg_via_local_path():
  82. flexmock(module).should_receive('execute_command').with_args(
  83. ('borg1', 'list', 'repo'), output_log_level=logging.WARNING, error_on_warnings=False
  84. )
  85. module.list_archives(
  86. repository='repo',
  87. storage_config={},
  88. list_arguments=flexmock(archive=None, json=False, successful=False),
  89. local_path='borg1',
  90. )
  91. def test_list_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  92. flexmock(module).should_receive('execute_command').with_args(
  93. ('borg', 'list', '--remote-path', 'borg1', 'repo'),
  94. output_log_level=logging.WARNING,
  95. error_on_warnings=False,
  96. )
  97. module.list_archives(
  98. repository='repo',
  99. storage_config={},
  100. list_arguments=flexmock(archive=None, json=False, successful=False),
  101. remote_path='borg1',
  102. )
  103. def test_list_archives_with_short_calls_borg_with_short_parameter():
  104. flexmock(module).should_receive('execute_command').with_args(
  105. ('borg', 'list', '--short', 'repo'),
  106. output_log_level=logging.WARNING,
  107. error_on_warnings=False,
  108. ).and_return('[]')
  109. module.list_archives(
  110. repository='repo',
  111. storage_config={},
  112. list_arguments=flexmock(archive=None, json=False, successful=False, short=True),
  113. )
  114. @pytest.mark.parametrize(
  115. 'argument_name',
  116. (
  117. 'prefix',
  118. 'glob_archives',
  119. 'sort_by',
  120. 'first',
  121. 'last',
  122. 'exclude',
  123. 'exclude_from',
  124. 'pattern',
  125. 'patterns_from',
  126. ),
  127. )
  128. def test_list_archives_passes_through_arguments_to_borg(argument_name):
  129. flexmock(module).should_receive('execute_command').with_args(
  130. ('borg', 'list', '--' + argument_name.replace('_', '-'), 'value', 'repo'),
  131. output_log_level=logging.WARNING,
  132. error_on_warnings=False,
  133. ).and_return('[]')
  134. module.list_archives(
  135. repository='repo',
  136. storage_config={},
  137. list_arguments=flexmock(
  138. archive=None, json=False, successful=False, **{argument_name: 'value'}
  139. ),
  140. )
  141. def test_list_archives_with_successful_calls_borg_to_exclude_checkpoints():
  142. flexmock(module).should_receive('execute_command').with_args(
  143. ('borg', 'list', '--glob-archives', module.BORG_EXCLUDE_CHECKPOINTS_GLOB, 'repo'),
  144. output_log_level=logging.WARNING,
  145. error_on_warnings=False,
  146. ).and_return('[]')
  147. module.list_archives(
  148. repository='repo',
  149. storage_config={},
  150. list_arguments=flexmock(archive=None, json=False, successful=True),
  151. )
  152. def test_list_archives_with_json_calls_borg_with_json_parameter():
  153. flexmock(module).should_receive('execute_command').with_args(
  154. ('borg', 'list', '--json', 'repo'), output_log_level=None, error_on_warnings=False
  155. ).and_return('[]')
  156. json_output = module.list_archives(
  157. repository='repo',
  158. storage_config={},
  159. list_arguments=flexmock(archive=None, json=True, successful=False),
  160. )
  161. assert json_output == '[]'