test_execute.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic import execute as module
  5. def test_execute_command_calls_full_command():
  6. full_command = ['foo', 'bar']
  7. flexmock(module.os, environ={'a': 'b'})
  8. flexmock(module).should_receive('execute_and_log_output').with_args(
  9. full_command, output_log_level=logging.INFO, shell=False, environment=None
  10. ).once()
  11. output = module.execute_command(full_command)
  12. assert output is None
  13. def test_execute_command_calls_full_command_with_shell():
  14. full_command = ['foo', 'bar']
  15. flexmock(module.os, environ={'a': 'b'})
  16. flexmock(module).should_receive('execute_and_log_output').with_args(
  17. full_command, output_log_level=logging.INFO, shell=True, environment=None
  18. ).once()
  19. output = module.execute_command(full_command, shell=True)
  20. assert output is None
  21. def test_execute_command_calls_full_command_with_extra_environment():
  22. full_command = ['foo', 'bar']
  23. flexmock(module.os, environ={'a': 'b'})
  24. flexmock(module).should_receive('execute_and_log_output').with_args(
  25. full_command, output_log_level=logging.INFO, shell=False, environment={'a': 'b', 'c': 'd'}
  26. ).once()
  27. output = module.execute_command(full_command, extra_environment={'c': 'd'})
  28. assert output is None
  29. def test_execute_command_captures_output():
  30. full_command = ['foo', 'bar']
  31. expected_output = '[]'
  32. flexmock(module.os, environ={'a': 'b'})
  33. flexmock(module.subprocess).should_receive('check_output').with_args(
  34. full_command, shell=False, env=None
  35. ).and_return(flexmock(decode=lambda: expected_output)).once()
  36. output = module.execute_command(full_command, output_log_level=None)
  37. assert output == expected_output
  38. def test_execute_command_captures_output_with_shell():
  39. full_command = ['foo', 'bar']
  40. expected_output = '[]'
  41. flexmock(module.os, environ={'a': 'b'})
  42. flexmock(module.subprocess).should_receive('check_output').with_args(
  43. full_command, shell=True, env=None
  44. ).and_return(flexmock(decode=lambda: expected_output)).once()
  45. output = module.execute_command(full_command, output_log_level=None, shell=True)
  46. assert output == expected_output
  47. def test_execute_command_captures_output_with_extra_environment():
  48. full_command = ['foo', 'bar']
  49. expected_output = '[]'
  50. flexmock(module.os, environ={'a': 'b'})
  51. flexmock(module.subprocess).should_receive('check_output').with_args(
  52. full_command, shell=False, env={'a': 'b', 'c': 'd'}
  53. ).and_return(flexmock(decode=lambda: expected_output)).once()
  54. output = module.execute_command(
  55. full_command, output_log_level=None, shell=False, extra_environment={'c': 'd'}
  56. )
  57. assert output == expected_output
  58. def test_execute_command_without_capture_does_not_raise_on_success():
  59. flexmock(module.subprocess).should_receive('check_call').and_raise(
  60. module.subprocess.CalledProcessError(0, 'borg init')
  61. )
  62. module.execute_command_without_capture(('borg', 'init'))
  63. def test_execute_command_without_capture_does_not_raise_on_warning():
  64. flexmock(module.subprocess).should_receive('check_call').and_raise(
  65. module.subprocess.CalledProcessError(1, 'borg init')
  66. )
  67. module.execute_command_without_capture(('borg', 'init'))
  68. def test_execute_command_without_capture_raises_on_error():
  69. flexmock(module.subprocess).should_receive('check_call').and_raise(
  70. module.subprocess.CalledProcessError(2, 'borg init')
  71. )
  72. with pytest.raises(module.subprocess.CalledProcessError):
  73. module.execute_command_without_capture(('borg', 'init'))