Skip to content

Commit 6ea817c

Browse files
committed
tests: Test to make sure partition and disk names match libparted
In the past there have been problems with pyparted not including all of the partition flags from libparted due to its use of enums in the _LAST macros. These were not caught by the tests because they skipped flags that had no attribute (a valid case when building against an older parted). This adds tests that include all known parted flags, and uses libparted to decide which ones must be included. The test will return errors for all flags that libparted reports that are not attributes of _ped If libparted reports a flag that pyparted does not support it is not a failure and will be reported as a skipped test: skipped 'Unknown flag: linux-home'
1 parent ee4bf04 commit 6ea817c

File tree

1 file changed

+103
-43
lines changed

1 file changed

+103
-43
lines changed

tests/test__ped_ped.py

Lines changed: 103 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,87 @@
1212

1313
from tests.baseclass import BuildList, RequiresDevice, RequiresFileSystem
1414

15+
# Mapping of flags to names
16+
disk_flags = ["DISK_CYLINDER_ALIGNMENT", "DISK_GPT_PMBR_BOOT"]
17+
disk_names = ["cylinder_alignment", "pmbr_boot"]
18+
19+
partition_flags = [
20+
"PARTITION_BOOT",
21+
"PARTITION_ROOT",
22+
"PARTITION_SWAP",
23+
"PARTITION_HIDDEN",
24+
"PARTITION_RAID",
25+
"PARTITION_LVM",
26+
"PARTITION_LBA",
27+
"PARTITION_HPSERVICE",
28+
"PARTITION_PALO",
29+
"PARTITION_PREP",
30+
"PARTITION_MSFT_RESERVED",
31+
"PARTITION_BIOS_GRUB",
32+
"PARTITION_APPLE_TV_RECOVERY",
33+
"PARTITION_DIAG",
34+
"PARTITION_LEGACY_BOOT",
35+
"PARTITION_MSFT_DATA",
36+
"PARTITION_IRST",
37+
"PARTITION_ESP",
38+
"PARTITION_CHROMEOS_KERNEL",
39+
"PARTITION_BLS_BOOT",
40+
"PARTITION_LINUX_HOME",
41+
"PARTITION_NO_AUTOMOUNT",
42+
]
43+
partition_names = [
44+
"boot",
45+
"root",
46+
"swap",
47+
"hidden",
48+
"raid",
49+
"lvm",
50+
"lba",
51+
"hp-service",
52+
"palo",
53+
"prep",
54+
"msftres",
55+
"bios_grub",
56+
"atvrecv",
57+
"diag",
58+
"legacy_boot",
59+
"msftdata",
60+
"irst",
61+
"esp",
62+
"chromeos_kernel",
63+
"bls_boot",
64+
"linux-home",
65+
"no_automount",
66+
]
67+
1568
# One class per method, multiple tests per class. For these simple methods,
1669
# that seems like good organization. More complicated methods may require
1770
# multiple classes and their own test suite.
71+
72+
73+
# This checks all the partition flags that libparted reports to make sure they
74+
# have been compiled into pyparted as attributes
75+
class PartitionFlagKnownNamesTestCase(unittest.TestCase):
76+
def runTest(self):
77+
all_flags = dict(zip(partition_names, partition_flags))
78+
f = 0
79+
while True:
80+
f = _ped.partition_flag_next(f)
81+
if f == 0:
82+
break
83+
n = _ped.partition_flag_get_name(f)
84+
with self.subTest(n=n):
85+
if n not in all_flags:
86+
self.skipTest(f"Unknown flag: {n}")
87+
attr = all_flags[n]
88+
89+
# Confirm that the build included this flag attr
90+
self.assertTrue(hasattr(_ped, attr), attr)
91+
92+
1893
class PartitionFlagGetNameTestCase(unittest.TestCase):
1994
def runTest(self):
20-
for f in [
21-
"PARTITION_BOOT",
22-
"PARTITION_ROOT",
23-
"PARTITION_SWAP",
24-
"PARTITION_HIDDEN",
25-
"PARTITION_RAID",
26-
"PARTITION_LVM",
27-
"PARTITION_LBA",
28-
"PARTITION_HPSERVICE",
29-
"PARTITION_PALO",
30-
"PARTITION_PREP",
31-
"PARTITION_MSFT_RESERVED",
32-
"PARTITION_APPLE_TV_RECOVERY",
33-
"PARTITION_BIOS_GRUB",
34-
"PARTITION_DIAG",
35-
"PARTITION_MSFT_DATA",
36-
"PARTITION_IRST",
37-
"PARTITION_ESP",
38-
"PARTITION_NONFS",
39-
"PARTITION_CHROMEOS_KERNEL",
40-
"PARTITION_CHROMEOS_KERNEL",
41-
]:
95+
for f in partition_flags:
4296
if not hasattr(_ped, f):
4397
continue
4498
attr = getattr(_ped, f)
@@ -54,24 +108,7 @@ def runTest(self):
54108

55109
class PartitionFlagGetByNameTestCase(unittest.TestCase):
56110
def runTest(self):
57-
for f in [
58-
"boot",
59-
"root",
60-
"swap",
61-
"hidden",
62-
"raid",
63-
"lvm",
64-
"lba",
65-
"hp-service",
66-
"palo",
67-
"prep",
68-
"msftres",
69-
"bios_grub",
70-
"msftdata",
71-
"irst",
72-
"esp",
73-
"nonfs",
74-
]:
111+
for f in partition_names:
75112
self.assertNotEqual(
76113
_ped.partition_flag_get_by_name(f), "", "Could not get flag %s" % f
77114
)
@@ -97,11 +134,34 @@ def runTest(self):
97134
self.assertEqual(type(flag).__name__, "int")
98135

99136

137+
# This checks all the disk flags that libparted reports to make sure they
138+
# have been compiled into pyparted as attributes
139+
class DiskFlagKnownNamesTestCase(unittest.TestCase):
140+
def runTest(self):
141+
all_flags = dict(zip(disk_names, disk_flags))
142+
f = 0
143+
while True:
144+
f = _ped.disk_flag_next(f)
145+
if f == 0:
146+
break
147+
n = _ped.disk_flag_get_name(f)
148+
with self.subTest(n=n):
149+
if n not in all_flags:
150+
self.skipTest(f"Unknown flag: {n}")
151+
attr = all_flags[n]
152+
153+
# Confirm that the build included this flag attr
154+
self.assertTrue(hasattr(_ped, attr), attr)
155+
156+
100157
class DiskFlagGetNameTestCase(unittest.TestCase):
101158
def runTest(self):
102-
for f in [_ped.DISK_CYLINDER_ALIGNMENT]:
159+
for f in disk_flags:
160+
if not hasattr(_ped, f):
161+
continue
162+
attr = getattr(_ped, f)
103163
self.assertNotEqual(
104-
_ped.disk_flag_get_name(f), "", "Could not get name for flag %s" % f
164+
_ped.disk_flag_get_name(attr), "", "Could not get name for flag %s" % f
105165
)
106166

107167
self.assertRaises(ValueError, _ped.disk_flag_get_name, -1)
@@ -110,7 +170,7 @@ def runTest(self):
110170

111171
class DiskFlagGetByNameTestCase(unittest.TestCase):
112172
def runTest(self):
113-
for f in ["cylinder_alignment"]:
173+
for f in disk_names:
114174
self.assertNotEqual(
115175
_ped.disk_flag_get_by_name(f), 0, "Could not get flag %s" % f
116176
)

0 commit comments

Comments
 (0)