File: test__ped_device.py

package info (click to toggle)
pyparted 3.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 952 kB
  • sloc: ansic: 7,453; python: 4,579; makefile: 91; sh: 4
file content (587 lines) | stat: -rwxr-xr-x 17,763 bytes parent folder | download
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
#
# Copyright The pyparted Project Authors
# SPDX-License-Identifier: GPL-2.0-or-later
#

import _ped
import unittest

from tests.baseclass import RequiresDevice

# One class per method, multiple tests per class.  For these simple methods,
# that seems like good organization.  More complicated methods may require
# multiple classes and their own test suite.
class DeviceNewTestCase(unittest.TestCase):
    def runTest(self):
        # You're not allowed to create a new Device object by hand.
        self.assertRaises(TypeError, _ped.Device)


class DeviceGetSetTestCase(RequiresDevice):
    def runTest(self):
        # All attributes should be readable, but none should be writeable.
        for attr in [
            "model",
            "path",
            "type",
            "sector_size",
            "phys_sector_size",
            "length",
            "open_count",
            "read_only",
            "external_mode",
            "dirty",
            "boot_dirty",
            "host",
            "did",
        ]:
            self.assertNotEqual(getattr(self._device, attr), None)
            self.assertRaises(AttributeError, setattr, self._device, attr, 47)


class DeviceIsBusyTestCase(RequiresDevice):
    def runTest(self):
        # Devices aren't busy until they're mounted.
        self.assertFalse(self._device.is_busy())


# TODO:  need to figure out how to make a loopback device look mounted to
# libparted
#        self.mkfs()
#        self.doMount()
#        self.assertTrue(self._device.is_busy())


class DeviceOpenTestCase(RequiresDevice):
    def runTest(self):
        self.assertTrue(self._device.open())
        self.assertEqual(self._device.open_count, 1)
        self._device.close()

        # Not allowed to open a device that's already been opened for external
        # access, so test that now.
        self._device.begin_external_access()
        self.assertRaises(_ped.IOException, self._device.open)
        self._device.end_external_access()
        self.assertTrue(self._device.open())

        # You're allowed to open a device multiple times.  It's already been
        # opened once above.  Try to open it again and make sure the count is
        # is right.
        self.assertTrue(self._device.open())
        self.assertEqual(self._device.open_count, 2)
        self._device.close()
        self._device.close()


class DeviceCloseTestCase(RequiresDevice):
    def runTest(self):
        self._device.open()
        self.assertTrue(self._device.close())
        self.assertEqual(self._device.open_count, 0)

        # Not allowed to close a device that's already been opened for external
        # access, so test that now.
        self._device.open()
        self._device.begin_external_access()
        self.assertRaises(_ped.IOException, self._device.close)
        self._device.end_external_access()
        self.assertTrue(self._device.close())

        # Test opening a device multiple times and then closing it too many.
        self._device.open()
        self._device.open()
        self.assertEqual(self._device.open_count, 2)
        self._device.close()
        self.assertEqual(self._device.open_count, 1)
        self._device.close()
        self.assertEqual(self._device.open_count, 0)
        self.assertRaises(_ped.IOException, self._device.close)


@unittest.skip("Unimplemented test case.")
class DeviceDestroyTestCase(RequiresDevice):
    def runTest(self):
        # XXX: still broken, need to fix destroy function in pydevice.c
        # self.assertEqual(self._device.destroy(), None)
        self.fail("Unimplemented test case.")


class DeviceCacheRemoveTestCase(RequiresDevice):
    def runTest(self):
        self.assertEqual(self._device.cache_remove(), None)


class DeviceBeginExternalAccessTestCase(RequiresDevice):
    def runTest(self):
        # First test external access on a device that's not open.
        self.assertEqual(self._device.external_mode, 0)
        self.assertTrue(self._device.begin_external_access())
        self.assertEqual(self._device.external_mode, 1)
        self.assertEqual(self._device.open_count, 0)

        # Now stop external access, open the device, and re-test.
        self._device.end_external_access()
        self._device.open()
        self.assertEqual(self._device.open_count, 1)
        self.assertTrue(self._device.begin_external_access())
        self.assertEqual(self._device.open_count, 1)
        self._device.end_external_access()
        self._device.close()


class DeviceEndExternalAccessTestCase(RequiresDevice):
    def runTest(self):
        # Attempt to end external access on a device that never had it begun.
        self.assertRaises(_ped.IOException, self._device.end_external_access)

        # Now test external access on a device that's not open.
        self._device.begin_external_access()
        self.assertEqual(self._device.external_mode, 1)
        self.assertEqual(self._device.open_count, 0)
        self.assertTrue(self._device.end_external_access())
        self.assertEqual(self._device.external_mode, 0)
        self.assertEqual(self._device.open_count, 0)

        # Now on a device that's open.
        self._device.open()
        self._device.begin_external_access()
        self.assertEqual(self._device.external_mode, 1)
        self.assertEqual(self._device.open_count, 1)
        self.assertTrue(self._device.end_external_access())
        self.assertEqual(self._device.external_mode, 0)
        self.assertEqual(self._device.open_count, 1)
        self._device.close()


@unittest.skip("Unimplemented test case.")
class DeviceReadTestCase(unittest.TestCase):
    def runTest(self):
        # TODO
        self.fail("Unimplemented test case.")


@unittest.skip("Unimplemented test case.")
class DeviceWriteTestCase(unittest.TestCase):
    def runTest(self):
        # TODO
        self.fail("Unimplemented test case.")


class DeviceSyncTestCase(RequiresDevice):
    def runTest(self):
        # Can't sync a device that's not open or is in external mode.
        self.assertRaises(_ped.IOException, self._device.sync)

        self._device.open()
        self._device.begin_external_access()
        self.assertRaises(_ped.IOException, self._device.sync)

        # But this call should work.
        self._device.end_external_access()
        self.assertTrue(self._device.sync())
        self._device.close()


class DeviceSyncFastTestCase(RequiresDevice):
    def runTest(self):
        # Can't sync a device that's not open or is in external mode.
        self.assertRaises(_ped.IOException, self._device.sync_fast)

        self._device.open()
        self._device.begin_external_access()
        self.assertRaises(_ped.IOException, self._device.sync_fast)

        # But this call should work.
        self._device.end_external_access()
        self.assertTrue(self._device.sync_fast())
        self._device.close()


class DeviceCheckTestCase(RequiresDevice):
    def runTest(self):
        self._device.open()
        self.assertEqual(self._device.check(0, 20), 20)
        self._device.close()


class DeviceGetConstraintTestCase(RequiresDevice):
    def runTest(self):
        # XXX: This test case would be a lot more useful testing on real
        # hardware with unusual sector sizes.
        self.assertIsInstance(self._device.get_constraint(), _ped.Constraint)


class DeviceGetMinimalAlignedConstraintTestCase(RequiresDevice):
    def runTest(self):
        # XXX: This test case would be a lot more useful testing on real
        # hardware with unusual sector sizes.
        constraint = self._device.get_minimal_aligned_constraint()
        self.assertIsInstance(constraint, _ped.Constraint)
        self.assertEqual(constraint.start_align.offset, 0)
        self.assertEqual(constraint.start_align.grain_size, 1)
        self.assertEqual(constraint.end_align.offset, 0)
        self.assertEqual(constraint.end_align.grain_size, 1)


class DeviceGetOptimalAlignedConstraintTestCase(RequiresDevice):
    def runTest(self):
        # XXX: This test case would be a lot more useful testing on real
        # hardware with unusual sector sizes.
        constraint = self._device.get_minimal_aligned_constraint()
        self.assertIsInstance(constraint, _ped.Constraint)
        self.assertEqual(constraint.start_align.offset, 0)
        self.assertEqual(constraint.start_align.grain_size, 1)
        self.assertEqual(constraint.end_align.offset, 0)
        self.assertEqual(constraint.end_align.grain_size, 1)


class DeviceGetMinimumAlignmentTestCase(RequiresDevice):
    def runTest(self):
        # XXX: This test case would be a lot more useful testing on real
        # hardware with unusual sector sizes.
        alignment = self._device.get_minimum_alignment()
        self.assertIsInstance(alignment, _ped.Alignment)
        self.assertEqual(alignment.grain_size, 1)
        self.assertEqual(alignment.offset, 0)


class DeviceGetOptimumAlignmentTestCase(RequiresDevice):
    def runTest(self):
        # XXX: This test case would be a lot more useful testing on real
        # hardware with unusual sector sizes.
        alignment = self._device.get_optimum_alignment()
        self.assertIsInstance(alignment, _ped.Alignment)
        self.assertEqual(alignment.grain_size, 2048)
        self.assertEqual(alignment.offset, 0)


class UnitFormatCustomByteTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        pr = "%f" % (47.0 / self._device.unit_get_size(_ped.UNIT_PERCENT),)
        self.pairs = [
            (
                _ped.UNIT_SECTOR,
                "0s",
            ),
            (
                _ped.UNIT_BYTE,
                "47B",
            ),
            (
                _ped.UNIT_KILOBYTE,
                "0.05kB",
            ),
            (
                _ped.UNIT_MEGABYTE,
                "0.00MB",
            ),
            (
                _ped.UNIT_GIGABYTE,
                "0.00GB",
            ),
            (
                _ped.UNIT_TERABYTE,
                "0.00TB",
            ),
            (
                _ped.UNIT_COMPACT,
                "47.0B",
            ),
            (
                _ped.UNIT_CYLINDER,
                "0cyl",
            ),
            (
                _ped.UNIT_CHS,
                "0,0,0",
            ),
            (
                _ped.UNIT_PERCENT,
                pr[:4] + "%",
            ),
            (
                _ped.UNIT_KIBIBYTE,
                "0.05kiB",
            ),
            (
                _ped.UNIT_MEBIBYTE,
                "0.00MiB",
            ),
            (
                _ped.UNIT_GIBIBYTE,
                "0.00GiB",
            ),
            (
                _ped.UNIT_TEBIBYTE,
                "0.00TiB",
            ),
        ]

    def runTest(self):
        for (
            unit,
            expected,
        ) in self.pairs:
            self.assertEqual(self._device.unit_format_custom_byte(47, unit), expected)


class UnitFormatByteTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        pr = "%f" % (47.0 / self._device.unit_get_size(_ped.UNIT_PERCENT),)
        self._initialDefault = _ped.unit_get_default()
        self.pairs = [
            (
                _ped.UNIT_SECTOR,
                "0s",
            ),
            (
                _ped.UNIT_BYTE,
                "47B",
            ),
            (
                _ped.UNIT_KILOBYTE,
                "0.05kB",
            ),
            (
                _ped.UNIT_MEGABYTE,
                "0.00MB",
            ),
            (
                _ped.UNIT_GIGABYTE,
                "0.00GB",
            ),
            (
                _ped.UNIT_TERABYTE,
                "0.00TB",
            ),
            (
                _ped.UNIT_COMPACT,
                "47.0B",
            ),
            (
                _ped.UNIT_CYLINDER,
                "0cyl",
            ),
            (
                _ped.UNIT_CHS,
                "0,0,0",
            ),
            (
                _ped.UNIT_PERCENT,
                pr[:4] + "%",
            ),
            (
                _ped.UNIT_KIBIBYTE,
                "0.05kiB",
            ),
            (
                _ped.UNIT_MEBIBYTE,
                "0.00MiB",
            ),
            (
                _ped.UNIT_GIBIBYTE,
                "0.00GiB",
            ),
            (
                _ped.UNIT_TEBIBYTE,
                "0.00TiB",
            ),
        ]

    def runTest(self):
        for (unit, expected) in self.pairs:
            _ped.unit_set_default(unit)
            result = self._device.unit_format_byte(47)
            self.assertEqual(result, expected)

    def tearDown(self):
        _ped.unit_set_default(self._initialDefault)


class UnitFormatCustomTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        sector_size = self._device.sector_size
        size = self._device.unit_get_size(_ped.UNIT_PERCENT)
        pr = "%f" % ((47.0 * sector_size) / size,)
        self.pairs = [
            (
                _ped.UNIT_SECTOR,
                "47s",
            ),
            (
                _ped.UNIT_BYTE,
                "24064B",
            ),
            (
                _ped.UNIT_KILOBYTE,
                "24.1kB",
            ),
            (
                _ped.UNIT_MEGABYTE,
                "0.02MB",
            ),
            (
                _ped.UNIT_GIGABYTE,
                "0.00GB",
            ),
            (
                _ped.UNIT_TERABYTE,
                "0.00TB",
            ),
            (
                _ped.UNIT_COMPACT,
                "24.1kB",
            ),
            (
                _ped.UNIT_CYLINDER,
                "0cyl",
            ),
            (
                _ped.UNIT_CHS,
                "0,1,15",
            ),
            (
                _ped.UNIT_PERCENT,
                pr[:4] + "%",
            ),
            (
                _ped.UNIT_KIBIBYTE,
                "23.5kiB",
            ),
            (
                _ped.UNIT_MEBIBYTE,
                "0.02MiB",
            ),
            (
                _ped.UNIT_GIBIBYTE,
                "0.00GiB",
            ),
            (
                _ped.UNIT_TEBIBYTE,
                "0.00TiB",
            ),
        ]

    def runTest(self):
        for (unit, expected) in self.pairs:
            result = self._device.unit_format_custom(47, unit)
            self.assertEqual(result, expected)


class UnitFormatTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        sector_size = self._device.sector_size
        size = self._device.unit_get_size(_ped.UNIT_PERCENT)
        pr = "%f" % ((47.0 * sector_size) / size,)
        self._initialDefault = _ped.unit_get_default()
        self.pairs = [
            (
                _ped.UNIT_SECTOR,
                "47s",
            ),
            (
                _ped.UNIT_BYTE,
                "24064B",
            ),
            (
                _ped.UNIT_KILOBYTE,
                "24.1kB",
            ),
            (
                _ped.UNIT_MEGABYTE,
                "0.02MB",
            ),
            (
                _ped.UNIT_GIGABYTE,
                "0.00GB",
            ),
            (
                _ped.UNIT_TERABYTE,
                "0.00TB",
            ),
            (
                _ped.UNIT_COMPACT,
                "24.1kB",
            ),
            (
                _ped.UNIT_CYLINDER,
                "0cyl",
            ),
            (
                _ped.UNIT_CHS,
                "0,1,15",
            ),
            (
                _ped.UNIT_PERCENT,
                pr[:4] + "%",
            ),
            (
                _ped.UNIT_KIBIBYTE,
                "23.5kiB",
            ),
            (
                _ped.UNIT_MEBIBYTE,
                "0.02MiB",
            ),
            (
                _ped.UNIT_GIBIBYTE,
                "0.00GiB",
            ),
            (
                _ped.UNIT_TEBIBYTE,
                "0.00TiB",
            ),
        ]

    def runTest(self):
        for (unit, expected) in self.pairs:
            _ped.unit_set_default(unit)
            result = self._device.unit_format(47)
            self.assertEqual(result, expected)

    def tearDown(self):
        _ped.unit_set_default(self._initialDefault)


@unittest.skip("Unimplemented test case.")
class UnitParseTestCase(unittest.TestCase):
    # TODO
    def runTest(self):
        self.fail("Unimplemented test case.")


@unittest.skip("Unimplemented test case.")
class UnitParseCustomTestCase(unittest.TestCase):
    # TODO
    def runTest(self):
        self.fail("Unimplemented test case.")


class DeviceStrTestCase(RequiresDevice):
    def runTest(self):
        expected = (
            "_ped.Device instance --\n  model: %s  path: %s  type: %d\n  sector_size: %d  phys_sector_size: %d\n  length: %d  open_count: %d  read_only: %d\n  external_mode: %d  dirty: %d  boot_dirty: %d\n  host: %d  did: %d\n  hw_geom: %s  bios_geom: %s"
            % (
                self._device.model,
                self._device.path,
                self._device.type,
                self._device.sector_size,
                self._device.phys_sector_size,
                self._device.length,
                self._device.open_count,
                self._device.read_only,
                self._device.external_mode,
                self._device.dirty,
                self._device.boot_dirty,
                self._device.host,
                self._device.did,
                repr(self._device.hw_geom),
                repr(self._device.bios_geom),
            )
        )
        self.assertEqual(str(self._device), expected)