test_execute.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. stdout=module.subprocess.PIPE,
  30. stderr=module.subprocess.STDOUT,
  31. shell=False,
  32. env=None,
  33. cwd=None,
  34. ).and_return(flexmock(stdout=None)).once()
  35. flexmock(module).should_receive('log_output')
  36. output = module.execute_command(full_command)
  37. assert output is None
  38. def test_execute_command_calls_full_command_with_output_file():
  39. full_command = ['foo', 'bar']
  40. output_file = flexmock()
  41. flexmock(module.os, environ={'a': 'b'})
  42. flexmock(module.subprocess).should_receive('Popen').with_args(
  43. full_command,
  44. stdout=output_file,
  45. stderr=module.subprocess.PIPE,
  46. shell=False,
  47. env=None,
  48. cwd=None,
  49. ).and_return(flexmock(stderr=None)).once()
  50. flexmock(module).should_receive('log_output')
  51. output = module.execute_command(full_command, output_file=output_file)
  52. assert output is None
  53. def test_execute_command_calls_full_command_with_shell():
  54. full_command = ['foo', 'bar']
  55. flexmock(module.os, environ={'a': 'b'})
  56. flexmock(module.subprocess).should_receive('Popen').with_args(
  57. full_command,
  58. stdout=module.subprocess.PIPE,
  59. stderr=module.subprocess.STDOUT,
  60. shell=True,
  61. env=None,
  62. cwd=None,
  63. ).and_return(flexmock(stdout=None)).once()
  64. flexmock(module).should_receive('log_output')
  65. output = module.execute_command(full_command, shell=True)
  66. assert output is None
  67. def test_execute_command_calls_full_command_with_extra_environment():
  68. full_command = ['foo', 'bar']
  69. flexmock(module.os, environ={'a': 'b'})
  70. flexmock(module.subprocess).should_receive('Popen').with_args(
  71. full_command,
  72. stdout=module.subprocess.PIPE,
  73. stderr=module.subprocess.STDOUT,
  74. shell=False,
  75. env={'a': 'b', 'c': 'd'},
  76. cwd=None,
  77. ).and_return(flexmock(stdout=None)).once()
  78. flexmock(module).should_receive('log_output')
  79. output = module.execute_command(full_command, extra_environment={'c': 'd'})
  80. assert output is None
  81. def test_execute_command_calls_full_command_with_working_directory():
  82. full_command = ['foo', 'bar']
  83. flexmock(module.os, environ={'a': 'b'})
  84. flexmock(module.subprocess).should_receive('Popen').with_args(
  85. full_command,
  86. stdout=module.subprocess.PIPE,
  87. stderr=module.subprocess.STDOUT,
  88. shell=False,
  89. env=None,
  90. cwd='/working',
  91. ).and_return(flexmock(stdout=None)).once()
  92. flexmock(module).should_receive('log_output')
  93. output = module.execute_command(full_command, working_directory='/working')
  94. assert output is None
  95. def test_execute_command_captures_output():
  96. full_command = ['foo', 'bar']
  97. expected_output = '[]'
  98. flexmock(module.os, environ={'a': 'b'})
  99. flexmock(module.subprocess).should_receive('check_output').with_args(
  100. full_command, shell=False, env=None, cwd=None
  101. ).and_return(flexmock(decode=lambda: expected_output)).once()
  102. output = module.execute_command(full_command, output_log_level=None)
  103. assert output == expected_output
  104. def test_execute_command_captures_output_with_shell():
  105. full_command = ['foo', 'bar']
  106. expected_output = '[]'
  107. flexmock(module.os, environ={'a': 'b'})
  108. flexmock(module.subprocess).should_receive('check_output').with_args(
  109. full_command, shell=True, env=None, cwd=None
  110. ).and_return(flexmock(decode=lambda: expected_output)).once()
  111. output = module.execute_command(full_command, output_log_level=None, shell=True)
  112. assert output == expected_output
  113. def test_execute_command_captures_output_with_extra_environment():
  114. full_command = ['foo', 'bar']
  115. expected_output = '[]'
  116. flexmock(module.os, environ={'a': 'b'})
  117. flexmock(module.subprocess).should_receive('check_output').with_args(
  118. full_command, shell=False, env={'a': 'b', 'c': 'd'}, cwd=None
  119. ).and_return(flexmock(decode=lambda: expected_output)).once()
  120. output = module.execute_command(
  121. full_command, output_log_level=None, shell=False, extra_environment={'c': 'd'}
  122. )
  123. assert output == expected_output
  124. def test_execute_command_captures_output_with_working_directory():
  125. full_command = ['foo', 'bar']
  126. expected_output = '[]'
  127. flexmock(module.os, environ={'a': 'b'})
  128. flexmock(module.subprocess).should_receive('check_output').with_args(
  129. full_command, shell=False, env=None, cwd='/working'
  130. ).and_return(flexmock(decode=lambda: expected_output)).once()
  131. output = module.execute_command(
  132. full_command, output_log_level=None, shell=False, working_directory='/working'
  133. )
  134. assert output == expected_output
  135. def test_execute_command_without_capture_does_not_raise_on_success():
  136. flexmock(module.subprocess).should_receive('check_call').and_raise(
  137. module.subprocess.CalledProcessError(0, 'borg init')
  138. )
  139. module.execute_command_without_capture(('borg', 'init'))
  140. def test_execute_command_without_capture_does_not_raise_on_warning():
  141. flexmock(module).should_receive('exit_code_indicates_error').and_return(False)
  142. flexmock(module.subprocess).should_receive('check_call').and_raise(
  143. module.subprocess.CalledProcessError(1, 'borg init')
  144. )
  145. module.execute_command_without_capture(('borg', 'init'))
  146. def test_execute_command_without_capture_raises_on_error():
  147. flexmock(module).should_receive('exit_code_indicates_error').and_return(True)
  148. flexmock(module.subprocess).should_receive('check_call').and_raise(
  149. module.subprocess.CalledProcessError(2, 'borg init')
  150. )
  151. with pytest.raises(module.subprocess.CalledProcessError):
  152. module.execute_command_without_capture(('borg', 'init'))