File: disk_test.py

package info (click to toggle)
kiwi 10.2.36-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 7,664 kB
  • sloc: python: 69,179; sh: 4,228; xml: 3,383; ansic: 391; makefile: 353
file content (518 lines) | stat: -rw-r--r-- 19,198 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
import logging
from unittest.mock import (
    patch, mock_open, call, Mock
)
from pytest import (
    fixture, raises
)

import unittest.mock as mock

from kiwi.storage.disk import ptable_entry_type
from kiwi.storage.disk import Disk
from kiwi.exceptions import (
    KiwiCustomPartitionConflictError,
    KiwiCommandError
)


class TestDisk:
    @fixture(autouse=True)
    def inject_fixtures(self, caplog):
        self._caplog = caplog

    @patch.object(Disk, 'get_discoverable_partition_ids')
    @patch('kiwi.storage.disk.Partitioner.new')
    @patch('kiwi.storage.disk.RuntimeConfig')
    def setup(
        self, mock_RuntimeConfig, mock_partitioner,
        mock_get_discoverable_partition_ids
    ):
        runtime_config = Mock()
        runtime_config.get_mapper_tool.return_value = 'partx'
        mock_RuntimeConfig.return_value = runtime_config
        self.tempfile = mock.Mock()
        self.tempfile.name = 'tempfile'

        self.partitioner = mock.Mock()
        self.partitioner.create = mock.Mock()
        self.partitioner.get_id = mock.Mock(
            return_value=1
        )
        mock_partitioner.return_value = self.partitioner
        self.storage_provider = mock.Mock()
        self.storage_provider.is_loop = mock.Mock(
            return_value=True
        )
        self.storage_provider.get_device = mock.Mock(
            return_value='/dev/loop0'
        )
        self.disk = Disk('gpt', self.storage_provider)

    @patch('kiwi.storage.disk.Partitioner.new')
    @patch('kiwi.storage.disk.RuntimeConfig')
    def setup_method(self, cls, mock_RuntimeConfig, mock_partitioner):
        self.setup()

    @patch('os.path.exists')
    def test_get_device(self, mock_exists):
        mock_exists.return_value = True
        self.disk.partition_map['root'] = '/dev/root-device'
        assert self.disk.get_device()['root'].get_device() == '/dev/root-device'

    def test_is_loop(self):
        self.disk.is_loop()
        self.storage_provider.is_loop.assert_called_once_with()

    def test_create_root_partition(self):
        self.disk.create_root_partition('100', 1)
        assert self.partitioner.create.call_args_list == [
            call(
                name='p.lxrootclone1',
                mbsize='100',
                type_name='t.linux',
                partition_id=None
            ),
            call(
                name='p.lxroot',
                mbsize='100',
                type_name='t.linux',
                partition_id=None
            )
        ]

    def test_create_root_which_is_also_boot_partition(self):
        self.disk.create_root_partition('200')
        self.partitioner.create.assert_called_once_with(
            name='p.lxroot',
            mbsize='200',
            type_name='t.linux',
            partition_id=None
        )
        assert self.disk.public_partition_id_map['kiwi_RootPart'] == 1
        assert self.disk.public_partition_id_map['kiwi_BootPart'] == 1

    def test_create_root_which_is_also_read_write_partition(self):
        self.disk.public_partition_id_map['kiwi_ROPart'] = 1
        self.disk.create_root_partition('200')
        self.partitioner.create.assert_called_once_with(
            name='p.lxroot',
            mbsize='200',
            type_name='t.linux',
            partition_id=None
        )
        assert self.disk.public_partition_id_map['kiwi_RootPart'] == 1
        assert self.disk.public_partition_id_map['kiwi_RWPart'] == 1

    def test_create_root_lvm_partition(self):
        self.disk.create_root_lvm_partition('100', 1)
        assert self.partitioner.create.call_args_list == [
            call(
                name='p.lxrootclone1',
                mbsize='100',
                type_name='t.lvm',
                partition_id=None
            ),
            call(
                name='p.lxlvm',
                mbsize='100',
                type_name='t.lvm',
                partition_id=None
            )
        ]
        assert self.disk.public_partition_id_map['kiwi_rootPartClone1'] == 1
        assert self.disk.public_partition_id_map['kiwi_RootPart'] == 1

    def test_create_root_raid_partition(self):
        self.disk.create_root_raid_partition('100', 1)
        assert self.partitioner.create.call_args_list == [
            call(
                name='p.lxrootclone1',
                mbsize='100',
                type_name='t.raid',
                partition_id=None
            ),
            call(
                name='p.lxraid',
                mbsize='100',
                type_name='t.raid',
                partition_id=None
            )
        ]
        assert self.disk.public_partition_id_map['kiwi_rootPartClone1'] == 1
        assert self.disk.public_partition_id_map['kiwi_RootPart'] == 1
        assert self.disk.public_partition_id_map['kiwi_RaidPart'] == 1

    def test_create_root_readonly_partition(self):
        self.disk.create_root_readonly_partition('100', 1)
        assert self.partitioner.create.call_args_list == [
            call(
                name='p.lxrootclone1',
                mbsize='100',
                type_name='t.linux',
                partition_id=None
            ),
            call(
                name='p.lxreadonly',
                mbsize='100',
                type_name='t.linux',
                partition_id=None
            )
        ]
        assert self.disk.public_partition_id_map['kiwi_rootPartClone1'] == 1
        assert self.disk.public_partition_id_map['kiwi_ROPart'] == 1

    def test_create_boot_partition(self):
        self.disk.create_boot_partition('100', 1)
        assert self.partitioner.create.call_args_list == [
            call(
                name='p.lxbootclone1',
                mbsize='100',
                type_name='t.linux',
                partition_id=None
            ),
            call(
                name='p.lxboot',
                mbsize='100',
                type_name='t.linux',
                partition_id=None
            )
        ]
        assert self.disk.public_partition_id_map['kiwi_bootPartClone1'] == 1
        assert self.disk.public_partition_id_map['kiwi_BootPart'] == 1

    def test_create_efi_csm_partition(self):
        self.disk.create_efi_csm_partition('100')
        self.partitioner.create.assert_called_once_with(
            name='p.legacy', mbsize='100', type_name='t.csm', partition_id=None
        )
        assert self.disk.public_partition_id_map['kiwi_BiosGrub'] == 1

    def test_create_efi_partition(self):
        self.disk.create_efi_partition('100')
        self.partitioner.create.assert_called_once_with(
            name='p.UEFI', mbsize='100', type_name='t.efi', partition_id=None
        )
        assert self.disk.public_partition_id_map['kiwi_EfiPart'] == 1

    def test_create_spare_partition(self):
        self.disk.create_spare_partition('42')
        self.partitioner.create.assert_called_once_with(
            name='p.spare', mbsize='42', type_name='t.linux', partition_id=None
        )
        assert self.disk.public_partition_id_map['kiwi_SparePart'] == 1

    def test_create_swap_partition(self):
        self.disk.create_swap_partition('42')
        self.partitioner.create.assert_called_once_with(
            name='p.swap', mbsize='42', type_name='t.swap', partition_id=None
        )
        assert self.disk.public_partition_id_map['kiwi_SwapPart'] == 1

    @patch('kiwi.storage.disk.Command.run')
    def test_create_prep_partition(self, mock_command):
        self.disk.create_prep_partition('8')
        self.partitioner.create.assert_called_once_with(
            name='p.prep', mbsize='8', type_name='t.prep', partition_id=None
        )
        assert self.disk.public_partition_id_map['kiwi_PrepPart'] == 1

    @patch('kiwi.storage.disk.Command.run')
    def test_create_custom_partitions(self, mock_command):
        table_entries = {
            'var': ptable_entry_type(
                mbsize='100',
                clone=2,
                partition_name='p.lxvar',
                partition_type='t.linux',
                partition_id=None,
                mountpoint='/var',
                filesystem='ext3',
                label='var'
            )
        }
        self.disk.create_custom_partitions(table_entries)
        assert self.partitioner.create.call_args_list == [
            call(
                name='p.lxvarclone1',
                mbsize='100',
                type_name='t.linux',
                partition_id=None
            ),
            call(
                name='p.lxvarclone2',
                mbsize='100',
                type_name='t.linux',
                partition_id=None
            ),
            call(
                name='p.lxvar',
                mbsize='100',
                type_name='t.linux',
                partition_id=None
            )
        ]
        assert self.disk.public_partition_id_map['kiwi_varPartClone1'] == 1
        assert self.disk.public_partition_id_map['kiwi_varPartClone2'] == 1
        assert self.disk.public_partition_id_map['kiwi_VarPart'] == 1

    @patch('kiwi.storage.disk.Command.run')
    def test_create_custom_partitions_with_custom_ID(self, mock_command):
        table_entries = {
            'var': ptable_entry_type(
                mbsize='100',
                clone=2,
                partition_name='p.lxvar',
                partition_type='t.linux',
                partition_id=42,
                mountpoint='/var',
                filesystem='ext3',
                label='var'
            )
        }
        self.disk.create_custom_partitions(table_entries)
        assert self.partitioner.create.call_args_list == [
            call(
                name='p.lxvarclone43',
                mbsize='100',
                type_name='t.linux',
                partition_id=43
            ),
            call(
                name='p.lxvarclone44',
                mbsize='100',
                type_name='t.linux',
                partition_id=44
            ),
            call(
                name='p.lxvar',
                mbsize='100',
                type_name='t.linux',
                partition_id=42
            )
        ]
        assert self.disk.public_partition_id_map['kiwi_varPartClone43'] == 1
        assert self.disk.public_partition_id_map['kiwi_varPartClone44'] == 1
        assert self.disk.public_partition_id_map['kiwi_VarPart'] == 1

    def test_create_custom_partitions_reserved_name(self):
        table_entries = {
            'root': ptable_entry_type(
                mbsize='100',
                clone=0,
                partition_name='p.lxroot',
                partition_type='t.linux',
                partition_id=None,
                mountpoint='/',
                filesystem='ext3',
                label='root'
            )
        }
        with raises(KiwiCustomPartitionConflictError):
            self.disk.create_custom_partitions(table_entries)

    @patch('kiwi.storage.disk.Command.run')
    def test_device_map_efi_partition_partx(self, mock_command):
        self.disk.create_efi_partition('100')
        self.disk.map_partitions()
        assert self.disk.partition_map == {'efi': '/dev/loop0p1'}
        self.disk.is_mapped = False

    @patch('kiwi.storage.disk.Command.run')
    def test_device_map_efi_partition_kpartx(self, mock_command):
        self.disk.partition_mapper = 'kpartx'
        self.disk.create_efi_partition('100')
        self.disk.map_partitions()
        assert self.disk.partition_map == {'efi': '/dev/mapper/loop0p1'}
        self.disk.is_mapped = False

    @patch('kiwi.storage.disk.Command.run')
    def test_device_map_prep_partition(self, mock_command):
        self.disk.create_prep_partition('8')
        self.disk.map_partitions()
        assert self.disk.partition_map == {'prep': '/dev/loop0p1'}
        self.disk.is_mapped = False

    @patch('kiwi.storage.disk.Command.run')
    def test_device_map_linux_dev_sda(self, mock_command):
        self.storage_provider.is_loop.return_value = False
        self.storage_provider.get_device = mock.Mock(
            return_value='/dev/sda'
        )
        self.disk.create_efi_partition('100')
        self.disk.map_partitions()
        assert self.disk.partition_map == {'efi': '/dev/sda1'}
        self.disk.is_mapped = False

    @patch('kiwi.storage.disk.Command.run')
    def test_device_map_linux_dev_c0d0(self, mock_command):
        self.storage_provider.is_loop.return_value = False
        self.storage_provider.get_device = mock.Mock(
            return_value='/dev/c0d0'
        )
        self.disk.create_efi_partition('100')
        self.disk.map_partitions()
        assert self.disk.partition_map == {'efi': '/dev/c0d0p1'}
        self.disk.is_mapped = False

    @patch('kiwi.storage.disk.Command.run')
    def test_activate_boot_partition_is_boot_partition(self, mock_command):
        self.disk.create_boot_partition('100')
        self.disk.create_root_partition('100')
        self.disk.activate_boot_partition()
        self.partitioner.set_flag(1, 'f.active')

    @patch('kiwi.storage.disk.Command.run')
    def test_activate_boot_partition_is_root_partition(self, mock_command):
        self.disk.create_root_partition('100')
        self.disk.activate_boot_partition()
        self.partitioner.set_flag(1, 'f.active')

    @patch('kiwi.storage.disk.Command.run')
    def test_activate_boot_partition_is_prep_partition(self, mock_command):
        self.disk.create_prep_partition('8')
        self.disk.activate_boot_partition()
        self.partitioner.set_flag(1, 'f.active')

    @patch('kiwi.storage.disk.Command.run')
    def test_wipe_gpt(self, mock_command):
        self.disk.wipe()
        mock_command.assert_called_once_with(
            ['sgdisk', '--zap-all', '/dev/loop0']
        )

    @patch('kiwi.storage.disk.Command.run')
    @patch('kiwi.storage.disk.Temporary.new_file')
    def test_wipe_dasd(self, mock_temp, mock_command):
        mock_command.side_effect = Exception
        self.disk.table_type = 'dasd'
        mock_temp.return_value = self.tempfile

        m_open = mock_open()
        with patch('builtins.open', m_open, create=True):
            self.disk.wipe()

        m_open.return_value.write.assert_called_once_with(
            'y\n\nw\nq\n'
        )
        with self._caplog.at_level(logging.DEBUG):
            mock_command.assert_called_once_with(
                ['bash', '-c', 'cat tempfile | fdasd -f /dev/loop0']
            )

    @patch('kiwi.storage.disk.Command.run')
    def test_map_partitions_loop_partx(self, mock_command):
        self.disk.map_partitions()
        mock_command.assert_called_once_with(
            ['partx', '--add', '/dev/loop0']
        )
        self.disk.is_mapped = False

    @patch('kiwi.storage.disk.Command.run')
    def test_map_partitions_loop_kpartx(self, mock_command):
        self.disk.partition_mapper = 'kpartx'
        self.disk.map_partitions()
        mock_command.assert_called_once_with(
            ['kpartx', '-s', '-a', '/dev/loop0']
        )
        self.disk.is_mapped = False

    @patch('kiwi.storage.disk.Command.run')
    def test_map_partitions_other(self, mock_command):
        self.storage_provider.is_loop.return_value = False
        self.disk.map_partitions()
        mock_command.assert_called_once_with(
            ['partprobe', '/dev/loop0']
        )

    @patch.object(Disk, 'get_discoverable_partition_ids')
    @patch('kiwi.storage.disk.Command.run')
    def test_context_manager_exit_partx_loop_cleanup_failed(
        self, mock_command, mock_get_discoverable_partition_ids
    ):
        mock_command.side_effect = Exception
        with Disk('gpt', self.storage_provider) as disk:
            disk.is_mapped = True
            disk.partition_map = {'root': '/dev/loop0p1'}
        with self._caplog.at_level(logging.WARNING):
            mock_command.assert_called_once_with(
                ['partx', '--delete', '/dev/loop0']
            )

    @patch.object(Disk, 'get_discoverable_partition_ids')
    @patch('kiwi.storage.disk.Command.run')
    def test_context_manager_exit_dm_loop_cleanup_failed(
        self, mock_command, mock_get_discoverable_partition_ids
    ):
        mock_command.side_effect = Exception
        with Disk('gpt', self.storage_provider) as disk:
            disk.partition_mapper = 'kpartx'
            disk.is_mapped = True
            disk.partition_map = {'root': '/dev/mapper/loop0p1'}
        with self._caplog.at_level(logging.WARNING):
            mock_command.assert_called_once_with(
                ['dmsetup', 'remove', '/dev/mapper/loop0p1']
            )

    @patch.object(Disk, 'get_discoverable_partition_ids')
    @patch('kiwi.storage.disk.Command.run')
    def test_context_manager_exit_partx(
        self, mock_command, mock_get_discoverable_partition_ids
    ):
        with Disk('gpt', self.storage_provider) as disk:
            disk.is_mapped = True
            disk.partition_map = {'root': '/dev/loop0p1'}
        assert mock_command.call_args_list == [
            call(['partx', '--delete', '/dev/loop0'])
        ]

    @patch.object(Disk, 'get_discoverable_partition_ids')
    @patch('kiwi.storage.disk.Command.run')
    def test_context_manager_exit_kpartx(
        self, mock_command, mock_get_discoverable_partition_ids
    ):
        with Disk('gpt', self.storage_provider) as disk:
            disk.partition_mapper = 'kpartx'
            disk.is_mapped = True
            disk.partition_map = {'root': '/dev/mapper/loop0p1'}
        assert mock_command.call_args_list == [
            call(['dmsetup', 'remove', '/dev/mapper/loop0p1']),
            call(['kpartx', '-d', '/dev/loop0'])
        ]

    def test_get_public_partition_id_map(self):
        assert self.disk.get_public_partition_id_map() == {}

    def test_create_hybrid_mbr(self):
        self.disk.create_hybrid_mbr()
        self.partitioner.set_hybrid_mbr.assert_called_once_with()

    def test_create_mbr(self):
        self.disk.create_mbr()
        self.partitioner.set_mbr.assert_called_once_with()

    def test_set_start_sector(self):
        self.disk.set_start_sector(4096)
        self.partitioner.set_start_sector.assert_called_once_with(4096)

    def test_parse_size(self):
        (size, _) = self.disk._parse_size('100')
        assert size == '100'
        (size, clone_size) = self.disk._parse_size('all_free')
        assert size == 'all_free'
        assert clone_size == 'all_free'
        (size, clone_size) = self.disk._parse_size('clone:100:all_free')
        assert size == '100'
        assert clone_size == 'all_free'

    @patch('kiwi.storage.disk.Command.run')
    def test_get_discoverable_partition_ids(self, mock_Command_run):
        command = Mock()
        with open('../data/systemd-id128.out') as ids:
            command.output = ids.read()
        mock_Command_run.return_value = command
        assert self.disk.get_discoverable_partition_ids()['root'] == \
            '4f68bce3e8cd4db196e7fbcaf984b709'
        mock_Command_run.side_effect = KiwiCommandError('issue')
        assert self.disk.get_discoverable_partition_ids().get('root') == \
            '4f68bce3e8cd4db196e7fbcaf984b709'