test_execute.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import logging
  2. import subprocess
  3. import pytest
  4. from flexmock import flexmock
  5. from borgmatic import execute as module
  6. def test_borg_command_identifies_borg_command():
  7. assert module.borg_command(['/usr/bin/borg1', 'info'])
  8. def test_borg_command_does_not_identify_other_command():
  9. assert not module.borg_command(['grep', 'stuff'])
  10. def test_execute_and_log_output_logs_each_line_separately():
  11. flexmock(module.logger).should_receive('log').with_args(logging.INFO, 'hi').once()
  12. flexmock(module.logger).should_receive('log').with_args(logging.INFO, 'there').once()
  13. flexmock(module).should_receive('borg_command').and_return(False)
  14. module.execute_and_log_output(
  15. ['echo', 'hi'], output_log_level=logging.INFO, shell=False, environment=None
  16. )
  17. module.execute_and_log_output(
  18. ['echo', 'there'], output_log_level=logging.INFO, shell=False, environment=None
  19. )
  20. def test_execute_and_log_output_with_borg_warning_does_not_raise():
  21. flexmock(module.logger).should_receive('log')
  22. flexmock(module).should_receive('borg_command').and_return(True)
  23. module.execute_and_log_output(
  24. ['false'], output_log_level=logging.INFO, shell=False, environment=None
  25. )
  26. def test_execute_and_log_output_includes_borg_error_output_in_exception():
  27. flexmock(module.logger).should_receive('log')
  28. flexmock(module).should_receive('borg_command').and_return(True)
  29. with pytest.raises(subprocess.CalledProcessError) as error:
  30. module.execute_and_log_output(
  31. ['grep'], output_log_level=logging.INFO, shell=False, environment=None
  32. )
  33. assert error.value.returncode == 2
  34. assert error.value.output
  35. def test_execute_and_log_output_with_non_borg_error_raises():
  36. flexmock(module.logger).should_receive('log')
  37. flexmock(module).should_receive('borg_command').and_return(False)
  38. with pytest.raises(subprocess.CalledProcessError) as error:
  39. module.execute_and_log_output(
  40. ['false'], output_log_level=logging.INFO, shell=False, environment=None
  41. )
  42. assert error.value.returncode == 1
  43. def test_execute_and_log_output_truncates_long_borg_error_output():
  44. flexmock(module).ERROR_OUTPUT_MAX_LINE_COUNT = 0
  45. flexmock(module.logger).should_receive('log')
  46. flexmock(module).should_receive('borg_command').and_return(False)
  47. with pytest.raises(subprocess.CalledProcessError) as error:
  48. module.execute_and_log_output(
  49. ['grep'], output_log_level=logging.INFO, shell=False, environment=None
  50. )
  51. assert error.value.returncode == 2
  52. assert error.value.output.startswith('...')
  53. def test_execute_and_log_output_with_no_output_logs_nothing():
  54. flexmock(module.logger).should_receive('log').never()
  55. flexmock(module).should_receive('borg_command').and_return(False)
  56. module.execute_and_log_output(
  57. ['true'], output_log_level=logging.INFO, shell=False, environment=None
  58. )
  59. def test_execute_and_log_output_with_error_exit_status_raises():
  60. flexmock(module.logger).should_receive('log')
  61. flexmock(module).should_receive('borg_command').and_return(False)
  62. with pytest.raises(subprocess.CalledProcessError):
  63. module.execute_and_log_output(
  64. ['grep'], output_log_level=logging.INFO, shell=False, environment=None
  65. )