| 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.microdnf import PackageManagerMicroDnf |
| 8 |
|
| 9 |
from kiwi.exceptions import KiwiRequestError |
| 10 |
|
| 11 |
|
| 12 |
class TestPackageManagerMicroDnf: |
| 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 |
repository.shared_dnf_dir = { |
| 24 |
'reposd-dir': 'repos', |
| 25 |
'cache-dir': 'cache', |
| 26 |
'pluginconf-dir': 'pluginconf', |
| 27 |
'vars-dir': 'vars' |
| 28 |
} |
| 29 |
self.manager = PackageManagerMicroDnf(repository) |
| 30 |
|
| 31 |
def setup_method(self, cls): |
| 32 |
self.setup() |
| 33 |
|
| 34 |
def test_request_package(self): |
| 35 |
self.manager.request_package('name') |
| 36 |
assert self.manager.package_requests == ['name'] |
| 37 |
|
| 38 |
def test_request_collection(self): |
| 39 |
self.manager.request_collection('name') |
| 40 |
assert self.manager.collection_requests == [] |
| 41 |
|
| 42 |
def test_request_product(self): |
| 43 |
self.manager.request_product('name') |
| 44 |
assert self.manager.product_requests == [] |
| 45 |
|
| 46 |
def test_request_package_exclusion(self): |
| 47 |
self.manager.request_package_exclusion('name') |
| 48 |
assert self.manager.exclude_requests == ['name'] |
| 49 |
|
| 50 |
@patch('kiwi.command.Command.run') |
| 51 |
def test_setup_repository_modules(self, mock_run): |
| 52 |
self.manager.setup_repository_modules( |
| 53 |
{ |
| 54 |
'disable': ['mod_c'], |
| 55 |
'enable': ['mod_a:stream', 'mod_b'] |
| 56 |
} |
| 57 |
) |
| 58 |
microdnf_call_args = [ |
| 59 |
'microdnf', '--refresh', '--config', '/root-dir/dnf.conf', |
| 60 |
'-y', '--installroot', '/root-dir', '--releasever=0', |
| 61 |
'--noplugins', '--setopt=cachedir=cache', |
| 62 |
'--setopt=reposdir=repos', |
| 63 |
'--setopt=varsdir=vars' |
| 64 |
] |
| 65 |
assert mock_run.call_args_list == [ |
| 66 |
call( |
| 67 |
microdnf_call_args + [ |
| 68 |
'module', 'disable', 'mod_c' |
| 69 |
], ['env'] |
| 70 |
), |
| 71 |
call( |
| 72 |
microdnf_call_args + [ |
| 73 |
'module', 'reset', 'mod_a' |
| 74 |
], ['env'] |
| 75 |
), |
| 76 |
call( |
| 77 |
microdnf_call_args + [ |
| 78 |
'module', 'enable', 'mod_a:stream' |
| 79 |
], ['env'] |
| 80 |
), |
| 81 |
call( |
| 82 |
microdnf_call_args + [ |
| 83 |
'module', 'reset', 'mod_b' |
| 84 |
], ['env'] |
| 85 |
), |
| 86 |
call( |
| 87 |
microdnf_call_args + [ |
| 88 |
'module', 'enable', 'mod_b' |
| 89 |
], ['env'] |
| 90 |
) |
| 91 |
] |
| 92 |
|
| 93 |
@patch('kiwi.command.Command.call') |
| 94 |
@patch('kiwi.command.Command.run') |
| 95 |
def test_process_install_requests_bootstrap(self, mock_run, mock_call): |
| 96 |
self.manager.request_package('vim') |
| 97 |
self.manager.request_collection('collection') |
| 98 |
self.manager.process_install_requests_bootstrap() |
| 99 |
mock_call.assert_called_once_with( |
| 100 |
[ |
| 101 |
'microdnf', '--refresh', '--config', '/root-dir/dnf.conf', |
| 102 |
'-y', '--installroot', '/root-dir', '--releasever=0', |
| 103 |
'--noplugins', '--setopt=cachedir=cache', |
| 104 |
'--setopt=reposdir=repos', |
| 105 |
'--setopt=varsdir=vars', |
| 106 |
'install', 'vim' |
| 107 |
], ['env'] |
| 108 |
) |
| 109 |
|
| 110 |
@patch('kiwi.command.Command.call') |
| 111 |
def test_process_install_requests(self, mock_call): |
| 112 |
self.manager.request_package('vim') |
| 113 |
self.manager.request_collection('collection') |
| 114 |
self.manager.request_package_exclusion('skipme') |
| 115 |
self.manager.process_install_requests() |
| 116 |
mock_call.assert_called_once_with( |
| 117 |
[ |
| 118 |
'chroot', '/root-dir', 'microdnf', '--config', '/dnf.conf', |
| 119 |
'-y', '--releasever=0', '--exclude=skipme', 'install', 'vim' |
| 120 |
], ['env'] |
| 121 |
) |
| 122 |
|
| 123 |
@patch('kiwi.command.Command.call') |
| 124 |
@patch('kiwi.command.Command.run') |
| 125 |
def test_process_delete_requests_force(self, mock_run, mock_call): |
| 126 |
self.manager.request_package('vim') |
| 127 |
self.manager.process_delete_requests(True) |
| 128 |
mock_call.assert_called_once_with( |
| 129 |
[ |
| 130 |
'chroot', '/root-dir', 'rpm', '-e', |
| 131 |
'--nodeps', '--allmatches', '--noscripts', 'vim' |
| 132 |
], |
| 133 |
[ |
| 134 |
'env' |
| 135 |
] |
| 136 |
) |
| 137 |
|
| 138 |
@patch('kiwi.command.Command.call') |
| 139 |
@patch('kiwi.command.Command.run') |
| 140 |
def test_process_delete_requests_no_force(self, mock_run, mock_call): |
| 141 |
self.manager.request_package('vim') |
| 142 |
self.manager.process_delete_requests() |
| 143 |
mock_call.assert_called_once_with( |
| 144 |
[ |
| 145 |
'chroot', '/root-dir', 'microdnf', |
| 146 |
'--config', '/dnf.conf', '-y', |
| 147 |
'--releasever=0', 'remove', 'vim' |
| 148 |
], |
| 149 |
['env'] |
| 150 |
) |
| 151 |
|
| 152 |
@patch('kiwi.command.Command.run') |
| 153 |
@patch('kiwi.command.Command.call') |
| 154 |
def test_process_delete_requests_package_missing( |
| 155 |
self, mock_call, mock_run |
| 156 |
): |
| 157 |
mock_run.side_effect = Exception |
| 158 |
self.manager.request_package('vim') |
| 159 |
with raises(KiwiRequestError): |
| 160 |
self.manager.process_delete_requests(force=True) |
| 161 |
mock_run.assert_called_once_with( |
| 162 |
['chroot', '/root-dir', 'rpm', '-q', 'vim'] |
| 163 |
) |
| 164 |
|
| 165 |
@patch('kiwi.command.Command.call') |
| 166 |
def test_update(self, mock_call): |
| 167 |
self.manager.update() |
| 168 |
mock_call.assert_called_once_with( |
| 169 |
[ |
| 170 |
'chroot', '/root-dir', 'microdnf', |
| 171 |
'--config', '/dnf.conf', '-y', '--releasever=0', 'upgrade' |
| 172 |
], ['env'] |
| 173 |
) |
| 174 |
|
| 175 |
def test_process_only_required(self): |
| 176 |
self.manager.process_only_required() |
| 177 |
assert self.manager.custom_args == ['--setopt=install_weak_deps=0'] |
| 178 |
|
| 179 |
def test_process_plus_recommended(self): |
| 180 |
self.manager.process_only_required() |
| 181 |
assert self.manager.custom_args == ['--setopt=install_weak_deps=0'] |
| 182 |
self.manager.process_plus_recommended() |
| 183 |
assert \ |
| 184 |
'--setopt=install_weak_deps=0' not in self.manager.custom_args |
| 185 |
|
| 186 |
def test_match_package_installed(self): |
| 187 |
assert self.manager.match_package_installed('foo', 'Installing : foo') |
| 188 |
|
| 189 |
def test_match_package_deleted(self): |
| 190 |
assert self.manager.match_package_deleted('foo', 'Removing: foo') |
| 191 |
|
| 192 |
@patch('kiwi.package_manager.microdnf.RpmDataBase') |
| 193 |
def test_post_process_install_requests_bootstrap(self, mock_RpmDataBase): |
| 194 |
rpmdb = mock.Mock() |
| 195 |
rpmdb.has_rpm.return_value = True |
| 196 |
mock_RpmDataBase.return_value = rpmdb |
| 197 |
self.manager.post_process_install_requests_bootstrap() |
| 198 |
rpmdb.rebuild_database.assert_called_once_with() |
| 199 |
rpmdb.set_database_to_image_path.assert_called_once_with() |
| 200 |
|
| 201 |
@patch('kiwi.package_manager.microdnf.Rpm') |
| 202 |
def test_clean_leftovers(self, mock_rpm): |
| 203 |
mock_rpm.return_value = mock.Mock() |
| 204 |
self.manager.clean_leftovers() |
| 205 |
mock_rpm.assert_called_once_with( |
| 206 |
'/root-dir', 'macros.kiwi-image-config' |
| 207 |
) |
| 208 |
mock_rpm.return_value.wipe_config.assert_called_once_with() |