test_list.py 5.1 KB

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