Debian Codegraph @

test/unit/system/setup_test.py

raw
1 import sys
2 import os
3 import logging
4 import io
5 from unittest.mock import (
6 ANY, DEFAULT, patch, call, Mock, MagicMock, mock_open
7 )
8 from pytest import (
9 raises, fixture
10 )
11 from collections import namedtuple
12
13 from ..test_helper import argv_kiwi_tests
14
15 from kiwi.system.setup import SystemSetup
16 from kiwi.xml_description import XMLDescription
17 from kiwi.xml_state import XMLState
18 from kiwi.defaults import Defaults
19
20 from kiwi.exceptions import (
21 KiwiCommandError,
22 KiwiScriptFailed,
23 KiwiImportDescriptionError,
24 KiwiFileNotFound
25 )
26
27 from kiwi.command import CommandT
28
29
30 class TestSystemSetup:
31 @fixture(autouse=True)
32 def inject_fixtures(self, caplog):
33 self._caplog = caplog
34
35 @patch('kiwi.system.setup.RuntimeConfig')
36 def setup(self, mock_RuntimeConfig):
37 Defaults.set_platform_name('x86_64')
38 self.runtime_config = Mock()
39 self.runtime_config.get_package_changes = Mock(
40 return_value=True
41 )
42 mock_RuntimeConfig.return_value = self.runtime_config
43 self.xml_state = MagicMock()
44 self.xml_state.get_package_manager = Mock(
45 return_value='zypper'
46 )
47 self.xml_state.build_type.get_selinux_policy = Mock(
48 return_value=None
49 )
50 self.xml_state.build_type.get_filesystem = Mock(
51 return_value='ext3'
52 )
53 self.xml_state.xml_data.get_name = Mock(
54 return_value='some-image'
55 )
56 self.xml_state.get_image_version = Mock(
57 return_value='1.2.3'
58 )
59 self.xml_state.build_type.get_provide_system_files = Mock(
60 return_value=True
61 )
62 self.xml_state.xml_data.description_dir = 'description_dir'
63 self.setup = SystemSetup(
64 self.xml_state, 'root_dir'
65 )
66 description = XMLDescription(
67 description='../data/example_config.xml',
68 derived_from='derived/description'
69 )
70 self.description_dir = os.path.dirname(description.description_origin)
71 self.setup_with_real_xml = SystemSetup(
72 XMLState(description.load()), 'root_dir'
73 )
74 command_run = namedtuple(
75 'command', ['output', 'error', 'returncode']
76 )
77 self.run_result = command_run(
78 output='password-hash\n',
79 error='stderr',
80 returncode=0
81 )
82 self.selinux_policies = os.scandir(
83 '../data/etc/selinux/targeted/policy'
84 )
85
86 @patch('kiwi.system.setup.RuntimeConfig')
87 def setup_method(self, cls, mock_RuntimeConfig):
88 self.setup()
89
90 def teardown(self):
91 sys.argv = argv_kiwi_tests
92
93 def teardown_method(self, cls):
94 self.teardown()
95
96 def test_setup_ix86(self):
97 Defaults.set_platform_name('i686')
98 setup = SystemSetup(
99 MagicMock(), 'root_dir'
100 )
101 assert setup.arch == 'ix86'
102
103 @patch('kiwi.command.Command.run')
104 @patch('os.path.exists')
105 @patch('kiwi.system.setup.glob.iglob')
106 def test_import_description(
107 self, mock_iglob, mock_path, mock_command
108 ):
109 mock_iglob.return_value = ['config-cdroot.tar.xz']
110 mock_path.return_value = True
111
112 with patch('builtins.open'):
113 self.setup_with_real_xml.import_description()
114
115 mock_iglob.assert_called_once_with(
116 '{0}/config-cdroot.tar*'.format(self.description_dir)
117 )
118 assert mock_command.call_args_list == [
119 call(
120 [
121 'cp', '{0}/config-host-overlay.sh'.format(self.description_dir),
122 'root_dir/image/config-host-overlay.sh'
123 ]
124 ),
125 call(
126 [
127 'cp', '{0}/config-overlay.sh'.format(self.description_dir),
128 'root_dir/image/config-overlay.sh'
129 ]
130 ),
131 call(
132 [
133 'cp', '{0}/config.sh'.format(self.description_dir),
134 'root_dir/image/config.sh'
135 ]
136 ),
137 call(
138 [
139 'cp', '{0}/disk.sh'.format(self.description_dir),
140 'root_dir/image/disk.sh'
141 ]
142 ),
143 call(
144 [
145 'cp',
146 '{0}/my_edit_boot_script'.format(self.description_dir),
147 'root_dir/image/edit_boot_config.sh'
148 ]
149 ),
150 call(
151 [
152 'cp', '/absolute/path/to/my_edit_boot_install',
153 'root_dir/image/edit_boot_install.sh'
154 ]
155 ),
156 call(
157 [
158 'cp', '{0}/images.sh'.format(self.description_dir),
159 'root_dir/image/images.sh'
160 ]
161 ),
162 call(
163 [
164 'cp', '{0}/post_bootstrap.sh'.format(self.description_dir),
165 'root_dir/image/post_bootstrap.sh'
166 ]
167 ),
168 call(
169 [
170 'cp', '{0}/pre_disk_sync.sh'.format(self.description_dir),
171 'root_dir/image/pre_disk_sync.sh'
172 ]
173 ),
174 call(
175 [
176 'cp', Defaults.project_file('config/functions.sh'),
177 'root_dir/.kconfig'
178 ]
179 ),
180 call(
181 [
182 'cp', '/absolute/path/to/image.tgz', 'root_dir/image/'
183 ]
184 ),
185 call(
186 [
187 'cp', '{0}/bootstrap.tgz'.format(self.description_dir),
188 'root_dir/image/'
189 ]
190 ),
191 call(
192 [
193 'cp', '{0}/some'.format(self.description_dir),
194 'root_dir/image/'
195 ]
196 ),
197 call(
198 [
199 'cp', '/absolute/path/to/some',
200 'root_dir/image/'
201 ]
202 ),
203 call(
204 [
205 'cp', '{0}/some'.format(self.description_dir),
206 'root_dir/image/'
207 ]
208 ),
209 call(
210 ['cp', 'config-cdroot.tar.xz', 'root_dir/image/']
211 )
212 ]
213
214 @patch('kiwi.path.Path.create')
215 @patch('kiwi.command.Command.run')
216 @patch('os.path.exists')
217 def test_import_description_archive_from_derived(
218 self, mock_path, mock_command, mock_create
219 ):
220 path_return_values = [
221 True, True, True, False, True, False, True, True,
222 True, True, True, True, True, True, True, True
223 ]
224
225 def side_effect(arg):
226 return path_return_values.pop()
227
228 mock_path.side_effect = side_effect
229
230 with patch('builtins.open'):
231 self.setup_with_real_xml.import_description()
232
233 assert mock_command.call_args_list == [
234 call(
235 [
236 'cp', '{0}/config-host-overlay.sh'.format(self.description_dir),
237 'root_dir/image/config-host-overlay.sh'
238 ]
239 ),
240 call(
241 [
242 'cp', '{0}/config-overlay.sh'.format(self.description_dir),
243 'root_dir/image/config-overlay.sh'
244 ]
245 ),
246 call(
247 [
248 'cp', '{0}/config.sh'.format(self.description_dir),
249 'root_dir/image/config.sh'
250 ]
251 ),
252 call(
253 [
254 'cp', '{0}/disk.sh'.format(self.description_dir),
255 'root_dir/image/disk.sh'
256 ]
257 ),
258 call(
259 [
260 'cp',
261 '{0}/my_edit_boot_script'.format(self.description_dir),
262 'root_dir/image/edit_boot_config.sh'
263 ]
264 ),
265 call(
266 [
267 'cp', '/absolute/path/to/my_edit_boot_install',
268 'root_dir/image/edit_boot_install.sh'
269 ]
270 ),
271 call(
272 [
273 'cp', '{0}/images.sh'.format(self.description_dir),
274 'root_dir/image/images.sh'
275 ]
276 ),
277 call(
278 [
279 'cp', '{0}/post_bootstrap.sh'.format(self.description_dir),
280 'root_dir/image/post_bootstrap.sh'
281 ]
282 ),
283 call(
284 [
285 'cp', '{0}/pre_disk_sync.sh'.format(self.description_dir),
286 'root_dir/image/pre_disk_sync.sh'
287 ]
288 ),
289 call(
290 [
291 'cp', Defaults.project_file('config/functions.sh'),
292 'root_dir/.kconfig'
293 ]
294 ),
295 call(
296 ['cp', '/absolute/path/to/image.tgz', 'root_dir/image/']
297 ),
298 call(
299 ['cp', 'derived/description/bootstrap.tgz', 'root_dir/image/']
300 ),
301 call(
302 [
303 'cp', 'derived/description/some',
304 'root_dir/image/'
305 ]
306 ),
307 call(
308 [
309 'cp', '/absolute/path/to/some',
310 'root_dir/image/'
311 ]
312 ),
313 call(
314 [
315 'cp', '{0}/some'.format(self.description_dir),
316 'root_dir/image/'
317 ]
318 )
319 ]
320 mock_create.assert_called_once_with('root_dir/image')
321
322 @patch('kiwi.command.Command.run')
323 @patch('os.path.exists')
324 def test_import_description_configured_editboot_scripts_not_found(
325 self, mock_path, mock_command
326 ):
327 path_return_values = [False, True, True, True, True, True]
328
329 def side_effect(arg):
330 return path_return_values.pop()
331
332 mock_path.side_effect = side_effect
333 with patch('builtins.open'):
334 with raises(KiwiImportDescriptionError):
335 self.setup_with_real_xml.import_description()
336
337 @patch('kiwi.path.Path.create')
338 @patch('kiwi.command.Command.run')
339 @patch('os.path.exists')
340 def test_import_description_configured_archives_not_found(
341 self, mock_path, mock_command, mock_create
342 ):
343 path_return_values = [
344 False, False, True, True, True, True, True, True, True, True
345 ]
346
347 def side_effect(arg):
348 return path_return_values.pop()
349
350 mock_path.side_effect = side_effect
351
352 with patch('builtins.open'):
353 with raises(KiwiImportDescriptionError):
354 self.setup_with_real_xml.import_description()
355
356 @patch('kiwi.path.Path.create')
357 @patch('kiwi.command.Command.run')
358 @patch('os.path.exists')
359 def test_import_description_configured_files_not_found(
360 self, mock_path, mock_command, mock_create
361 ):
362 path_return_values = [
363 False, False, True, True, True, True, True,
364 True, True, True, True, True, True
365 ]
366
367 def side_effect(arg):
368 return path_return_values.pop()
369
370 mock_path.side_effect = side_effect
371
372 with patch('builtins.open'):
373 with raises(KiwiImportDescriptionError):
374 self.setup_with_real_xml.import_description()
375
376 @patch('kiwi.command.Command.run')
377 @patch('os.path.isdir')
378 @patch('kiwi.system.setup.Path.wipe')
379 @patch('kiwi.system.setup.MountManager')
380 def test_cleanup(
381 self,
382 mock_MountManager,
383 mock_Path_wipe,
384 mock_os_path_isdir,
385 mock_Command_run
386 ):
387 image_meta = Mock()
388 mock_MountManager.return_value = image_meta
389 mock_os_path_isdir.return_value = True
390 self.setup.cleanup()
391 mock_Command_run.assert_called_once_with(
392 [
393 'chroot', 'root_dir', 'rm', '-f',
394 '.kconfig',
395 '.profile',
396 'config.bootoptions'
397 ]
398 )
399 mock_MountManager.assert_called_once_with(
400 device='none', mountpoint='root_dir/image'
401 )
402 image_meta.umount.assert_called_once_with()
403 mock_Path_wipe.assert_called_once_with('root_dir/image')
404
405 @patch('kiwi.system.setup.ArchiveTar')
406 @patch('kiwi.system.setup.glob.iglob')
407 def test_import_cdroot_files(self, mock_iglob, mock_ArchiveTar):
408 archive = Mock()
409 mock_ArchiveTar.return_value = archive
410 mock_iglob.return_value = ['config-cdroot.tar.xz']
411 self.setup.import_cdroot_files('target_dir')
412 mock_iglob.assert_called_once_with('description_dir/config-cdroot.tar*')
413 mock_ArchiveTar.assert_called_once_with('config-cdroot.tar.xz')
414 archive.extract.assert_called_once_with('target_dir')
415
416 @patch('kiwi.command.Command.run')
417 @patch('kiwi.system.setup.DataSync')
418 @patch('kiwi.system.setup.Path.create')
419 def test_import_files(
420 self, mock_Path_create, mock_DataSync, mock_Command_run
421 ):
422 data = Mock()
423 mock_DataSync.return_value = data
424 self.setup_with_real_xml.import_files()
425 assert mock_Command_run.call_args_list == [
426 call(
427 [
428 'chroot', 'root_dir',
429 'chown', 'bob:users', '/image//absolute/path/to/some'
430 ]
431 ),
432 call(
433 [
434 'chroot', 'root_dir',
435 'chmod', 'u+x', '/image//absolute/path/to/some'
436 ]
437 )
438 ]
439 mock_Path_create.assert_called_once_with('root_dir/some')
440 assert data.sync_data.called
441
442 @patch('kiwi.command.Command.run')
443 @patch('kiwi.system.setup.DataSync')
444 @patch('os.path.exists')
445 def test_import_overlay_files_copy_links(
446 self, mock_os_path, mock_DataSync, mock_command
447 ):
448 data = Mock()
449 mock_DataSync.return_value = data
450 mock_os_path.return_value = True
451 self.setup.import_overlay_files(
452 follow_links=True, preserve_owner_group=True
453 )
454 mock_DataSync.assert_called_once_with(
455 'description_dir/root/', 'root_dir'
456 )
457 data.sync_data.assert_called_once_with(
458 options=[
459 '-r', '-p', '-t', '-D', '-H', '-X', '-A',
460 '--one-file-system', '--copy-links', '-o', '-g'
461 ]
462 )
463
464 @patch('kiwi.command.Command.run')
465 @patch('kiwi.system.setup.DataSync')
466 @patch('os.path.exists')
467 def test_import_overlay_files_links(
468 self, mock_os_path, mock_DataSync, mock_command
469 ):
470 data = Mock()
471 mock_DataSync.return_value = data
472 mock_os_path.return_value = True
473 self.setup.import_overlay_files(
474 follow_links=False, preserve_owner_group=True
475 )
476 mock_DataSync.assert_called_once_with(
477 'description_dir/root/', 'root_dir'
478 )
479 data.sync_data.assert_called_once_with(
480 options=[
481 '-r', '-p', '-t', '-D', '-H', '-X', '-A',
482 '--one-file-system', '--links', '-o', '-g'
483 ]
484 )
485
486 @patch('kiwi.system.setup.ArchiveTar')
487 @patch('os.path.exists')
488 def test_import_overlay_files_from_archive(
489 self, mock_os_path, mock_archive
490 ):
491 archive = Mock()
492 mock_archive.return_value = archive
493
494 exists_results = [True, False]
495
496 def side_effect(arg):
497 return exists_results.pop()
498
499 mock_os_path.side_effect = side_effect
500
501 self.setup.import_overlay_files()
502
503 mock_archive.assert_called_once_with(
504 'description_dir/root.tar.gz'
505 )
506 archive.extract.assert_called_once_with(
507 'root_dir'
508 )
509
510 @patch('kiwi.system.setup.DataSync')
511 @patch('os.path.exists')
512 def test_import_overlay_files_from_profile(
513 self, mock_os_path, mock_DataSync
514 ):
515 exists_results = [True, False, False]
516
517 def side_effect(arg):
518 return exists_results.pop()
519
520 data = Mock()
521 mock_DataSync.return_value = data
522 mock_os_path.side_effect = side_effect
523 self.xml_state.profiles = ['profile_root']
524 self.setup.import_overlay_files()
525 mock_DataSync.assert_called_once_with(
526 'description_dir/profile_root/', 'root_dir'
527 )
528 data.sync_data.assert_called_once_with(
529 options=[
530 '-r', '-p', '-t', '-D', '-H', '-X', '-A',
531 '--one-file-system', '--links'
532 ]
533 )
534
535 @patch('kiwi.system.setup.Shell.run_common_function')
536 @patch('kiwi.system.setup.Command.run')
537 @patch('os.path.exists')
538 def test_setup_keyboard_map(self, mock_path, mock_run, mock_shell):
539 mock_path.return_value = True
540 self.setup.preferences['keytable'] = 'keytable'
541 self.setup.setup_keyboard_map()
542 mock_shell.assert_called_once_with(
543 'baseUpdateSysConfig', [
544 'root_dir/etc/sysconfig/keyboard', 'KEYTABLE', '"keytable"'
545 ]
546 )
547
548 @patch('kiwi.system.setup.CommandCapabilities.has_option_in_help')
549 @patch('kiwi.system.setup.Shell.run_common_function')
550 @patch('kiwi.system.setup.Command.run')
551 @patch('os.path.exists')
552 def test_setup_keyboard_map_with_systemd(
553 self, mock_path, mock_run, mock_shell, mock_caps
554 ):
555 mock_caps.return_value = True
556 mock_path.return_value = True
557 mock_run.return_value = CommandT("", "", 0)
558 self.setup.preferences['keytable'] = 'keytable'
559 self.setup.setup_keyboard_map()
560 mock_run.assert_has_calls([
561 call(['rm', '-r', '-f', 'root_dir/etc/vconsole.conf']),
562 call([
563 'chroot', 'root_dir', 'systemd-firstboot',
564 '--keymap=keytable'
565 ], raise_on_error=False)
566 ])
567
568 @patch('os.path.exists')
569 @patch('kiwi.system.setup.CommandCapabilities.has_option_in_help')
570 def test_setup_keyboard_skipped(self, mock_caps, mock_exists):
571 mock_caps.return_value = False
572 mock_exists.return_value = False
573 self.setup.preferences['keytable'] = 'keytable'
574 with self._caplog.at_level(logging.WARNING):
575 self.setup.setup_keyboard_map()
576
577 @patch('kiwi.system.setup.CommandCapabilities.has_option_in_help')
578 @patch('kiwi.system.setup.Command.run')
579 def test_setup_keyboard_skipped_systemd_incompatible(self, mock_run, mock_caps):
580 mock_caps.return_value = True # systemd-firstboot --keymap found in help
581 self.setup.preferences['keytable'] = 'keytable'
582
583 # mock the Command.run for `localectl list-keymaps`
584 def side_effect(args, **kwargs):
585 if args == ['chroot', ANY, 'systemd-firstboot', '--keymap=keytable']:
586 return CommandT("", "Keymap us is not installed.", 1)
587 return DEFAULT
588 mock_run.side_effect = side_effect
589 with raises(KiwiCommandError) as excinfo:
590 self.setup.setup_keyboard_map()
591 assert 'your system does not support the systemd way of managing keymaps' in str(excinfo.value)
592
593 @patch('kiwi.system.setup.CommandCapabilities.has_option_in_help')
594 @patch('kiwi.system.setup.Command.run')
595 @patch('os.path.exists')
596 def test_setup_locale(
597 self, mock_path, mock_run, mock_caps
598 ):
599 mock_caps.return_valure = True
600 mock_path.return_value = True
601 self.setup.preferences['locale'] = 'locale1,locale2'
602 self.setup.setup_locale()
603 mock_run.assert_has_calls([
604 call(['rm', '-r', '-f', 'root_dir/etc/locale.conf']),
605 call([
606 'chroot', 'root_dir', 'systemd-firstboot',
607 '--locale=locale1.UTF-8'
608 ])
609 ])
610
611 @patch('kiwi.system.setup.CommandCapabilities.has_option_in_help')
612 @patch('kiwi.system.setup.Command.run')
613 @patch('os.path.exists')
614 def test_setup_locale_POSIX(self, mock_path, mock_run, mock_caps):
615 mock_caps.return_valure = True
616 mock_path.return_value = True
617 self.setup.preferences['locale'] = 'POSIX,locale2'
618 self.setup.setup_locale()
619 mock_run.assert_has_calls([
620 call(['rm', '-r', '-f', 'root_dir/etc/locale.conf']),
621 call([
622 'chroot', 'root_dir', 'systemd-firstboot',
623 '--locale=POSIX'
624 ])
625 ])
626
627 @patch('kiwi.system.setup.Command.run')
628 def test_setup_timezone(self, mock_command):
629 self.setup.preferences['timezone'] = 'timezone'
630 self.setup.setup_timezone()
631 mock_command.assert_has_calls([
632 call([
633 'chroot', 'root_dir', 'ln', '-s', '-f',
634 '/usr/share/zoneinfo/timezone', '/etc/localtime'
635 ])
636 ])
637
638 @patch('kiwi.system.setup.CommandCapabilities.has_option_in_help')
639 @patch('kiwi.system.setup.Command.run')
640 def test_setup_timezone_with_systemd(self, mock_command, mock_caps):
641 mock_caps.return_value = True
642 self.setup.preferences['timezone'] = 'timezone'
643 self.setup.setup_timezone()
644 mock_command.assert_has_calls([
645 call([
646 'chroot', 'root_dir', 'systemd-firstboot',
647 '--timezone=timezone'
648 ])
649 ])
650
651 @patch('kiwi.system.setup.Users')
652 def test_setup_groups(self, mock_users):
653 users = Mock()
654 users.group_exists = Mock(
655 return_value=False
656 )
657 mock_users.return_value = users
658
659 self.setup_with_real_xml.setup_groups()
660
661 calls = [
662 call('users'),
663 call('kiwi'),
664 call('admin')
665 ]
666 users.group_exists.assert_has_calls(calls)
667
668 calls = [
669 call('users', []),
670 call('kiwi', []),
671 call('admin', ['-g', '42'])
672 ]
673 users.group_add.assert_has_calls(calls)
674
675 @patch('kiwi.system.setup.Users')
676 @patch('kiwi.system.setup.Command.run')
677 def test_setup_users_add(self, mock_command, mock_users):
678 users = Mock()
679 users.user_exists = Mock(
680 return_value=False
681 )
682 mock_users.return_value = users
683 mock_command.return_value = self.run_result
684
685 self.setup_with_real_xml.setup_users()
686
687 calls = [
688 call('root'),
689 call('tux'),
690 call('kiwi')
691 ]
692 users.user_exists.assert_has_calls(calls)
693
694 calls = [
695 call(
696 'root', [
697 '-p', 'password-hash',
698 '-s', '/bin/bash',
699 '-u', '815', '-c', 'Bob',
700 '-m', '-d', '/root'
701 ]
702 ),
703 call(
704 'tux', [
705 '-p', 'password-hash',
706 '-g', 'users',
707 '-m', '-d', '/home/tux'
708 ]
709 ),
710 call(
711 'kiwi', [
712 '-p', 'password-hash',
713 '-g', 'kiwi', '-G', 'admin,users', '-m'
714 ]
715 )
716 ]
717 users.user_add.assert_has_calls(calls)
718
719 mock_command.assert_called_with(
720 ['openssl', 'passwd', '-1', '-salt', 'xyz', 'mypwd']
721 )
722
723 @patch('kiwi.system.setup.Users')
724 @patch('kiwi.system.setup.Command.run')
725 def test_setup_users_modify(self, mock_command, mock_users):
726 users = Mock()
727 users.user_exists = Mock(
728 return_value=True
729 )
730 mock_users.return_value = users
731 mock_command.return_value = self.run_result
732
733 self.setup_with_real_xml.setup_users()
734 calls = [
735 call('root'),
736 call('tux'),
737 call('kiwi')
738 ]
739 users.user_exists.assert_has_calls(calls)
740
741 calls = [
742 call(
743 'root', [
744 '-p', 'password-hash',
745 '-s', '/bin/bash', '-u', '815', '-c', 'Bob'
746 ]
747 ),
748 call(
749 'tux', [
750 '-p', 'password-hash',
751 '-g', 'users'
752 ]
753 ),
754 call(
755 'kiwi', [
756 '-p', 'password-hash',
757 '-g', 'kiwi', '-G', 'admin,users'
758 ]
759 )
760 ]
761 users.user_modify.assert_has_calls(calls)
762
763 @patch('kiwi.system.setup.Path.which')
764 @patch('kiwi.system.setup.Command.run')
765 def test_setup_plymouth_splash(self, mock_command, mock_which):
766 mock_which.return_value = 'plymouth-set-default-theme'
767 preferences = Mock()
768 preferences.get_bootsplash_theme = Mock(
769 return_value=['some-theme']
770 )
771 self.xml_state.get_preferences_sections = Mock(
772 return_value=[preferences]
773 )
774 self.setup.setup_plymouth_splash()
775 mock_which.assert_called_once_with(
776 root_dir='root_dir',
777 filename='plymouth-set-default-theme'
778 )
779 mock_command.assert_called_once_with(
780 ['chroot', 'root_dir', 'plymouth-set-default-theme', 'some-theme']
781 )
782
783 @patch('os.path.exists')
784 def test_import_image_identifier(self, mock_os_path):
785 self.xml_state.xml_data.get_id = Mock(
786 return_value='42'
787 )
788 mock_os_path.return_value = True
789
790 m_open = mock_open()
791 with patch('builtins.open', m_open, create=True):
792 self.setup.import_image_identifier()
793
794 m_open.assert_called_once_with('root_dir/etc/ImageID', 'w')
795 m_open.return_value.write.assert_called_once_with('42\n')
796
797 @patch.object(SystemSetup, 'setup_selinux_file_contexts')
798 @patch('kiwi.system.setup.Profile')
799 @patch('kiwi.command.Command.call')
800 @patch('kiwi.command_process.CommandProcess.poll_and_watch')
801 @patch('os.path.exists')
802 @patch('os.stat')
803 @patch('os.access')
804 @patch('copy.deepcopy')
805 def test_call_non_excutable_config_script(
806 self, mock_copy_deepcopy, mock_access, mock_stat, mock_os_path,
807 mock_watch, mock_command, mock_Profile, mock_setup_selinux_file_contexts
808 ):
809 mock_copy_deepcopy.return_value = {}
810 profile = Mock()
811 mock_Profile.return_value = profile
812 profile.get_settings.return_value = {}
813 result_type = namedtuple(
814 'result', ['stderr', 'returncode']
815 )
816 mock_result = result_type(stderr='stderr', returncode=0)
817 mock_os_path.return_value = True
818 mock_watch.return_value = mock_result
819 mock_access.return_value = False
820
821 self.setup.call_config_script()
822 mock_copy_deepcopy.assert_called_once_with(os.environ)
823 mock_command.assert_called_once_with(
824 ['chroot', 'root_dir', 'bash', '/image/config.sh'], {}
825 )
826 mock_setup_selinux_file_contexts.assert_called_once_with()
827
828 @patch.object(SystemSetup, 'setup_selinux_file_contexts')
829 @patch('kiwi.system.setup.Profile')
830 @patch('kiwi.command.Command.call')
831 @patch('kiwi.command_process.CommandProcess.poll_and_watch')
832 @patch('os.path.exists')
833 @patch('os.stat')
834 @patch('os.access')
835 @patch('copy.deepcopy')
836 def test_call_excutable_config_script(
837 self, mock_copy_deepcopy, mock_access, mock_stat, mock_os_path,
838 mock_watch, mock_command, mock_Profile, mock_setup_selinux_file_contexts
839 ):
840 mock_copy_deepcopy.return_value = {}
841 profile = Mock()
842 mock_Profile.return_value = profile
843 profile.get_settings.return_value = {}
844 result_type = namedtuple(
845 'result', ['stderr', 'returncode']
846 )
847 mock_result = result_type(stderr='stderr', returncode=0)
848 mock_os_path.return_value = True
849 mock_watch.return_value = mock_result
850
851 # pretend that the script is executable
852 mock_access.return_value = True
853 self.setup.call_config_script()
854
855 mock_copy_deepcopy.assert_called_once_with(os.environ)
856 mock_command.assert_called_once_with(
857 ['chroot', 'root_dir', '/image/config.sh'], {}
858 )
859 mock_setup_selinux_file_contexts.assert_called_once_with()
860
861 @patch.object(SystemSetup, 'setup_selinux_file_contexts')
862 @patch('kiwi.system.setup.Profile')
863 @patch('kiwi.command.Command.call')
864 @patch('kiwi.command_process.CommandProcess.poll_and_watch')
865 @patch('os.path.exists')
866 @patch('os.stat')
867 @patch('os.access')
868 @patch('copy.deepcopy')
869 def test_call_excutable_post_bootstrap_script(
870 self, mock_copy_deepcopy, mock_access, mock_stat, mock_os_path,
871 mock_watch, mock_command, mock_Profile, mock_setup_selinux_file_contexts
872 ):
873 mock_copy_deepcopy.return_value = {}
874 profile = Mock()
875 mock_Profile.return_value = profile
876 profile.get_settings.return_value = {}
877 result_type = namedtuple(
878 'result', ['stderr', 'returncode']
879 )
880 mock_result = result_type(stderr='stderr', returncode=0)
881 mock_os_path.return_value = True
882 mock_watch.return_value = mock_result
883
884 # pretend that the script is executable
885 mock_access.return_value = True
886 self.setup.call_post_bootstrap_script()
887
888 mock_copy_deepcopy.assert_called_once_with(os.environ)
889 mock_command.assert_called_once_with(
890 ['chroot', 'root_dir', '/image/post_bootstrap.sh'], {}
891 )
892 mock_setup_selinux_file_contexts.assert_called_once_with()
893
894 @patch.object(SystemSetup, 'setup_selinux_file_contexts')
895 @patch('kiwi.system.setup.Defaults.is_buildservice_worker')
896 @patch('kiwi.logger.Logger.getLogFlags')
897 @patch('kiwi.system.setup.Profile')
898 @patch('kiwi.command.Command.call')
899 @patch('kiwi.command_process.CommandProcess.poll_and_watch')
900 @patch('os.path.exists')
901 @patch('os.stat')
902 @patch('os.access')
903 @patch('copy.deepcopy')
904 @patch('kiwi.system.setup.MountManager')
905 def test_call_disk_script(
906 self,
907 mock_MountManager,
908 mock_copy_deepcopy,
909 mock_access,
910 mock_stat,
911 mock_os_path,
912 mock_watch,
913 mock_command,
914 mock_Profile,
915 mock_getLogFlags,
916 mock_is_buildservice_worker,
917 mock_setup_selinux_file_contexts
918 ):
919 chroot_mount = Mock()
920 chroot_mount.is_mounted.return_value = False
921 mock_MountManager.return_value = chroot_mount
922 mock_is_buildservice_worker.return_value = False
923 mock_getLogFlags.return_value = {
924 'run-scripts-in-screen': True
925 }
926 mock_copy_deepcopy.return_value = {}
927 profile = Mock()
928 mock_Profile.return_value = profile
929 profile.get_settings.return_value = {}
930 result_type = namedtuple(
931 'result_type', ['stderr', 'returncode']
932 )
933 mock_result = result_type(stderr='stderr', returncode=0)
934 mock_os_path.return_value = True
935 mock_watch.return_value = mock_result
936 mock_access.return_value = False
937
938 self.setup.call_disk_script()
939 mock_copy_deepcopy.assert_called_once_with(os.environ)
940 mock_command.assert_called_once_with(
941 [
942 'screen', '-t', '-X',
943 'chroot', 'root_dir', 'bash', '/image/disk.sh'
944 ], {}
945 )
946 mock_setup_selinux_file_contexts.assert_called_once_with()
947 mock_MountManager.assert_called_once_with(
948 device='root_dir', mountpoint='root_dir'
949 )
950 chroot_mount.bind_mount.assert_called_once_with()
951
952 @patch.object(SystemSetup, 'setup_selinux_file_contexts')
953 @patch('kiwi.system.setup.Defaults.is_buildservice_worker')
954 @patch('kiwi.logger.Logger.getLogFlags')
955 @patch('kiwi.system.setup.Profile')
956 @patch('kiwi.command.Command.call')
957 @patch('kiwi.command_process.CommandProcess.poll_and_watch')
958 @patch('os.path.exists')
959 @patch('os.stat')
960 @patch('os.access')
961 @patch('copy.deepcopy')
962 def test_call_pre_disk_script(
963 self, mock_copy_deepcopy, mock_access, mock_stat, mock_os_path,
964 mock_watch, mock_command, mock_Profile, mock_getLogFlags,
965 mock_is_buildservice_worker, mock_setup_selinux_file_contexts
966 ):
967 mock_is_buildservice_worker.return_value = False
968 mock_getLogFlags.return_value = {
969 'run-scripts-in-screen': True
970 }
971 mock_copy_deepcopy.return_value = {}
972 profile = Mock()
973 mock_Profile.return_value = profile
974 profile.get_settings.return_value = {}
975 result_type = namedtuple(
976 'result_type', ['stderr', 'returncode']
977 )
978 mock_result = result_type(stderr='stderr', returncode=0)
979 mock_os_path.return_value = True
980 mock_watch.return_value = mock_result
981 mock_access.return_value = False
982
983 self.setup.call_pre_disk_script()
984 mock_copy_deepcopy.assert_called_once_with(os.environ)
985 mock_command.assert_called_once_with(
986 [
987 'screen', '-t', '-X',
988 'chroot', 'root_dir', 'bash', '/image/pre_disk_sync.sh'
989 ], {}
990 )
991 mock_setup_selinux_file_contexts.assert_called_once_with()
992
993 @patch.object(SystemSetup, 'setup_selinux_file_contexts')
994 @patch('kiwi.system.setup.Profile')
995 @patch('kiwi.command.Command.call')
996 @patch('kiwi.command_process.CommandProcess.poll_and_watch')
997 @patch('os.path.exists')
998 @patch('os.stat')
999 @patch('os.access')
1000 @patch('copy.deepcopy')
1001 def test_call_image_script(
1002 self, mock_copy_deepcopy, mock_access, mock_stat, mock_os_path,
1003 mock_watch, mock_command, mock_Profile, mock_setup_selinux_file_contexts
1004 ):
1005 mock_copy_deepcopy.return_value = {}
1006 profile = Mock()
1007 mock_Profile.return_value = profile
1008 profile.get_settings.return_value = {}
1009 result_type = namedtuple(
1010 'result_type', ['stderr', 'returncode']
1011 )
1012 mock_result = result_type(stderr='stderr', returncode=0)
1013 mock_os_path.return_value = True
1014 mock_watch.return_value = mock_result
1015 mock_access.return_value = False
1016
1017 self.setup.call_image_script()
1018 mock_copy_deepcopy.assert_called_once_with(os.environ)
1019 mock_command.assert_called_once_with(
1020 ['chroot', 'root_dir', 'bash', '/image/images.sh'], {}
1021 )
1022 mock_setup_selinux_file_contexts.assert_called_once_with()
1023
1024 @patch.object(SystemSetup, 'setup_selinux_file_contexts')
1025 @patch('kiwi.system.setup.Profile')
1026 @patch('kiwi.command.Command.call')
1027 @patch('kiwi.command_process.CommandProcess.poll_and_watch')
1028 @patch('os.path.exists')
1029 @patch('os.stat')
1030 @patch('os.access')
1031 @patch('copy.deepcopy')
1032 def test_call_config_overlay_script(
1033 self, mock_copy_deepcopy, mock_access, mock_stat, mock_os_path,
1034 mock_watch, mock_command, mock_Profile, mock_setup_selinux_file_contexts
1035 ):
1036 mock_copy_deepcopy.return_value = {}
1037 profile = Mock()
1038 mock_Profile.return_value = profile
1039 profile.get_settings.return_value = {}
1040 result_type = namedtuple(
1041 'result_type', ['stderr', 'returncode']
1042 )
1043 mock_result = result_type(stderr='stderr', returncode=0)
1044 mock_os_path.return_value = True
1045 mock_watch.return_value = mock_result
1046 mock_access.return_value = False
1047
1048 self.setup.call_config_overlay_script()
1049 mock_copy_deepcopy.assert_called_once_with(os.environ)
1050 mock_command.assert_called_once_with(
1051 ['chroot', 'root_dir', 'bash', '/image/config-overlay.sh'], {}
1052 )
1053 mock_setup_selinux_file_contexts.assert_called_once_with()
1054
1055 @patch('kiwi.system.setup.Profile')
1056 @patch('kiwi.command.Command.call')
1057 @patch('kiwi.command_process.CommandProcess.poll_and_watch')
1058 @patch('os.path.exists')
1059 @patch('os.path.abspath')
1060 @patch('copy.deepcopy')
1061 def test_call_config_host_overlay_script(
1062 self,
1063 mock_copy_deepcopy,
1064 mock_abspath,
1065 mock_exists,
1066 mock_watch,
1067 mock_command,
1068 mock_Profile
1069 ):
1070 mock_copy_deepcopy.return_value = {}
1071 profile = Mock()
1072 mock_Profile.return_value = profile
1073 profile.get_settings.return_value = {}
1074 result_type = namedtuple(
1075 'result_type', ['stderr', 'returncode']
1076 )
1077 mock_result = result_type(stderr='stderr', returncode=0)
1078 mock_exists.return_value = True
1079 mock_abspath.return_value = '/root_dir/image/config-host-overlay.sh'
1080 mock_watch.return_value = mock_result
1081 self.setup.call_config_host_overlay_script()
1082 mock_abspath.assert_called_once_with(
1083 'root_dir/image/config-host-overlay.sh'
1084 )
1085 mock_command.assert_called_once_with(
1086 [
1087 'bash', '-c',
1088 'cd root_dir && bash --norc /root_dir/image/config-host-overlay.sh '
1089 ], {}
1090 )
1091
1092 @patch('kiwi.system.setup.Profile')
1093 @patch('kiwi.command.Command.call')
1094 @patch('kiwi.command_process.CommandProcess.poll_and_watch')
1095 @patch('os.path.exists')
1096 @patch('os.path.abspath')
1097 @patch('copy.deepcopy')
1098 def test_call_edit_boot_config_script(
1099 self,
1100 mock_copy_deepcopy,
1101 mock_abspath,
1102 mock_exists,
1103 mock_watch,
1104 mock_command,
1105 mock_Profile
1106 ):
1107 mock_copy_deepcopy.return_value = {}
1108 profile = Mock()
1109 mock_Profile.return_value = profile
1110 profile.get_settings.return_value = {}
1111 result_type = namedtuple(
1112 'result_type', ['stderr', 'returncode']
1113 )
1114 mock_result = result_type(stderr='stderr', returncode=0)
1115 mock_exists.return_value = True
1116 mock_abspath.return_value = '/root_dir/image/edit_boot_config.sh'
1117 mock_watch.return_value = mock_result
1118 self.setup.call_edit_boot_config_script('ext4', 1)
1119 mock_abspath.assert_called_once_with(
1120 'root_dir/image/edit_boot_config.sh'
1121 )
1122 mock_command.assert_called_once_with(
1123 [
1124 'bash', '-c',
1125 'cd root_dir && bash --norc /root_dir/image/edit_boot_config.sh '
1126 'ext4 1'
1127 ], {}
1128 )
1129
1130 @patch('kiwi.system.setup.Profile')
1131 @patch('kiwi.system.setup.Defaults.is_buildservice_worker')
1132 @patch('kiwi.logger.Logger.getLogFlags')
1133 @patch('kiwi.command.Command.call')
1134 @patch('kiwi.command_process.CommandProcess.poll_and_watch')
1135 @patch('os.path.exists')
1136 @patch('os.path.abspath')
1137 @patch('copy.deepcopy')
1138 def test_call_edit_boot_install_script(
1139 self,
1140 mock_copy_deepcopy,
1141 mock_abspath,
1142 mock_exists,
1143 mock_watch,
1144 mock_command,
1145 mock_getLogFlags,
1146 mock_is_buildservice_worker,
1147 mock_Profile
1148 ):
1149 mock_copy_deepcopy.return_value = {}
1150 profile = Mock()
1151 mock_Profile.return_value = profile
1152 profile.get_settings.return_value = {}
1153 mock_is_buildservice_worker.return_value = False
1154 mock_getLogFlags.return_value = {
1155 'run-scripts-in-screen': True
1156 }
1157 result_type = namedtuple(
1158 'result_type', ['stderr', 'returncode']
1159 )
1160 mock_result = result_type(stderr='stderr', returncode=0)
1161 mock_exists.return_value = True
1162 mock_abspath.return_value = '/root_dir/image/edit_boot_install.sh'
1163 mock_watch.return_value = mock_result
1164 self.setup.call_edit_boot_install_script(
1165 'my_image.raw', '/dev/mapper/loop0p1'
1166 )
1167 mock_abspath.assert_called_once_with(
1168 'root_dir/image/edit_boot_install.sh'
1169 )
1170 mock_command.assert_called_once_with(
1171 [
1172 'screen', '-t', '-X',
1173 'bash', '-c',
1174 'cd root_dir && bash --norc '
1175 '/root_dir/image/edit_boot_install.sh '
1176 'my_image.raw /dev/mapper/loop0p1'
1177 ], {}
1178 )
1179
1180 @patch('kiwi.system.setup.Profile')
1181 @patch('kiwi.command.Command.call')
1182 @patch('kiwi.command_process.CommandProcess.poll_and_watch')
1183 @patch('os.path.exists')
1184 @patch('os.stat')
1185 @patch('os.access')
1186 def test_call_image_script_raises(
1187 self, mock_access, mock_stat, mock_os_path, mock_watch,
1188 mock_command, mock_Profile
1189 ):
1190 profile = Mock()
1191 mock_Profile.return_value = profile
1192 profile.get_settings.return_value = {}
1193 result_type = namedtuple(
1194 'result_type', ['stderr', 'returncode']
1195 )
1196 mock_result = result_type(stderr='stderr', returncode=1)
1197 mock_os_path.return_value = True
1198 mock_watch.return_value = mock_result
1199 mock_access.return_value = False
1200 with raises(KiwiScriptFailed):
1201 self.setup.call_image_script()
1202
1203 @patch('kiwi.system.setup.Profile')
1204 @patch('kiwi.command.Command.call')
1205 @patch('kiwi.command_process.CommandProcess.poll_and_watch')
1206 @patch('os.path.exists')
1207 @patch('copy.deepcopy')
1208 def test_call_edit_boot_install_script_raises(
1209 self,
1210 mock_copy_deepcopy,
1211 mock_os_path,
1212 mock_watch,
1213 mock_command,
1214 mock_Profile
1215 ):
1216 mock_copy_deepcopy.return_value = {}
1217 profile = Mock()
1218 mock_Profile.return_value = profile
1219 profile.get_settings.return_value = {}
1220 result_type = namedtuple(
1221 'result_type', ['stderr', 'returncode']
1222 )
1223 mock_result = result_type(stderr='stderr', returncode=1)
1224 mock_os_path.return_value = True
1225 mock_watch.return_value = mock_result
1226 with raises(KiwiScriptFailed):
1227 self.setup.call_edit_boot_install_script(
1228 'my_image.raw', '/dev/mapper/loop0p1'
1229 )
1230
1231 @patch('kiwi.command.Command.run')
1232 def test_create_init_link_from_linuxrc(self, mock_command):
1233 self.setup.create_init_link_from_linuxrc()
1234 mock_command.assert_called_once_with(
1235 ['ln', 'root_dir/linuxrc', 'root_dir/init']
1236 )
1237
1238 @patch('kiwi.command.Command.run')
1239 def test_create_recovery_archive_cleanup_only(self, mock_command):
1240 self.setup.oemconfig['recovery'] = False
1241 self.setup.create_recovery_archive()
1242 assert mock_command.call_args_list[0] == call(
1243 ['bash', '-c', 'rm -f root_dir/recovery.*']
1244 )
1245
1246 @patch('os.path.exists')
1247 @patch('kiwi.system.setup.Path.wipe')
1248 @patch.object(SystemSetup, 'setup_selinux_file_contexts')
1249 @patch('kiwi.system.setup.Profile')
1250 @patch('kiwi.command.Command.call')
1251 @patch('kiwi.command.Command.run')
1252 @patch('kiwi.command_process.CommandProcess.poll_and_watch')
1253 @patch('os.path.exists')
1254 @patch('os.stat')
1255 @patch('os.access')
1256 @patch('copy.deepcopy')
1257 def test_create_fstab(
1258 self, mock_copy_deepcopy, mock_access, mock_stat, mock_os_path,
1259 mock_watch, mock_command_run, mock_command_call, mock_Profile,
1260 mock_setup_selinux_file_contexts, mock_wipe, mock_exists
1261 ):
1262 fstab = Mock()
1263 mock_exists.return_value = True
1264
1265 mock_copy_deepcopy.return_value = {}
1266 profile = Mock()
1267 mock_Profile.return_value = profile
1268 profile.get_settings.return_value = {}
1269 result_type = namedtuple(
1270 'result_type', ['stderr', 'returncode']
1271 )
1272 mock_result = result_type(stderr='stderr', returncode=0)
1273 mock_os_path.return_value = True
1274 mock_watch.return_value = mock_result
1275 mock_access.return_value = True
1276
1277 m_open = mock_open(read_data='append_entry')
1278 with patch('builtins.open', m_open, create=True):
1279 self.setup.create_fstab(fstab)
1280
1281 fstab.export.assert_called_once_with('root_dir/etc/fstab')
1282
1283 assert m_open.call_args_list == [
1284 call('root_dir/etc/fstab', 'a'),
1285 call('root_dir/etc/fstab.append', 'r')
1286 ]
1287 assert m_open.return_value.write.call_args_list == [
1288 call('append_entry')
1289 ]
1290 mock_command_run.assert_called_once_with(
1291 ['patch', 'root_dir/etc/fstab', 'root_dir/etc/fstab.patch']
1292 )
1293 mock_command_call.assert_called_once_with(
1294 ['chroot', 'root_dir', '/etc/fstab.script'], {}
1295 )
1296 assert mock_wipe.call_args_list == [
1297 call('root_dir/etc/fstab.append'),
1298 call('root_dir/etc/fstab.patch'),
1299 call('root_dir/etc/fstab.script')
1300 ]
1301 mock_setup_selinux_file_contexts.assert_called_once_with()
1302
1303 @patch('kiwi.command.Command.run')
1304 @patch('pathlib.Path.touch')
1305 @patch('kiwi.system.setup.ArchiveTar')
1306 @patch('kiwi.system.setup.Compress')
1307 @patch('os.path.getsize')
1308 @patch('kiwi.system.setup.Path.wipe')
1309 def test_create_recovery_archive(
1310 self, mock_wipe, mock_getsize, mock_compress,
1311 mock_archive, mock_pathlib_Path_touch, mock_command
1312 ):
1313 mock_getsize.return_value = 42
1314 compress = Mock()
1315 mock_compress.return_value = compress
1316 archive = Mock()
1317 mock_archive.return_value = archive
1318 self.setup.oemconfig['recovery'] = True
1319 self.setup.oemconfig['recovery_inplace'] = True
1320
1321 m_open = mock_open()
1322 with patch('builtins.open', m_open, create=True):
1323 self.setup.create_recovery_archive()
1324
1325 assert mock_command.call_args_list[0] == call(
1326 ['bash', '-c', 'rm -f root_dir/recovery.*']
1327 )
1328 mock_archive.assert_called_once_with(
1329 create_from_file_list=False, filename='root_dir/recovery.tar'
1330 )
1331 archive.create.assert_called_once_with(
1332 exclude=['dev', 'proc', 'sys'],
1333 options=[
1334 '--numeric-owner',
1335 '--hard-dereference',
1336 '--preserve-permissions'
1337 ],
1338 source_dir='root_dir'
1339 )
1340 assert m_open.call_args_list[0] == call(
1341 'root_dir/recovery.tar.filesystem', 'w'
1342 )
1343 assert m_open.return_value.write.call_args_list[0] == call('ext3')
1344
1345 assert mock_command.call_args_list[1] == call(
1346 ['bash', '-c', 'tar -tf root_dir/recovery.tar | wc -l']
1347 )
1348 assert m_open.call_args_list[1] == call(
1349 'root_dir/recovery.tar.files', 'w'
1350 )
1351 assert mock_getsize.call_args_list[0] == call(
1352 'root_dir/recovery.tar'
1353 )
1354 assert m_open.return_value.write.call_args_list[1] == call('1\n')
1355 assert m_open.call_args_list[2] == call(
1356 'root_dir/recovery.tar.size', 'w'
1357 )
1358 assert m_open.return_value.write.call_args_list[2] == call('42')
1359 mock_compress.assert_called_once_with(
1360 'root_dir/recovery.tar'
1361 )
1362 compress.gzip.assert_called_once_with()
1363 assert mock_getsize.call_args_list[1] == call(
1364 'root_dir/recovery.tar.gz'
1365 )
1366 assert m_open.call_args_list[3] == call(
1367 'root_dir/recovery.partition.size', 'w'
1368 )
1369 assert m_open.return_value.write.call_args_list[3] == call('300')
1370 mock_wipe.assert_called_once_with(
1371 'root_dir/recovery.tar.gz'
1372 )
1373
1374 @patch('kiwi.system.setup.Command.run')
1375 @patch('kiwi.system.setup.Path.create')
1376 @patch('kiwi.system.setup.DataSync')
1377 @patch('os.path.exists')
1378 def test_export_modprobe_setup(
1379 self, mock_exists, mock_DataSync, mock_path, mock_command
1380 ):
1381 data = Mock()
1382 mock_DataSync.return_value = data
1383 mock_exists.return_value = True
1384 self.setup.export_modprobe_setup('target_root_dir')
1385 mock_path.assert_called_once_with('target_root_dir/etc')
1386 mock_DataSync.assert_called_once_with(
1387 'root_dir/etc/modprobe.d', 'target_root_dir/etc/'
1388 )
1389 data.sync_data.assert_called_once_with(
1390 options=['-a']
1391 )
1392
1393 @patch('kiwi.defaults.Defaults.get_default_packager_tool')
1394 def test_export_package_list_unknown_packager(
1395 self, mock_get_default_packager_tool
1396 ):
1397 assert self.setup.export_package_list('target_dir') == ''
1398
1399 @patch('kiwi.defaults.Defaults.get_default_packager_tool')
1400 def test_export_flake_pilot_system_file_list_unknown_packager(
1401 self, mock_get_default_packager_tool
1402 ):
1403 assert self.setup.export_flake_pilot_system_file_list(
1404 'target_dir', 'system_files'
1405 ) == ''
1406
1407 @patch('kiwi.defaults.Defaults.get_default_packager_tool')
1408 def test_export_package_changes_unknown_packager(
1409 self, mock_get_default_packager_tool
1410 ):
1411 assert self.setup.export_package_changes('target_dir') == ''
1412
1413 @patch('kiwi.defaults.Defaults.get_default_packager_tool')
1414 def test_export_package_verification_unknown_packager(
1415 self, mock_get_default_packager_tool
1416 ):
1417 assert self.setup.export_package_verification('target_dir') == ''
1418
1419 @patch('kiwi.system.setup.Command.run')
1420 @patch('kiwi.system.setup.RpmDataBase')
1421 @patch('kiwi.system.setup.MountManager')
1422 def test_export_flake_pilot_system_file_list(
1423 self, mock_MountManager, mock_RpmDataBase, mock_command_run
1424 ):
1425 rpmdb = Mock()
1426 rpmdb.rpmdb_image.expand_query.return_value = 'image_dbpath'
1427 rpmdb.rpmdb_host.expand_query.return_value = 'host_dbpath'
1428 rpmdb.has_rpm.return_value = True
1429 mock_RpmDataBase.return_value = rpmdb
1430 self.xml_state.get_system_files_ignore_packages = Mock(
1431 return_value=['rpm', 'zypper']
1432 )
1433 mock_command_run.return_value = Mock(output='glibc\nzypper\n')
1434
1435 with patch('builtins.open') as mock_open:
1436 mock_open.return_value = MagicMock(spec=io.IOBase)
1437 file_handle = mock_open.return_value.__enter__.return_value
1438 result = self.setup.export_flake_pilot_system_file_list(
1439 'target_dir', 'system_files'
1440 )
1441 mock_open.assert_called_once_with(
1442 'target_dir/system_files', 'w', encoding='utf-8'
1443 )
1444 assert file_handle.write.call_args_list == [
1445 call('set -e\n'), call('rpm --noghost -ql glibc\n')
1446 ]
1447 assert result == 'target_dir/system_files'
1448 mock_command_run.assert_called_once_with(
1449 [
1450 'rpm', '--root', 'root_dir',
1451 '-qa', '--qf', '%{NAME}\n', '--dbpath', 'image_dbpath'
1452 ]
1453 )
1454
1455 @patch('kiwi.system.setup.Command.run')
1456 @patch('kiwi.system.setup.RpmDataBase')
1457 @patch('kiwi.system.setup.MountManager')
1458 def test_export_package_list_rpm(
1459 self, mock_MountManager, mock_RpmDataBase, mock_command
1460 ):
1461 rpmdb = Mock()
1462 rpmdb.rpmdb_image.expand_query.return_value = 'image_dbpath'
1463 rpmdb.rpmdb_host.expand_query.return_value = 'host_dbpath'
1464 rpmdb.has_rpm.return_value = True
1465 mock_RpmDataBase.return_value = rpmdb
1466 command = Mock()
1467 command.output = 'packages_data'
1468 mock_command.return_value = command
1469
1470 with patch('builtins.open') as m_open:
1471 result = self.setup.export_package_list('target_dir')
1472 m_open.assert_called_once_with(
1473 'target_dir/some-image.x86_64-1.2.3.packages', 'w',
1474 encoding='utf-8'
1475 )
1476
1477 assert result == 'target_dir/some-image.x86_64-1.2.3.packages'
1478 mock_command.assert_called_once_with(
1479 [
1480 'rpm', '--root', 'root_dir', '-qa', '--qf',
1481 '%{NAME}|%{EPOCH}|%{VERSION}|%{RELEASE}|%{ARCH}|'
1482 '%{DISTURL}|%{LICENSE}\\n',
1483 '--dbpath', 'image_dbpath'
1484 ]
1485 )
1486 rpmdb.has_rpm.return_value = False
1487 mock_command.reset_mock()
1488
1489 with patch('builtins.open'):
1490 result = self.setup.export_package_list('target_dir')
1491
1492 assert result == 'target_dir/some-image.x86_64-1.2.3.packages'
1493 mock_command.assert_called_once_with(
1494 [
1495 'rpm', '--root', 'root_dir', '-qa', '--qf',
1496 '%{NAME}|%{EPOCH}|%{VERSION}|%{RELEASE}|%{ARCH}|'
1497 '%{DISTURL}|%{LICENSE}\\n',
1498 '--dbpath', 'host_dbpath'
1499 ]
1500 )
1501
1502 @patch('kiwi.system.setup.os.path.exists')
1503 def test_setup_machine_id(self, mock_path_exists):
1504 # No /etc/machine-id -> no creation
1505 mock_path_exists.return_value = False
1506 with patch('builtins.open') as m_open:
1507 self.setup.setup_machine_id()
1508 m_open.assert_not_called()
1509
1510 mock_path_exists.return_value = True
1511
1512 # Empty /etc/machine-id -> truncate
1513 m_open = mock_open(read_data='')
1514 with patch('builtins.open', m_open):
1515 self.setup.setup_machine_id()
1516 m_open.assert_called_once_with(
1517 'root_dir/etc/machine-id', 'r+'
1518 )
1519 m_open().truncate.assert_called_once_with(0)
1520
1521 # Filled /etc/machine-id -> truncate
1522 m_open = mock_open(read_data='9d75e31cca5943b3a527345e0684d5b6')
1523 with patch('builtins.open', m_open):
1524 self.setup.setup_machine_id()
1525 m_open.assert_called_once_with(
1526 'root_dir/etc/machine-id', 'r+'
1527 )
1528 m_open().truncate.assert_called_once_with(0)
1529
1530 # Explicitly uninitialized /etc/machine-id -> keep
1531 m_open = mock_open(read_data='uninitialized')
1532 with patch('builtins.open', m_open):
1533 self.setup.setup_machine_id()
1534 m_open.assert_called_once_with(
1535 'root_dir/etc/machine-id', 'r+'
1536 )
1537 m_open().truncate.assert_not_called()
1538
1539 @patch('kiwi.system.setup.Command.run')
1540 @patch('kiwi.system.setup.Path.which')
1541 def test_setup_permissions(
1542 self, mock_path_which, mock_command
1543 ):
1544 mock_path_which.return_value = 'chkstat'
1545 self.setup.setup_permissions()
1546 mock_command.assert_called_once_with(
1547 ['chroot', 'root_dir', 'chkstat', '--system', '--set']
1548 )
1549 mock_path_which.return_value = None
1550 with self._caplog.at_level(logging.WARNING):
1551 self.setup.setup_permissions()
1552
1553 @patch.object(SystemSetup, 'export_flake_pilot_system_file_list')
1554 def test_create_system_files(
1555 self, mock_export_flake_pilot_system_file_list
1556 ):
1557 self.setup.create_system_files()
1558 mock_export_flake_pilot_system_file_list.assert_called_once_with(
1559 'root_dir', 'systemfiles'
1560 )
1561
1562 @patch('kiwi.system.setup.Command.run')
1563 def test_export_package_list_dpkg(self, mock_command):
1564 command = Mock()
1565 command.output = 'packages_data'
1566 mock_command.return_value = command
1567 self.xml_state.get_package_manager = Mock(
1568 return_value='apt'
1569 )
1570
1571 with patch('builtins.open') as m_open:
1572 result = self.setup.export_package_list('target_dir')
1573 m_open.assert_called_once_with(
1574 'target_dir/some-image.x86_64-1.2.3.packages', 'w',
1575 encoding='utf-8'
1576 )
1577
1578 assert result == 'target_dir/some-image.x86_64-1.2.3.packages'
1579 mock_command.assert_called_once_with([
1580 'dpkg-query', '--admindir', 'root_dir/var/lib/dpkg', '-W',
1581 '-f', '${Package}|None|${Version}|None|${Architecture}|None|None\\n'
1582 ])
1583
1584 @patch('kiwi.system.setup.Command.run')
1585 def test_export_package_list_pacman(self, mock_command):
1586 command = Mock()
1587 command.output = 'packages_data'
1588 mock_command.return_value = command
1589 self.xml_state.get_package_manager = Mock(
1590 return_value='pacman'
1591 )
1592
1593 with patch('builtins.open') as m_open:
1594 result = self.setup.export_package_list('target_dir')
1595 m_open.assert_called_once_with(
1596 'target_dir/some-image.x86_64-1.2.3.packages', 'w'
1597 )
1598
1599 assert result == 'target_dir/some-image.x86_64-1.2.3.packages'
1600 mock_command.assert_called_once_with(
1601 ['pacman', '--query', '--dbpath', 'root_dir/var/lib/pacman']
1602 )
1603
1604 @patch('kiwi.system.setup.Command.run')
1605 @patch('kiwi.system.setup.RpmDataBase')
1606 @patch('kiwi.system.setup.MountManager')
1607 def test_export_package_changes_rpm(
1608 self, mock_MountManager, mock_RpmDataBase, mock_command
1609 ):
1610 rpmdb = Mock()
1611 rpmdb.rpmdb_image.expand_query.return_value = 'image_dbpath'
1612 rpmdb.rpmdb_host.expand_query.return_value = 'host_dbpath'
1613 rpmdb.has_rpm.return_value = True
1614 mock_RpmDataBase.return_value = rpmdb
1615 command = Mock()
1616 command.output = 'package|\nchanges'
1617 mock_command.return_value = command
1618
1619 with patch('builtins.open', create=True) as mock_open:
1620 mock_open.return_value = MagicMock(spec=io.IOBase)
1621 result = self.setup.export_package_changes('target_dir')
1622 mock_open.assert_called_once_with(
1623 'target_dir/some-image.x86_64-1.2.3.changes', 'w',
1624 encoding='utf-8'
1625 )
1626
1627 assert result == 'target_dir/some-image.x86_64-1.2.3.changes'
1628 mock_command.assert_called_once_with(
1629 [
1630 'rpm', '--root', 'root_dir', '-qa', '--qf',
1631 '%{NAME}|\\n', '--changelog', '--dbpath', 'image_dbpath'
1632 ]
1633 )
1634 self.runtime_config.get_package_changes.assert_called_once_with()
1635
1636 @patch('kiwi.system.setup.Command.run')
1637 @patch('os.path.exists')
1638 @patch('os.listdir')
1639 def test_export_package_changes_dpkg(
1640 self, mock_os_listdir, mock_os_path_exists, mock_command
1641 ):
1642 command = Mock()
1643 command.output = 'changes'
1644 mock_command.return_value = command
1645 self.xml_state.get_package_manager = Mock(
1646 return_value='apt'
1647 )
1648 mock_os_listdir.return_value = ['package_b', 'package_a']
1649 mock_os_path_exists.return_value = True
1650
1651 with patch('builtins.open', create=True) as mock_open:
1652 mock_open.return_value = MagicMock(spec=io.IOBase)
1653 result = self.setup.export_package_changes('target_dir')
1654 file_handle = mock_open.return_value.__enter__.return_value
1655 mock_open.assert_called_once_with(
1656 'target_dir/some-image.x86_64-1.2.3.changes', 'w',
1657 encoding='utf-8'
1658 )
1659 assert result == 'target_dir/some-image.x86_64-1.2.3.changes'
1660 assert file_handle.write.call_args_list == [
1661 call('package_a|\n'),
1662 call('changes\n'),
1663 call('package_b|\n'),
1664 call('changes\n')
1665 ]
1666 self.runtime_config.get_package_changes.assert_called_once_with()
1667
1668 @patch('kiwi.system.setup.Command.run')
1669 @patch('kiwi.system.setup.RpmDataBase')
1670 @patch('kiwi.system.setup.MountManager')
1671 def test_export_package_verification(
1672 self, mock_MountManager, mock_RpmDataBase, mock_command
1673 ):
1674 is_mounted_return = [True, False]
1675
1676 def is_mounted():
1677 return is_mounted_return.pop()
1678
1679 shared_mount = Mock()
1680 shared_mount.is_mounted.side_effect = is_mounted
1681 mock_MountManager.return_value = shared_mount
1682 rpmdb = Mock()
1683 rpmdb.rpmdb_image.expand_query.return_value = 'image_dbpath'
1684 rpmdb.rpmdb_host.expand_query.return_value = 'host_dbpath'
1685 rpmdb.has_rpm.return_value = True
1686 mock_RpmDataBase.return_value = rpmdb
1687 command = Mock()
1688 command.output = 'verification_data'
1689 mock_command.return_value = command
1690
1691 with patch('builtins.open') as m_open:
1692 result = self.setup.export_package_verification('target_dir')
1693 m_open.assert_called_once_with(
1694 'target_dir/some-image.x86_64-1.2.3.verified', 'w',
1695 encoding='utf-8'
1696 )
1697
1698 assert result == 'target_dir/some-image.x86_64-1.2.3.verified'
1699 mock_command.assert_called_once_with(
1700 command=[
1701 'rpm', '--root', 'root_dir', '-Va',
1702 '--dbpath', 'image_dbpath'
1703 ], raise_on_error=False
1704 )
1705
1706 mock_MountManager.assert_called_once_with(
1707 device='/dev', mountpoint='root_dir/dev'
1708 )
1709 shared_mount.bind_mount.assert_called_once_with()
1710 shared_mount.umount_lazy.assert_called_once_with()
1711 rpmdb.has_rpm.return_value = False
1712 is_mounted_return = [True, False]
1713 mock_command.reset_mock()
1714 shared_mount.reset_mock()
1715
1716 with patch('builtins.open'):
1717 result = self.setup.export_package_verification('target_dir')
1718
1719 assert result == 'target_dir/some-image.x86_64-1.2.3.verified'
1720 mock_command.assert_called_once_with(
1721 command=[
1722 'rpm', '--root', 'root_dir', '-Va',
1723 '--dbpath', 'host_dbpath'
1724 ], raise_on_error=False
1725 )
1726 shared_mount.bind_mount.assert_called_once_with()
1727 shared_mount.umount_lazy.assert_called_once_with()
1728
1729 @patch('kiwi.system.setup.Command.run')
1730 def test_export_package_verification_dpkg(self, mock_command):
1731 command = Mock()
1732 command.output = 'verification_data'
1733 mock_command.return_value = command
1734 self.xml_state.get_package_manager = Mock(
1735 return_value='apt'
1736 )
1737
1738 with patch('builtins.open') as m_open:
1739 result = self.setup.export_package_verification('target_dir')
1740 m_open.assert_called_once_with(
1741 'target_dir/some-image.x86_64-1.2.3.verified', 'w',
1742 encoding='utf-8'
1743 )
1744
1745 assert result == 'target_dir/some-image.x86_64-1.2.3.verified'
1746 mock_command.assert_called_once_with(
1747 command=[
1748 'dpkg', '--root', 'root_dir', '-V',
1749 '--verify-format', 'rpm'
1750 ],
1751 raise_on_error=False
1752 )
1753
1754 @patch('kiwi.system.setup.CommandCapabilities.has_option_in_help')
1755 @patch('kiwi.system.setup.Command.run')
1756 @patch('os.scandir')
1757 def test_set_selinux_file_contexts_new_version(
1758 self, mock_os_scandir, mock_command, mock_has_option_in_help
1759 ):
1760 mock_has_option_in_help.return_value = False
1761 mock_os_scandir.return_value = self.selinux_policies
1762 self.setup.set_selinux_file_contexts('security_context_file')
1763 mock_command.assert_called_once_with(
1764 [
1765 'chroot', 'root_dir',
1766 'setfiles', '-T0', '-F', '-p',
1767 '-c', '/etc/selinux/targeted/policy/policy.some_policy',
1768 '-e', '/proc', '-e', '/sys', '-e', '/dev',
1769 'security_context_file', '/'
1770 ]
1771 )
1772
1773 @patch('kiwi.system.setup.CommandCapabilities.has_option_in_help')
1774 @patch('kiwi.system.setup.Command.run')
1775 @patch('os.scandir')
1776 @patch('os.access')
1777 def test_set_selinux_file_contexts_read_only_root(
1778 self, mock_os_access, mock_os_scandir, mock_command,
1779 mock_has_option_in_help
1780 ):
1781 mock_os_access.return_value = False
1782 mock_has_option_in_help.return_value = False
1783 mock_os_scandir.return_value = self.selinux_policies
1784 self.setup.set_selinux_file_contexts('security_context_file')
1785 assert not mock_command.called
1786
1787 @patch('kiwi.system.setup.CommandCapabilities.has_option_in_help')
1788 @patch('kiwi.system.setup.Command.run')
1789 @patch('os.scandir')
1790 def test_set_selinux_file_contexts_old_version(
1791 self, mock_os_scandir, mock_command, mock_has_option_in_help
1792 ):
1793 mock_has_option_in_help.return_value = True
1794 mock_os_scandir.return_value = self.selinux_policies
1795 self.setup.set_selinux_file_contexts('security_context_file')
1796 assert mock_command.call_args_list == [
1797 call(
1798 [
1799 'chroot', 'root_dir', 'setfiles', '-c',
1800 '/etc/selinux/targeted/policy/policy.some_policy',
1801 'security_context_file'
1802 ]
1803 ),
1804 call(
1805 [
1806 'chroot', 'root_dir', 'setfiles', '-F', '-p',
1807 '-e', '/proc', '-e', '/sys', '-e', '/dev',
1808 'security_context_file', '/'
1809 ]
1810 )
1811 ]
1812
1813 @patch('kiwi.system.setup.Command.run')
1814 @patch('os.scandir')
1815 def test_set_selinux_file_contexts_raises(
1816 self, mock_os_scandir, mock_command
1817 ):
1818 mock_os_scandir.side_effect = Exception
1819 with raises(KiwiFileNotFound):
1820 self.setup.set_selinux_file_contexts('security_context_file')
1821
1822 @patch('os.path.exists')
1823 @patch.object(SystemSetup, 'set_selinux_file_contexts')
1824 @patch('kiwi.system.setup.Path.which')
1825 def test_setup_selinux_file_contexts(
1826 self, mock_Path_which, mock_set_selinux_file_contexts,
1827 mock_os_path_exists
1828 ):
1829 mock_os_path_exists.return_value = True
1830 mock_Path_which.return_value = 'setfiles'
1831 self.setup.setup_selinux_file_contexts()
1832 mock_set_selinux_file_contexts.assert_called_once_with(
1833 '/etc/selinux/targeted/contexts/files/file_contexts'
1834 )
1835 mock_Path_which.return_value = None
1836 with self._caplog.at_level(logging.WARNING):
1837 self.setup.setup_selinux_file_contexts()
1838 assert 'setfiles tool not installed' in self._caplog.text
1839
1840 @patch('kiwi.system.setup.Repository.new')
1841 @patch('kiwi.system.setup.Uri')
1842 def test_import_repositories_marked_as_imageinclude(
1843 self, mock_uri, mock_repo
1844 ):
1845 uri = Mock()
1846 mock_uri.return_value = uri
1847 uri.translate = Mock(
1848 return_value="uri"
1849 )
1850 uri.alias = Mock(
1851 return_value="uri-alias"
1852 )
1853 uri.credentials_file_name = Mock(
1854 return_value='kiwiRepoCredentials'
1855 )
1856 mock_uri.return_value = uri
1857 repo = Mock()
1858 mock_repo.return_value.__enter__.return_value = repo
1859 self.setup_with_real_xml.import_repositories_marked_as_imageinclude()
1860 assert repo.add_repo.call_args_list[0] == call(
1861 'uri-alias', 'uri', 'rpm-md', None, None, None, None, None,
1862 'kiwiRepoCredentials', None, None, None, '../data/script', None
1863 )
1864
1865 @patch('os.path.exists')
1866 def test_script_exists(self, mock_path_exists):
1867 assert self.setup.script_exists('some-script') == \
1868 mock_path_exists.return_value
1869
1870 @patch('pathlib.Path')
1871 @patch('kiwi.system.setup.Command.run')
1872 def test_setup_registry_import(self, mock_Command_run, mock_Path):
1873 with patch('builtins.open'):
1874 self.setup_with_real_xml.setup_registry_import()
1875 assert mock_Command_run.call_args_list == [
1876 call(
1877 [
1878 'chroot', 'root_dir', '/usr/bin/skopeo', 'copy',
1879 'docker://registry.suse.com/home/mschaefer/images_pubcloud'
1880 '/pct/rmtserver:latest',
1881 'oci-archive:/var/tmp/kiwi_containers/'
1882 'rmtserver_latest:registry.suse.com/home/mschaefer/'
1883 'images_pubcloud/pct/rmtserver:latest'
1884 ]
1885 ),
1886 call(
1887 [
1888 'chroot', 'root_dir', '/usr/bin/skopeo', 'copy',
1889 'docker://registry.suse.com/some:latest',
1890 'oci-archive:/var/tmp/kiwi_containers/'
1891 'some_latest:registry.suse.com/some:latest'
1892 ]
1893 ),
1894 call(
1895 [
1896 'chroot', 'root_dir', '/usr/bin/skopeo', 'copy',
1897 'docker://docker.io/foo:latest',
1898 'oci-archive:/var/tmp/kiwi_containers/'
1899 'foo_latest:docker.io/foo:latest'
1900 ]
1901 ),
1902 call(
1903 [
1904 'chroot', 'root_dir', '/usr/bin/skopeo', 'copy',
1905 'docker://registry.example.com/test-app:v1.0',
1906 'oci-archive:/var/tmp/kiwi_containers/'
1907 'test-app_v1.0:registry.example.com/test-app:v1.0'
1908 ]
1909 ),
1910 call(
1911 [
1912 'chroot', 'root_dir', 'systemctl',
1913 'enable', 'kiwi_containers'
1914 ]
1915 )
1916 ]