2
0

test_execute.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import logging
  2. import subprocess
  3. import pytest
  4. from flexmock import flexmock
  5. from borgmatic import execute as module
  6. def test_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. flexmock(module).should_receive('exit_code_indicates_error').and_return(False)
  10. hi_process = subprocess.Popen(['echo', 'hi'], stdout=subprocess.PIPE)
  11. module.log_output(
  12. hi_process, hi_process.stdout, output_log_level=logging.INFO, error_on_warnings=False
  13. )
  14. there_process = subprocess.Popen(['echo', 'there'], stdout=subprocess.PIPE)
  15. module.log_output(
  16. there_process, there_process.stdout, output_log_level=logging.INFO, error_on_warnings=False
  17. )
  18. def test_log_output_includes_error_output_in_exception():
  19. flexmock(module.logger).should_receive('log')
  20. flexmock(module).should_receive('exit_code_indicates_error').and_return(True)
  21. process = subprocess.Popen(['grep'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  22. with pytest.raises(subprocess.CalledProcessError) as error:
  23. module.log_output(
  24. process, process.stdout, output_log_level=logging.INFO, error_on_warnings=False
  25. )
  26. assert error.value.returncode == 2
  27. assert error.value.output
  28. def test_log_output_truncates_long_error_output():
  29. flexmock(module).ERROR_OUTPUT_MAX_LINE_COUNT = 0
  30. flexmock(module.logger).should_receive('log')
  31. flexmock(module).should_receive('exit_code_indicates_error').and_return(True)
  32. process = subprocess.Popen(['grep'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  33. with pytest.raises(subprocess.CalledProcessError) as error:
  34. module.log_output(
  35. process, process.stdout, output_log_level=logging.INFO, error_on_warnings=False
  36. )
  37. assert error.value.returncode == 2
  38. assert error.value.output.startswith('...')
  39. def test_log_output_with_no_output_logs_nothing():
  40. flexmock(module.logger).should_receive('log').never()
  41. flexmock(module).should_receive('exit_code_indicates_error').and_return(False)
  42. process = subprocess.Popen(['true'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  43. module.log_output(
  44. process, process.stdout, output_log_level=logging.INFO, error_on_warnings=False
  45. )