root / test / py / ganeti.block.bdev_unittest.py @ 38396ae2
History | View | Annotate | Download (23 kB)
1 |
#!/usr/bin/python
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2006, 2007, 2010, 2012, 2013 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Script for unittesting the bdev module"""
|
23 |
|
24 |
|
25 |
import os |
26 |
import random |
27 |
import unittest |
28 |
|
29 |
from ganeti import compat |
30 |
from ganeti import constants |
31 |
from ganeti import errors |
32 |
from ganeti import objects |
33 |
from ganeti import utils |
34 |
from ganeti.block import bdev |
35 |
from ganeti.block import drbd |
36 |
|
37 |
import testutils |
38 |
|
39 |
|
40 |
class TestBaseDRBD(testutils.GanetiTestCase): |
41 |
def testGetVersion(self): |
42 |
data = [ |
43 |
["version: 8.0.12 (api:76/proto:86-91)"],
|
44 |
["version: 8.2.7 (api:88/proto:0-100)"],
|
45 |
["version: 8.3.7.49 (api:188/proto:13-191)"],
|
46 |
] |
47 |
result = [ |
48 |
{ |
49 |
"k_major": 8, |
50 |
"k_minor": 0, |
51 |
"k_point": 12, |
52 |
"api": 76, |
53 |
"proto": 86, |
54 |
"proto2": "91", |
55 |
}, |
56 |
{ |
57 |
"k_major": 8, |
58 |
"k_minor": 2, |
59 |
"k_point": 7, |
60 |
"api": 88, |
61 |
"proto": 0, |
62 |
"proto2": "100", |
63 |
}, |
64 |
{ |
65 |
"k_major": 8, |
66 |
"k_minor": 3, |
67 |
"k_point": 7, |
68 |
"api": 188, |
69 |
"proto": 13, |
70 |
"proto2": "191", |
71 |
} |
72 |
] |
73 |
for d,r in zip(data, result): |
74 |
self.assertEqual(drbd.BaseDRBD._GetVersion(d), r)
|
75 |
|
76 |
|
77 |
class TestDRBD8Runner(testutils.GanetiTestCase): |
78 |
"""Testing case for drbd.DRBD8"""
|
79 |
|
80 |
@staticmethod
|
81 |
def _has_disk(data, dname, mname): |
82 |
"""Check local disk corectness"""
|
83 |
retval = ( |
84 |
"local_dev" in data and |
85 |
data["local_dev"] == dname and |
86 |
"meta_dev" in data and |
87 |
data["meta_dev"] == mname and |
88 |
"meta_index" in data and |
89 |
data["meta_index"] == 0 |
90 |
) |
91 |
return retval
|
92 |
|
93 |
@staticmethod
|
94 |
def _has_net(data, local, remote): |
95 |
"""Check network connection parameters"""
|
96 |
retval = ( |
97 |
"local_addr" in data and |
98 |
data["local_addr"] == local and |
99 |
"remote_addr" in data and |
100 |
data["remote_addr"] == remote
|
101 |
) |
102 |
return retval
|
103 |
|
104 |
def testParserCreation(self): |
105 |
"""Test drbdsetup show parser creation"""
|
106 |
drbd.DRBD8._GetShowParser() |
107 |
|
108 |
def testParser80(self): |
109 |
"""Test drbdsetup show parser for disk and network version 8.0"""
|
110 |
data = testutils.ReadTestData("bdev-drbd-8.0.txt")
|
111 |
result = drbd.DRBD8._GetDevInfo(data) |
112 |
self.failUnless(self._has_disk(result, "/dev/xenvg/test.data", |
113 |
"/dev/xenvg/test.meta"),
|
114 |
"Wrong local disk info")
|
115 |
self.failUnless(self._has_net(result, ("192.0.2.1", 11000), |
116 |
("192.0.2.2", 11000)), |
117 |
"Wrong network info (8.0.x)")
|
118 |
|
119 |
def testParser83(self): |
120 |
"""Test drbdsetup show parser for disk and network version 8.3"""
|
121 |
data = testutils.ReadTestData("bdev-drbd-8.3.txt")
|
122 |
result = drbd.DRBD8._GetDevInfo(data) |
123 |
self.failUnless(self._has_disk(result, "/dev/xenvg/test.data", |
124 |
"/dev/xenvg/test.meta"),
|
125 |
"Wrong local disk info")
|
126 |
self.failUnless(self._has_net(result, ("192.0.2.1", 11000), |
127 |
("192.0.2.2", 11000)), |
128 |
"Wrong network info (8.0.x)")
|
129 |
|
130 |
def testParserNetIP4(self): |
131 |
"""Test drbdsetup show parser for IPv4 network"""
|
132 |
data = testutils.ReadTestData("bdev-drbd-net-ip4.txt")
|
133 |
result = drbd.DRBD8._GetDevInfo(data) |
134 |
self.failUnless(("local_dev" not in result and |
135 |
"meta_dev" not in result and |
136 |
"meta_index" not in result), |
137 |
"Should not find local disk info")
|
138 |
self.failUnless(self._has_net(result, ("192.0.2.1", 11002), |
139 |
("192.0.2.2", 11002)), |
140 |
"Wrong network info (IPv4)")
|
141 |
|
142 |
def testParserNetIP6(self): |
143 |
"""Test drbdsetup show parser for IPv6 network"""
|
144 |
data = testutils.ReadTestData("bdev-drbd-net-ip6.txt")
|
145 |
result = drbd.DRBD8._GetDevInfo(data) |
146 |
self.failUnless(("local_dev" not in result and |
147 |
"meta_dev" not in result and |
148 |
"meta_index" not in result), |
149 |
"Should not find local disk info")
|
150 |
self.failUnless(self._has_net(result, ("2001:db8:65::1", 11048), |
151 |
("2001:db8:66::1", 11048)), |
152 |
"Wrong network info (IPv6)")
|
153 |
|
154 |
def testParserDisk(self): |
155 |
"""Test drbdsetup show parser for disk"""
|
156 |
data = testutils.ReadTestData("bdev-drbd-disk.txt")
|
157 |
result = drbd.DRBD8._GetDevInfo(data) |
158 |
self.failUnless(self._has_disk(result, "/dev/xenvg/test.data", |
159 |
"/dev/xenvg/test.meta"),
|
160 |
"Wrong local disk info")
|
161 |
self.failUnless(("local_addr" not in result and |
162 |
"remote_addr" not in result), |
163 |
"Should not find network info")
|
164 |
|
165 |
def testBarriersOptions(self): |
166 |
"""Test class method that generates drbdsetup options for disk barriers"""
|
167 |
# Tests that should fail because of wrong version/options combinations
|
168 |
should_fail = [ |
169 |
(8, 0, 12, "bfd", True), |
170 |
(8, 0, 12, "fd", False), |
171 |
(8, 0, 12, "b", True), |
172 |
(8, 2, 7, "bfd", True), |
173 |
(8, 2, 7, "b", True) |
174 |
] |
175 |
|
176 |
for vmaj, vmin, vrel, opts, meta in should_fail: |
177 |
self.assertRaises(errors.BlockDeviceError,
|
178 |
drbd.DRBD8._ComputeDiskBarrierArgs, |
179 |
vmaj, vmin, vrel, opts, meta) |
180 |
|
181 |
# get the valid options from the frozenset(frozenset()) in constants.
|
182 |
valid_options = [list(x)[0] for x in constants.DRBD_VALID_BARRIER_OPT] |
183 |
|
184 |
# Versions that do not support anything
|
185 |
for vmaj, vmin, vrel in ((8, 0, 0), (8, 0, 11), (8, 2, 6)): |
186 |
for opts in valid_options: |
187 |
self.assertRaises(errors.BlockDeviceError,
|
188 |
drbd.DRBD8._ComputeDiskBarrierArgs, |
189 |
vmaj, vmin, vrel, opts, True)
|
190 |
|
191 |
# Versions with partial support (testing only options that are supported)
|
192 |
tests = [ |
193 |
(8, 0, 12, "n", False, []), |
194 |
(8, 0, 12, "n", True, ["--no-md-flushes"]), |
195 |
(8, 2, 7, "n", False, []), |
196 |
(8, 2, 7, "fd", False, ["--no-disk-flushes", "--no-disk-drain"]), |
197 |
(8, 0, 12, "n", True, ["--no-md-flushes"]), |
198 |
] |
199 |
|
200 |
# Versions that support everything
|
201 |
for vmaj, vmin, vrel in ((8, 3, 0), (8, 3, 12)): |
202 |
tests.append((vmaj, vmin, vrel, "bfd", True, |
203 |
["--no-disk-barrier", "--no-disk-drain", |
204 |
"--no-disk-flushes", "--no-md-flushes"])) |
205 |
tests.append((vmaj, vmin, vrel, "n", False, [])) |
206 |
tests.append((vmaj, vmin, vrel, "b", True, |
207 |
["--no-disk-barrier", "--no-md-flushes"])) |
208 |
tests.append((vmaj, vmin, vrel, "fd", False, |
209 |
["--no-disk-flushes", "--no-disk-drain"])) |
210 |
tests.append((vmaj, vmin, vrel, "n", True, ["--no-md-flushes"])) |
211 |
|
212 |
# Test execution
|
213 |
for test in tests: |
214 |
vmaj, vmin, vrel, disabled_barriers, disable_meta_flush, expected = test |
215 |
args = \ |
216 |
drbd.DRBD8._ComputeDiskBarrierArgs(vmaj, vmin, vrel, |
217 |
disabled_barriers, |
218 |
disable_meta_flush) |
219 |
self.failUnless(set(args) == set(expected), |
220 |
"For test %s, got wrong results %s" % (test, args))
|
221 |
|
222 |
# Unsupported or invalid versions
|
223 |
for vmaj, vmin, vrel in ((0, 7, 25), (9, 0, 0), (7, 0, 0), (8, 4, 0)): |
224 |
self.assertRaises(errors.BlockDeviceError,
|
225 |
drbd.DRBD8._ComputeDiskBarrierArgs, |
226 |
vmaj, vmin, vrel, "n", True) |
227 |
|
228 |
# Invalid options
|
229 |
for option in ("", "c", "whatever", "nbdfc", "nf"): |
230 |
self.assertRaises(errors.BlockDeviceError,
|
231 |
drbd.DRBD8._ComputeDiskBarrierArgs, |
232 |
8, 3, 11, option, True) |
233 |
|
234 |
|
235 |
class TestDRBD8Status(testutils.GanetiTestCase): |
236 |
"""Testing case for DRBD8 /proc status"""
|
237 |
|
238 |
def setUp(self): |
239 |
"""Read in txt data"""
|
240 |
testutils.GanetiTestCase.setUp(self)
|
241 |
proc_data = testutils.TestDataFilename("proc_drbd8.txt")
|
242 |
proc80e_data = testutils.TestDataFilename("proc_drbd80-emptyline.txt")
|
243 |
proc83_data = testutils.TestDataFilename("proc_drbd83.txt")
|
244 |
proc83_sync_data = testutils.TestDataFilename("proc_drbd83_sync.txt")
|
245 |
proc83_sync_krnl_data = \ |
246 |
testutils.TestDataFilename("proc_drbd83_sync_krnl2.6.39.txt")
|
247 |
self.proc_data = drbd.DRBD8._GetProcData(filename=proc_data)
|
248 |
self.proc80e_data = drbd.DRBD8._GetProcData(filename=proc80e_data)
|
249 |
self.proc83_data = drbd.DRBD8._GetProcData(filename=proc83_data)
|
250 |
self.proc83_sync_data = drbd.DRBD8._GetProcData(filename=proc83_sync_data)
|
251 |
self.proc83_sync_krnl_data = \
|
252 |
drbd.DRBD8._GetProcData(filename=proc83_sync_krnl_data) |
253 |
self.mass_data = drbd.DRBD8._JoinProcDataPerMinor(self.proc_data) |
254 |
self.mass80e_data = drbd.DRBD8._JoinProcDataPerMinor(self.proc80e_data) |
255 |
self.mass83_data = drbd.DRBD8._JoinProcDataPerMinor(self.proc83_data) |
256 |
self.mass83_sync_data = \
|
257 |
drbd.DRBD8._JoinProcDataPerMinor(self.proc83_sync_data)
|
258 |
self.mass83_sync_krnl_data = \
|
259 |
drbd.DRBD8._JoinProcDataPerMinor(self.proc83_sync_krnl_data)
|
260 |
|
261 |
def testIOErrors(self): |
262 |
"""Test handling of errors while reading the proc file."""
|
263 |
temp_file = self._CreateTempFile()
|
264 |
os.unlink(temp_file) |
265 |
self.failUnlessRaises(errors.BlockDeviceError,
|
266 |
drbd.DRBD8._GetProcData, filename=temp_file) |
267 |
|
268 |
def testHelper(self): |
269 |
"""Test reading usermode_helper in /sys."""
|
270 |
sys_drbd_helper = testutils.TestDataFilename("sys_drbd_usermode_helper.txt")
|
271 |
drbd_helper = drbd.DRBD8.GetUsermodeHelper(filename=sys_drbd_helper) |
272 |
self.failUnlessEqual(drbd_helper, "/bin/true") |
273 |
|
274 |
def testHelperIOErrors(self): |
275 |
"""Test handling of errors while reading usermode_helper in /sys."""
|
276 |
temp_file = self._CreateTempFile()
|
277 |
os.unlink(temp_file) |
278 |
self.failUnlessRaises(errors.BlockDeviceError,
|
279 |
drbd.DRBD8.GetUsermodeHelper, filename=temp_file) |
280 |
|
281 |
def testMinorNotFound(self): |
282 |
"""Test not-found-minor in /proc"""
|
283 |
self.failUnless(9 not in self.mass_data) |
284 |
self.failUnless(9 not in self.mass83_data) |
285 |
self.failUnless(3 not in self.mass80e_data) |
286 |
|
287 |
def testLineNotMatch(self): |
288 |
"""Test wrong line passed to drbd.DRBD8Status"""
|
289 |
self.assertRaises(errors.BlockDeviceError, drbd.DRBD8Status, "foo") |
290 |
|
291 |
def testMinor0(self): |
292 |
"""Test connected, primary device"""
|
293 |
for data in [self.mass_data, self.mass83_data]: |
294 |
stats = drbd.DRBD8Status(data[0])
|
295 |
self.failUnless(stats.is_in_use)
|
296 |
self.failUnless(stats.is_connected and stats.is_primary and |
297 |
stats.peer_secondary and stats.is_disk_uptodate)
|
298 |
|
299 |
def testMinor1(self): |
300 |
"""Test connected, secondary device"""
|
301 |
for data in [self.mass_data, self.mass83_data]: |
302 |
stats = drbd.DRBD8Status(data[1])
|
303 |
self.failUnless(stats.is_in_use)
|
304 |
self.failUnless(stats.is_connected and stats.is_secondary and |
305 |
stats.peer_primary and stats.is_disk_uptodate)
|
306 |
|
307 |
def testMinor2(self): |
308 |
"""Test unconfigured device"""
|
309 |
for data in [self.mass_data, self.mass83_data, self.mass80e_data]: |
310 |
stats = drbd.DRBD8Status(data[2])
|
311 |
self.failIf(stats.is_in_use)
|
312 |
|
313 |
def testMinor4(self): |
314 |
"""Test WFconn device"""
|
315 |
for data in [self.mass_data, self.mass83_data]: |
316 |
stats = drbd.DRBD8Status(data[4])
|
317 |
self.failUnless(stats.is_in_use)
|
318 |
self.failUnless(stats.is_wfconn and stats.is_primary and |
319 |
stats.rrole == "Unknown" and |
320 |
stats.is_disk_uptodate) |
321 |
|
322 |
def testMinor6(self): |
323 |
"""Test diskless device"""
|
324 |
for data in [self.mass_data, self.mass83_data]: |
325 |
stats = drbd.DRBD8Status(data[6])
|
326 |
self.failUnless(stats.is_in_use)
|
327 |
self.failUnless(stats.is_connected and stats.is_secondary and |
328 |
stats.peer_primary and stats.is_diskless)
|
329 |
|
330 |
def testMinor8(self): |
331 |
"""Test standalone device"""
|
332 |
for data in [self.mass_data, self.mass83_data]: |
333 |
stats = drbd.DRBD8Status(data[8])
|
334 |
self.failUnless(stats.is_in_use)
|
335 |
self.failUnless(stats.is_standalone and |
336 |
stats.rrole == "Unknown" and |
337 |
stats.is_disk_uptodate) |
338 |
|
339 |
def testDRBD83SyncFine(self): |
340 |
stats = drbd.DRBD8Status(self.mass83_sync_data[3]) |
341 |
self.failUnless(stats.is_in_resync)
|
342 |
self.failUnless(stats.sync_percent is not None) |
343 |
|
344 |
def testDRBD83SyncBroken(self): |
345 |
stats = drbd.DRBD8Status(self.mass83_sync_krnl_data[3]) |
346 |
self.failUnless(stats.is_in_resync)
|
347 |
self.failUnless(stats.sync_percent is not None) |
348 |
|
349 |
|
350 |
class TestRADOSBlockDevice(testutils.GanetiTestCase): |
351 |
def setUp(self): |
352 |
"""Set up input data"""
|
353 |
testutils.GanetiTestCase.setUp(self)
|
354 |
|
355 |
self.plain_output_old_ok = \
|
356 |
testutils.ReadTestData("bdev-rbd/plain_output_old_ok.txt")
|
357 |
self.plain_output_old_no_matches = \
|
358 |
testutils.ReadTestData("bdev-rbd/plain_output_old_no_matches.txt")
|
359 |
self.plain_output_old_extra_matches = \
|
360 |
testutils.ReadTestData("bdev-rbd/plain_output_old_extra_matches.txt")
|
361 |
self.plain_output_old_empty = \
|
362 |
testutils.ReadTestData("bdev-rbd/plain_output_old_empty.txt")
|
363 |
self.plain_output_new_ok = \
|
364 |
testutils.ReadTestData("bdev-rbd/plain_output_new_ok.txt")
|
365 |
self.plain_output_new_no_matches = \
|
366 |
testutils.ReadTestData("bdev-rbd/plain_output_new_no_matches.txt")
|
367 |
self.plain_output_new_extra_matches = \
|
368 |
testutils.ReadTestData("bdev-rbd/plain_output_new_extra_matches.txt")
|
369 |
# This file is completely empty, and as such it's not shipped.
|
370 |
self.plain_output_new_empty = "" |
371 |
self.json_output_ok = testutils.ReadTestData("bdev-rbd/json_output_ok.txt") |
372 |
self.json_output_no_matches = \
|
373 |
testutils.ReadTestData("bdev-rbd/json_output_no_matches.txt")
|
374 |
self.json_output_extra_matches = \
|
375 |
testutils.ReadTestData("bdev-rbd/json_output_extra_matches.txt")
|
376 |
self.json_output_empty = \
|
377 |
testutils.ReadTestData("bdev-rbd/json_output_empty.txt")
|
378 |
self.output_invalid = testutils.ReadTestData("bdev-rbd/output_invalid.txt") |
379 |
|
380 |
self.volume_name = "d7ab910a-4933-4ffe-88d0-faf2ce31390a.rbd.disk0" |
381 |
|
382 |
def test_ParseRbdShowmappedJson(self): |
383 |
parse_function = bdev.RADOSBlockDevice._ParseRbdShowmappedJson |
384 |
|
385 |
self.assertEqual(parse_function(self.json_output_ok, self.volume_name), |
386 |
"/dev/rbd3")
|
387 |
self.assertEqual(parse_function(self.json_output_empty, self.volume_name), |
388 |
None)
|
389 |
self.assertEqual(parse_function(self.json_output_no_matches, |
390 |
self.volume_name), None) |
391 |
self.assertRaises(errors.BlockDeviceError, parse_function,
|
392 |
self.json_output_extra_matches, self.volume_name) |
393 |
self.assertRaises(errors.BlockDeviceError, parse_function,
|
394 |
self.output_invalid, self.volume_name) |
395 |
|
396 |
def test_ParseRbdShowmappedPlain(self): |
397 |
parse_function = bdev.RADOSBlockDevice._ParseRbdShowmappedPlain |
398 |
|
399 |
self.assertEqual(parse_function(self.plain_output_new_ok, |
400 |
self.volume_name), "/dev/rbd3") |
401 |
self.assertEqual(parse_function(self.plain_output_old_ok, |
402 |
self.volume_name), "/dev/rbd3") |
403 |
self.assertEqual(parse_function(self.plain_output_new_empty, |
404 |
self.volume_name), None) |
405 |
self.assertEqual(parse_function(self.plain_output_old_empty, |
406 |
self.volume_name), None) |
407 |
self.assertEqual(parse_function(self.plain_output_new_no_matches, |
408 |
self.volume_name), None) |
409 |
self.assertEqual(parse_function(self.plain_output_old_no_matches, |
410 |
self.volume_name), None) |
411 |
self.assertRaises(errors.BlockDeviceError, parse_function,
|
412 |
self.plain_output_new_extra_matches, self.volume_name) |
413 |
self.assertRaises(errors.BlockDeviceError, parse_function,
|
414 |
self.plain_output_old_extra_matches, self.volume_name) |
415 |
self.assertRaises(errors.BlockDeviceError, parse_function,
|
416 |
self.output_invalid, self.volume_name) |
417 |
|
418 |
class TestComputeWrongFileStoragePathsInternal(unittest.TestCase): |
419 |
def testPaths(self): |
420 |
paths = bdev._GetForbiddenFileStoragePaths() |
421 |
|
422 |
for path in ["/bin", "/usr/local/sbin", "/lib64", "/etc", "/sys"]: |
423 |
self.assertTrue(path in paths) |
424 |
|
425 |
self.assertEqual(set(map(os.path.normpath, paths)), paths) |
426 |
|
427 |
def test(self): |
428 |
vfsp = bdev._ComputeWrongFileStoragePaths |
429 |
self.assertEqual(vfsp([]), [])
|
430 |
self.assertEqual(vfsp(["/tmp"]), []) |
431 |
self.assertEqual(vfsp(["/bin/ls"]), ["/bin/ls"]) |
432 |
self.assertEqual(vfsp(["/bin"]), ["/bin"]) |
433 |
self.assertEqual(vfsp(["/usr/sbin/vim", "/srv/file-storage"]), |
434 |
["/usr/sbin/vim"])
|
435 |
|
436 |
|
437 |
class TestComputeWrongFileStoragePaths(testutils.GanetiTestCase): |
438 |
def test(self): |
439 |
tmpfile = self._CreateTempFile()
|
440 |
|
441 |
utils.WriteFile(tmpfile, data="""
|
442 |
/tmp
|
443 |
x/y///z/relative
|
444 |
# This is a test file
|
445 |
/srv/storage
|
446 |
/bin
|
447 |
/usr/local/lib32/
|
448 |
relative/path
|
449 |
""")
|
450 |
|
451 |
self.assertEqual(bdev.ComputeWrongFileStoragePaths(_filename=tmpfile), [
|
452 |
"/bin",
|
453 |
"/usr/local/lib32",
|
454 |
"relative/path",
|
455 |
"x/y/z/relative",
|
456 |
]) |
457 |
|
458 |
|
459 |
class TestCheckFileStoragePathInternal(unittest.TestCase): |
460 |
def testNonAbsolute(self): |
461 |
for i in ["", "tmp", "foo/bar/baz"]: |
462 |
self.assertRaises(errors.FileStoragePathError,
|
463 |
bdev._CheckFileStoragePath, i, ["/tmp"])
|
464 |
|
465 |
self.assertRaises(errors.FileStoragePathError,
|
466 |
bdev._CheckFileStoragePath, "/tmp", ["tmp", "xyz"]) |
467 |
|
468 |
def testNoAllowed(self): |
469 |
self.assertRaises(errors.FileStoragePathError,
|
470 |
bdev._CheckFileStoragePath, "/tmp", [])
|
471 |
|
472 |
def testNoAdditionalPathComponent(self): |
473 |
self.assertRaises(errors.FileStoragePathError,
|
474 |
bdev._CheckFileStoragePath, "/tmp/foo", ["/tmp/foo"]) |
475 |
|
476 |
def testAllowed(self): |
477 |
bdev._CheckFileStoragePath("/tmp/foo/a", ["/tmp/foo"]) |
478 |
bdev._CheckFileStoragePath("/tmp/foo/a/x", ["/tmp/foo"]) |
479 |
|
480 |
|
481 |
class TestCheckFileStoragePath(testutils.GanetiTestCase): |
482 |
def testNonExistantFile(self): |
483 |
filename = "/tmp/this/file/does/not/exist"
|
484 |
assert not os.path.exists(filename) |
485 |
self.assertRaises(errors.FileStoragePathError,
|
486 |
bdev.CheckFileStoragePath, "/bin/", _filename=filename)
|
487 |
self.assertRaises(errors.FileStoragePathError,
|
488 |
bdev.CheckFileStoragePath, "/srv/file-storage",
|
489 |
_filename=filename) |
490 |
|
491 |
def testAllowedPath(self): |
492 |
tmpfile = self._CreateTempFile()
|
493 |
|
494 |
utils.WriteFile(tmpfile, data="""
|
495 |
/srv/storage
|
496 |
""")
|
497 |
|
498 |
bdev.CheckFileStoragePath("/srv/storage/inst1", _filename=tmpfile)
|
499 |
|
500 |
# No additional path component
|
501 |
self.assertRaises(errors.FileStoragePathError,
|
502 |
bdev.CheckFileStoragePath, "/srv/storage",
|
503 |
_filename=tmpfile) |
504 |
|
505 |
# Forbidden path
|
506 |
self.assertRaises(errors.FileStoragePathError,
|
507 |
bdev.CheckFileStoragePath, "/usr/lib64/xyz",
|
508 |
_filename=tmpfile) |
509 |
|
510 |
|
511 |
class TestLoadAllowedFileStoragePaths(testutils.GanetiTestCase): |
512 |
def testDevNull(self): |
513 |
self.assertEqual(bdev._LoadAllowedFileStoragePaths("/dev/null"), []) |
514 |
|
515 |
def testNonExistantFile(self): |
516 |
filename = "/tmp/this/file/does/not/exist"
|
517 |
assert not os.path.exists(filename) |
518 |
self.assertEqual(bdev._LoadAllowedFileStoragePaths(filename), [])
|
519 |
|
520 |
def test(self): |
521 |
tmpfile = self._CreateTempFile()
|
522 |
|
523 |
utils.WriteFile(tmpfile, data="""
|
524 |
# This is a test file
|
525 |
/tmp
|
526 |
/srv/storage
|
527 |
relative/path
|
528 |
""")
|
529 |
|
530 |
self.assertEqual(bdev._LoadAllowedFileStoragePaths(tmpfile), [
|
531 |
"/tmp",
|
532 |
"/srv/storage",
|
533 |
"relative/path",
|
534 |
]) |
535 |
|
536 |
|
537 |
class TestExclusiveStoragePvs(unittest.TestCase): |
538 |
"""Test cases for functions dealing with LVM PV and exclusive storage"""
|
539 |
# Allowance for rounding
|
540 |
_EPS = 1e-4
|
541 |
_MARGIN = constants.PART_MARGIN + constants.PART_RESERVED + _EPS |
542 |
|
543 |
@staticmethod
|
544 |
def _GenerateRandomPvInfo(rnd, name, vg): |
545 |
# Granularity is .01 MiB
|
546 |
size = rnd.randint(1024 * 100, 10 * 1024 * 1024 * 100) |
547 |
if rnd.choice([False, True]): |
548 |
free = float(rnd.randint(0, size)) / 100.0 |
549 |
else:
|
550 |
free = float(size) / 100.0 |
551 |
size = float(size) / 100.0 |
552 |
attr = "a-"
|
553 |
return objects.LvmPvInfo(name=name, vg_name=vg, size=size, free=free,
|
554 |
attributes=attr) |
555 |
|
556 |
def testGetStdPvSize(self): |
557 |
"""Test cases for bdev.LogicalVolume._GetStdPvSize()"""
|
558 |
rnd = random.Random(9517)
|
559 |
for _ in range(0, 50): |
560 |
# Identical volumes
|
561 |
pvi = self._GenerateRandomPvInfo(rnd, "disk", "myvg") |
562 |
onesize = bdev.LogicalVolume._GetStdPvSize([pvi]) |
563 |
self.assertTrue(onesize <= pvi.size)
|
564 |
self.assertTrue(onesize > pvi.size * (1 - self._MARGIN)) |
565 |
for length in range(2, 10): |
566 |
n_size = bdev.LogicalVolume._GetStdPvSize([pvi] * length) |
567 |
self.assertEqual(onesize, n_size)
|
568 |
|
569 |
# Mixed volumes
|
570 |
for length in range(1, 10): |
571 |
pvlist = [self._GenerateRandomPvInfo(rnd, "disk", "myvg") |
572 |
for _ in range(0, length)] |
573 |
std_size = bdev.LogicalVolume._GetStdPvSize(pvlist) |
574 |
self.assertTrue(compat.all(std_size <= pvi.size for pvi in pvlist)) |
575 |
self.assertTrue(compat.any(std_size > pvi.size * (1 - self._MARGIN) |
576 |
for pvi in pvlist)) |
577 |
pvlist.append(pvlist[0])
|
578 |
p1_size = bdev.LogicalVolume._GetStdPvSize(pvlist) |
579 |
self.assertEqual(std_size, p1_size)
|
580 |
|
581 |
def testComputeNumPvs(self): |
582 |
"""Test cases for bdev.LogicalVolume._ComputeNumPvs()"""
|
583 |
rnd = random.Random(8067)
|
584 |
for _ in range(0, 1000): |
585 |
pvlist = [self._GenerateRandomPvInfo(rnd, "disk", "myvg")] |
586 |
lv_size = float(rnd.randint(10 * 100, 1024 * 1024 * 100)) / 100.0 |
587 |
num_pv = bdev.LogicalVolume._ComputeNumPvs(lv_size, pvlist) |
588 |
std_size = bdev.LogicalVolume._GetStdPvSize(pvlist) |
589 |
self.assertTrue(num_pv >= 1) |
590 |
self.assertTrue(num_pv * std_size >= lv_size)
|
591 |
self.assertTrue((num_pv - 1) * std_size < lv_size * (1 + self._EPS)) |
592 |
|
593 |
def testGetEmptyPvNames(self): |
594 |
"""Test cases for bdev.LogicalVolume._GetEmptyPvNames()"""
|
595 |
rnd = random.Random(21126)
|
596 |
for _ in range(0, 100): |
597 |
num_pvs = rnd.randint(1, 20) |
598 |
pvlist = [self._GenerateRandomPvInfo(rnd, "disk%d" % n, "myvg") |
599 |
for n in range(0, num_pvs)] |
600 |
for num_req in range(1, num_pvs + 2): |
601 |
epvs = bdev.LogicalVolume._GetEmptyPvNames(pvlist, num_req) |
602 |
epvs_set = compat.UniqueFrozenset(epvs) |
603 |
if len(epvs) > 1: |
604 |
self.assertEqual(len(epvs), len(epvs_set)) |
605 |
for pvi in pvlist: |
606 |
if pvi.name in epvs_set: |
607 |
self.assertEqual(pvi.size, pvi.free)
|
608 |
else:
|
609 |
# There should be no remaining empty PV when less than the
|
610 |
# requeste number of PVs has been returned
|
611 |
self.assertTrue(len(epvs) == num_req or pvi.free != pvi.size) |
612 |
|
613 |
|
614 |
if __name__ == "__main__": |
615 |
testutils.GanetiTestProgram() |