| 1 |
from unittest.mock import ( |
| 2 |
patch, call |
| 3 |
) |
| 4 |
from pytest import raises |
| 5 |
import unittest.mock as mock |
| 6 |
|
| 7 |
from kiwi.package_manager.dnf4 import PackageManagerDnf4 |
| 8 |
|
| 9 |
from kiwi.exceptions import KiwiRequestError |
| 10 |
|
| 11 |
|
| 12 |
class TestPackageManagerDnf4: |
| 13 |
def setup(self): |
| 14 |
repository = mock.Mock() |
| 15 |
repository.root_dir = '/root-dir' |
| 16 |
|
| 17 |
repository.runtime_config = mock.Mock( |
| 18 |
return_value={ |
| 19 |
'dnf_args': ['--config', '/root-dir/dnf.conf', '-y'], |
| 20 |
'command_env': ['env'] |
| 21 |
} |
| 22 |
) |
| 23 |
self.manager = PackageManagerDnf4(repository) |
| 24 |
|
| 25 |
def setup_method(self, cls): |
| 26 |
self.setup() |
| 27 |
|
| 28 |
def test_request_package(self): |
| 29 |
self.manager.request_package('name') |
| 30 |
assert self.manager.package_requests == ['name'] |
| 31 |
|
| 32 |
def test_request_collection(self): |
| 33 |
self.manager.request_collection('name') |
| 34 |
assert self.manager.collection_requests == ['@name'] |
| 35 |
|
| 36 |
def test_request_product(self): |
| 37 |
self.manager.request_product('name') |
| 38 |
assert self.manager.product_requests == [] |
| 39 |
|
| 40 |
def test_request_package_exclusion(self): |
| 41 |
self.manager.request_package_exclusion('name') |
| 42 |
assert self.manager.exclude_requests == ['name'] |
| 43 |
|
| 44 |
@patch('kiwi.path.Path.which') |
| 45 |
def test_get_dnf4_binary_name(self, mock_which): |
| 46 |
mock_which.return_value = '/usr/bin/dnf4' |
| 47 |
assert self.manager._get_dnf4_binary_name() == 'dnf4' |
| 48 |
mock_which.assert_called_once_with( |
| 49 |
access_mode=1, custom_env=None, |
| 50 |
filename='dnf4' |
| 51 |
) |
| 52 |
mock_which.return_value = None |
| 53 |
mock_which.reset_mock() |
| 54 |
assert self.manager._get_dnf4_binary_name(root='/some/root') == 'dnf-3' |
| 55 |
mock_which.assert_called_once_with( |
| 56 |
access_mode=1, custom_env={'PATH': '/some/root/usr/bin'}, |
| 57 |
filename='dnf4' |
| 58 |
) |
| 59 |
|
| 60 |
@patch('kiwi.path.Path.which') |
| 61 |
@patch('kiwi.command.Command.run') |
| 62 |
def test_setup_repository_modules(self, mock_run, mock_exists): |
| 63 |
mock_exists.return_value = None |
| 64 |
self.manager.setup_repository_modules( |
| 65 |
{ |
| 66 |
'disable': ['mod_c'], |
| 67 |
'enable': ['mod_a:stream', 'mod_b'] |
| 68 |
} |
| 69 |
) |
| 70 |
dnf_call_args = [ |
| 71 |
'dnf-3', '--config', '/root-dir/dnf.conf', |
| 72 |
'-y', '--installroot', '/root-dir', '--releasever=0' |
| 73 |
] |
| 74 |
assert mock_run.call_args_list == [ |
| 75 |
call( |
| 76 |
dnf_call_args + [ |
| 77 |
'module', 'disable', 'mod_c' |
| 78 |
], ['env'] |
| 79 |
), |
| 80 |
call( |
| 81 |
dnf_call_args + [ |
| 82 |
'module', 'reset', 'mod_a' |
| 83 |
], ['env'] |
| 84 |
), |
| 85 |
call( |
| 86 |
dnf_call_args + [ |
| 87 |
'module', 'enable', 'mod_a:stream' |
| 88 |
], ['env'] |
| 89 |
), |
| 90 |
call( |
| 91 |
dnf_call_args + [ |
| 92 |
'module', 'reset', 'mod_b' |
| 93 |
], ['env'] |
| 94 |
), |
| 95 |
call( |
| 96 |
dnf_call_args + [ |
| 97 |
'module', 'enable', 'mod_b' |
| 98 |
], ['env'] |
| 99 |
) |
| 100 |
] |
| 101 |
|
| 102 |
@patch('kiwi.path.Path.which') |
| 103 |
@patch('kiwi.command.Command.call') |
| 104 |
@patch('kiwi.command.Command.run') |
| 105 |
def test_process_install_requests_bootstrap(self, mock_run, mock_call, mock_exists): |
| 106 |
mock_exists.return_value = None |
| 107 |
self.manager.request_package('vim') |
| 108 |
self.manager.request_collection('collection') |
| 109 |
self.manager.request_package_exclusion('skipme') |
| 110 |
self.manager.process_install_requests_bootstrap() |
| 111 |
mock_run.assert_called_once_with( |
| 112 |
[ |
| 113 |
'dnf-3', '--config', '/root-dir/dnf.conf', '-y', |
| 114 |
'--releasever=0', 'makecache' |
| 115 |
] |
| 116 |
) |
| 117 |
mock_call.assert_called_once_with( |
| 118 |
[ |
| 119 |
'dnf-3', '--config', '/root-dir/dnf.conf', '-y', |
| 120 |
'--installroot', '/root-dir', '--releasever=0', |
| 121 |
'--exclude=skipme', |
| 122 |
'install', 'vim', '@collection' |
| 123 |
], ['env'] |
| 124 |
) |
| 125 |
|
| 126 |
@patch('kiwi.path.Path.which') |
| 127 |
@patch('kiwi.command.Command.call') |
| 128 |
def test_process_install_requests(self, mock_call, mock_exists): |
| 129 |
mock_exists.return_value = None |
| 130 |
self.manager.request_package('vim') |
| 131 |
self.manager.request_collection('collection') |
| 132 |
self.manager.request_package_exclusion('skipme') |
| 133 |
self.manager.process_install_requests() |
| 134 |
mock_call.assert_called_once_with( |
| 135 |
[ |
| 136 |
'chroot', '/root-dir', 'dnf-3', '--config', '/dnf.conf', '-y', |
| 137 |
'--releasever=0', '--exclude=skipme', |
| 138 |
'install', 'vim', '@collection' |
| 139 |
], ['env'] |
| 140 |
) |
| 141 |
|
| 142 |
@patch('kiwi.command.Command.call') |
| 143 |
@patch('kiwi.command.Command.run') |
| 144 |
def test_process_delete_requests_force(self, mock_run, mock_call): |
| 145 |
self.manager.request_package('vim') |
| 146 |
self.manager.process_delete_requests(True) |
| 147 |
mock_call.assert_called_once_with( |
| 148 |
[ |
| 149 |
'chroot', '/root-dir', 'rpm', '-e', |
| 150 |
'--nodeps', '--allmatches', '--noscripts', 'vim' |
| 151 |
], |
| 152 |
[ |
| 153 |
'env' |
| 154 |
] |
| 155 |
) |
| 156 |
|
| 157 |
@patch('kiwi.path.Path.which') |
| 158 |
@patch('kiwi.command.Command.call') |
| 159 |
@patch('kiwi.command.Command.run') |
| 160 |
def test_process_delete_requests_no_force(self, mock_run, mock_call, mock_exists): |
| 161 |
mock_exists.return_value = None |
| 162 |
self.manager.request_package('vim') |
| 163 |
self.manager.process_delete_requests() |
| 164 |
mock_call.assert_called_once_with( |
| 165 |
[ |
| 166 |
'chroot', '/root-dir', 'dnf-3', |
| 167 |
'--config', '/dnf.conf', '-y', |
| 168 |
'--releasever=0', 'autoremove', 'vim' |
| 169 |
], |
| 170 |
['env'] |
| 171 |
) |
| 172 |
|
| 173 |
@patch('kiwi.command.Command.run') |
| 174 |
@patch('kiwi.command.Command.call') |
| 175 |
def test_process_delete_requests_package_missing( |
| 176 |
self, mock_call, mock_run |
| 177 |
): |
| 178 |
mock_run.side_effect = Exception |
| 179 |
self.manager.request_package('vim') |
| 180 |
with raises(KiwiRequestError): |
| 181 |
self.manager.process_delete_requests(force=True) |
| 182 |
mock_run.assert_called_once_with( |
| 183 |
['chroot', '/root-dir', 'rpm', '-q', 'vim'] |
| 184 |
) |
| 185 |
|
| 186 |
@patch('kiwi.path.Path.which') |
| 187 |
@patch('kiwi.command.Command.call') |
| 188 |
def test_update(self, mock_call, mock_exists): |
| 189 |
mock_exists.return_value = None |
| 190 |
self.manager.update() |
| 191 |
mock_call.assert_called_once_with( |
| 192 |
[ |
| 193 |
'chroot', '/root-dir', 'dnf-3', |
| 194 |
'--config', '/dnf.conf', '-y', |
| 195 |
'--releasever=0', 'upgrade' |
| 196 |
], ['env'] |
| 197 |
) |
| 198 |
|
| 199 |
def test_process_only_required(self): |
| 200 |
self.manager.process_only_required() |
| 201 |
assert self.manager.custom_args == ['--setopt=install_weak_deps=False'] |
| 202 |
|
| 203 |
def test_process_plus_recommended(self): |
| 204 |
self.manager.process_only_required() |
| 205 |
assert self.manager.custom_args == ['--setopt=install_weak_deps=False'] |
| 206 |
self.manager.process_plus_recommended() |
| 207 |
assert \ |
| 208 |
'--setopt=install_weak_deps=False' not in self.manager.custom_args |
| 209 |
|
| 210 |
def test_match_package_installed(self): |
| 211 |
assert self.manager.match_package_installed('foo', 'Installing : foo') |
| 212 |
|
| 213 |
def test_match_package_deleted(self): |
| 214 |
assert self.manager.match_package_deleted('foo', 'Removing: foo') |
| 215 |
|
| 216 |
@patch('kiwi.package_manager.dnf4.RpmDataBase') |
| 217 |
def test_post_process_install_requests_bootstrap(self, mock_RpmDataBase): |
| 218 |
rpmdb = mock.Mock() |
| 219 |
rpmdb.has_rpm.return_value = True |
| 220 |
mock_RpmDataBase.return_value = rpmdb |
| 221 |
self.manager.post_process_install_requests_bootstrap() |
| 222 |
rpmdb.rebuild_database.assert_called_once_with() |
| 223 |
rpmdb.set_database_to_image_path.assert_called_once_with() |
| 224 |
|
| 225 |
@patch('kiwi.package_manager.dnf4.Rpm') |
| 226 |
def test_clean_leftovers(self, mock_rpm): |
| 227 |
mock_rpm.return_value = mock.Mock() |
| 228 |
self.manager.clean_leftovers() |
| 229 |
mock_rpm.assert_called_once_with( |
| 230 |
'/root-dir', 'macros.kiwi-image-config' |
| 231 |
) |
| 232 |
mock_rpm.return_value.wipe_config.assert_called_once_with() |