2
0

test_list.py 5.9 KB

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