Debian Codegraph @

test/unit/storage/disk_test.py

raw
1 import logging
2 from unittest.mock import (
3 patch, mock_open, call, Mock
4 )
5 from pytest import (
6 fixture, raises
7 )
8
9 import unittest.mock as mock
10
11 from kiwi.defaults import Defaults
12 from kiwi.storage.disk import ptable_entry_type
13 from kiwi.storage.disk import Disk
14 from kiwi.exceptions import (
15 KiwiCustomPartitionConflictError,
16 KiwiCommandError
17 )
18
19
20 class TestDisk:
21 @fixture(autouse=True)
22 def inject_fixtures(self, caplog):
23 self._caplog = caplog
24
25 @patch.object(Disk, 'get_discoverable_partition_ids')
26 @patch('kiwi.storage.disk.Partitioner.new')
27 @patch('kiwi.storage.disk.RuntimeConfig')
28 def setup(
29 self, mock_RuntimeConfig, mock_partitioner,
30 mock_get_discoverable_partition_ids
31 ):
32 Defaults.set_platform_name('x86_64')
33 runtime_config = Mock()
34 runtime_config.get_mapper_tool.return_value = 'partx'
35 mock_RuntimeConfig.return_value = runtime_config
36 self.tempfile = mock.Mock()
37 self.tempfile.name = 'tempfile'
38
39 self.partitioner = mock.Mock()
40 self.partitioner.create = mock.Mock()
41 self.partitioner.get_id = mock.Mock(
42 return_value=1
43 )
44 mock_partitioner.return_value = self.partitioner
45 self.storage_provider = mock.Mock()
46 self.storage_provider.is_loop = mock.Mock(
47 return_value=True
48 )
49 self.storage_provider.get_device = mock.Mock(
50 return_value='/dev/loop0'
51 )
52 self.disk = Disk('gpt', self.storage_provider)
53
54 @patch('kiwi.storage.disk.Partitioner.new')
55 @patch('kiwi.storage.disk.RuntimeConfig')
56 def setup_method(self, cls, mock_RuntimeConfig, mock_partitioner):
57 self.setup()
58
59 @patch('os.path.exists')
60 def test_get_device(self, mock_exists):
61 mock_exists.return_value = True
62 self.disk.partition_map['root'] = '/dev/root-device'
63 assert self.disk.get_device()['root'].get_device() == '/dev/root-device'
64
65 def test_is_loop(self):
66 self.disk.is_loop()
67 self.storage_provider.is_loop.assert_called_once_with()
68
69 def test_create_root_partition(self):
70 self.disk.create_root_partition('100', 1)
71 assert self.partitioner.create.call_args_list == [
72 call(
73 name='p.lxrootclone1',
74 mbsize='100',
75 type_name='t.linux',
76 partition_id=None
77 ),
78 call(
79 name='p.lxroot',
80 mbsize='100',
81 type_name='t.linux',
82 partition_id=None
83 )
84 ]
85
86 def test_create_root_which_is_also_boot_partition(self):
87 self.disk.create_root_partition('200')
88 self.partitioner.create.assert_called_once_with(
89 name='p.lxroot',
90 mbsize='200',
91 type_name='t.linux',
92 partition_id=None
93 )
94 assert self.disk.public_partition_id_map['kiwi_RootPart'] == 1
95 assert self.disk.public_partition_id_map['kiwi_BootPart'] == 1
96
97 def test_create_root_which_is_also_read_write_partition(self):
98 self.disk.public_partition_id_map['kiwi_ROPart'] = 1
99 self.disk.create_root_partition('200')
100 self.partitioner.create.assert_called_once_with(
101 name='p.lxroot',
102 mbsize='200',
103 type_name='t.linux',
104 partition_id=None
105 )
106 assert self.disk.public_partition_id_map['kiwi_RootPart'] == 1
107 assert self.disk.public_partition_id_map['kiwi_RWPart'] == 1
108
109 def test_create_root_lvm_partition(self):
110 self.disk.create_root_lvm_partition('100', 1)
111 assert self.partitioner.create.call_args_list == [
112 call(
113 name='p.lxrootclone1',
114 mbsize='100',
115 type_name='t.lvm',
116 partition_id=None
117 ),
118 call(
119 name='p.lxlvm',
120 mbsize='100',
121 type_name='t.lvm',
122 partition_id=None
123 )
124 ]
125 assert self.disk.public_partition_id_map['kiwi_rootPartClone1'] == 1
126 assert self.disk.public_partition_id_map['kiwi_RootPart'] == 1
127
128 def test_create_root_raid_partition(self):
129 self.disk.create_root_raid_partition('100', 1)
130 assert self.partitioner.create.call_args_list == [
131 call(
132 name='p.lxrootclone1',
133 mbsize='100',
134 type_name='t.raid',
135 partition_id=None
136 ),
137 call(
138 name='p.lxraid',
139 mbsize='100',
140 type_name='t.raid',
141 partition_id=None
142 )
143 ]
144 assert self.disk.public_partition_id_map['kiwi_rootPartClone1'] == 1
145 assert self.disk.public_partition_id_map['kiwi_RootPart'] == 1
146 assert self.disk.public_partition_id_map['kiwi_RaidPart'] == 1
147
148 def test_create_root_readonly_partition(self):
149 self.disk.create_root_readonly_partition('100', 1)
150 assert self.partitioner.create.call_args_list == [
151 call(
152 name='p.lxrootclone1',
153 mbsize='100',
154 type_name='t.linux',
155 partition_id=None
156 ),
157 call(
158 name='p.lxreadonly',
159 mbsize='100',
160 type_name='t.linux',
161 partition_id=None
162 )
163 ]
164 assert self.disk.public_partition_id_map['kiwi_rootPartClone1'] == 1
165 assert self.disk.public_partition_id_map['kiwi_ROPart'] == 1
166
167 def test_create_boot_partition(self):
168 self.disk.create_boot_partition('100', 1)
169 assert self.partitioner.create.call_args_list == [
170 call(
171 name='p.lxbootclone1',
172 mbsize='100',
173 type_name='t.linux',
174 partition_id=None
175 ),
176 call(
177 name='p.lxboot',
178 mbsize='100',
179 type_name='t.linux',
180 partition_id=None
181 )
182 ]
183 assert self.disk.public_partition_id_map['kiwi_bootPartClone1'] == 1
184 assert self.disk.public_partition_id_map['kiwi_BootPart'] == 1
185
186 def test_create_efi_csm_partition(self):
187 self.disk.create_efi_csm_partition('100')
188 self.partitioner.create.assert_called_once_with(
189 name='p.legacy', mbsize='100', type_name='t.csm', partition_id=None
190 )
191 assert self.disk.public_partition_id_map['kiwi_BiosGrub'] == 1
192
193 def test_create_efi_partition(self):
194 self.disk.create_efi_partition('100')
195 self.partitioner.create.assert_called_once_with(
196 name='p.UEFI', mbsize='100', type_name='t.efi', partition_id=None
197 )
198 assert self.disk.public_partition_id_map['kiwi_EfiPart'] == 1
199
200 def test_create_spare_partition(self):
201 self.disk.create_spare_partition('42')
202 self.partitioner.create.assert_called_once_with(
203 name='p.spare', mbsize='42', type_name='t.linux', partition_id=None
204 )
205 assert self.disk.public_partition_id_map['kiwi_SparePart'] == 1
206
207 def test_create_swap_partition(self):
208 self.disk.create_swap_partition('42')
209 self.partitioner.create.assert_called_once_with(
210 name='p.swap', mbsize='42', type_name='t.swap', partition_id=None
211 )
212 assert self.disk.public_partition_id_map['kiwi_SwapPart'] == 1
213
214 @patch('kiwi.storage.disk.Command.run')
215 def test_create_prep_partition(self, mock_command):
216 self.disk.create_prep_partition('8')
217 self.partitioner.create.assert_called_once_with(
218 name='p.prep', mbsize='8', type_name='t.prep', partition_id=None
219 )
220 assert self.disk.public_partition_id_map['kiwi_PrepPart'] == 1
221
222 @patch('kiwi.storage.disk.Command.run')
223 def test_create_custom_partitions(self, mock_command):
224 table_entries = {
225 'var': ptable_entry_type(
226 mbsize='100',
227 clone=2,
228 partition_name='p.lxvar',
229 partition_type='t.linux',
230 partition_id=None,
231 mountpoint='/var',
232 filesystem='ext3',
233 label='var'
234 )
235 }
236 self.disk.create_custom_partitions(table_entries)
237 assert self.partitioner.create.call_args_list == [
238 call(
239 name='p.lxvarclone1',
240 mbsize='100',
241 type_name='t.linux',
242 partition_id=None
243 ),
244 call(
245 name='p.lxvarclone2',
246 mbsize='100',
247 type_name='t.linux',
248 partition_id=None
249 ),
250 call(
251 name='p.lxvar',
252 mbsize='100',
253 type_name='t.linux',
254 partition_id=None
255 )
256 ]
257 assert self.disk.public_partition_id_map['kiwi_varPartClone1'] == 1
258 assert self.disk.public_partition_id_map['kiwi_varPartClone2'] == 1
259 assert self.disk.public_partition_id_map['kiwi_VarPart'] == 1
260
261 @patch('kiwi.storage.disk.Command.run')
262 def test_create_custom_partitions_with_custom_ID(self, mock_command):
263 table_entries = {
264 'var': ptable_entry_type(
265 mbsize='100',
266 clone=2,
267 partition_name='p.lxvar',
268 partition_type='t.linux',
269 partition_id=42,
270 mountpoint='/var',
271 filesystem='ext3',
272 label='var'
273 )
274 }
275 self.disk.create_custom_partitions(table_entries)
276 assert self.partitioner.create.call_args_list == [
277 call(
278 name='p.lxvarclone43',
279 mbsize='100',
280 type_name='t.linux',
281 partition_id=43
282 ),
283 call(
284 name='p.lxvarclone44',
285 mbsize='100',
286 type_name='t.linux',
287 partition_id=44
288 ),
289 call(
290 name='p.lxvar',
291 mbsize='100',
292 type_name='t.linux',
293 partition_id=42
294 )
295 ]
296 assert self.disk.public_partition_id_map['kiwi_varPartClone43'] == 1
297 assert self.disk.public_partition_id_map['kiwi_varPartClone44'] == 1
298 assert self.disk.public_partition_id_map['kiwi_VarPart'] == 1
299
300 def test_create_custom_partitions_reserved_name(self):
301 table_entries = {
302 'root': ptable_entry_type(
303 mbsize='100',
304 clone=0,
305 partition_name='p.lxroot',
306 partition_type='t.linux',
307 partition_id=None,
308 mountpoint='/',
309 filesystem='ext3',
310 label='root'
311 )
312 }
313 with raises(KiwiCustomPartitionConflictError):
314 self.disk.create_custom_partitions(table_entries)
315
316 @patch('kiwi.storage.disk.Command.run')
317 def test_device_map_efi_partition_partx(self, mock_command):
318 self.disk.create_efi_partition('100')
319 self.disk.map_partitions()
320 assert self.disk.partition_map == {'efi': '/dev/loop0p1'}
321 self.disk.is_mapped = False
322
323 @patch('kiwi.storage.disk.Command.run')
324 def test_device_map_efi_partition_kpartx(self, mock_command):
325 self.disk.partition_mapper = 'kpartx'
326 self.disk.create_efi_partition('100')
327 self.disk.map_partitions()
328 assert self.disk.partition_map == {'efi': '/dev/mapper/loop0p1'}
329 self.disk.is_mapped = False
330
331 @patch('kiwi.storage.disk.Command.run')
332 def test_device_map_prep_partition(self, mock_command):
333 self.disk.create_prep_partition('8')
334 self.disk.map_partitions()
335 assert self.disk.partition_map == {'prep': '/dev/loop0p1'}
336 self.disk.is_mapped = False
337
338 @patch('kiwi.storage.disk.Command.run')
339 def test_device_map_linux_dev_sda(self, mock_command):
340 self.storage_provider.is_loop.return_value = False
341 self.storage_provider.get_device = mock.Mock(
342 return_value='/dev/sda'
343 )
344 self.disk.create_efi_partition('100')
345 self.disk.map_partitions()
346 assert self.disk.partition_map == {'efi': '/dev/sda1'}
347 self.disk.is_mapped = False
348
349 @patch('kiwi.storage.disk.Command.run')
350 def test_device_map_linux_dev_c0d0(self, mock_command):
351 self.storage_provider.is_loop.return_value = False
352 self.storage_provider.get_device = mock.Mock(
353 return_value='/dev/c0d0'
354 )
355 self.disk.create_efi_partition('100')
356 self.disk.map_partitions()
357 assert self.disk.partition_map == {'efi': '/dev/c0d0p1'}
358 self.disk.is_mapped = False
359
360 @patch('kiwi.storage.disk.Command.run')
361 def test_activate_boot_partition_is_boot_partition(self, mock_command):
362 self.disk.create_boot_partition('100')
363 self.disk.create_root_partition('100')
364 self.disk.activate_boot_partition()
365 self.partitioner.set_flag(1, 'f.active')
366
367 @patch('kiwi.storage.disk.Command.run')
368 def test_activate_boot_partition_is_root_partition(self, mock_command):
369 self.disk.create_root_partition('100')
370 self.disk.activate_boot_partition()
371 self.partitioner.set_flag(1, 'f.active')
372
373 @patch('kiwi.storage.disk.Command.run')
374 def test_activate_boot_partition_is_prep_partition(self, mock_command):
375 self.disk.create_prep_partition('8')
376 self.disk.activate_boot_partition()
377 self.partitioner.set_flag(1, 'f.active')
378
379 @patch('kiwi.storage.disk.Command.run')
380 def test_wipe_gpt(self, mock_command):
381 self.disk.wipe()
382 mock_command.assert_called_once_with(
383 ['sgdisk', '--zap-all', '/dev/loop0']
384 )
385
386 @patch('kiwi.storage.disk.Command.run')
387 @patch('kiwi.storage.disk.Temporary.new_file')
388 def test_wipe_dasd(self, mock_temp, mock_command):
389 mock_command.side_effect = Exception
390 self.disk.table_type = 'dasd'
391 mock_temp.return_value = self.tempfile
392
393 m_open = mock_open()
394 with patch('builtins.open', m_open, create=True):
395 self.disk.wipe()
396
397 m_open.return_value.write.assert_called_once_with(
398 'y\n\nw\nq\n'
399 )
400 with self._caplog.at_level(logging.DEBUG):
401 mock_command.assert_called_once_with(
402 ['bash', '-c', 'cat tempfile | fdasd -f /dev/loop0']
403 )
404
405 @patch('kiwi.storage.disk.Command.run')
406 def test_map_partitions_loop_partx(self, mock_command):
407 self.disk.map_partitions()
408 mock_command.assert_called_once_with(
409 ['partx', '--add', '/dev/loop0']
410 )
411 self.disk.is_mapped = False
412
413 @patch('kiwi.storage.disk.Command.run')
414 def test_map_partitions_loop_kpartx(self, mock_command):
415 self.disk.partition_mapper = 'kpartx'
416 self.disk.map_partitions()
417 mock_command.assert_called_once_with(
418 ['kpartx', '-s', '-a', '/dev/loop0']
419 )
420 self.disk.is_mapped = False
421
422 @patch('kiwi.storage.disk.Command.run')
423 def test_map_partitions_other(self, mock_command):
424 self.storage_provider.is_loop.return_value = False
425 self.disk.map_partitions()
426 mock_command.assert_called_once_with(
427 ['partprobe', '/dev/loop0']
428 )
429
430 @patch.object(Disk, 'get_discoverable_partition_ids')
431 @patch('kiwi.storage.disk.Command.run')
432 def test_context_manager_exit_partx_loop_cleanup_failed(
433 self, mock_command, mock_get_discoverable_partition_ids
434 ):
435 mock_command.side_effect = Exception
436 with Disk('gpt', self.storage_provider) as disk:
437 disk.is_mapped = True
438 disk.partition_map = {'root': '/dev/loop0p1'}
439 with self._caplog.at_level(logging.WARNING):
440 mock_command.assert_called_once_with(
441 ['partx', '--delete', '/dev/loop0']
442 )
443
444 @patch.object(Disk, 'get_discoverable_partition_ids')
445 @patch('kiwi.storage.disk.Command.run')
446 def test_context_manager_exit_dm_loop_cleanup_failed(
447 self, mock_command, mock_get_discoverable_partition_ids
448 ):
449 mock_command.side_effect = Exception
450 with Disk('gpt', self.storage_provider) as disk:
451 disk.partition_mapper = 'kpartx'
452 disk.is_mapped = True
453 disk.partition_map = {'root': '/dev/mapper/loop0p1'}
454 with self._caplog.at_level(logging.WARNING):
455 mock_command.assert_called_once_with(
456 ['dmsetup', 'remove', '/dev/mapper/loop0p1']
457 )
458
459 @patch.object(Disk, 'get_discoverable_partition_ids')
460 @patch('kiwi.storage.disk.Command.run')
461 def test_context_manager_exit_partx(
462 self, mock_command, mock_get_discoverable_partition_ids
463 ):
464 with Disk('gpt', self.storage_provider) as disk:
465 disk.is_mapped = True
466 disk.partition_map = {'root': '/dev/loop0p1'}
467 assert mock_command.call_args_list == [
468 call(['partx', '--delete', '/dev/loop0'])
469 ]
470
471 @patch.object(Disk, 'get_discoverable_partition_ids')
472 @patch('kiwi.storage.disk.Command.run')
473 def test_context_manager_exit_kpartx(
474 self, mock_command, mock_get_discoverable_partition_ids
475 ):
476 with Disk('gpt', self.storage_provider) as disk:
477 disk.partition_mapper = 'kpartx'
478 disk.is_mapped = True
479 disk.partition_map = {'root': '/dev/mapper/loop0p1'}
480 assert mock_command.call_args_list == [
481 call(['dmsetup', 'remove', '/dev/mapper/loop0p1']),
482 call(['kpartx', '-d', '/dev/loop0'])
483 ]
484
485 def test_get_public_partition_id_map(self):
486 assert self.disk.get_public_partition_id_map() == {}
487
488 def test_create_hybrid_mbr(self):
489 self.disk.create_hybrid_mbr()
490 self.partitioner.set_hybrid_mbr.assert_called_once_with()
491
492 def test_create_mbr(self):
493 self.disk.create_mbr()
494 self.partitioner.set_mbr.assert_called_once_with()
495
496 def test_set_start_sector(self):
497 self.disk.set_start_sector(4096)
498 self.partitioner.set_start_sector.assert_called_once_with(4096)
499
500 def test_parse_size(self):
501 (size, _) = self.disk._parse_size('100')
502 assert size == '100'
503 (size, clone_size) = self.disk._parse_size('all_free')
504 assert size == 'all_free'
505 assert clone_size == 'all_free'
506 (size, clone_size) = self.disk._parse_size('clone:100:all_free')
507 assert size == '100'
508 assert clone_size == 'all_free'
509
510 @patch('kiwi.storage.disk.Command.run')
511 def test_get_discoverable_partition_ids(self, mock_Command_run):
512 command = Mock()
513 with open('../data/systemd-id128.out') as ids:
514 command.output = ids.read()
515 mock_Command_run.return_value = command
516 assert self.disk.get_discoverable_partition_ids()['root'] == \
517 '4f68bce3e8cd4db196e7fbcaf984b709'
518 mock_Command_run.side_effect = KiwiCommandError('issue')
519 assert self.disk.get_discoverable_partition_ids().get('root') == \
520 '4f68bce3e8cd4db196e7fbcaf984b709'