test_execute.py 8.0 KB

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