Debian Codegraph @

test/unit/system/prepare_test.py

raw
1 import logging
2 import os
3 from pytest import (
4 raises, fixture
5 )
6 from unittest.mock import (
7 patch, call, Mock, MagicMock, ANY
8 )
9
10 from kiwi.exceptions import (
11 KiwiBootStrapPhaseFailed,
12 KiwiSystemUpdateFailed,
13 KiwiSystemInstallPackagesFailed,
14 KiwiSystemDeletePackagesFailed,
15 KiwiInstallPhaseFailed,
16 KiwiPackagesDeletePhaseFailed
17 )
18
19 from kiwi.defaults import Defaults
20 from kiwi.system.prepare import SystemPrepare
21 from kiwi.xml_description import XMLDescription
22 from kiwi.xml_state import XMLState
23
24
25 class TestSystemPrepare:
26 @fixture(autouse=True)
27 def inject_fixtures(self, caplog):
28 self._caplog = caplog
29
30 @patch('kiwi.system.prepare.RootInit')
31 @patch('kiwi.system.prepare.RootBind')
32 @patch('kiwi.logger.Logger.get_logfile')
33 def setup(self, mock_get_logfile, mock_root_bind, mock_root_init):
34 Defaults.set_platform_name('x86_64')
35 mock_get_logfile.return_value = None
36 description = XMLDescription(
37 description='../data/example_config.xml',
38 derived_from='derived/description'
39 )
40 self.description_dir = os.path.dirname(description.description_origin)
41 self.xml = description.load()
42
43 self.manager = MagicMock(
44 return_value=MagicMock()
45 )
46 self.manager.package_requests = ['foo']
47 self.manager.collection_requests = ['foo']
48 self.manager.product_requests = ['foo']
49
50 root_init = MagicMock()
51 mock_root_init.return_value = root_init
52 root_bind = MagicMock()
53 root_bind.root_dir = 'root_dir'
54 mock_root_bind.return_value = root_bind
55 self.state = XMLState(
56 self.xml
57 )
58 self.system = SystemPrepare(
59 xml_state=self.state, root_dir='root_dir',
60 allow_existing=True
61 )
62 mock_root_init.assert_called_once_with(
63 'root_dir', True
64 )
65 root_init.create.assert_called_once_with()
66 mock_root_bind.assert_called_once_with(
67 root_init
68 )
69 root_bind.setup_intermediate_config.assert_called_once_with()
70 root_bind.mount_kernel_file_systems.assert_called_once_with(None)
71
72 @patch('kiwi.system.prepare.RootInit')
73 @patch('kiwi.system.prepare.RootBind')
74 @patch('kiwi.logger.Logger.get_logfile')
75 def setup_method(self, cls, mock_get_logfile, mock_root_bind, mock_root_init):
76 self.setup()
77
78 @patch('kiwi.system.prepare.RootImport.new')
79 @patch('kiwi.system.prepare.RootInit')
80 @patch('kiwi.system.prepare.RootBind')
81 @patch('kiwi.logger.Logger.get_logfile')
82 def test_init_with_derived_from_image(
83 self, mock_get_logfile, mock_root_bind, mock_root_init, mock_root_import
84 ):
85 mock_get_logfile.return_value = 'logfile'
86 description = XMLDescription(
87 description='../data/example_config.xml',
88 derived_from='derived/description'
89 )
90 xml = description.load()
91
92 root_init = MagicMock()
93 mock_root_init.return_value = root_init
94 root_import = Mock()
95 root_import.sync_data = Mock()
96 mock_root_import.return_value = root_import
97 root_bind = MagicMock()
98 root_bind.root_dir = 'root_dir'
99 mock_root_bind.return_value = root_bind
100 state = XMLState(
101 xml, profiles=['containerFlavour'], build_type='docker'
102 )
103 uri = Mock()
104 get_derived_from_image_uri = Mock(
105 return_value=uri
106 )
107 state.get_derived_from_image_uri = get_derived_from_image_uri
108 system = SystemPrepare(
109 xml_state=state, root_dir='root_dir',
110 )
111 mock_root_init.assert_called_once_with(
112 'root_dir', False
113 )
114 root_init.create.assert_called_once_with()
115 mock_root_import.assert_called_once_with(
116 'root_dir', uri,
117 state.build_type.get_image()
118 )
119 root_import.sync_data.assert_called_once_with()
120 mock_root_bind.assert_called_once_with(
121 root_init
122 )
123 root_bind.setup_intermediate_config.assert_called_once_with()
124 root_bind.mount_kernel_file_systems.assert_called_once_with(None)
125 assert system.issue_message == '{headline}: {reason}'
126
127 @patch('kiwi.system.prepare.RootImport.new')
128 @patch('kiwi.system.prepare.RootInit')
129 @patch('kiwi.system.prepare.RootBind')
130 @patch('kiwi.logger.Logger.get_logfile')
131 def test_init_with_derived_from_image_for_delta_root(
132 self, mock_get_logfile, mock_root_bind, mock_root_init, mock_root_import
133 ):
134 mock_get_logfile.return_value = 'logfile'
135 description = XMLDescription(
136 description='../data/example_config.xml',
137 derived_from='derived/description'
138 )
139 xml = description.load()
140
141 root_init = MagicMock()
142 mock_root_init.return_value = root_init
143 root_import = Mock()
144 root_import.overlay_data = Mock()
145
146 mock_root_import.return_value = root_import
147 root_bind = MagicMock()
148 root_bind.root_dir = 'root_dir'
149 mock_root_bind.return_value = root_bind
150 state = XMLState(
151 xml, profiles=['containerFlavour'], build_type='docker'
152 )
153 uri = Mock()
154 get_derived_from_image_uri = Mock(
155 return_value=uri
156 )
157 get_delta_root = Mock(
158 return_value=True
159 )
160 state.get_derived_from_image_uri = get_derived_from_image_uri
161 state.build_type.get_delta_root = get_delta_root
162 system = SystemPrepare(
163 xml_state=state, root_dir='root_dir',
164 )
165 mock_root_init.assert_called_once_with(
166 'root_dir', False
167 )
168 root_init.create.assert_called_once_with()
169 mock_root_import.assert_called_once_with(
170 'root_dir', uri,
171 state.build_type.get_image()
172 )
173 root_import.overlay_data.assert_called_once_with()
174 mock_root_bind.assert_called_once_with(
175 root_init
176 )
177 root_bind.setup_intermediate_config.assert_called_once_with()
178 root_bind.mount_kernel_file_systems.assert_called_once_with(True)
179 assert system.issue_message == '{headline}: {reason}'
180
181 @patch('kiwi.system.prepare.CommandProcess.poll_show_progress')
182 def test_install_bootstrap_packages_raises(self, mock_poll):
183 self.manager.get_error_details.return_value = 'error-details'
184 mock_poll.side_effect = Exception('some_error')
185 with raises(KiwiBootStrapPhaseFailed) as issue:
186 self.system.install_bootstrap(self.manager)
187 assert issue.value.message == self.system.issue_message.format(
188 headline='Bootstrap package installation failed',
189 reason='some_error: error-details'
190 )
191
192 @patch('kiwi.system.prepare.CommandProcess.poll_show_progress')
193 @patch('kiwi.system.prepare.ArchiveTar')
194 def test_install_bootstrap_archives_raises(
195 self, mock_tar, mock_poll
196 ):
197 mock_tar.side_effect = Exception
198 with raises(KiwiBootStrapPhaseFailed):
199 self.system.install_bootstrap(self.manager)
200
201 @patch('kiwi.system.prepare.CommandProcess.poll')
202 def test_update_system_raises(self, mock_poll):
203 mock_poll.side_effect = Exception
204 with raises(KiwiSystemUpdateFailed):
205 self.system.update_system(self.manager)
206
207 @patch('kiwi.system.prepare.CommandProcess.poll_show_progress')
208 def test_install_packages_raises(self, mock_poll):
209 mock_poll.side_effect = Exception
210 with raises(KiwiSystemInstallPackagesFailed):
211 self.system.install_packages(self.manager, ['package'])
212
213 @patch('kiwi.system.prepare.CommandProcess.poll_show_progress')
214 def test_install_system_packages_raises(self, mock_poll):
215 mock_poll.side_effect = Exception
216 with raises(KiwiInstallPhaseFailed):
217 self.system.install_system(self.manager)
218
219 @patch('kiwi.system.prepare.CommandProcess.poll_show_progress')
220 @patch('kiwi.system.prepare.ArchiveTar')
221 def test_install_system_archives_raises(
222 self, mock_tar, mock_poll
223 ):
224 mock_tar.side_effect = KiwiInstallPhaseFailed
225 with raises(KiwiInstallPhaseFailed):
226 self.system.install_system(self.manager)
227
228 @patch('kiwi.system.prepare.CommandProcess.poll_show_progress')
229 def test_delete_packages_raises(self, mock_poll):
230 mock_poll.side_effect = Exception
231 with raises(KiwiSystemDeletePackagesFailed):
232 self.system.delete_packages(self.manager, ['package'])
233
234 @patch('kiwi.system.prepare.Repository.new')
235 @patch('kiwi.system.prepare.Uri')
236 @patch('kiwi.system.prepare.PackageManager')
237 @patch('kiwi.xml_state.XMLState.get_package_manager')
238 @patch('os.path.exists')
239 def test_setup_repositories(
240 self, mock_exists, mock_package_manager,
241 mock_manager, mock_uri, mock_repo
242 ):
243 mock_exists.return_value = True
244 mock_package_manager.return_value = 'package-manager-name'
245 uri = Mock()
246 mock_uri.return_value = uri
247 self.system.root_bind = Mock()
248 uri.is_remote = Mock(
249 return_value=False
250 )
251 uri.translate = Mock(
252 return_value='uri'
253 )
254 uri.alias = Mock(
255 return_value='uri-alias'
256 )
257 uri.credentials_file_name = Mock(
258 return_value='credentials-file'
259 )
260 repo = Mock()
261 mock_repo.return_value.__enter__.return_value = repo
262
263 self.system.setup_repositories(
264 clear_cache=True,
265 signing_keys=['key-file-a.asc', 'key-file-b.asc'],
266 target_arch='x86_64'
267 )
268
269 mock_repo.assert_called_once_with(
270 self.system.root_bind, 'package-manager-name', [
271 'check_signatures', 'exclude_docs',
272 '_install_langs%POSIX:C:C.UTF-8:en_US:de_DE',
273 '_target_arch%x86_64'
274 ]
275 )
276 # mock local repos will be translated and bind mounted
277 assert uri.translate.call_args_list == [
278 call(), call()
279 ]
280 assert self.system.root_bind.mount_shared_directory.call_args_list == [
281 call('uri'), call('uri')
282 ]
283 assert uri.alias.call_args_list == [
284 call(), call()
285 ]
286 assert repo.add_repo.call_args_list == [
287 call(
288 'uri-alias', 'uri', None, 42,
289 None, None, None, None, 'credentials-file', None, None,
290 'baseurl', None, None
291 ),
292 call(
293 'uri-alias', 'uri', 'rpm-md', None,
294 None, None, None, None, 'credentials-file', None, None,
295 None, '../data/script', None
296 )
297 ]
298 assert repo.delete_repo_cache.call_args_list == [
299 call('uri-alias'),
300 call('uri-alias')
301 ]
302 repo.setup_package_database_configuration.assert_called_once_with()
303 repo.import_trusted_keys.assert_called_once_with(
304 ['key-file-a.asc', 'key-file-b.asc']
305 )
306
307 @patch('kiwi.system.prepare.Repository.new')
308 @patch('kiwi.system.prepare.Uri')
309 @patch('kiwi.system.prepare.PackageManager')
310 @patch('kiwi.xml_state.XMLState.get_package_manager')
311 @patch('os.path.exists')
312 def test_setup_repositories_local_not_existing(
313 self, mock_exists, mock_package_manager,
314 mock_manager, mock_uri, mock_repo
315 ):
316 mock_exists.return_value = False
317 mock_package_manager.return_value = 'package-manager-name'
318 uri = Mock()
319 mock_uri.return_value = uri
320 self.system.root_bind = Mock()
321 uri.is_remote = Mock(
322 return_value=False
323 )
324 uri.translate = Mock(
325 return_value='uri'
326 )
327 uri.alias = Mock(
328 return_value='uri-alias'
329 )
330 repo = Mock()
331 mock_repo.return_value.__enter__.return_value = repo
332 with self._caplog.at_level(logging.WARNING):
333 self.system.setup_repositories()
334
335 @patch('kiwi.xml_state.XMLState.get_bootstrap_collection_type')
336 @patch('kiwi.system.prepare.CommandProcess.poll_show_progress')
337 @patch('kiwi.system.prepare.ArchiveTar')
338 @patch('os.path.exists')
339 def test_install_bootstrap(
340 self, mock_exists, mock_tar, mock_poll, mock_collection_type
341 ):
342 mock_exists.return_value = True
343 tar = Mock()
344 tar.extract = Mock()
345 mock_tar.return_value = tar
346 mock_collection_type.return_value = 'onlyRequired'
347 self.system.install_bootstrap(self.manager)
348 self.manager.process_only_required.assert_called_once_with()
349 self.manager.request_package.assert_any_call(
350 'filesystem'
351 )
352 self.manager.request_package.assert_any_call(
353 'zypper'
354 )
355 self.manager.request_collection.assert_called_once_with(
356 'bootstrap-collection'
357 )
358 self.manager.request_product.assert_called_once_with(
359 'kiwi'
360 )
361 self.manager.setup_repository_modules.assert_called_once_with(
362 {'disable': ['mod_c'], 'enable': ['mod_a:stream', 'mod_b']}
363 )
364 self.manager.process_install_requests_bootstrap.assert_called_once_with(
365 self.system.root_bind, None
366 )
367 mock_tar.assert_called_once_with(
368 '{0}/bootstrap.tgz'.format(self.description_dir)
369 )
370 tar.extract.assert_called_once_with('root_dir')
371 self.manager.post_process_install_requests_bootstrap.assert_called_once_with(
372 self.system.root_bind, None
373 )
374
375 @patch('kiwi.system.prepare.RootInit')
376 @patch('kiwi.system.prepare.RootBind')
377 @patch('kiwi.system.prepare.CommandProcess.poll_show_progress')
378 @patch('kiwi.system.prepare.ArchiveTar')
379 def test_install_bootstrap_archive_target_dir(
380 self, mock_tar, mock_poll, mock_root_bind, mock_root_init
381 ):
382 Defaults.set_platform_name('x86_64')
383 description = XMLDescription(
384 description='../data/example_config_target_dir.xml'
385 )
386 self.xml = description.load()
387 root_bind = MagicMock()
388 root_bind.root_dir = 'root_dir'
389 mock_root_bind.return_value = root_bind
390 self.state = XMLState(
391 self.xml
392 )
393 self.system = SystemPrepare(
394 xml_state=self.state, root_dir='root_dir',
395 allow_existing=True
396 )
397 tar = Mock()
398 mock_tar.return_value = tar
399
400 self.system.install_bootstrap(self.manager)
401
402 assert tar.extract.call_args_list == [
403 call('root_dir/foo'),
404 call('root_dir/etc')
405 ]
406
407 @patch('kiwi.xml_state.XMLState.get_bootstrap_packages_sections')
408 def test_install_bootstrap_skipped(self, mock_bootstrap_section):
409 mock_bootstrap_section.return_value = []
410 self.system.install_bootstrap(self.manager)
411 with self._caplog.at_level(logging.WARNING):
412 mock_bootstrap_section.assert_called_once_with()
413
414 @patch('kiwi.xml_state.XMLState.get_bootstrap_collection_type')
415 @patch('kiwi.system.prepare.CommandProcess.poll_show_progress')
416 @patch('kiwi.system.prepare.ArchiveTar')
417 @patch('os.path.exists')
418 def test_install_bootstrap_archive_from_derived_description(
419 self, mock_exists, mock_tar, mock_poll,
420 mock_collection_type
421 ):
422 mock_exists.return_value = False
423 self.system.install_bootstrap(self.manager)
424 mock_tar.assert_called_once_with(
425 'derived/description/bootstrap.tgz'
426 )
427
428 @patch('kiwi.xml_state.XMLState.get_system_collection_type')
429 @patch('kiwi.system.prepare.CommandProcess.poll_show_progress')
430 @patch('kiwi.system.prepare.ArchiveTar')
431 @patch('os.path.exists')
432 def test_install_system(
433 self, mock_exists, mock_tar, mock_poll, mock_collection_type
434 ):
435 mock_exists.return_value = True
436 tar = Mock()
437 tar.extract = Mock()
438 mock_tar.return_value = tar
439 mock_collection_type.return_value = 'onlyRequired'
440 self.system.install_system(self.manager)
441 self.manager.process_only_required.assert_called_once_with()
442 self.manager.request_package.assert_any_call(
443 'plymouth-branding-openSUSE'
444 )
445 self.manager.request_collection.assert_any_call(
446 'base'
447 )
448 self.manager.request_product.assert_any_call(
449 'openSUSE'
450 )
451 self.manager.request_package_exclusion.assert_any_call(
452 'foo'
453 )
454 self.manager.process_install_requests.assert_called_once_with()
455 mock_tar.assert_called_once_with('/absolute/path/to/image.tgz')
456 tar.extract.assert_called_once_with('root_dir')
457
458 @patch('kiwi.system.prepare.CommandProcess.poll_show_progress')
459 def test_install_packages(self, mock_poll):
460 self.system.install_packages(self.manager, ['foo'])
461 self.manager.request_package.assert_called_once_with('foo')
462
463 @patch('kiwi.system.prepare.CommandProcess.poll_show_progress')
464 def test_delete_packages(self, mock_poll):
465 self.system.delete_packages(self.manager, ['foo'])
466 self.manager.request_package.assert_called_once_with('foo')
467
468 @patch('kiwi.system.prepare.CommandProcess.poll_show_progress')
469 def test_pinch_system(self, mock_poll):
470 self.system.pinch_system(self.manager, force=False)
471 self.system.pinch_system(self.manager, force=True)
472 self.manager.process_delete_requests.assert_has_calls(
473 [call(False), call(True)]
474 )
475 self.manager.post_process_delete_requests.assert_has_calls(
476 [call(self.system.root_bind), call(self.system.root_bind)]
477 )
478
479 @patch('kiwi.system.prepare.CommandProcess.poll_show_progress')
480 @patch('kiwi.system.prepare.Command.run')
481 def test_pinch_system_in_delta_root_mode(self, mock_Command_run, mock_poll):
482 self.system.delta_root = True
483 self.system.pinch_system(self.manager)
484 mock_Command_run.assert_called_once_with(
485 ['rsync', '-a', 'root_dir_cow/', 'root_dir_cow_before_pinch']
486 )
487
488 @patch('kiwi.system.prepare.CommandProcess.poll_show_progress')
489 def test_pinch_system_raises(self, mock_poll):
490 mock_poll.side_effect = Exception
491 with raises(KiwiPackagesDeletePhaseFailed):
492 self.system.pinch_system(self.manager)
493 self.manager.process_delete_requests.assert_called_once_with(False)
494
495 @patch('kiwi.package_manager.zypper.PackageManagerZypper.process_delete_requests')
496 @patch('kiwi.system.prepare.Repository.new')
497 @patch('kiwi.system.prepare.CommandProcess.poll_show_progress')
498 def test_pinch_system_without_manager(
499 self, mock_poll, mock_repo, mock_requests
500 ):
501 self.system.pinch_system()
502 mock_repo.assert_called_once_with(ANY, 'zypper')
503 mock_requests.assert_called_once_with(False)
504
505 @patch('kiwi.system.prepare.CommandProcess.poll')
506 def test_update_system(self, mock_poll):
507 self.system.update_system(self.manager)
508 self.manager.update.assert_called_once_with()
509
510 @patch('kiwi.system.prepare.RootBind')
511 @patch('kiwi.system.prepare.RootInit')
512 @patch('kiwi.logger.Logger.get_logfile')
513 def test_context_manager_exit(
514 self, mock_get_logfile, mock_RootInit, mock_RootBind
515 ):
516 root_bind = Mock()
517 mock_RootBind.return_value = root_bind
518 root_import = Mock()
519 with SystemPrepare(
520 xml_state=self.state, root_dir='root_dir', allow_existing=True
521 ) as system:
522 system.root_import = root_import
523 root_bind.cleanup.assert_called_once_with()
524 root_import.overlay_finalize.assert_called_once_with(self.state)
525
526 @patch('kiwi.system.prepare.Repository.new')
527 @patch('kiwi.system.prepare.PackageManager.new')
528 def test_clean_package_manager_leftovers(self, mock_manager, mock_repo):
529 manager = Mock()
530 mock_manager.return_value.__enter__.return_value = manager
531 self.system.clean_package_manager_leftovers()
532 manager.clean_leftovers.assert_called_once_with()
533
534 @patch('os.path.isdir', return_value=True)
535 @patch('os.path.exists', return_value=True)
536 def test_setup_ca_certificates_name_exists_but_is_dir(
537 self, mock_os_path_exists, mock_os_path_isdir
538 ):
539 with self._caplog.at_level(logging.WARNING):
540 self.system.setup_ca_certificates()
541 assert '/some/ca/filename: does not exist or is directory' in \
542 self._caplog.text
543
544 def test_setup_ca_certificates_no_update_info(self):
545 self.state.get_certificates_target_distribution = Mock(
546 return_value=None
547 )
548 with self._caplog.at_level(logging.WARNING):
549 self.system.setup_ca_certificates()
550 assert 'Could not determine CA update tool, skipping setup' in \
551 self._caplog.text
552
553 @patch('kiwi.system.prepare.Command.run')
554 @patch('kiwi.system.prepare.DataSync')
555 @patch('os.path.exists', return_value=True)
556 @patch('kiwi.system.prepare.Path')
557 @patch('os.path.isdir', return_value=False)
558 def test_setup_ca_certificates_success(
559 self,
560 mock_os_path_isdir,
561 mock_Path,
562 mock_os_path_exists,
563 mock_DataSync,
564 mock_Command_run
565 ):
566 self.state.get_certificates_target_distribution = Mock(
567 return_value='suse'
568 )
569 mock_DataSync_instance = Mock()
570 mock_DataSync.return_value = mock_DataSync_instance
571
572 self.system.setup_ca_certificates()
573
574 ca_chroot_path = 'root_dir/etc/pki/trust/anchors'
575 mock_Path.create.assert_called_once_with(ca_chroot_path)
576 mock_DataSync.assert_called_once_with(
577 '/some/ca/filename', ca_chroot_path
578 )
579 mock_Command_run.assert_called_once_with(
580 ['chroot', 'root_dir', 'update-ca-certificates']
581 )
582
583 @patch('kiwi.system.prepare.Command.run')
584 @patch('kiwi.system.prepare.DataSync')
585 @patch('os.path.exists', return_value=True)
586 @patch('kiwi.system.prepare.Path')
587 @patch('os.path.isdir', return_value=False)
588 def test_setup_ca_certificates_raises(
589 self,
590 mock_os_path_isdir,
591 mock_Path,
592 mock_os_path_exists,
593 mock_DataSync,
594 mock_Command_run
595 ):
596 self.state.get_certificates_target_distribution = Mock(
597 return_value='suse'
598 )
599 mock_DataSync.side_effect = Exception
600
601 with raises(KiwiBootStrapPhaseFailed):
602 self.system.setup_ca_certificates()