|
@@ -6,11 +6,54 @@ from flexmock import flexmock
|
|
|
from borgmatic import execute as module
|
|
|
|
|
|
|
|
|
+def test_exit_code_indicates_error_with_borg_error_is_true():
|
|
|
+ assert module.exit_code_indicates_error(('/usr/bin/borg1', 'init'), 2)
|
|
|
+
|
|
|
+
|
|
|
+def test_exit_code_indicates_error_with_borg_warning_is_false():
|
|
|
+ assert not module.exit_code_indicates_error(('/usr/bin/borg1', 'init'), 1)
|
|
|
+
|
|
|
+
|
|
|
+def test_exit_code_indicates_error_with_borg_success_is_false():
|
|
|
+ assert not module.exit_code_indicates_error(('/usr/bin/borg1', 'init'), 0)
|
|
|
+
|
|
|
+
|
|
|
+def test_exit_code_indicates_error_with_borg_error_and_error_on_warnings_is_true():
|
|
|
+ assert module.exit_code_indicates_error(('/usr/bin/borg1', 'init'), 2, error_on_warnings=True)
|
|
|
+
|
|
|
+
|
|
|
+def test_exit_code_indicates_error_with_borg_warning_and_error_on_warnings_is_true():
|
|
|
+ assert module.exit_code_indicates_error(('/usr/bin/borg1', 'init'), 1, error_on_warnings=True)
|
|
|
+
|
|
|
+
|
|
|
+def test_exit_code_indicates_error_with_borg_success_and_error_on_warnings_is_false():
|
|
|
+ assert not module.exit_code_indicates_error(
|
|
|
+ ('/usr/bin/borg1', 'init'), 0, error_on_warnings=True
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+def test_exit_code_indicates_error_with_non_borg_error_is_true():
|
|
|
+ assert module.exit_code_indicates_error(('/usr/bin/command',), 2)
|
|
|
+
|
|
|
+
|
|
|
+def test_exit_code_indicates_error_with_non_borg_warning_is_true():
|
|
|
+ assert module.exit_code_indicates_error(('/usr/bin/command',), 1)
|
|
|
+
|
|
|
+
|
|
|
+def test_exit_code_indicates_error_with_non_borg_success_is_false():
|
|
|
+ assert not module.exit_code_indicates_error(('/usr/bin/command',), 0)
|
|
|
+
|
|
|
+
|
|
|
def test_execute_command_calls_full_command():
|
|
|
full_command = ['foo', 'bar']
|
|
|
flexmock(module.os, environ={'a': 'b'})
|
|
|
flexmock(module).should_receive('execute_and_log_output').with_args(
|
|
|
- full_command, output_log_level=logging.INFO, shell=False, environment=None
|
|
|
+ full_command,
|
|
|
+ output_log_level=logging.INFO,
|
|
|
+ shell=False,
|
|
|
+ environment=None,
|
|
|
+ working_directory=None,
|
|
|
+ error_on_warnings=False,
|
|
|
).once()
|
|
|
|
|
|
output = module.execute_command(full_command)
|
|
@@ -22,7 +65,12 @@ def test_execute_command_calls_full_command_with_shell():
|
|
|
full_command = ['foo', 'bar']
|
|
|
flexmock(module.os, environ={'a': 'b'})
|
|
|
flexmock(module).should_receive('execute_and_log_output').with_args(
|
|
|
- full_command, output_log_level=logging.INFO, shell=True, environment=None
|
|
|
+ full_command,
|
|
|
+ output_log_level=logging.INFO,
|
|
|
+ shell=True,
|
|
|
+ environment=None,
|
|
|
+ working_directory=None,
|
|
|
+ error_on_warnings=False,
|
|
|
).once()
|
|
|
|
|
|
output = module.execute_command(full_command, shell=True)
|
|
@@ -34,7 +82,12 @@ def test_execute_command_calls_full_command_with_extra_environment():
|
|
|
full_command = ['foo', 'bar']
|
|
|
flexmock(module.os, environ={'a': 'b'})
|
|
|
flexmock(module).should_receive('execute_and_log_output').with_args(
|
|
|
- full_command, output_log_level=logging.INFO, shell=False, environment={'a': 'b', 'c': 'd'}
|
|
|
+ full_command,
|
|
|
+ output_log_level=logging.INFO,
|
|
|
+ shell=False,
|
|
|
+ environment={'a': 'b', 'c': 'd'},
|
|
|
+ working_directory=None,
|
|
|
+ error_on_warnings=False,
|
|
|
).once()
|
|
|
|
|
|
output = module.execute_command(full_command, extra_environment={'c': 'd'})
|
|
@@ -42,12 +95,46 @@ def test_execute_command_calls_full_command_with_extra_environment():
|
|
|
assert output is None
|
|
|
|
|
|
|
|
|
+def test_execute_command_calls_full_command_with_working_directory():
|
|
|
+ full_command = ['foo', 'bar']
|
|
|
+ flexmock(module.os, environ={'a': 'b'})
|
|
|
+ flexmock(module).should_receive('execute_and_log_output').with_args(
|
|
|
+ full_command,
|
|
|
+ output_log_level=logging.INFO,
|
|
|
+ shell=False,
|
|
|
+ environment=None,
|
|
|
+ working_directory='/working',
|
|
|
+ error_on_warnings=False,
|
|
|
+ ).once()
|
|
|
+
|
|
|
+ output = module.execute_command(full_command, working_directory='/working')
|
|
|
+
|
|
|
+ assert output is None
|
|
|
+
|
|
|
+
|
|
|
+def test_execute_command_calls_full_command_with_error_on_warnings():
|
|
|
+ full_command = ['foo', 'bar']
|
|
|
+ flexmock(module.os, environ={'a': 'b'})
|
|
|
+ flexmock(module).should_receive('execute_and_log_output').with_args(
|
|
|
+ full_command,
|
|
|
+ output_log_level=logging.INFO,
|
|
|
+ shell=False,
|
|
|
+ environment=None,
|
|
|
+ working_directory=None,
|
|
|
+ error_on_warnings=True,
|
|
|
+ ).once()
|
|
|
+
|
|
|
+ output = module.execute_command(full_command, error_on_warnings=True)
|
|
|
+
|
|
|
+ assert output is None
|
|
|
+
|
|
|
+
|
|
|
def test_execute_command_captures_output():
|
|
|
full_command = ['foo', 'bar']
|
|
|
expected_output = '[]'
|
|
|
flexmock(module.os, environ={'a': 'b'})
|
|
|
flexmock(module.subprocess).should_receive('check_output').with_args(
|
|
|
- full_command, shell=False, env=None
|
|
|
+ full_command, shell=False, env=None, cwd=None
|
|
|
).and_return(flexmock(decode=lambda: expected_output)).once()
|
|
|
|
|
|
output = module.execute_command(full_command, output_log_level=None)
|
|
@@ -60,7 +147,7 @@ def test_execute_command_captures_output_with_shell():
|
|
|
expected_output = '[]'
|
|
|
flexmock(module.os, environ={'a': 'b'})
|
|
|
flexmock(module.subprocess).should_receive('check_output').with_args(
|
|
|
- full_command, shell=True, env=None
|
|
|
+ full_command, shell=True, env=None, cwd=None
|
|
|
).and_return(flexmock(decode=lambda: expected_output)).once()
|
|
|
|
|
|
output = module.execute_command(full_command, output_log_level=None, shell=True)
|
|
@@ -73,7 +160,7 @@ def test_execute_command_captures_output_with_extra_environment():
|
|
|
expected_output = '[]'
|
|
|
flexmock(module.os, environ={'a': 'b'})
|
|
|
flexmock(module.subprocess).should_receive('check_output').with_args(
|
|
|
- full_command, shell=False, env={'a': 'b', 'c': 'd'}
|
|
|
+ full_command, shell=False, env={'a': 'b', 'c': 'd'}, cwd=None
|
|
|
).and_return(flexmock(decode=lambda: expected_output)).once()
|
|
|
|
|
|
output = module.execute_command(
|
|
@@ -83,6 +170,21 @@ def test_execute_command_captures_output_with_extra_environment():
|
|
|
assert output == expected_output
|
|
|
|
|
|
|
|
|
+def test_execute_command_captures_output_with_working_directory():
|
|
|
+ full_command = ['foo', 'bar']
|
|
|
+ expected_output = '[]'
|
|
|
+ flexmock(module.os, environ={'a': 'b'})
|
|
|
+ flexmock(module.subprocess).should_receive('check_output').with_args(
|
|
|
+ full_command, shell=False, env=None, cwd='/working'
|
|
|
+ ).and_return(flexmock(decode=lambda: expected_output)).once()
|
|
|
+
|
|
|
+ output = module.execute_command(
|
|
|
+ full_command, output_log_level=None, shell=False, working_directory='/working'
|
|
|
+ )
|
|
|
+
|
|
|
+ assert output == expected_output
|
|
|
+
|
|
|
+
|
|
|
def test_execute_command_without_capture_does_not_raise_on_success():
|
|
|
flexmock(module.subprocess).should_receive('check_call').and_raise(
|
|
|
module.subprocess.CalledProcessError(0, 'borg init')
|
|
@@ -92,6 +194,7 @@ def test_execute_command_without_capture_does_not_raise_on_success():
|
|
|
|
|
|
|
|
|
def test_execute_command_without_capture_does_not_raise_on_warning():
|
|
|
+ flexmock(module).should_receive('exit_code_indicates_error').and_return(False)
|
|
|
flexmock(module.subprocess).should_receive('check_call').and_raise(
|
|
|
module.subprocess.CalledProcessError(1, 'borg init')
|
|
|
)
|
|
@@ -100,6 +203,7 @@ def test_execute_command_without_capture_does_not_raise_on_warning():
|
|
|
|
|
|
|
|
|
def test_execute_command_without_capture_raises_on_error():
|
|
|
+ flexmock(module).should_receive('exit_code_indicates_error').and_return(True)
|
|
|
flexmock(module.subprocess).should_receive('check_call').and_raise(
|
|
|
module.subprocess.CalledProcessError(2, 'borg init')
|
|
|
)
|