| 1 |
import logging |
| 2 |
from unittest.mock import ( |
| 3 |
patch, Mock |
| 4 |
) |
| 5 |
from pytest import ( |
| 6 |
raises, fixture |
| 7 |
) |
| 8 |
from builtins import bytes |
| 9 |
|
| 10 |
from kiwi.command_process import ( |
| 11 |
CommandProcess, |
| 12 |
CommandIterator |
| 13 |
) |
| 14 |
|
| 15 |
from kiwi.exceptions import KiwiCommandError |
| 16 |
|
| 17 |
|
| 18 |
class TestCommandProcess: |
| 19 |
@fixture(autouse=True) |
| 20 |
def inject_fixtures(self, caplog): |
| 21 |
self._caplog = caplog |
| 22 |
|
| 23 |
def fake_matcher(self, item, output): |
| 24 |
return True |
| 25 |
|
| 26 |
def poll(self): |
| 27 |
return self.data_flow.pop() |
| 28 |
|
| 29 |
def outavailable(self): |
| 30 |
return True |
| 31 |
|
| 32 |
def erravailable(self): |
| 33 |
return True |
| 34 |
|
| 35 |
def outdata(self): |
| 36 |
return self.data_out.pop() |
| 37 |
|
| 38 |
def errdata(self): |
| 39 |
return self.data_err.pop() |
| 40 |
|
| 41 |
def create_flow_method(self, method): |
| 42 |
def create_method(arg=None): |
| 43 |
return method() |
| 44 |
return create_method |
| 45 |
|
| 46 |
def setup(self): |
| 47 |
self.data_flow = [True, None, None, None, None, None, None, None] |
| 48 |
self.data_out = [ |
| 49 |
bytes(b''), bytes(b''), bytes(b'\n'), bytes(b'a'), |
| 50 |
bytes(b't'), bytes(b'a'), bytes(b'd') |
| 51 |
] |
| 52 |
self.data_err = [ |
| 53 |
bytes(b''), bytes(b'\n'), bytes(b'r'), bytes(b'o'), |
| 54 |
bytes(b'r'), bytes(b'r'), bytes(b'e') |
| 55 |
] |
| 56 |
self.flow = self.create_flow_method(self.poll) |
| 57 |
self.flow_out_available = self.create_flow_method(self.outavailable) |
| 58 |
self.flow_err_available = self.create_flow_method(self.erravailable) |
| 59 |
self.flow_out = self.create_flow_method(self.outdata) |
| 60 |
self.flow_err = self.create_flow_method(self.errdata) |
| 61 |
|
| 62 |
def setup_method(self, cls): |
| 63 |
self.setup() |
| 64 |
|
| 65 |
@patch('kiwi.command.Command') |
| 66 |
def test_returncode(self, mock_command): |
| 67 |
command = Mock() |
| 68 |
mock_command.return_value = command |
| 69 |
process = CommandProcess(command) |
| 70 |
assert process.returncode() == command.process.returncode |
| 71 |
|
| 72 |
@patch('kiwi.command.Command') |
| 73 |
def test_poll_show_progress(self, mock_command): |
| 74 |
match_method = CommandProcess(mock_command).create_match_method( |
| 75 |
self.fake_matcher |
| 76 |
) |
| 77 |
process = CommandProcess(mock_command) |
| 78 |
process.command.command.process.poll = self.flow |
| 79 |
process.command.command.output_available = self.flow_out_available |
| 80 |
process.command.command.error_available = self.flow_err_available |
| 81 |
process.command.command.output.read = self.flow_out |
| 82 |
process.command.command.error.read = self.flow_err |
| 83 |
process.command.command.process.returncode = 0 |
| 84 |
with self._caplog.at_level(logging.DEBUG): |
| 85 |
process.poll_show_progress(['a', 'b'], match_method, True) |
| 86 |
assert 'system: data' in self._caplog.text |
| 87 |
|
| 88 |
@patch('kiwi.command.Command') |
| 89 |
def test_poll_show_progress_raises(self, mock_command): |
| 90 |
match_method = CommandProcess(mock_command).create_match_method( |
| 91 |
self.fake_matcher |
| 92 |
) |
| 93 |
process = CommandProcess(mock_command) |
| 94 |
process.command.command.process.poll = self.flow |
| 95 |
process.command.command.output_available = self.flow_out_available |
| 96 |
process.command.command.error_available = self.flow_err_available |
| 97 |
process.command.command.output.read = self.flow_out |
| 98 |
process.command.command.error.read = self.flow_err |
| 99 |
process.command.command.process.returncode = 1 |
| 100 |
with raises(KiwiCommandError): |
| 101 |
process.poll_show_progress(['a', 'b'], match_method) |
| 102 |
|
| 103 |
@patch('kiwi.command.Command') |
| 104 |
def test_poll(self, mock_command): |
| 105 |
process = CommandProcess(mock_command) |
| 106 |
process.command.command.process.poll = self.flow |
| 107 |
process.command.command.output_available = self.flow_out_available |
| 108 |
process.command.command.error_available = self.flow_err_available |
| 109 |
process.command.command.output.read = self.flow_out |
| 110 |
process.command.command.error.read = self.flow_err |
| 111 |
process.command.command.process.returncode = 0 |
| 112 |
with self._caplog.at_level(logging.DEBUG): |
| 113 |
process.poll() |
| 114 |
assert 'system: data' in self._caplog.text |
| 115 |
|
| 116 |
@patch('kiwi.command.Command') |
| 117 |
def test_poll_raises(self, mock_command): |
| 118 |
process = CommandProcess(mock_command) |
| 119 |
process.command.command.process.poll = self.flow |
| 120 |
process.command.command.output_available = self.flow_out_available |
| 121 |
process.command.command.error_available = self.flow_err_available |
| 122 |
process.command.command.output.read = self.flow_out |
| 123 |
process.command.command.error.read = self.flow_err |
| 124 |
process.command.command.output.read.return_value = 'data' |
| 125 |
process.command.command.process.returncode = 1 |
| 126 |
with raises(KiwiCommandError): |
| 127 |
process.poll() |
| 128 |
|
| 129 |
@patch('kiwi.command.Command') |
| 130 |
def test_poll_and_watch(self, mock_command): |
| 131 |
process = CommandProcess(mock_command) |
| 132 |
process.command.command.process.poll = self.flow |
| 133 |
process.command.command.output_available = self.flow_out_available |
| 134 |
process.command.command.error_available = self.flow_err_available |
| 135 |
process.command.command.output.read = self.flow_out |
| 136 |
process.command.command.error.read = self.flow_err |
| 137 |
process.command.command.process.returncode = 1 |
| 138 |
with self._caplog.at_level(logging.DEBUG): |
| 139 |
result = process.poll_and_watch() |
| 140 |
assert '--------------out start-------------' in self._caplog.text |
| 141 |
assert 'data' in self._caplog.text |
| 142 |
assert '--------------out stop--------------' in self._caplog.text |
| 143 |
assert result.stderr == 'error\n' |
| 144 |
|
| 145 |
@patch('kiwi.command.Command') |
| 146 |
def test_create_match_method(self, mock_command): |
| 147 |
match_method = CommandProcess(mock_command).create_match_method( |
| 148 |
self.fake_matcher |
| 149 |
) |
| 150 |
assert match_method('a', 'b') is True |
| 151 |
|
| 152 |
def test_command_iterator(self): |
| 153 |
iterator = CommandIterator(Mock()) |
| 154 |
assert iterator.__iter__() == iterator |
| 155 |
|
| 156 |
def test_kill(self): |
| 157 |
iterator = CommandIterator(Mock()) |
| 158 |
iterator.kill() |
| 159 |
iterator.command.process.kill.assert_called_once_with() |
| 160 |
|
| 161 |
def test_get_pid(self): |
| 162 |
iterator = CommandIterator(Mock()) |
| 163 |
assert iterator.get_pid() == iterator.command.process.pid |