| 1 |
from pytest import fixture |
| 2 |
from unittest.mock import ( |
| 3 |
patch, call, mock_open |
| 4 |
) |
| 5 |
import unittest.mock as mock |
| 6 |
|
| 7 |
from kiwi.repository.dnf4 import RepositoryDnf4 |
| 8 |
|
| 9 |
|
| 10 |
class TestRepositoryDnf4: |
| 11 |
@fixture(autouse=True) |
| 12 |
def inject_fixtures(self, caplog): |
| 13 |
self._caplog = caplog |
| 14 |
|
| 15 |
@patch('kiwi.repository.dnf4.Temporary.unmanaged_file') |
| 16 |
@patch('kiwi.repository.dnf4.ConfigParser') |
| 17 |
@patch('kiwi.repository.dnf4.Path.create') |
| 18 |
def setup(self, mock_path, mock_config, mock_temp): |
| 19 |
runtime_dnf_config = mock.MagicMock() |
| 20 |
mock_config.return_value = runtime_dnf_config |
| 21 |
tmpfile = mock.Mock() |
| 22 |
tmpfile.name = 'tmpfile' |
| 23 |
mock_temp.return_value = tmpfile |
| 24 |
root_bind = mock.Mock() |
| 25 |
root_bind.root_dir = '../data' |
| 26 |
root_bind.shared_location = '/shared-dir' |
| 27 |
|
| 28 |
with patch('builtins.open', create=True): |
| 29 |
self.repo = RepositoryDnf4( |
| 30 |
root_bind, [ |
| 31 |
'exclude_docs', '_install_langs%en_US:de_DE', |
| 32 |
'_target_arch%x86_64' |
| 33 |
] |
| 34 |
) |
| 35 |
|
| 36 |
assert runtime_dnf_config.__setitem__.call_args_list == [ |
| 37 |
call('main', { |
| 38 |
'cachedir': '/shared-dir/dnf/cache', |
| 39 |
'reposdir': '/shared-dir/dnf/repos', |
| 40 |
'varsdir': '/shared-dir/dnf/vars', |
| 41 |
'pluginconfpath': '/shared-dir/dnf/pluginconf', |
| 42 |
'keepcache': '1', |
| 43 |
'debuglevel': '2', |
| 44 |
'best': '1', |
| 45 |
'obsoletes': '1', |
| 46 |
'plugins': '1', |
| 47 |
'gpgcheck': '0' |
| 48 |
}) |
| 49 |
] |
| 50 |
|
| 51 |
@patch('kiwi.repository.dnf4.Temporary.unmanaged_file') |
| 52 |
@patch('kiwi.repository.dnf4.ConfigParser') |
| 53 |
@patch('kiwi.repository.dnf4.Path.create') |
| 54 |
def setup_method(self, cls, mock_path, mock_config, mock_temp): |
| 55 |
self.setup() |
| 56 |
|
| 57 |
@patch('kiwi.repository.dnf4.Temporary.unmanaged_file') |
| 58 |
@patch('kiwi.repository.dnf4.Path.create') |
| 59 |
def test_post_init_no_custom_args(self, mock_path, mock_temp): |
| 60 |
with patch('builtins.open', create=True): |
| 61 |
self.repo.post_init() |
| 62 |
assert self.repo.custom_args == [] |
| 63 |
|
| 64 |
@patch('kiwi.repository.dnf4.Temporary.unmanaged_file') |
| 65 |
@patch('kiwi.repository.dnf4.Path.create') |
| 66 |
@patch('os.path.exists') |
| 67 |
def test_post_init_with_custom_args( |
| 68 |
self, mock_exists, mock_path, mock_temp |
| 69 |
): |
| 70 |
mock_exists.return_value = True |
| 71 |
m_open = mock_open() |
| 72 |
with patch('builtins.open', m_open, create=True): |
| 73 |
self.repo.post_init(['check_signatures']) |
| 74 |
assert m_open.call_args_list == [ |
| 75 |
call(mock_temp.return_value.name, 'w') |
| 76 |
] |
| 77 |
assert self.repo.custom_args == [] |
| 78 |
assert self.repo.gpg_check == '1' |
| 79 |
|
| 80 |
@patch('kiwi.repository.dnf4.ConfigParser') |
| 81 |
def test_use_default_location(self, mock_config): |
| 82 |
runtime_dnf_config = mock.MagicMock() |
| 83 |
mock_config.return_value = runtime_dnf_config |
| 84 |
|
| 85 |
with patch('builtins.open', create=True): |
| 86 |
self.repo.use_default_location() |
| 87 |
|
| 88 |
assert runtime_dnf_config.__setitem__.call_args_list == [ |
| 89 |
call('main', { |
| 90 |
'cachedir': '../data/var/cache/dnf', |
| 91 |
'reposdir': '../data/etc/yum.repos.d', |
| 92 |
'varsdir': '../data/etc/dnf/vars', |
| 93 |
'pluginconfpath': '../data/etc/dnf/plugins', |
| 94 |
'keepcache': '1', |
| 95 |
'debuglevel': '2', |
| 96 |
'best': '1', |
| 97 |
'obsoletes': '1', |
| 98 |
'plugins': '1', |
| 99 |
'gpgcheck': '0' |
| 100 |
}) |
| 101 |
] |
| 102 |
|
| 103 |
def test_runtime_config(self): |
| 104 |
assert self.repo.runtime_config()['dnf_args'] == \ |
| 105 |
self.repo.dnf_args |
| 106 |
assert self.repo.runtime_config()['command_env'] == \ |
| 107 |
self.repo.command_env |
| 108 |
|
| 109 |
@patch('kiwi.repository.dnf4.ConfigParser') |
| 110 |
@patch('kiwi.repository.dnf4.Defaults.is_buildservice_worker') |
| 111 |
@patch('os.path.exists') |
| 112 |
@patch('kiwi.command.Command.run') |
| 113 |
@patch('os.path.isdir') |
| 114 |
@patch('kiwi.repository.dnf4.Path') |
| 115 |
def test_add_repo( |
| 116 |
self, mock_Path, mock_os_path_isdir, mock_Command_run, mock_exists, |
| 117 |
mock_buildservice, mock_config |
| 118 |
): |
| 119 |
mock_os_path_isdir.return_value = False |
| 120 |
repo_config = mock.Mock() |
| 121 |
mock_buildservice.return_value = False |
| 122 |
mock_config.return_value = repo_config |
| 123 |
mock_exists.return_value = True |
| 124 |
|
| 125 |
with patch('builtins.open', create=True) as mock_open: |
| 126 |
self.repo.add_repo( |
| 127 |
'foo', 'kiwi_iso_mount/uri', 'rpm-md', 42, |
| 128 |
customization_script='custom_script' |
| 129 |
) |
| 130 |
|
| 131 |
repo_config.add_section.assert_called_once_with('foo') |
| 132 |
assert repo_config.set.call_args_list == [ |
| 133 |
call('foo', 'name', 'foo'), |
| 134 |
call('foo', 'baseurl', 'file://kiwi_iso_mount/uri'), |
| 135 |
call('foo', 'priority', '42'), |
| 136 |
call('foo', 'repo_gpgcheck', '0'), |
| 137 |
call('foo', 'gpgcheck', '0'), |
| 138 |
] |
| 139 |
mock_open.assert_called_once_with( |
| 140 |
'/shared-dir/dnf/repos/foo.repo', 'w' |
| 141 |
) |
| 142 |
mock_Command_run.assert_called_once_with( |
| 143 |
[ |
| 144 |
'bash', '--norc', 'custom_script', |
| 145 |
'/shared-dir/dnf/repos/foo.repo' |
| 146 |
] |
| 147 |
) |
| 148 |
mock_Path.create.assert_called_once_with('/shared-dir/dnf/repos') |
| 149 |
|
| 150 |
repo_config.add_section.reset_mock() |
| 151 |
repo_config.set.reset_mock() |
| 152 |
mock_exists.return_value = False |
| 153 |
with patch('builtins.open', create=True) as mock_open: |
| 154 |
self.repo.add_repo( |
| 155 |
'bar', 'https://metalink', 'rpm-md', sourcetype='metalink' |
| 156 |
) |
| 157 |
|
| 158 |
repo_config.add_section.assert_called_once_with('bar') |
| 159 |
assert repo_config.set.call_args_list == [ |
| 160 |
call('bar', 'name', 'bar'), |
| 161 |
call('bar', 'metalink', 'https://metalink'), |
| 162 |
call('bar', 'repo_gpgcheck', '0'), |
| 163 |
call('bar', 'gpgcheck', '0') |
| 164 |
] |
| 165 |
mock_open.assert_called_once_with( |
| 166 |
'/shared-dir/dnf/repos/bar.repo', 'w' |
| 167 |
) |
| 168 |
|
| 169 |
@patch('kiwi.repository.dnf4.ConfigParser') |
| 170 |
@patch('kiwi.repository.dnf4.Defaults.is_buildservice_worker') |
| 171 |
@patch('os.path.exists') |
| 172 |
@patch('kiwi.repository.dnf4.Path') |
| 173 |
def test_add_repo_inside_buildservice( |
| 174 |
self, mock_path, mock_exists, mock_buildservice, mock_config |
| 175 |
): |
| 176 |
repo_config = mock.Mock() |
| 177 |
mock_buildservice.return_value = True |
| 178 |
mock_config.return_value = repo_config |
| 179 |
mock_exists.return_value = True |
| 180 |
|
| 181 |
with patch('builtins.open', create=True) as mock_open: |
| 182 |
self.repo.add_repo('foo', 'kiwi_iso_mount/uri', 'rpm-md', 42) |
| 183 |
|
| 184 |
repo_config.add_section.assert_called_once_with('foo') |
| 185 |
assert repo_config.set.call_args_list == [ |
| 186 |
call('foo', 'name', 'foo'), |
| 187 |
call('foo', 'baseurl', 'file://kiwi_iso_mount/uri'), |
| 188 |
call('foo', 'priority', '42'), |
| 189 |
call('foo', 'repo_gpgcheck', '0'), |
| 190 |
call('foo', 'gpgcheck', '0'), |
| 191 |
call('foo', 'module_hotfixes', '1') |
| 192 |
] |
| 193 |
mock_open.assert_called_once_with( |
| 194 |
'/shared-dir/dnf/repos/foo.repo', 'w' |
| 195 |
) |
| 196 |
|
| 197 |
@patch('kiwi.repository.dnf4.RpmDataBase') |
| 198 |
@patch('kiwi.command.Command.run') |
| 199 |
@patch('kiwi.repository.dnf4.Path.create') |
| 200 |
def test_setup_package_database_configuration( |
| 201 |
self, mock_Path_create, mock_Command_run, mock_RpmDataBase |
| 202 |
): |
| 203 |
rpmdb = mock.Mock() |
| 204 |
rpmdb.has_rpm.return_value = False |
| 205 |
rpmdb.rpmdb_host.expand_query.return_value = '/usr/lib/sysimage/rpm' |
| 206 |
mock_RpmDataBase.return_value = rpmdb |
| 207 |
self.repo.setup_package_database_configuration() |
| 208 |
assert mock_RpmDataBase.call_args_list == [ |
| 209 |
call('../data', 'macros.kiwi-image-config'), |
| 210 |
call('../data') |
| 211 |
] |
| 212 |
rpmdb.set_macro_from_string.assert_called_once_with( |
| 213 |
'_install_langs%en_US:de_DE' |
| 214 |
) |
| 215 |
rpmdb.write_config.assert_called_once_with() |
| 216 |
rpmdb.set_database_to_host_path.assert_called_once_with() |
| 217 |
mock_Path_create.assert_called_once_with('../data/var/lib') |
| 218 |
mock_Command_run.assert_called_once_with( |
| 219 |
[ |
| 220 |
'ln', '-s', '--no-target-directory', |
| 221 |
'../../usr/lib/sysimage/rpm', '../data/var/lib/rpm' |
| 222 |
], raise_on_error=False |
| 223 |
) |
| 224 |
|
| 225 |
@patch('kiwi.repository.dnf4.RpmDataBase') |
| 226 |
@patch('kiwi.command.Command.run') |
| 227 |
@patch('kiwi.repository.dnf4.Path.create') |
| 228 |
def test_setup_package_database_configuration_bootstrapped_system( |
| 229 |
self, mock_Path_create, mock_Command_run, mock_RpmDataBase |
| 230 |
): |
| 231 |
rpmdb = mock.Mock() |
| 232 |
rpmdb.has_rpm.return_value = True |
| 233 |
rpmdb.rpmdb_host.expand_query.return_value = '/usr/lib/sysimage/rpm' |
| 234 |
mock_RpmDataBase.return_value = rpmdb |
| 235 |
self.repo.setup_package_database_configuration() |
| 236 |
assert mock_RpmDataBase.call_args_list == [ |
| 237 |
call('../data', 'macros.kiwi-image-config'), |
| 238 |
call('../data') |
| 239 |
] |
| 240 |
rpmdb.set_macro_from_string.assert_called_once_with( |
| 241 |
'_install_langs%en_US:de_DE' |
| 242 |
) |
| 243 |
rpmdb.write_config.assert_called_once_with() |
| 244 |
rpmdb.link_database_to_host_path.assert_called_once_with() |
| 245 |
mock_Path_create.assert_called_once_with('../data/var/lib') |
| 246 |
mock_Command_run.assert_called_once_with( |
| 247 |
[ |
| 248 |
'ln', '-s', '--no-target-directory', |
| 249 |
'../../usr/lib/sysimage/rpm', '../data/var/lib/rpm' |
| 250 |
], raise_on_error=False |
| 251 |
) |
| 252 |
|
| 253 |
@patch('kiwi.repository.dnf4.ConfigParser') |
| 254 |
@patch('kiwi.repository.dnf4.Defaults.is_buildservice_worker') |
| 255 |
@patch('os.path.exists') |
| 256 |
@patch('kiwi.repository.dnf4.Path') |
| 257 |
def test_add_repo_with_gpgchecks( |
| 258 |
self, mock_path, mock_exists, mock_buildservice, mock_config |
| 259 |
): |
| 260 |
repo_config = mock.Mock() |
| 261 |
mock_buildservice.return_value = False |
| 262 |
mock_config.return_value = repo_config |
| 263 |
mock_exists.return_value = True |
| 264 |
|
| 265 |
with patch('builtins.open', create=True) as mock_open: |
| 266 |
self.repo.add_repo( |
| 267 |
'foo', 'kiwi_iso_mount/uri', 'rpm-md', 42, |
| 268 |
repo_gpgcheck=False, pkg_gpgcheck=True |
| 269 |
) |
| 270 |
|
| 271 |
repo_config.add_section.assert_called_once_with('foo') |
| 272 |
assert repo_config.set.call_args_list == [ |
| 273 |
call('foo', 'name', 'foo'), |
| 274 |
call('foo', 'baseurl', 'file://kiwi_iso_mount/uri'), |
| 275 |
call('foo', 'priority', '42'), |
| 276 |
call('foo', 'repo_gpgcheck', '0'), |
| 277 |
call('foo', 'gpgcheck', '1') |
| 278 |
] |
| 279 |
mock_open.assert_called_once_with( |
| 280 |
'/shared-dir/dnf/repos/foo.repo', 'w' |
| 281 |
) |
| 282 |
|
| 283 |
@patch('kiwi.repository.dnf4.RpmDataBase') |
| 284 |
def test_import_trusted_keys(self, mock_RpmDataBase): |
| 285 |
rpmdb = mock.Mock() |
| 286 |
mock_RpmDataBase.return_value = rpmdb |
| 287 |
signing_keys = ['key-file-a.asc', 'key-file-b.asc'] |
| 288 |
self.repo.import_trusted_keys(signing_keys) |
| 289 |
assert rpmdb.import_signing_key_to_image.call_args_list == [ |
| 290 |
call('key-file-a.asc'), |
| 291 |
call('key-file-b.asc') |
| 292 |
] |
| 293 |
|
| 294 |
@patch('kiwi.path.Path.wipe') |
| 295 |
def test_delete_repo(self, mock_wipe): |
| 296 |
self.repo.delete_repo('foo') |
| 297 |
mock_wipe.assert_called_once_with( |
| 298 |
'/shared-dir/dnf/repos/foo.repo' |
| 299 |
) |
| 300 |
|
| 301 |
@patch('kiwi.path.Path.wipe') |
| 302 |
@patch('os.walk') |
| 303 |
def test_cleanup_unused_repos(self, mock_walk, mock_path): |
| 304 |
mock_walk.return_value = [ |
| 305 |
('/foo', ('bar', 'baz'), ('spam', 'eggs')) |
| 306 |
] |
| 307 |
self.repo.repo_names = ['eggs'] |
| 308 |
self.repo.cleanup_unused_repos() |
| 309 |
mock_path.assert_called_once_with( |
| 310 |
'/shared-dir/dnf/repos/spam' |
| 311 |
) |
| 312 |
|
| 313 |
@patch('kiwi.path.Path.wipe') |
| 314 |
@patch('kiwi.path.Path.create') |
| 315 |
def test_delete_all_repos(self, mock_create, mock_wipe): |
| 316 |
self.repo.delete_all_repos() |
| 317 |
mock_wipe.assert_called_once_with( |
| 318 |
'/shared-dir/dnf/repos' |
| 319 |
) |
| 320 |
mock_create.assert_called_once_with( |
| 321 |
'/shared-dir/dnf/repos' |
| 322 |
) |
| 323 |
|
| 324 |
@patch('kiwi.path.Path.wipe') |
| 325 |
@patch('kiwi.repository.dnf4.glob.iglob') |
| 326 |
def test_delete_repo_cache(self, mock_glob, mock_wipe): |
| 327 |
mock_glob.return_value = ['foo_cache'] |
| 328 |
self.repo.delete_repo_cache('foo') |
| 329 |
mock_glob.assert_called_once_with( |
| 330 |
'/shared-dir/dnf/cache/foo*' |
| 331 |
) |
| 332 |
mock_wipe.assert_called_once_with( |
| 333 |
'foo_cache' |
| 334 |
) |
| 335 |
|
| 336 |
@patch('os.path.isfile') |
| 337 |
@patch('os.unlink') |
| 338 |
def test_cleanup(self, mock_os_unlink, mock_os_path_isfile): |
| 339 |
self.repo.cleanup() |
| 340 |
mock_os_unlink.assert_called_once_with('tmpfile') |