test_info.py 4.7 KB

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