test_execute.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import logging
  2. import subprocess
  3. import pytest
  4. from flexmock import flexmock
  5. from borgmatic import execute as module
  6. def test_execute_and_log_output_logs_each_line_separately():
  7. flexmock(module.logger).should_receive('log').with_args(logging.INFO, 'hi').once()
  8. flexmock(module.logger).should_receive('log').with_args(logging.INFO, 'there').once()
  9. module.execute_and_log_output(['echo', 'hi'], output_log_level=logging.INFO, shell=False)
  10. module.execute_and_log_output(['echo', 'there'], output_log_level=logging.INFO, shell=False)
  11. def test_execute_and_log_output_with_borg_warning_does_not_raise():
  12. flexmock(module.logger).should_receive('log')
  13. # Borg's exit code 1 is a warning, not an error.
  14. module.execute_and_log_output(['false'], output_log_level=logging.INFO, shell=False)
  15. def test_execute_and_log_output_includes_borg_error_output_in_exception():
  16. flexmock(module.logger).should_receive('log')
  17. with pytest.raises(subprocess.CalledProcessError) as error:
  18. module.execute_and_log_output(['grep'], output_log_level=logging.INFO, shell=False)
  19. assert error.value.returncode == 2
  20. assert error.value.output
  21. def test_execute_and_log_output_truncates_long_borg_error_output():
  22. flexmock(module).ERROR_OUTPUT_MAX_LINE_COUNT = 0
  23. flexmock(module.logger).should_receive('log')
  24. with pytest.raises(subprocess.CalledProcessError) as error:
  25. module.execute_and_log_output(['grep'], output_log_level=logging.INFO, shell=False)
  26. assert error.value.returncode == 2
  27. assert error.value.output.startswith('...')
  28. def test_execute_and_log_output_with_no_output_logs_nothing():
  29. flexmock(module.logger).should_receive('log').never()
  30. module.execute_and_log_output(['true'], output_log_level=logging.INFO, shell=False)
  31. def test_execute_and_log_output_with_error_exit_status_raises():
  32. flexmock(module.logger).should_receive('log')
  33. with pytest.raises(subprocess.CalledProcessError):
  34. module.execute_and_log_output(['grep'], output_log_level=logging.INFO, shell=False)