Debian Codegraph @

test/unit/runtime_checker_test.py

raw
1 import sys
2 import logging
3 from unittest.mock import (
4 patch, Mock
5 )
6 from pytest import (
7 raises, fixture
8 )
9 import pytest
10
11 from .test_helper import argv_kiwi_tests
12
13 import kiwi
14
15 from kiwi.defaults import Defaults
16 from kiwi.xml_state import XMLState
17 from kiwi.xml_description import XMLDescription
18 from kiwi.runtime_checker import RuntimeChecker
19 from kiwi.exceptions import KiwiRuntimeError
20
21
22 class TestRuntimeChecker:
23 @fixture(autouse=True)
24 def inject_fixtures(self, caplog):
25 self._caplog = caplog
26
27 def setup(self):
28 Defaults.set_runtime_checker_metadata(
29 '../data/runtime_checker_metadata.yml'
30 )
31 self.description = XMLDescription(
32 '../data/example_runtime_checker_config.xml'
33 )
34 self.xml_state = XMLState(
35 self.description.load()
36 )
37 self.runtime_config = Mock()
38 self.runtime_config.get_oci_archive_tool.return_value = 'umoci'
39 kiwi.runtime_checker.RuntimeConfig = Mock(
40 return_value=self.runtime_config
41 )
42 self.runtime_checker = RuntimeChecker(self.xml_state)
43
44 def setup_method(self, cls):
45 self.setup()
46
47 @patch('kiwi.runtime_checker.Uri')
48 def test_check_image_include_repos_publicly_resolvable(self, mock_Uri):
49 uri = Mock()
50 uri.is_public.return_value = False
51 mock_Uri.return_value = uri
52 with raises(KiwiRuntimeError):
53 self.runtime_checker.check_image_include_repos_publicly_resolvable()
54
55 @patch('kiwi.runtime_checker.Command.run')
56 def test_check_target_dir_on_unsupported_filesystem(self, mock_Command_run):
57 stat = Mock()
58 stat.output = 'bogus'
59 mock_Command_run.return_value = stat
60 with raises(KiwiRuntimeError):
61 self.runtime_checker.check_target_dir_on_unsupported_filesystem(
62 '/some/root_dir'
63 )
64
65 def test_invalid_target_dir_pointing_to_shared_cache_1(self):
66 with raises(KiwiRuntimeError):
67 self.runtime_checker.check_target_directory_not_in_shared_cache(
68 '/var/cache//kiwi/foo'
69 )
70
71 def test_invalid_target_dir_pointing_to_shared_cache_2(self):
72 with raises(KiwiRuntimeError):
73 self.runtime_checker.check_target_directory_not_in_shared_cache(
74 '/var/cache/kiwi'
75 )
76
77 @patch('os.getcwd')
78 def test_invalid_target_dir_pointing_to_shared_cache_3(self, mock_getcwd):
79 mock_getcwd.return_value = '/'
80 with raises(KiwiRuntimeError):
81 self.runtime_checker.check_target_directory_not_in_shared_cache(
82 'var/cache/kiwi'
83 )
84
85 def test_invalid_target_dir_pointing_to_shared_cache_4(self):
86 with raises(KiwiRuntimeError):
87 self.runtime_checker.check_target_directory_not_in_shared_cache(
88 '//var/cache//kiwi/foo'
89 )
90
91 @patch('os.path.isdir')
92 def test_check_dracut_module_versions_compatible_to_kiwi_no_dracut(
93 self, mock_os_path_isdir
94 ):
95 mock_os_path_isdir.return_value = False
96 self.runtime_checker.\
97 check_dracut_module_versions_compatible_to_kiwi('root_dir')
98 # does not raise if no dracut is installed
99
100 @patch('os.listdir')
101 @patch('os.path.isdir')
102 @patch('kiwi.runtime_checker.Command.run')
103 def test_check_dracut_module_versions_compatible_to_kiwi(
104 self, mock_Command_run, mock_os_path_isdir, mock_os_listdir
105 ):
106 mock_os_path_isdir.return_value = True
107 command = Mock()
108 command.output = '1.2.3-1.2'
109 mock_Command_run.return_value = command
110 mock_os_listdir.return_value = ['55kiwi-dump']
111 package_manager = Mock()
112 package_manager.return_value = 'zypper'
113 self.xml_state.get_package_manager = package_manager
114 with raises(KiwiRuntimeError) as exception_data:
115 self.runtime_checker.\
116 check_dracut_module_versions_compatible_to_kiwi('root_dir')
117 assert "'dracut-kiwi-oem-dump': 'got:1.2.3, need:>=9.20.1'" in str(
118 exception_data.value
119 )
120 mock_Command_run.assert_called_once_with(
121 [
122 'chroot', 'root_dir', 'rpm', '-q',
123 '--qf', '%{VERSION}', 'dracut-kiwi-oem-dump'
124 ]
125 )
126 package_manager.return_value = 'apt'
127 mock_Command_run.reset_mock()
128 with raises(KiwiRuntimeError) as exception_data:
129 self.runtime_checker.\
130 check_dracut_module_versions_compatible_to_kiwi('root_dir')
131 assert "'dracut-kiwi-oem-dump': 'got:1.2.3, need:>=9.20.1'" in str(
132 exception_data.value
133 )
134 mock_Command_run.assert_called_once_with(
135 [
136 'chroot', 'root_dir', 'dpkg-query',
137 '-W', '-f', '${Version}', 'dracut-kiwi-oem-dump'
138 ]
139 )
140 mock_Command_run.side_effect = Exception
141 # If the package manager call failed with an exception
142 # the expected behavior is the runtime check to just pass
143 # because there is no data to operate on. Best guess approach
144 with self._caplog.at_level(logging.DEBUG):
145 self.runtime_checker.\
146 check_dracut_module_versions_compatible_to_kiwi('root_dir')
147
148 def test_valid_target_dir_1(self):
149 assert self.runtime_checker.check_target_directory_not_in_shared_cache(
150 '/var/cache/kiwi-fast-tmpsystemdir'
151 ) is None
152
153 def test_valid_target_dir_2(self):
154 assert self.runtime_checker.check_target_directory_not_in_shared_cache(
155 '/foo/bar'
156 ) is None
157
158 def test_check_repositories_configured(self):
159 self.xml_state.delete_repository_sections()
160 with raises(KiwiRuntimeError):
161 self.runtime_checker.check_repositories_configured()
162
163 def test_check_volume_setup_defines_reserved_labels(self):
164 xml_state = XMLState(
165 self.description.load(), ['vmxSimpleFlavour'], 'oem'
166 )
167 runtime_checker = RuntimeChecker(xml_state)
168 with raises(KiwiRuntimeError):
169 runtime_checker.check_volume_setup_defines_reserved_labels()
170
171 def test_appx_invalid_name(self):
172 xml_state = XMLState(
173 self.description.load(), ['wsl_launcher'], 'appx'
174 )
175 runtime_checker = RuntimeChecker(xml_state)
176 with raises(KiwiRuntimeError):
177 runtime_checker.check_appx_naming_conventions_valid()
178
179 def test_appx_invalid_id(self):
180 xml_state = XMLState(
181 self.description.load(), ['wsl_id'], 'appx'
182 )
183 runtime_checker = RuntimeChecker(xml_state)
184 with raises(KiwiRuntimeError):
185 runtime_checker.check_appx_naming_conventions_valid()
186
187 def test_check_volume_setup_defines_multiple_fullsize_volumes(self):
188 with raises(KiwiRuntimeError):
189 self.runtime_checker.\
190 check_volume_setup_defines_multiple_fullsize_volumes()
191
192 def test_check_volume_setup_has_no_root_definition(self):
193 with raises(KiwiRuntimeError):
194 self.runtime_checker.check_volume_setup_has_no_root_definition()
195
196 def test_check_partuuid_persistency_type_used_with_mbr(self):
197 xml_state = XMLState(
198 self.description.load(), ['vmxFlavour'], 'iso'
199 )
200 runtime_checker = RuntimeChecker(xml_state)
201 with raises(KiwiRuntimeError):
202 runtime_checker.check_partuuid_persistency_type_used_with_mbr()
203
204 @patch('kiwi.runtime_checker.CommandCapabilities.has_option_in_help')
205 def test_check_luksformat_options_valid(self, mock_has_option_in_help):
206 mock_has_option_in_help.return_value = False
207 with raises(KiwiRuntimeError):
208 self.runtime_checker.check_luksformat_options_valid()
209
210 @patch('kiwi.runtime_checker.Path.which')
211 def test_check_container_tool_chain_installed(self, mock_which):
212 mock_which.return_value = False
213 xml_state = XMLState(
214 self.description.load(), ['docker'], 'docker'
215 )
216 runtime_checker = RuntimeChecker(xml_state)
217 with raises(KiwiRuntimeError):
218 runtime_checker.check_container_tool_chain_installed()
219
220 @patch('kiwi.runtime_checker.Path.which')
221 def test_check_container_tool_chain_installed_unknown_tool(
222 self, mock_which
223 ):
224 self.runtime_config.get_oci_archive_tool.return_value = 'budah'
225 mock_which.return_value = False
226 xml_state = XMLState(
227 self.description.load(), ['docker'], 'docker'
228 )
229 runtime_checker = RuntimeChecker(xml_state)
230 with raises(KiwiRuntimeError):
231 runtime_checker.check_container_tool_chain_installed()
232
233 @patch('kiwi.runtime_checker.Path.which')
234 def test_check_container_tool_chain_installed_buildah(
235 self, mock_which
236 ):
237 self.runtime_config.get_oci_archive_tool.return_value = 'buildah'
238 mock_which.return_value = False
239 xml_state = XMLState(
240 self.description.load(), ['docker'], 'docker'
241 )
242 runtime_checker = RuntimeChecker(xml_state)
243 with raises(KiwiRuntimeError):
244 runtime_checker.check_container_tool_chain_installed()
245
246 @patch('kiwi.runtime_checker.Path.which')
247 @patch('kiwi.runtime_checker.CommandCapabilities.check_version')
248 def test_check_container_tool_chain_installed_with_version(
249 self, mock_cmdver, mock_which
250 ):
251 mock_which.return_value = True
252 mock_cmdver.return_value = False
253 xml_state = XMLState(
254 self.description.load(), ['docker'], 'docker'
255 )
256 runtime_checker = RuntimeChecker(xml_state)
257 with raises(KiwiRuntimeError):
258 runtime_checker.check_container_tool_chain_installed()
259
260 @patch('kiwi.runtime_checker.Path.which')
261 @patch('kiwi.runtime_checker.CommandCapabilities.check_version')
262 @patch('kiwi.runtime_checker.CommandCapabilities.has_option_in_help')
263 def test_check_container_tool_chain_installed_with_multitags(
264 self, mock_cmdoptions, mock_cmdver, mock_which
265 ):
266 mock_which.return_value = True
267 mock_cmdver.return_value = True
268 mock_cmdoptions.return_value = False
269 xml_state = XMLState(
270 self.description.load(), ['docker'], 'docker'
271 )
272 runtime_checker = RuntimeChecker(xml_state)
273 with raises(KiwiRuntimeError):
274 runtime_checker.check_container_tool_chain_installed()
275
276 @patch('kiwi.runtime_checker.Defaults.get_boot_image_description_path')
277 def test_check_consistent_kernel_in_boot_and_system_image(
278 self, mock_boot_path
279 ):
280 Defaults.set_platform_name('x86_64')
281 mock_boot_path.return_value = '../data'
282 xml_state = XMLState(
283 self.description.load(), ['vmxFlavour'], 'oem'
284 )
285 runtime_checker = RuntimeChecker(xml_state)
286 with raises(KiwiRuntimeError):
287 runtime_checker.check_consistent_kernel_in_boot_and_system_image()
288
289 def test_check_initrd_selection_required(self):
290 description = XMLDescription(
291 '../data/example_runtime_checker_no_initrd_system_reference.xml'
292 )
293 xml_state = XMLState(description.load())
294 runtime_checker = RuntimeChecker(xml_state)
295 with raises(KiwiRuntimeError):
296 runtime_checker.check_initrd_selection_required()
297
298 def test_check_boot_description_exists_no_boot_ref(self):
299 description = XMLDescription(
300 '../data/example_runtime_checker_no_boot_reference.xml'
301 )
302 xml_state = XMLState(description.load())
303 runtime_checker = RuntimeChecker(xml_state)
304 with raises(KiwiRuntimeError):
305 runtime_checker.check_boot_description_exists()
306
307 def test_check_boot_description_exists_does_not_exist(self):
308 description = XMLDescription(
309 '../data/example_runtime_checker_boot_desc_not_found.xml'
310 )
311 xml_state = XMLState(description.load())
312 runtime_checker = RuntimeChecker(xml_state)
313 with raises(KiwiRuntimeError):
314 runtime_checker.check_boot_description_exists()
315
316 def test_check_xen_uniquely_setup_as_server_or_guest_for_ec2(self):
317 self.xml_state.build_type.get_firmware = Mock(
318 return_value='ec2'
319 )
320 self.xml_state.is_xen_server = Mock(
321 return_value=True
322 )
323 self.xml_state.is_xen_guest = Mock(
324 return_value=True
325 )
326 with raises(KiwiRuntimeError):
327 self.runtime_checker.check_xen_uniquely_setup_as_server_or_guest()
328
329 def test_check_efi_fat_image_has_correct_size(self):
330 self.xml_state.build_type.get_efifatimagesize = Mock(
331 return_value='200'
332 )
333 with raises(KiwiRuntimeError):
334 self.runtime_checker.check_efi_fat_image_has_correct_size()
335
336 def test_check_xen_uniquely_setup_as_server_or_guest_for_xen(self):
337 self.xml_state.build_type.get_firmware = Mock(
338 return_value=None
339 )
340 self.xml_state.is_xen_server = Mock(
341 return_value=True
342 )
343 self.xml_state.is_xen_guest = Mock(
344 return_value=True
345 )
346 with raises(KiwiRuntimeError):
347 self.runtime_checker.check_xen_uniquely_setup_as_server_or_guest()
348
349 def test_check_dracut_module_for_disk_overlay_in_package_list(self):
350 xml_state = XMLState(
351 self.description.load(), ['vmxFlavour'], 'iso'
352 )
353 xml_state.build_type.get_overlayroot = Mock(
354 return_value=True
355 )
356 runtime_checker = RuntimeChecker(xml_state)
357 with raises(KiwiRuntimeError):
358 runtime_checker.\
359 check_dracut_module_for_disk_overlay_in_package_list()
360
361 @pytest.mark.parametrize("tool_name, tool_binary", [("isomd5sum", "implantisomd5"), ("checkmedia", "tagmedia")])
362 @patch('kiwi.runtime_checker.Path.which')
363 @patch('kiwi.runtime_checker.RuntimeConfig')
364 def test_check_mediacheck_installed_implantisomd5_missing(
365 self, mock_RuntimeConfig, mock_which, tool_name, tool_binary
366 ):
367 runtime_config = Mock()
368 runtime_config.get_iso_media_tag_tool.return_value = tool_name
369 mock_RuntimeConfig.return_value = runtime_config
370
371 mock_which.return_value = False
372 xml_state = XMLState(
373 self.description.load(), ['vmxFlavour'], 'iso'
374 )
375 runtime_checker = RuntimeChecker(xml_state)
376 with raises(KiwiRuntimeError) as rt_err_ctx:
377 runtime_checker.check_mediacheck_installed()
378
379 assert f"Required tool {tool_binary} not found in caller environment" in str(rt_err_ctx.value)
380
381 @patch('kiwi.runtime_checker.Path.which')
382 @patch('kiwi.runtime_checker.RuntimeConfig')
383 @patch('kiwi.runtime_checker.FirmWare')
384 @patch('kiwi.runtime_checker.CommandCapabilities.check_version')
385 def test_check_checkmedia_used_with_msdos_table(
386 self,
387 mock_CommandCapabilities_check_version,
388 mock_FirmWare,
389 mock_RuntimeConfig,
390 mock_which
391 ):
392 mock_CommandCapabilities_check_version.return_value = False
393 firmware = Mock()
394 firmware.get_partition_table_type.return_value = 'gpt'
395 mock_FirmWare.return_value = firmware
396 runtime_config = Mock()
397 runtime_config.get_iso_media_tag_tool.return_value = 'checkmedia'
398 mock_RuntimeConfig.return_value = runtime_config
399 mock_which.return_value = True
400 xml_state = XMLState(
401 self.description.load(), ['vmxFlavour'], 'iso'
402 )
403 runtime_checker = RuntimeChecker(xml_state)
404 with raises(KiwiRuntimeError) as runtime_context:
405 runtime_checker.check_checkmedia_used_with_msdos_table()
406
407 assert 'ISO media tag tool tagmedia < v6.6 does not support gpt' in \
408 format(runtime_context)
409
410 def test_check_dracut_module_for_live_iso_in_package_list(self):
411 xml_state = XMLState(
412 self.description.load(), ['vmxFlavour'], 'iso'
413 )
414 runtime_checker = RuntimeChecker(xml_state)
415 with raises(KiwiRuntimeError):
416 runtime_checker.check_dracut_module_for_live_iso_in_package_list()
417
418 def test_check_dracut_module_for_disk_oem_in_package_list(self):
419 xml_state = XMLState(
420 self.description.load(), ['vmxFlavour'], 'oem'
421 )
422 runtime_checker = RuntimeChecker(xml_state)
423 with raises(KiwiRuntimeError):
424 runtime_checker.check_dracut_module_for_disk_oem_in_package_list()
425
426 def test_check_dracut_module_for_oem_install_in_package_list(self):
427 xml_state = XMLState(
428 self.description.load(), ['vmxFlavour'], 'oem'
429 )
430 runtime_checker = RuntimeChecker(xml_state)
431 with raises(KiwiRuntimeError):
432 runtime_checker.\
433 check_dracut_module_for_oem_install_in_package_list()
434
435 def test_check_volume_label_used_with_lvm(self):
436 with raises(KiwiRuntimeError):
437 self.runtime_checker.check_volume_label_used_with_lvm()
438
439 def test_check_swap_name_used_with_lvm(self):
440 xml_state = XMLState(
441 self.description.load(), ['vmxFlavour'], 'oem'
442 )
443 runtime_checker = RuntimeChecker(xml_state)
444 with raises(KiwiRuntimeError):
445 runtime_checker.check_swap_name_used_with_lvm()
446
447 def test_check_preferences_data_no_version(self):
448 xml_state = XMLState(
449 self.description.load(), ['docker'], 'docker'
450 )
451 runtime_checker = RuntimeChecker(xml_state)
452 with raises(KiwiRuntimeError):
453 runtime_checker.check_image_version_provided()
454
455 def test_check_image_type_unique(self):
456 description = XMLDescription(
457 '../data/example_runtime_checker_conflicting_types.xml'
458 )
459 xml_state = XMLState(description.load())
460 runtime_checker = RuntimeChecker(xml_state)
461 with raises(KiwiRuntimeError):
462 runtime_checker.check_image_type_unique()
463
464 def test_check_include_references_unresolvable(self):
465 description = XMLDescription(
466 '../data/example_runtime_checker_include_nested_reference.xml'
467 )
468 xml_state = XMLState(description.load())
469 runtime_checker = RuntimeChecker(xml_state)
470 with raises(KiwiRuntimeError):
471 runtime_checker.check_include_references_unresolvable()
472
473 def test_check_bootloader_env_compatible_with_loader(self):
474 self.xml_state.get_build_type_bootloader_name = Mock(
475 return_value='systemd_boot'
476 )
477 self.xml_state.get_build_type_bootloader_environment_variables = Mock(
478 return_value=['some']
479 )
480 with raises(KiwiRuntimeError):
481 self.runtime_checker.check_bootloader_env_compatible_with_loader()
482
483 def test_check_package_in_list(self):
484 assert RuntimeChecker._package_in_list(['a', 'b'], ['b']) == 'b'
485 assert RuntimeChecker._package_in_list(['a', 'b'], ['c']) == ''
486
487 def teardown(self):
488 sys.argv = argv_kiwi_tests
489
490 def teardown_method(self, cls):
491 self.teardown()