On 08/30/2012 09:38 PM, Tang Chen wrote:
Hi Chris,

I saw libvirt_xml.py and the classes in it. And I'm not sure if I understand this file correctly. I think we can put all the xml related work in libvirt testing into this file.
And I think class LibvirtXML is used to represent a vm's xml.

So I add a function check_interface() into it. This function is used to check if there is an interface
in the xml which is exactly the same as the parameters describe.

If you want to verify if libvirt's XML configration is valid, you may use 'virt-xml-validate' tool to do this:

virt-xml-validate XML-FILE [SCHEMA-NAME] - validate libvirt XML files against a schema

If you want to generate libvirt's XML configration, we have ever done this in internal auto project, the basic idea is to create a XML construction function for each XML element block such as '<devices> .... </devices>', '<emulator> ... </emulator>', '<interface xxxxx> ... </interface>', '<disk xxxxx> ... </disk>', etc. it's very flexible and easy to reuse, you may use different XML construction function to generate different libvirt's XML configuration, and also call them to generate a guest XML configuration.

def emulator_xml(xxxx):
# check emulator based on currently hypervisor then construct <emulator> ... </emulator>

def iface_xml(xxxx):
    # to construct <interface> ... </interface>

def disk_xml(xxxx):
    # .....

def devices_xml(xxxx):
    ......
    emu_xml = emulator_xml(xxxx)
    iface_xml = iface_xml(xxxx)
    disk_xml = disk_xml(xxxx)
# if you want attach 2 disk then you may call disk_xml() 2 times with different parameters. # of course, you may use virsh attach-disk or attach-device to do this later.
    ......

def guest_xml(xxxx):
    ......
    devices_xml = devices_xml(xxxx)
    ......

Regards,
Alex


The function is not completed. This mail is just for some comments.
Please tell me if I am wrong.

Thanks. :)


Signed-off-by: Tang Chen <[email protected]>
---
client/virt/libvirt_xml.py | 57 +++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 56 insertions(+), 1 deletions(-)

diff --git a/client/virt/libvirt_xml.py b/client/virt/libvirt_xml.py
index cb577c6..02781ca 100644
--- a/client/virt/libvirt_xml.py
+++ b/client/virt/libvirt_xml.py
@@ -12,4 +12,59 @@ class LibvirtXMLVMNameError(LibvirtXMLError):


 class LibvirtXML(xml_utils.XMLTreeFile):
-    pass
+    """
+    A class representing a vm's xml file.
+    """
+
+    # vm's name
+    vmname = None
+
+    def __init__(self, xml):
+        """
+        Initialize an object representing a vm's xml configuration.
+        It can be initialized from a xml file or a string retuened
+        by virsh dumpxml.
+        And also, it attracts the vm's name from the xml.
+
+        param: xml: A filename or string containing XML
+        """
+
+        super(LibvirtXML, self).__init__(xml)
+        self.vmname = self.findtext("name")
+
+
+    def check_interface(self, type=None, mac=None,
+                        source=None, model=None):
+        """
+        Check if vm has a coincident interface
+        """
+        type_real = None
+        mac_real = None
+        model_real = None
+        source_real = None
+
+        interface = self.find("devices/interface")
+        if interface != None:
+            type_real = interface.attrib.get("type")
+
+        mac_attr = self.find("devices/interface/mac")
+        if mac_attr != None:
+            mac_real = mac_attr.attrib.get("address")
+
+        source_attr = self.find("devices/interface/source")
+        if source_attr != None:
+            source_real = source_attr.attrib.get(type_real)
+
+        model_attr = self.find("devices/interface/model")
+        if model_attr != None:
+            model_real = model_attr.attrib.get("type")
+
+        logging.debug("type: %s" % type_real)
+        logging.debug("mac: %s" % mac_real)
+        logging.debug("source: %s" % source_real)
+        logging.debug("model: %s" % model_real)
+
+        if type_real != type or mac_real != mac or \
+            source_real != source or model_real != model:
+            return False
+        return True

_______________________________________________
Autotest-kernel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/autotest-kernel

Reply via email to