2
0

test_execute.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic import execute as module
  5. def test_exit_code_indicates_error_with_borg_error_is_true():
  6. assert module.exit_code_indicates_error(('/usr/bin/borg1', 'init'), 2)
  7. def test_exit_code_indicates_error_with_borg_warning_is_false():
  8. assert not module.exit_code_indicates_error(('/usr/bin/borg1', 'init'), 1)
  9. def test_exit_code_indicates_error_with_borg_success_is_false():
  10. assert not module.exit_code_indicates_error(('/usr/bin/borg1', 'init'), 0)
  11. def test_exit_code_indicates_error_with_borg_error_and_error_on_warnings_is_true():
  12. assert module.exit_code_indicates_error(('/usr/bin/borg1', 'init'), 2, error_on_warnings=True)
  13. def test_exit_code_indicates_error_with_borg_warning_and_error_on_warnings_is_true():
  14. assert module.exit_code_indicates_error(('/usr/bin/borg1', 'init'), 1, error_on_warnings=True)
  15. def test_exit_code_indicates_error_with_borg_success_and_error_on_warnings_is_false():
  16. assert not module.exit_code_indicates_error(
  17. ('/usr/bin/borg1', 'init'), 0, error_on_warnings=True
  18. )
  19. def test_exit_code_indicates_error_with_non_borg_error_is_true():
  20. assert module.exit_code_indicates_error(('/usr/bin/command',), 2)
  21. def test_exit_code_indicates_error_with_non_borg_warning_is_true():
  22. assert module.exit_code_indicates_error(('/usr/bin/command',), 1)
  23. def test_exit_code_indicates_error_with_non_borg_success_is_false():
  24. assert not module.exit_code_indicates_error(('/usr/bin/command',), 0)
  25. def test_execute_command_calls_full_command():
  26. full_command = ['foo', 'bar']
  27. flexmock(module.os, environ={'a': 'b'})
  28. flexmock(module).should_receive('execute_and_log_output').with_args(
  29. full_command,
  30. output_log_level=logging.INFO,
  31. shell=False,
  32. environment=None,
  33. working_directory=None,
  34. error_on_warnings=False,
  35. ).once()
  36. output = module.execute_command(full_command)
  37. assert output is None
  38. def test_execute_command_calls_full_command_with_shell():
  39. full_command = ['foo', 'bar']
  40. flexmock(module.os, environ={'a': 'b'})
  41. flexmock(module).should_receive('execute_and_log_output').with_args(
  42. full_command,
  43. output_log_level=logging.INFO,
  44. shell=True,
  45. environment=None,
  46. working_directory=None,
  47. error_on_warnings=False,
  48. ).once()
  49. output = module.execute_command(full_command, shell=True)
  50. assert output is None
  51. def test_execute_command_calls_full_command_with_extra_environment():
  52. full_command = ['foo', 'bar']
  53. flexmock(module.os, environ={'a': 'b'})
  54. flexmock(module).should_receive('execute_and_log_output').with_args(
  55. full_command,
  56. output_log_level=logging.INFO,
  57. shell=False,
  58. environment={'a': 'b', 'c': 'd'},
  59. working_directory=None,
  60. error_on_warnings=False,
  61. ).once()
  62. output = module.execute_command(full_command, extra_environment={'c': 'd'})
  63. assert output is None
  64. def test_execute_command_calls_full_command_with_working_directory():
  65. full_command = ['foo', 'bar']
  66. flexmock(module.os, environ={'a': 'b'})
  67. flexmock(module).should_receive('execute_and_log_output').with_args(
  68. full_command,
  69. output_log_level=logging.INFO,
  70. shell=False,
  71. environment=None,
  72. working_directory='/working',
  73. error_on_warnings=False,
  74. ).once()
  75. output = module.execute_command(full_command, working_directory='/working')
  76. assert output is None
  77. def test_execute_command_calls_full_command_with_error_on_warnings():
  78. full_command = ['foo', 'bar']
  79. flexmock(module.os, environ={'a': 'b'})
  80. flexmock(module).should_receive('execute_and_log_output').with_args(
  81. full_command,
  82. output_log_level=logging.INFO,
  83. shell=False,
  84. environment=None,
  85. working_directory=None,
  86. error_on_warnings=True,
  87. ).once()
  88. output = module.execute_command(full_command, error_on_warnings=True)
  89. assert output is None
  90. def test_execute_command_captures_output():
  91. full_command = ['foo', 'bar']
  92. expected_output = '[]'
  93. flexmock(module.os, environ={'a': 'b'})
  94. flexmock(module.subprocess).should_receive('check_output').with_args(
  95. full_command, shell=False, env=None, cwd=None
  96. ).and_return(flexmock(decode=lambda: expected_output)).once()
  97. output = module.execute_command(full_command, output_log_level=None)
  98. assert output == expected_output
  99. def test_execute_command_captures_output_with_shell():
  100. full_command = ['foo', 'bar']
  101. expected_output = '[]'
  102. flexmock(module.os, environ={'a': 'b'})
  103. flexmock(module.subprocess).should_receive('check_output').with_args(
  104. full_command, shell=True, env=None, cwd=None
  105. ).and_return(flexmock(decode=lambda: expected_output)).once()
  106. output = module.execute_command(full_command, output_log_level=None, shell=True)
  107. assert output == expected_output
  108. def test_execute_command_captures_output_with_extra_environment():
  109. full_command = ['foo', 'bar']
  110. expected_output = '[]'
  111. flexmock(module.os, environ={'a': 'b'})
  112. flexmock(module.subprocess).should_receive('check_output').with_args(
  113. full_command, shell=False, env={'a': 'b', 'c': 'd'}, cwd=None
  114. ).and_return(flexmock(decode=lambda: expected_output)).once()
  115. output = module.execute_command(
  116. full_command, output_log_level=None, shell=False, extra_environment={'c': 'd'}
  117. )
  118. assert output == expected_output
  119. def test_execute_command_captures_output_with_working_directory():
  120. full_command = ['foo', 'bar']
  121. expected_output = '[]'
  122. flexmock(module.os, environ={'a': 'b'})
  123. flexmock(module.subprocess).should_receive('check_output').with_args(
  124. full_command, shell=False, env=None, cwd='/working'
  125. ).and_return(flexmock(decode=lambda: expected_output)).once()
  126. output = module.execute_command(
  127. full_command, output_log_level=None, shell=False, working_directory='/working'
  128. )
  129. assert output == expected_output
  130. def test_execute_command_without_capture_does_not_raise_on_success():
  131. flexmock(module.subprocess).should_receive('check_call').and_raise(
  132. module.subprocess.CalledProcessError(0, 'borg init')
  133. )
  134. module.execute_command_without_capture(('borg', 'init'))
  135. def test_execute_command_without_capture_does_not_raise_on_warning():
  136. flexmock(module).should_receive('exit_code_indicates_error').and_return(False)
  137. flexmock(module.subprocess).should_receive('check_call').and_raise(
  138. module.subprocess.CalledProcessError(1, 'borg init')
  139. )
  140. module.execute_command_without_capture(('borg', 'init'))
  141. def test_execute_command_without_capture_raises_on_error():
  142. flexmock(module).should_receive('exit_code_indicates_error').and_return(True)
  143. flexmock(module.subprocess).should_receive('check_call').and_raise(
  144. module.subprocess.CalledProcessError(2, 'borg init')
  145. )
  146. with pytest.raises(module.subprocess.CalledProcessError):
  147. module.execute_command_without_capture(('borg', 'init'))