| 1 |
import logging |
| 2 |
from unittest.mock import ( |
| 3 |
patch, MagicMock, mock_open, call |
| 4 |
) |
| 5 |
from pytest import ( |
| 6 |
raises, fixture |
| 7 |
) |
| 8 |
|
| 9 |
from kiwi.system.result import Result |
| 10 |
|
| 11 |
from kiwi.exceptions import KiwiResultError |
| 12 |
|
| 13 |
|
| 14 |
class TestResult: |
| 15 |
@fixture(autouse=True) |
| 16 |
def inject_fixtures(self, caplog): |
| 17 |
self._caplog = caplog |
| 18 |
|
| 19 |
def setup(self): |
| 20 |
self.xml_state = MagicMock() |
| 21 |
self.result = Result(self.xml_state) |
| 22 |
|
| 23 |
def setup_method(self, cls): |
| 24 |
self.setup() |
| 25 |
|
| 26 |
def test_add(self): |
| 27 |
assert self.result.add('foo', 'bar') is None |
| 28 |
result = self.result.get_results() |
| 29 |
assert isinstance(result, dict) |
| 30 |
assert result['foo'].filename == 'bar' |
| 31 |
assert result['foo'].use_for_bundle is True |
| 32 |
assert result['foo'].compress is False |
| 33 |
|
| 34 |
def test_print_results_no_data(self): |
| 35 |
assert self.result.print_results() is None |
| 36 |
assert not self._caplog.text |
| 37 |
|
| 38 |
def test_print_results_data_number_version(self): |
| 39 |
self.xml_state.get_image_version.return_value = '1.1.1' |
| 40 |
self.result.add('foo', 'bar') |
| 41 |
self.result.add_bundle_format('%N') |
| 42 |
with self._caplog.at_level(logging.INFO): |
| 43 |
self.result.print_results() |
| 44 |
|
| 45 |
def test_print_results_data_word_version(self): |
| 46 |
self.xml_state.get_image_version.return_value = 'test' |
| 47 |
self.result.add('foo', 'bar') |
| 48 |
self.result.add_bundle_format('%N') |
| 49 |
with self._caplog.at_level(logging.INFO): |
| 50 |
self.result.print_results() |
| 51 |
|
| 52 |
@patch('pickle.dump') |
| 53 |
@patch('simplejson.dumps') |
| 54 |
def test_dump(self, mock_simplejson_dumps, mock_pickle_dump): |
| 55 |
m_open = mock_open() |
| 56 |
with patch('builtins.open', m_open, create=True): |
| 57 |
assert self.result.dump('kiwi.result') is None |
| 58 |
|
| 59 |
assert m_open.call_args_list == [ |
| 60 |
call('kiwi.result', 'wb'), |
| 61 |
call('kiwi.result.json', 'w') |
| 62 |
] |
| 63 |
mock_pickle_dump.assert_called_once_with( |
| 64 |
self.result, m_open.return_value |
| 65 |
) |
| 66 |
mock_simplejson_dumps.assert_called_once_with( |
| 67 |
self.result.result_files, sort_keys=True, indent=4 |
| 68 |
) |
| 69 |
|
| 70 |
@patch('pickle.dump') |
| 71 |
def test_dump_failed(self, mock_pickle_dump): |
| 72 |
mock_pickle_dump.side_effect = Exception |
| 73 |
with patch('builtins.open'): |
| 74 |
with raises(KiwiResultError): |
| 75 |
self.result.dump('kiwi.result') |
| 76 |
|
| 77 |
@patch('pickle.load') |
| 78 |
@patch('os.path.exists') |
| 79 |
def test_load(self, mock_exists, mock_pickle_load): |
| 80 |
mock_exists.return_value = True |
| 81 |
|
| 82 |
m_open = mock_open() |
| 83 |
mock_pickle_load.return_value = Result |
| 84 |
with patch('builtins.open', m_open, create=True): |
| 85 |
assert Result.load('kiwi.result') is Result |
| 86 |
|
| 87 |
m_open.assert_called_once_with( |
| 88 |
'kiwi.result', 'rb' |
| 89 |
) |
| 90 |
mock_pickle_load.assert_called_once_with( |
| 91 |
m_open.return_value |
| 92 |
) |
| 93 |
|
| 94 |
@patch('os.path.exists') |
| 95 |
def test_load_result_not_present(self, mock_exists): |
| 96 |
mock_exists.return_value = False |
| 97 |
with raises(KiwiResultError): |
| 98 |
Result.load('kiwi.result') |
| 99 |
|
| 100 |
@patch('pickle.load') |
| 101 |
@patch('os.path.exists') |
| 102 |
def test_load_failed(self, mock_exists, mock_pickle_load): |
| 103 |
mock_exists.return_value = True |
| 104 |
mock_pickle_load.side_effect = Exception |
| 105 |
with raises(KiwiResultError): |
| 106 |
Result.load('kiwi.result') |
| 107 |
|
| 108 |
@patch('os.path.getsize') |
| 109 |
def test_build_constraint(self, mock_getsize): |
| 110 |
mock_getsize.return_value = 524288000 |
| 111 |
assert self.result.verify_image_size(524288000, 'foo') is None |
| 112 |
|
| 113 |
@patch('os.path.getsize') |
| 114 |
def test_build_constraint_failure(self, mock_getsize): |
| 115 |
mock_getsize.return_value = 524288000 |
| 116 |
with raises(KiwiResultError): |
| 117 |
self.result.verify_image_size(524287999, 'foo') |
| 118 |
|
| 119 |
def test_get_name_for_pattern(self): |
| 120 |
self.xml_state.get_image_version.return_value = '1.1.1' |
| 121 |
self.xml_state.xml_data.get_name.return_value = 'foo' |
| 122 |
self.xml_state.host_architecture = 'x86_64' |
| 123 |
self.xml_state.get_build_type_name.return_value = 'oem' |
| 124 |
self.result.add('foo', 'bar') |
| 125 |
self.result.add_bundle_format('%N_%I') |
| 126 |
assert self.result.get_name_for_pattern( |
| 127 |
command_args={'--id': '0'} |
| 128 |
) == 'foo_0' |