Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_call_args_invocation(self, popen, pipe_processor_loop):
command = 'command'
ret = 0
out = 'out'
err = 'err'
pipe = _mock_pipe(popen, pipe_processor_loop, ret, out, err)
cmd = command_wrappers.Command(command)
result = cmd('one', 'two')
popen.assert_called_with(
[command, 'one', 'two'], shell=False, env=None,
stdout=PIPE, stderr=PIPE, stdin=PIPE,
preexec_fn=mock.ANY, close_fds=True
)
assert not pipe.stdin.write.called
pipe.stdin.close.assert_called_once_with()
assert result == ret
assert cmd.ret == ret
assert cmd.out == out
assert cmd.err == err
def test_execute_invocation(self, popen, pipe_processor_loop, caplog):
# See all logs
caplog.set_level(0)
command = 'command'
ret = 0
out = 'out'
err = 'err'
stdin = 'in'
pipe = _mock_pipe(popen, pipe_processor_loop, ret, out, err)
with mock.patch('os.environ', new={'TEST0': 'VAL0'}):
cmd = command_wrappers.Command(command,
env_append={'TEST1': 'VAL1',
'TEST2': 'VAL2'})
result = cmd.execute(stdin=stdin)
popen.assert_called_with(
[command], shell=False,
env={'TEST0': 'VAL0', 'TEST1': 'VAL1', 'TEST2': 'VAL2'},
stdout=PIPE, stderr=PIPE, stdin=PIPE,
preexec_fn=mock.ANY, close_fds=True
)
pipe.stdin.write.assert_called_with(stdin)
pipe.stdin.close.assert_called_once_with()
assert result == ret
assert cmd.ret == ret
assert cmd.out is None
assert cmd.err is None
def test_both_args_invocation(self, popen, pipe_processor_loop):
command = 'command'
ret = 0
out = 'out'
err = 'err'
pipe = _mock_pipe(popen, pipe_processor_loop, ret, out, err)
cmd = command_wrappers.Command(command, args=['a', 'b'])
result = cmd('one', 'two')
popen.assert_called_with(
[command, 'a', 'b', 'one', 'two'], shell=False, env=None,
stdout=PIPE, stderr=PIPE, stdin=PIPE,
preexec_fn=mock.ANY, close_fds=True
)
assert not pipe.stdin.write.called
pipe.stdin.close.assert_called_once_with()
assert result == ret
assert cmd.ret == ret
assert cmd.out == out
assert cmd.err == err
def test_get_output_invocation(self, popen, pipe_processor_loop):
command = 'command'
ret = 0
out = 'out'
err = 'err'
stdin = 'in'
pipe = _mock_pipe(popen, pipe_processor_loop, ret, out, err)
with mock.patch('os.environ', new={'TEST0': 'VAL0'}):
cmd = command_wrappers.Command(command,
env_append={'TEST1': 'VAL1',
'TEST2': 'VAL2'})
result = cmd.get_output(stdin=stdin)
popen.assert_called_with(
[command], shell=False,
env={'TEST0': 'VAL0', 'TEST1': 'VAL1', 'TEST2': 'VAL2'},
stdout=PIPE, stderr=PIPE, stdin=PIPE,
preexec_fn=mock.ANY, close_fds=True
)
pipe.stdin.write.assert_called_with(stdin)
pipe.stdin.close.assert_called_once_with()
assert result == (out, err)
assert cmd.ret == ret
assert cmd.out == out
assert cmd.err == err
def test_execute_check_failed_invocation(self, popen,
pipe_processor_loop,
caplog):
# See all logs
caplog.set_level(0)
command = 'command'
ret = 1
out = 'out'
err = 'err'
pipe = _mock_pipe(popen, pipe_processor_loop, ret, out, err)
cmd = command_wrappers.Command(command, check=True)
with pytest.raises(CommandFailedException) as excinfo:
cmd.execute()
assert excinfo.value.args[0]['ret'] == ret
assert excinfo.value.args[0]['out'] is None
assert excinfo.value.args[0]['err'] is None
popen.assert_called_with(
[command], shell=False, env=None,
stdout=PIPE, stderr=PIPE, stdin=PIPE,
preexec_fn=mock.ANY, close_fds=True
)
assert not pipe.stdin.write.called
pipe.stdin.close.assert_called_once_with()
assert cmd.ret == ret
assert cmd.out is None
assert cmd.err is None
def test_env_path_invocation(self, popen, pipe_processor_loop):
command = 'command'
ret = 0
out = 'out'
err = 'err'
pipe = _mock_pipe(popen, pipe_processor_loop, ret, out, err)
with mock.patch('os.environ', new={'TEST0': 'VAL0'}):
cmd = command_wrappers.Command(command,
path='/path/one:/path/two',
env_append={'TEST1': 'VAL1',
'TEST2': 'VAL2'})
result = cmd()
popen.assert_called_with(
[command], shell=False,
env={'TEST0': 'VAL0', 'TEST1': 'VAL1', 'TEST2': 'VAL2',
'PATH': '/path/one:/path/two'},
stdout=PIPE, stderr=PIPE, stdin=PIPE,
preexec_fn=mock.ANY, close_fds=True
)
assert not pipe.stdin.write.called
pipe.stdin.close.assert_called_once_with()
assert result == ret
assert cmd.ret == ret
def test_execute_handlers(self, popen, pipe_processor_loop, caplog):
# See all logs
caplog.set_level(0)
command = 'command'
ret = 0
out = 'out'
err = 'err'
stdin = 'in'
pipe = _mock_pipe(popen, pipe_processor_loop, ret, out, err)
with mock.patch('os.environ', new={'TEST0': 'VAL0'}):
cmd = command_wrappers.Command(command,
env_append={'TEST1': 'VAL1',
'TEST2': 'VAL2'})
result = cmd.execute(
stdin=stdin,
out_handler=cmd.make_logging_handler(INFO, 'out: '),
err_handler=cmd.make_logging_handler(WARNING, 'err: '),
)
popen.assert_called_with(
[command], shell=False,
env={'TEST0': 'VAL0', 'TEST1': 'VAL1', 'TEST2': 'VAL2'},
stdout=PIPE, stderr=PIPE, stdin=PIPE,
preexec_fn=mock.ANY, close_fds=True
)
pipe.stdin.write.assert_called_with(stdin)
pipe.stdin.close.assert_called_once_with()
def test_handlers_multiline(self, popen, pipe_processor_loop, caplog):
command = 'command'
ret = 0
out = 'line1\nline2\n'
err = 'err1\nerr2' # no final newline here
stdin = 'in'
pipe = _mock_pipe(popen, pipe_processor_loop, ret, out, err)
out_list = []
err_list = []
with mock.patch('os.environ', new={'TEST0': 'VAL0'}):
cmd = command_wrappers.Command(command,
env_append={'TEST1': 'VAL1',
'TEST2': 'VAL2'},
out_handler=out_list.append,
err_handler=err_list.append)
result = cmd.execute(stdin=stdin)
popen.assert_called_with(
[command], shell=False,
env={'TEST0': 'VAL0', 'TEST1': 'VAL1', 'TEST2': 'VAL2'},
stdout=PIPE, stderr=PIPE, stdin=PIPE,
preexec_fn=mock.ANY, close_fds=True
)
pipe.stdin.write.assert_called_with(stdin)
pipe.stdin.close.assert_called_once_with()
assert result == ret
assert cmd.ret == ret
# Search the requested executable in every directory present
# in path and return a Command object first occurrence that exists,
# is executable and runs without errors.
for path_entry in path.split(os.path.pathsep):
for cmd in cls.COMMAND_ALTERNATIVES:
full_path = barman.utils.which(cmd, path_entry)
# It doesn't exist try another
if not full_path:
continue
# It exists, let's try invoking it with `--version` to check if
# it's real or not.
try:
command = Command(full_path, path=path, check=True)
command("--version")
return command
except CommandFailedException:
# It's only a inactive shim
continue
# We don't have such a command
raise CommandFailedException(
'command not in PATH, tried: %s' %
' '.join(cls.COMMAND_ALTERNATIVES))
options += ["--exclude=%s" % (pattern,)]
if exclude_and_protect:
for pattern in exclude_and_protect:
options += ["--exclude=%s" % (pattern,),
"--filter=P_%s" % (pattern,)]
if args:
options += self._args_for_suse(args)
if bwlimit is not None and bwlimit > 0:
options += ["--bwlimit=%s" % bwlimit]
# By default check is on and the allowed exit code are 0 and 24
if 'check' not in kwargs:
kwargs['check'] = True
if 'allowed_retval' not in kwargs:
kwargs['allowed_retval'] = (0, 24)
Command.__init__(self, rsync, args=options, path=path, **kwargs)