test_list.py 5.0 KB

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