def test_execute_check_failed_invocation(self, popen, pipe_processor_loop, caplog): 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 assert ('Command', INFO, out) in caplog.record_tuples assert ('Command', WARNING, err) in caplog.record_tuples
def test_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') result = cmd() popen.assert_called_with([command], shell=False, env={ 'TEST0': 'VAL0', '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 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_shell_invocation(self, popen, pipe_processor_loop): command = 'test -n' ret = 0 out = 'out' err = 'err' pipe = _mock_pipe(popen, pipe_processor_loop, ret, out, err) cmd = command_wrappers.Command(command, shell=True) result = cmd('shell test') popen.assert_called_with("test -n 'shell test'", shell=True, 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_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_execute_handlers(self, popen, pipe_processor_loop, caplog): 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() assert result == ret assert cmd.ret == ret assert cmd.out is None assert cmd.err is None assert ('Command', INFO, 'out: ' + out) in caplog.record_tuples assert ('Command', WARNING, 'err: ' + err) in caplog.record_tuples
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 assert cmd.out is None assert cmd.err is None assert '\n'.join(out_list) == out assert '\n'.join(err_list) == err
def test_debug_invocation(self, popen, pipe_processor_loop): command = 'command' ret = 1 out = 'out' err = 'err' pipe = _mock_pipe(popen, pipe_processor_loop, ret, out, err) stdout = StringIO() stderr = StringIO() with mock.patch.multiple('sys', stdout=stdout, stderr=stderr): cmd = command_wrappers.Command(command, debug=True) result = cmd() 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 result == ret assert cmd.ret == ret assert cmd.out == out assert cmd.err == err assert stdout.getvalue() == "" assert stderr.getvalue() == "Command: ['command']\n" \ "Command return code: 1\n"
def test_check_failed_invocation(self, popen, pipe_processor_loop): 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(barman.exceptions.CommandFailedException) \ as excinfo: cmd() assert excinfo.value.args[0]['ret'] == ret assert excinfo.value.args[0]['out'] == out assert excinfo.value.args[0]['err'] == err 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 == out assert cmd.err == err
def test_multiline_output(self, popen, pipe_processor_loop): command = 'command' ret = 0 out = 'line1\nline2\n' err = 'err1\nerr2\n' pipe = _mock_pipe(popen, pipe_processor_loop, ret, out, err) cmd = command_wrappers.Command(command) result = cmd() 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 result == ret assert cmd.ret == ret assert cmd.out == out assert cmd.err == err
def test_retry(self, get_output_no_retry_mock, sleep_mock, popen, pipe_processor_loop): """ Test the retry method :param mock.Mock get_output_no_retry_mock: simulate a Command._get_output_once() call :param mock.Mock sleep_mock: mimic the sleep timer :param mock.Mock popen: unused, mocked from the whole test class :param mock.Mock pipe_processor_loop: unused, mocked from the whole test class """ command = 'test string' cmd = command_wrappers.Command(command, check=True, retry_times=5, retry_sleep=10) # check for correct return value r = cmd.get_output('test string') get_output_no_retry_mock.assert_called_with('test string') assert get_output_no_retry_mock.return_value == r # check for correct number of calls and invocations of sleep method get_output_no_retry_mock.reset_mock() sleep_mock.reset_mock() expected = mock.Mock() get_output_no_retry_mock.side_effect = [ CommandFailedException('testException'), expected ] r = cmd.get_output('test string') assert get_output_no_retry_mock.call_count == 2 assert sleep_mock.call_count == 1 assert r == expected # check for correct number of tries and invocations of sleep method get_output_no_retry_mock.reset_mock() sleep_mock.reset_mock() e = CommandFailedException('testException') get_output_no_retry_mock.side_effect = [e, e, e, e, e, e] with pytest.raises(CommandMaxRetryExceeded) as exc_info: cmd.get_output('test string') assert exc_info.value.exc == e assert sleep_mock.call_count == 5 assert get_output_no_retry_mock.call_count == 6
def test_execute_invocation_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) 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 for line in out.splitlines(): assert ('Command', INFO, line) in caplog.record_tuples assert ('Command', INFO, '') not in caplog.record_tuples assert ('Command', INFO, None) not in caplog.record_tuples for line in err.splitlines(): assert ('Command', WARNING, line) in caplog.record_tuples assert ('Command', WARNING, '') not in caplog.record_tuples assert ('Command', WARNING, None) not in caplog.record_tuples
def test_simple_encoding(self, popen, pipe_processor_loop): command = 'command' ret = 0 out = u('\xe2\x98\xae\xe2\x82\xac\xc3\xa8\xc3\xa9') err = u('\xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf') pipe = _mock_pipe(popen, pipe_processor_loop, ret, out, err) cmd = command_wrappers.Command(command) result = cmd() 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 result == ret assert cmd.ret == ret assert cmd.out == out assert cmd.err == err