Merge "Add idmap to libvirt config"

This commit is contained in:
Jenkins 2014-09-01 17:45:46 +00:00 committed by Gerrit Code Review
commit 681acd3f07
2 changed files with 166 additions and 0 deletions

View File

@ -1168,6 +1168,55 @@ class LibvirtConfigGuestTest(LibvirtConfigBaseTest):
</devices>
</domain>""")
def test_config_lxc_with_idmap(self):
obj = config.LibvirtConfigGuest()
obj.virt_type = "lxc"
obj.memory = 100 * units.Mi
obj.vcpus = 2
obj.cpuset = set([0, 1, 3, 4, 5])
obj.name = "demo"
obj.uuid = "b38a3f43-4be2-4046-897f-b67c2f5e0147"
obj.os_type = "exe"
obj.os_init_path = "/sbin/init"
uidmap = config.LibvirtConfigGuestUIDMap()
uidmap.target = "10000"
uidmap.count = "1"
obj.idmaps.append(uidmap)
gidmap = config.LibvirtConfigGuestGIDMap()
gidmap.target = "10000"
gidmap.count = "1"
obj.idmaps.append(gidmap)
fs = config.LibvirtConfigGuestFilesys()
fs.source_dir = "/root/lxc"
fs.target_dir = "/"
obj.add_device(fs)
xml = obj.to_xml()
self.assertXmlEqual("""
<domain type="lxc">
<uuid>b38a3f43-4be2-4046-897f-b67c2f5e0147</uuid>
<name>demo</name>
<memory>104857600</memory>
<vcpu cpuset="0-1,3-5">2</vcpu>
<os>
<type>exe</type>
<init>/sbin/init</init>
</os>
<devices>
<filesystem type="mount">
<source dir="/root/lxc"/>
<target dir="/"/>
</filesystem>
</devices>
<idmap>
<uid start="0" target="10000" count="1"/>
<gid start="0" target="10000" count="1"/>
</idmap>
</domain>""", xml)
def test_config_xen_pv(self):
obj = config.LibvirtConfigGuest()
obj.virt_type = "xen"
@ -2033,3 +2082,59 @@ class LibvirtConfigGuestMetadataNovaTest(LibvirtConfigBaseTest):
<nova:root type="image" uuid="fe55c69a-8b2e-4bbc-811a-9ad2023a0426"/>
</nova:instance>
""")
class LibvirtConfigGuestIDMap(LibvirtConfigBaseTest):
def test_config_id_map_parse_start_not_int(self):
xmlin = "<uid start='a' target='20000' count='5'/>"
obj = config.LibvirtConfigGuestIDMap()
self.assertRaises(ValueError, obj.parse_str, xmlin)
def test_config_id_map_parse_target_not_int(self):
xmlin = "<uid start='2' target='a' count='5'/>"
obj = config.LibvirtConfigGuestIDMap()
self.assertRaises(ValueError, obj.parse_str, xmlin)
def test_config_id_map_parse_count_not_int(self):
xmlin = "<uid start='2' target='20000' count='a'/>"
obj = config.LibvirtConfigGuestIDMap()
self.assertRaises(ValueError, obj.parse_str, xmlin)
def test_config_uid_map(self):
obj = config.LibvirtConfigGuestUIDMap()
obj.start = 1
obj.target = 10000
obj.count = 2
xml = obj.to_xml()
self.assertXmlEqual("<uid start='1' target='10000' count='2'/>", xml)
def test_config_uid_map_parse(self):
xmlin = "<uid start='2' target='20000' count='5'/>"
obj = config.LibvirtConfigGuestUIDMap()
obj.parse_str(xmlin)
self.assertEqual(2, obj.start)
self.assertEqual(20000, obj.target)
self.assertEqual(5, obj.count)
def test_config_gid_map(self):
obj = config.LibvirtConfigGuestGIDMap()
obj.start = 1
obj.target = 10000
obj.count = 2
xml = obj.to_xml()
self.assertXmlEqual("<gid start='1' target='10000' count='2'/>", xml)
def test_config_gid_map_parse(self):
xmlin = "<gid start='2' target='20000' count='5'/>"
obj = config.LibvirtConfigGuestGIDMap()
obj.parse_str(xmlin)
self.assertEqual(2, obj.start)
self.assertEqual(20000, obj.target)
self.assertEqual(5, obj.count)

View File

@ -1015,6 +1015,43 @@ class LibvirtConfigGuestFilesys(LibvirtConfigGuestDevice):
return dev
class LibvirtConfigGuestIDMap(LibvirtConfigObject):
def __init__(self, **kwargs):
super(LibvirtConfigGuestIDMap, self).__init__(**kwargs)
self.start = 0
self.target = 0
self.count = 10000
def parse_dom(self, xmldoc):
self.start = int(xmldoc.get('start'))
self.target = int(xmldoc.get('target'))
self.count = int(xmldoc.get('count'))
def format_dom(self):
obj = super(LibvirtConfigGuestIDMap, self).format_dom()
obj.set("start", str(self.start))
obj.set("target", str(self.target))
obj.set("count", str(self.count))
return obj
class LibvirtConfigGuestUIDMap(LibvirtConfigGuestIDMap):
def __init__(self, **kwargs):
super(LibvirtConfigGuestUIDMap, self).__init__(root_name="uid",
**kwargs)
class LibvirtConfigGuestGIDMap(LibvirtConfigGuestIDMap):
def __init__(self, **kwargs):
super(LibvirtConfigGuestGIDMap, self).__init__(root_name="gid",
**kwargs)
class LibvirtConfigGuestInterface(LibvirtConfigGuestDevice):
def __init__(self, **kwargs):
@ -1512,6 +1549,7 @@ class LibvirtConfigGuest(LibvirtConfigObject):
self.os_mach_type = None
self.devices = []
self.metadata = []
self.idmaps = []
def _format_basic_props(self, root):
root.append(self._text_node("uuid", self.uuid))
@ -1577,6 +1615,14 @@ class LibvirtConfigGuest(LibvirtConfigObject):
devices.append(dev.format_dom())
root.append(devices)
def _format_idmaps(self, root):
if len(self.idmaps) == 0:
return
idmaps = etree.Element("idmap")
for idmap in self.idmaps:
idmaps.append(idmap.format_dom())
root.append(idmaps)
def format_dom(self):
root = super(LibvirtConfigGuest, self).format_dom()
@ -1601,11 +1647,15 @@ class LibvirtConfigGuest(LibvirtConfigObject):
self._format_devices(root)
self._format_idmaps(root)
return root
def parse_dom(self, xmldoc):
# Note: This cover only for: LibvirtConfigGuestDisks
# LibvirtConfigGuestHostdevPCI
# LibvirtConfigGuestUidMap
# LibvirtConfigGuestGidMap
# LibvirtConfigGuestCPU
for c in xmldoc.getchildren():
if c.tag == 'devices':
@ -1618,6 +1668,17 @@ class LibvirtConfigGuest(LibvirtConfigObject):
obj = LibvirtConfigGuestHostdevPCI()
obj.parse_dom(d)
self.devices.append(obj)
if c.tag == 'idmap':
for map in c.getchildren():
obj = None
if map.tag == 'uid':
obj = LibvirtConfigGuestUIDMap()
elif map.tag == 'gid':
obj = LibvirtConfigGuestGIDMap()
if obj:
obj.parse_dom(map)
self.idmaps.append(obj)
elif c.tag == 'cpu':
obj = LibvirtConfigGuestCPU()
obj.parse_dom(c)