test_execute.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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_with_shell_error_raises():
  22. flexmock(module.logger).should_receive('log')
  23. with pytest.raises(subprocess.CalledProcessError) as error:
  24. module.execute_and_log_output(['false'], output_log_level=logging.INFO, shell=True)
  25. assert error.value.returncode == 1
  26. def test_execute_and_log_output_truncates_long_borg_error_output():
  27. flexmock(module).ERROR_OUTPUT_MAX_LINE_COUNT = 0
  28. flexmock(module.logger).should_receive('log')
  29. with pytest.raises(subprocess.CalledProcessError) as error:
  30. module.execute_and_log_output(['grep'], output_log_level=logging.INFO, shell=False)
  31. assert error.value.returncode == 2
  32. assert error.value.output.startswith('...')
  33. def test_execute_and_log_output_with_no_output_logs_nothing():
  34. flexmock(module.logger).should_receive('log').never()
  35. module.execute_and_log_output(['true'], output_log_level=logging.INFO, shell=False)
  36. def test_execute_and_log_output_with_error_exit_status_raises():
  37. flexmock(module.logger).should_receive('log')
  38. with pytest.raises(subprocess.CalledProcessError):
  39. module.execute_and_log_output(['grep'], output_log_level=logging.INFO, shell=False)