/
fslabeling.py
174 lines (141 loc) · 6.75 KB
/
fslabeling.py
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
#!/usr/bin/python
import abc
from six import add_metaclass
from tests import loopbackedtestcase
from blivet.errors import FSError, FSReadLabelError
from blivet.size import Size
@add_metaclass(abc.ABCMeta)
class LabelingAsRoot(loopbackedtestcase.LoopBackedTestCase):
"""Tests various aspects of labeling a filesystem where there
is no easy way to read the filesystem's label once it has been
set and where the filesystem can not be relabeled.
"""
_fs_class = abc.abstractproperty(
doc="The class of the filesystem being tested on.")
_invalid_label = abc.abstractproperty(
doc="A label which is invalid for this filesystem.")
def __init__(self, methodName='runTest'):
super(LabelingAsRoot, self).__init__(methodName=methodName, deviceSpec=[Size("100 MiB")])
def setUp(self):
an_fs = self._fs_class()
if not an_fs.formattable:
self.skipTest("can not create filesystem %s" % an_fs.name)
if not an_fs.labeling():
self.skipTest("can not label filesystem %s" % an_fs.name)
super(LabelingAsRoot, self).setUp()
def testLabeling(self):
"""A sequence of tests of filesystem labeling.
* create the filesystem when passing an invalid label
* raise an exception when reading the filesystem
* raise an exception when relabeling the filesystem
"""
an_fs = self._fs_class(device=self.loopDevices[0], label=self._invalid_label)
if an_fs._readlabel.availabilityErrors or not an_fs.relabels():
self.skipTest("can not read or write label for filesystem %s" % an_fs.name)
self.assertIsNone(an_fs.create())
with self.assertRaises(FSReadLabelError):
an_fs.readLabel()
an_fs.label = "an fs"
with self.assertRaises(FSError):
an_fs.writeLabel()
def testCreating(self):
"""Create the filesystem when passing a valid label """
an_fs = self._fs_class(device=self.loopDevices[0], label="start")
self.assertIsNone(an_fs.create())
def testCreatingNone(self):
"""Create the filesystem when passing None
(indicates filesystem default)
"""
an_fs = self._fs_class(device=self.loopDevices[0], label=None)
self.assertIsNone(an_fs.create())
def testCreatingEmpty(self):
"""Create the filesystem when passing the empty label."""
an_fs = self._fs_class(device=self.loopDevices[0], label="")
self.assertIsNone(an_fs.create())
class LabelingWithRelabeling(LabelingAsRoot):
"""Tests labeling where it is possible to relabel.
"""
def testLabeling(self):
"""A sequence of tests of filesystem labeling.
* create the filesystem when passing an invalid label
* raise an exception when reading the filesystem
* relabel the filesystem with a valid label
* relabel the filesystem with an empty label
* raise an exception when relabeling when None is specified
* raise an exception when relabeling with an invalid label
"""
an_fs = self._fs_class(device=self.loopDevices[0], label=self._invalid_label)
if an_fs._readlabel.availabilityErrors or not an_fs.relabels():
self.skipTest("can not read or write label for filesystem %s" % an_fs.name)
self.assertIsNone(an_fs.create())
with self.assertRaises(FSReadLabelError):
an_fs.readLabel()
an_fs.label = "an fs"
self.assertIsNone(an_fs.writeLabel())
an_fs.label = ""
self.assertIsNone(an_fs.writeLabel())
an_fs.label = None
with self.assertRaisesRegexp(FSError, "default label"):
an_fs.writeLabel()
an_fs.label = self._invalid_label
with self.assertRaisesRegexp(FSError, "bad label format"):
an_fs.writeLabel()
class CompleteLabelingAsRoot(LabelingAsRoot):
"""Tests where it is possible to read the label and to relabel
an existing filesystem.
"""
def testLabeling(self):
"""A sequence of tests of filesystem labeling.
* create the filesystem when passing an invalid label
and verify that the filesystem has the default label
* relabel the filesystem with a valid label
and verify that the filesystem has that label
* relabel the filesystem with an empty label
and verify that the filesystem has that label
* raise an exception when relabeling when None is specified
* raise an exception when relabeling with an invalid label
"""
an_fs = self._fs_class(device=self.loopDevices[0], label=self._invalid_label)
if an_fs._readlabel.availabilityErrors or not an_fs.relabels():
self.skipTest("can not read or write label for filesystem %s" % an_fs.name)
self.assertIsNone(an_fs.create())
self.assertEqual(an_fs.readLabel(), an_fs._labelfs.default_label)
an_fs.label = "an_fs"
self.assertIsNone(an_fs.writeLabel())
self.assertEqual(an_fs.readLabel(), an_fs.label)
an_fs.label = ""
self.assertIsNone(an_fs.writeLabel())
self.assertEqual(an_fs.readLabel(), an_fs.label)
an_fs.label = None
with self.assertRaisesRegexp(FSError, "default label"):
an_fs.writeLabel()
an_fs.label = "n" * 129
with self.assertRaisesRegexp(FSError, "bad label format"):
an_fs.writeLabel()
def testCreating(self):
"""Create the filesystem when passing a valid label.
Verify that the filesystem has that label.
"""
an_fs = self._fs_class(device=self.loopDevices[0], label="start")
if an_fs._readlabel.availabilityErrors:
self.skipTest("can not read label for filesystem %s" % an_fs.name)
self.assertIsNone(an_fs.create())
self.assertEqual(an_fs.readLabel(), "start")
def testCreatingNone(self):
"""Create a filesystem with the label None.
Verify that the filesystem has the default label.
"""
an_fs = self._fs_class(device=self.loopDevices[0], label=None)
if an_fs._readlabel.availabilityErrors:
self.skipTest("can not read label for filesystem %s" % an_fs.name)
self.assertIsNone(an_fs.create())
self.assertEqual(an_fs.readLabel(), an_fs._labelfs.default_label)
def testCreatingEmpty(self):
"""Create a filesystem with an empty label.
Verify that the filesystem has the empty label.
"""
an_fs = self._fs_class(device=self.loopDevices[0], label="")
if an_fs._readlabel.availabilityErrors:
self.skipTest("can not read label for filesystem %s" % an_fs.name)
self.assertIsNone(an_fs.create())
self.assertEqual(an_fs.readLabel(), "")