test_borgmatic.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import json
  2. import sys
  3. from flexmock import flexmock
  4. from borgmatic.commands import borgmatic as module
  5. def test_run_commands_handles_multiple_json_outputs_in_array():
  6. (
  7. flexmock(module)
  8. .should_receive('_run_commands_on_repository')
  9. .times(3)
  10. .replace_with(
  11. lambda args, consistency, json_results, local_path, location, remote_path, retention, storage, unexpanded_repository: json_results.append(
  12. {"whatever": unexpanded_repository}
  13. )
  14. )
  15. )
  16. (
  17. flexmock(sys.stdout)
  18. .should_call("write")
  19. .with_args(
  20. json.dumps(
  21. json.loads(
  22. '''
  23. [
  24. {"whatever": "fake_repo1"},
  25. {"whatever": "fake_repo2"},
  26. {"whatever": "fake_repo3"}
  27. ]
  28. '''
  29. )
  30. )
  31. )
  32. )
  33. module._run_commands(
  34. args=flexmock(json=True),
  35. consistency=None,
  36. local_path=None,
  37. location={'repositories': ['fake_repo1', 'fake_repo2', 'fake_repo3']},
  38. remote_path=None,
  39. retention=None,
  40. storage=None,
  41. )
  42. def test_collect_configuration_run_summary_logs_info_for_success():
  43. flexmock(module.validate).should_receive('parse_configuration').and_return({'test.yaml': {}})
  44. flexmock(module).should_receive('run_configuration')
  45. logs = tuple(module.collect_configuration_run_summary_logs(('test.yaml',), args=()))
  46. assert any(log for log in logs if log.levelno == module.logging.INFO)
  47. def test_collect_configuration_run_summary_logs_critical_for_parse_error():
  48. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  49. flexmock(module).should_receive('run_configuration')
  50. logs = tuple(module.collect_configuration_run_summary_logs(('test.yaml',), args=()))
  51. assert any(log for log in logs if log.levelno == module.logging.CRITICAL)
  52. def test_collect_configuration_run_summary_logs_critical_for_run_error():
  53. flexmock(module.validate).should_receive('parse_configuration').and_return({'test.yaml': {}})
  54. flexmock(module).should_receive('run_configuration').and_raise(ValueError)
  55. logs = tuple(module.collect_configuration_run_summary_logs(('test.yaml',), args=()))
  56. assert any(log for log in logs if log.levelno == module.logging.CRITICAL)
  57. def test_collect_configuration_run_summary_logs_critical_for_missing_configs():
  58. flexmock(module.validate).should_receive('parse_configuration').and_return({'test.yaml': {}})
  59. logs = tuple(
  60. module.collect_configuration_run_summary_logs(
  61. config_filenames=(), args=flexmock(config_paths=())
  62. )
  63. )
  64. assert any(log for log in logs if log.levelno == module.logging.CRITICAL)