test_info.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.borg import info as module
  5. from ..test_verbosity import insert_logging_mock
  6. def test_display_archives_info_calls_borg_with_parameters():
  7. flexmock(module).should_receive('execute_command').with_args(
  8. ('borg', 'info', 'repo'), output_log_level=logging.WARNING, borg_local_path='borg'
  9. )
  10. module.display_archives_info(
  11. repository='repo', storage_config={}, info_arguments=flexmock(archive=None, json=False)
  12. )
  13. def test_display_archives_info_with_log_info_calls_borg_with_info_parameter():
  14. flexmock(module).should_receive('execute_command').with_args(
  15. ('borg', 'info', '--info', 'repo'), output_log_level=logging.WARNING, borg_local_path='borg'
  16. )
  17. insert_logging_mock(logging.INFO)
  18. module.display_archives_info(
  19. repository='repo', storage_config={}, info_arguments=flexmock(archive=None, json=False)
  20. )
  21. def test_display_archives_info_with_log_info_and_json_suppresses_most_borg_output():
  22. flexmock(module).should_receive('execute_command').with_args(
  23. ('borg', 'info', '--json', 'repo'), output_log_level=None, borg_local_path='borg'
  24. ).and_return('[]')
  25. insert_logging_mock(logging.INFO)
  26. json_output = module.display_archives_info(
  27. repository='repo', storage_config={}, info_arguments=flexmock(archive=None, json=True)
  28. )
  29. assert json_output == '[]'
  30. def test_display_archives_info_with_log_debug_calls_borg_with_debug_parameter():
  31. flexmock(module).should_receive('execute_command').with_args(
  32. ('borg', 'info', '--debug', '--show-rc', 'repo'),
  33. output_log_level=logging.WARNING,
  34. borg_local_path='borg',
  35. )
  36. insert_logging_mock(logging.DEBUG)
  37. module.display_archives_info(
  38. repository='repo', storage_config={}, info_arguments=flexmock(archive=None, json=False)
  39. )
  40. def test_display_archives_info_with_log_debug_and_json_suppresses_most_borg_output():
  41. flexmock(module).should_receive('execute_command').with_args(
  42. ('borg', 'info', '--json', 'repo'), output_log_level=None, borg_local_path='borg'
  43. ).and_return('[]')
  44. insert_logging_mock(logging.DEBUG)
  45. json_output = module.display_archives_info(
  46. repository='repo', storage_config={}, info_arguments=flexmock(archive=None, json=True)
  47. )
  48. assert json_output == '[]'
  49. def test_display_archives_info_with_json_calls_borg_with_json_parameter():
  50. flexmock(module).should_receive('execute_command').with_args(
  51. ('borg', 'info', '--json', 'repo'), output_log_level=None, borg_local_path='borg'
  52. ).and_return('[]')
  53. json_output = module.display_archives_info(
  54. repository='repo', storage_config={}, info_arguments=flexmock(archive=None, json=True)
  55. )
  56. assert json_output == '[]'
  57. def test_display_archives_info_with_archive_calls_borg_with_archive_parameter():
  58. flexmock(module).should_receive('execute_command').with_args(
  59. ('borg', 'info', 'repo::archive'), output_log_level=logging.WARNING, borg_local_path='borg'
  60. )
  61. module.display_archives_info(
  62. repository='repo', storage_config={}, info_arguments=flexmock(archive='archive', json=False)
  63. )
  64. def test_display_archives_info_with_local_path_calls_borg_via_local_path():
  65. flexmock(module).should_receive('execute_command').with_args(
  66. ('borg1', 'info', 'repo'), output_log_level=logging.WARNING, borg_local_path='borg1'
  67. )
  68. module.display_archives_info(
  69. repository='repo',
  70. storage_config={},
  71. info_arguments=flexmock(archive=None, json=False),
  72. local_path='borg1',
  73. )
  74. def test_display_archives_info_with_remote_path_calls_borg_with_remote_path_parameters():
  75. flexmock(module).should_receive('execute_command').with_args(
  76. ('borg', 'info', '--remote-path', 'borg1', 'repo'),
  77. output_log_level=logging.WARNING,
  78. borg_local_path='borg',
  79. )
  80. module.display_archives_info(
  81. repository='repo',
  82. storage_config={},
  83. info_arguments=flexmock(archive=None, json=False),
  84. remote_path='borg1',
  85. )
  86. def test_display_archives_info_with_lock_wait_calls_borg_with_lock_wait_parameters():
  87. storage_config = {'lock_wait': 5}
  88. flexmock(module).should_receive('execute_command').with_args(
  89. ('borg', 'info', '--lock-wait', '5', 'repo'),
  90. output_log_level=logging.WARNING,
  91. borg_local_path='borg',
  92. )
  93. module.display_archives_info(
  94. repository='repo',
  95. storage_config=storage_config,
  96. info_arguments=flexmock(archive=None, json=False),
  97. )
  98. @pytest.mark.parametrize('argument_name', ('prefix', 'glob_archives', 'sort_by', 'first', 'last'))
  99. def test_display_archives_info_passes_through_arguments_to_borg(argument_name):
  100. flexmock(module).should_receive('execute_command').with_args(
  101. ('borg', 'info', '--' + argument_name.replace('_', '-'), 'value', 'repo'),
  102. output_log_level=logging.WARNING,
  103. borg_local_path='borg',
  104. )
  105. module.display_archives_info(
  106. repository='repo',
  107. storage_config={},
  108. info_arguments=flexmock(archive=None, json=False, **{argument_name: 'value'}),
  109. )