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