On 08/30/2012 09:38 AM, 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.

Exactly! Once virsh module is complete, libvirt_xml will act as interface to virsh* functions dealing with XML input/output. My intention was for libvirt_xml module to help make writing tests easier. Yes, we should put helper functions and new classes into this module for general use.

And I think class LibvirtXML is used to represent a vm's xml.

Almost. My thought was for LibvirtXML to be base class - a container for very general libvirt-related XML functions.


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.

Let's make a new class to hold VM related methods like check_interface(). How about VMXML(LibvirtXML) or Vm_XML(LibvirtXML) or something like that?

The function is not completed. This mail is just for some comments.

No problem, I love to see ideas like this, thanks for sharing.

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")

Yep, this is good.

+ 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

Yes, this could work and it seems like a useful method to have.

Another approach might be to make a VM_Interface_XML class. Then use special methods __eq__, __ne__, __cmp__, etc. This could support very natural OOP, for example:

ifaceA = vm_xml("vm1").get_interface(mac)
ifaceB = VM_Interface_XML(type_name, mac=None, source, model)
if ifaceA == ifaceB:
        do_something()
else:
        vm_xml.modify_interface(mac, ifaceB)

But maybe this is over-complicated. In either case, you have the right idea- libvirt_xml module is intended to make writing tests that use libvirt XML "stuff" easier :) Hopefully it allows tests to not even need to call virsh_*() functions.

Thanks for sharing your ideas.

--
Chris Evich, RHCA, RHCE, RHCDS, RHCSS
Quality Assurance Engineer
e-mail: cevich + `@' + redhat.com o: 1-888-RED-HAT1 x44214

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

Reply via email to