test_execute.py 7.1 KB

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