tests: Add focused unit test for device numbering

This commit is contained in:
Cole Robinson 2013-10-03 07:20:54 -04:00
parent 0614e490c8
commit 5860b132dc
2 changed files with 38 additions and 40 deletions

View File

@ -1035,19 +1035,16 @@ class TestXMLConfig(unittest.TestCase):
virtinst.DomainNumatune.cpuset_str_to_tuple,
conn, "16")
def testManyVirtio(self):
d = VirtualDisk(utils.get_conn())
d.bus = "virtio"
d.path = "/dev/default-pool/testvol1.img"
targetlist = []
for ignore in range(0, (26 * 2) + 1):
d.target = None
d.generate_target(targetlist)
targetlist.append(d.target)
self.assertEquals("vdaa", targetlist[26])
self.assertEquals("vdba", targetlist[26 * 2])
def testDiskNumbers(self):
self.assertEquals("a", VirtualDisk.num_to_target(1))
self.assertEquals("b", VirtualDisk.num_to_target(2))
self.assertEquals("z", VirtualDisk.num_to_target(26))
self.assertEquals("aa", VirtualDisk.num_to_target(27))
self.assertEquals("ab", VirtualDisk.num_to_target(28))
self.assertEquals("az", VirtualDisk.num_to_target(52))
self.assertEquals("ba", VirtualDisk.num_to_target(53))
self.assertEquals("zz", VirtualDisk.num_to_target(27 * 26))
self.assertEquals("aaa", VirtualDisk.num_to_target(27 * 26 + 1))
def testFedoraTreeinfo(self):
i = utils.make_distro_installer(

View File

@ -395,6 +395,33 @@ class VirtualDisk(VirtualDevice):
def build_vol_install(*args, **kwargs):
return diskbackend.build_vol_install(*args, **kwargs)
@staticmethod
def num_to_target(num):
"""
Convert an index in range (1, 1024) to a disk /dev number
(like hda, hdb, hdaa, etc.)
"""
digits = []
for factor in range(0, 3):
amt = (num % (26 ** (factor + 1))) / (26 ** factor)
if amt == 0 and num >= (26 ** (factor + 1)):
amt = 26
num -= amt
digits.insert(0, amt)
seen_valid = False
gen_t = ""
for digit in digits:
if digit == 0:
if not seen_valid:
continue
digit = 1
seen_valid = True
gen_t += "%c" % (ord('a') + digit - 1)
return gen_t
_XML_PROP_ORDER = [
"type", "device",
@ -800,35 +827,9 @@ class VirtualDisk(VirtualDevice):
@rtype C{str}
"""
prefix, maxnode = self.get_target_prefix()
if prefix is None:
raise ValueError(_("Cannot determine device bus/type."))
if maxnode > (26 * 26 * 26):
raise RuntimeError("maxnode value is too high")
# Regular scanning
for i in range(1, maxnode + 1):
gen_t = prefix
tmp = i
digits = []
for factor in range(0, 3):
amt = (tmp % (26 ** (factor + 1))) / (26 ** factor)
if amt == 0 and tmp >= (26 ** (factor + 1)):
amt = 26
tmp -= amt
digits.insert(0, amt)
seen_valid = False
for digit in digits:
if digit == 0:
if not seen_valid:
continue
digit = 1
seen_valid = True
gen_t += "%c" % (ord('a') + digit - 1)
gen_t = prefix + self.num_to_target(i)
if gen_t not in skip_targets:
self.target = gen_t
return self.target