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_platform_name('x86_64')
29 Defaults.set_runtime_checker_metadata(
30 '../data/runtime_checker_metadata.yml'
31 )
32 self.description = XMLDescription(
33 '../data/example_runtime_checker_config.xml'
34 )
35 self.xml_state = XMLState(
36 self.description.load()
37 )
38 self.runtime_config = Mock()
39 self.runtime_config.get_oci_archive_tool.return_value = 'umoci'
40 kiwi.runtime_checker.RuntimeConfig = Mock(
41 return_value=self.runtime_config
42 )
43 self.runtime_checker = RuntimeChecker(self.xml_state)
44
45 def setup_method(self, cls):
46 self.setup()
47
48 @patch('kiwi.runtime_checker.Uri')
49 def test_check_image_include_repos_publicly_resolvable(self, mock_Uri):
50 uri = Mock()
51 uri.is_public.return_value = False
52 mock_Uri.return_value = uri
53 with raises(KiwiRuntimeError):
54 self.runtime_checker.check_image_include_repos_publicly_resolvable()
55
56 @patch('kiwi.runtime_checker.Command.run')
57 def test_check_target_dir_on_unsupported_filesystem(self, mock_Command_run):
58 stat = Mock()
59 stat.output = 'bogus'
60 mock_Command_run.return_value = stat
61 with raises(KiwiRuntimeError):
62 self.runtime_checker.check_target_dir_on_unsupported_filesystem(
63 '/some/root_dir'
64 )
65
66 def test_invalid_target_dir_pointing_to_shared_cache_1(self):
67 with raises(KiwiRuntimeError):
68 self.runtime_checker.check_target_directory_not_in_shared_cache(
69 '/var/cache//kiwi/foo'
70 )
71
72 def test_invalid_target_dir_pointing_to_shared_cache_2(self):
73 with raises(KiwiRuntimeError):
74 self.runtime_checker.check_target_directory_not_in_shared_cache(
75 '/var/cache/kiwi'
76 )
77
78 @patch('os.getcwd')
79 def test_invalid_target_dir_pointing_to_shared_cache_3(self, mock_getcwd):
80 mock_getcwd.return_value = '/'
81 with raises(KiwiRuntimeError):
82 self.runtime_checker.check_target_directory_not_in_shared_cache(
83 'var/cache/kiwi'
84 )
85
86 def test_invalid_target_dir_pointing_to_shared_cache_4(self):
87 with raises(KiwiRuntimeError):
88 self.runtime_checker.check_target_directory_not_in_shared_cache(
89 '//var/cache//kiwi/foo'
90 )
91
92 @patch('os.path.isdir')
93 def test_check_dracut_module_versions_compatible_to_kiwi_no_dracut(
94 self, mock_os_path_isdir
95 ):
96 mock_os_path_isdir.return_value = False
97 self.runtime_checker.\
98 check_dracut_module_versions_compatible_to_kiwi('root_dir')
99 # does not raise if no dracut is installed
100
101 @patch('os.listdir')
102 @patch('os.path.isdir')
103 @patch('kiwi.runtime_checker.Command.run')
104 def test_check_dracut_module_versions_compatible_to_kiwi(
105 self, mock_Command_run, mock_os_path_isdir, mock_os_listdir
106 ):
107 mock_os_path_isdir.return_value = True
108 command = Mock()
109 command.output = '1.2.3-1.2'
110 mock_Command_run.return_value = command
111 mock_os_listdir.return_value = ['55kiwi-dump']
112 package_manager = Mock()
113 package_manager.return_value = 'zypper'
114 self.xml_state.get_package_manager = package_manager
115 with raises(KiwiRuntimeError) as exception_data:
116 self.runtime_checker.\
117 check_dracut_module_versions_compatible_to_kiwi('root_dir')
118 assert "'dracut-kiwi-oem-dump': 'got:1.2.3, need:>=9.20.1'" in str(
119 exception_data.value
120 )
121 mock_Command_run.assert_called_once_with(
122 [
123 'chroot', 'root_dir', 'rpm', '-q',
124 '--qf', '%{VERSION}', 'dracut-kiwi-oem-dump'
125 ]
126 )
127 package_manager.return_value = 'apt'
128 mock_Command_run.reset_mock()
129 with raises(KiwiRuntimeError) as exception_data:
130 self.runtime_checker.\
131 check_dracut_module_versions_compatible_to_kiwi('root_dir')
132 assert "'dracut-kiwi-oem-dump': 'got:1.2.3, need:>=9.20.1'" in str(
133 exception_data.value
134 )
135 mock_Command_run.assert_called_once_with(
136 [
137 'chroot', 'root_dir', 'dpkg-query',
138 '-W', '-f', '${Version}', 'dracut-kiwi-oem-dump'
139 ]
140 )
141 mock_Command_run.side_effect = Exception
142 # If the package manager call failed with an exception
143 # the expected behavior is the runtime check to just pass
144 # because there is no data to operate on. Best guess approach
145 with self._caplog.at_level(logging.DEBUG):
146 self.runtime_checker.\
147 check_dracut_module_versions_compatible_to_kiwi('root_dir')
148
149 def test_valid_target_dir_1(self):
150 assert self.runtime_checker.check_target_directory_not_in_shared_cache(
151 '/var/cache/kiwi-fast-tmpsystemdir'
152 ) is None
153
154 def test_valid_target_dir_2(self):
155 assert self.runtime_checker.check_target_directory_not_in_shared_cache(
156 '/foo/bar'
157 ) is None
158
159 def test_check_repositories_configured(self):
160 self.xml_state.delete_repository_sections()
161 with raises(KiwiRuntimeError):
162 self.runtime_checker.check_repositories_configured()
163
164 def test_check_volume_setup_defines_reserved_labels(self):
165 xml_state = XMLState(
166 self.description.load(), ['vmxSimpleFlavour'], 'oem'
167 )
168 runtime_checker = RuntimeChecker(xml_state)
169 with raises(KiwiRuntimeError):
170 runtime_checker.check_volume_setup_defines_reserved_labels()
171
172 def test_appx_invalid_name(self):
173 xml_state = XMLState(
174 self.description.load(), ['wsl_launcher'], 'appx'
175 )
176 runtime_checker = RuntimeChecker(xml_state)
177 with raises(KiwiRuntimeError):
178 runtime_checker.check_appx_naming_conventions_valid()
179
180 def test_appx_invalid_id(self):
181 xml_state = XMLState(
182 self.description.load(), ['wsl_id'], 'appx'
183 )
184 runtime_checker = RuntimeChecker(xml_state)
185 with raises(KiwiRuntimeError):
186 runtime_checker.check_appx_naming_conventions_valid()
187
188 def test_check_volume_setup_defines_multiple_fullsize_volumes(self):
189 with raises(KiwiRuntimeError):
190 self.runtime_checker.\
191 check_volume_setup_defines_multiple_fullsize_volumes()
192
193 def test_check_volume_setup_has_no_root_definition(self):
194 with raises(KiwiRuntimeError):
195 self.runtime_checker.check_volume_setup_has_no_root_definition()
196
197 def test_check_partuuid_persistency_type_used_with_mbr(self):
198 xml_state = XMLState(
199 self.description.load(), ['vmxFlavour'], 'iso'
200 )
201 runtime_checker = RuntimeChecker(xml_state)
202 with raises(KiwiRuntimeError):
203 runtime_checker.check_partuuid_persistency_type_used_with_mbr()
204
205 @patch('kiwi.runtime_checker.CommandCapabilities.has_option_in_help')
206 def test_check_luksformat_options_valid(self, mock_has_option_in_help):
207 mock_has_option_in_help.return_value = False
208 with raises(KiwiRuntimeError):
209 self.runtime_checker.check_luksformat_options_valid()
210
211 @patch('kiwi.runtime_checker.Path.which')
212 def test_check_container_tool_chain_installed(self, mock_which):
213 mock_which.return_value = False
214 xml_state = XMLState(
215 self.description.load(), ['docker'], 'docker'
216 )
217 runtime_checker = RuntimeChecker(xml_state)
218 with raises(KiwiRuntimeError):
219 runtime_checker.check_container_tool_chain_installed()
220
221 @patch('kiwi.runtime_checker.Path.which')
222 def test_check_container_tool_chain_installed_unknown_tool(
223 self, mock_which
224 ):
225 self.runtime_config.get_oci_archive_tool.return_value = 'budah'
226 mock_which.return_value = False
227 xml_state = XMLState(
228 self.description.load(), ['docker'], 'docker'
229 )
230 runtime_checker = RuntimeChecker(xml_state)
231 with raises(KiwiRuntimeError):
232 runtime_checker.check_container_tool_chain_installed()
233
234 @patch('kiwi.runtime_checker.Path.which')
235 def test_check_container_tool_chain_installed_buildah(
236 self, mock_which
237 ):
238 self.runtime_config.get_oci_archive_tool.return_value = 'buildah'
239 mock_which.return_value = False
240 xml_state = XMLState(
241 self.description.load(), ['docker'], 'docker'
242 )
243 runtime_checker = RuntimeChecker(xml_state)
244 with raises(KiwiRuntimeError):
245 runtime_checker.check_container_tool_chain_installed()
246
247 @patch('kiwi.runtime_checker.Path.which')
248 @patch('kiwi.runtime_checker.CommandCapabilities.check_version')
249 def test_check_container_tool_chain_installed_with_version(
250 self, mock_cmdver, mock_which
251 ):
252 mock_which.return_value = True
253 mock_cmdver.return_value = False
254 xml_state = XMLState(
255 self.description.load(), ['docker'], 'docker'
256 )
257 runtime_checker = RuntimeChecker(xml_state)
258 with raises(KiwiRuntimeError):
259 runtime_checker.check_container_tool_chain_installed()
260
261 @patch('kiwi.runtime_checker.Path.which')
262 @patch('kiwi.runtime_checker.CommandCapabilities.check_version')
263 @patch('kiwi.runtime_checker.CommandCapabilities.has_option_in_help')
264 def test_check_container_tool_chain_installed_with_multitags(
265 self, mock_cmdoptions, mock_cmdver, mock_which
266 ):
267 mock_which.return_value = True
268 mock_cmdver.return_value = True
269 mock_cmdoptions.return_value = False
270 xml_state = XMLState(
271 self.description.load(), ['docker'], 'docker'
272 )
273 runtime_checker = RuntimeChecker(xml_state)
274 with raises(KiwiRuntimeError):
275 runtime_checker.check_container_tool_chain_installed()
276
277 @patch('kiwi.runtime_checker.Defaults.get_boot_image_description_path')
278 def test_check_consistent_kernel_in_boot_and_system_image(
279 self, mock_boot_path
280 ):
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()