On 09/06/2012 03:42 AM, Yu Mingfei wrote:
On 06/09/12 03:00, Chris Evich wrote:
FYI: I've been working on this. It brought to light some "issues" I
had not forseen with libvirt_xml and xml_utils. However, I think I've
got a fix. I'll post my proposal for changes back to you on this thread.
Hi Chris:
I have rebased rename based on your enhanced xml_utils.py with a single
patchset.
(Please refer to mail at 03/09/12 [Autotest] [PATCH 0/3 v2] Rename a vm
through libvirt_xml module)
Maybe we can put virt-edit here and complete rename function first.:-)
thanks~
Yep, this is essentially what I've done. Yes, we need to get the
underlying libvirt_xml stuff designed well before it makes sense to
write tests against it. Because, while reviewing I found some "issues"
with libvirt_xml and xml_utils which I fixed with
cbff87bb01f1bd21afaca5f997505af2285afae0 and pushed to next.
Taking what you did, and some of the ideas tangchen proposed, I hacked
and slashed, and moved things around, and even went a little crazy.
However, I think I've got all the pieces coming together nicely and
wanted to share the result.
It means the virsh-edit test will be broken for now, but I think not too
hard to fix. I'll do proper review of that patch also, because there
are some other issues to fix.
Anyway/again, I think we need to get some of the basic structural stuff
for libvirt_xml out of the way first. Then it will be much easier to
add new tests.
The attached patch (not tested) should apply cleanly (use git am <file>)
just after commit 14b5cab05fcf2f7c87931bfae6e3d4875b6f1bbe on next
branch. Take a look at tell me what you think. Good approach?
What about using a properties interface instead (similar to how
VirshBase class works)?
If you like it, and/or have more ideas, let's get them in. But...
---
There's a next-freeze because we've got a major change coming with lmr's
client/test split he's been working on. We've got to see that through
before any new test patches can make it in. I'm sure he'd appreciate
your feedback as well if you have a chance to take a look at and try
pull request https://github.com/autotest/autotest/pull/533
Once that's done I'll start looking at libvirt patches again.
--
Chris Evich, RHCA, RHCE, RHCDS, RHCSS
Quality Assurance Engineer
e-mail: cevich + `@' + redhat.com o: 1-888-RED-HAT1 x44214
>From 038427aab4351a99e8f646f3f989928144b40e49 Mon Sep 17 00:00:00 2001
From: Yu Mingfei <[email protected]>
From: Chris Evich <[email protected]>
Date: Thu, 30 Aug 2012 14:47:06 +0000
Subject: [PATCH 3/3] virt-libvirt: Impliment libvirt_xml classes
Cc: Chris Evich <[email protected]>
+ Impliment classes LibvirtXMLBase, LibvirtXML, and VMXML to supply
interface into XML-related virsh functions. The base class
defines a basic set of initialization and functional behavior.
+ The libvirtXML and VMXML classes build upon LibvirtXMLBase to
specialize in particular types of XML (capabilities and VMs
respectivly).
+ Impliment VMXML commit method for storing XML changes
back into libvirt.
Signed-off-by: Chris Evich <[email protected]>
+ Impliment reame method for tests to easily change a VMs name
(clearing uuid) within it's XML definition.
Signed-off-by: Yu Mingfei<[email protected]>
---
client/virt/libvirt_xml.py | 107 ++++++++++++++++++++++++++++++++++++--------
1 files changed, 88 insertions(+), 19 deletions(-)
diff --git a/client/virt/libvirt_xml.py b/client/virt/libvirt_xml.py
index a0a179d..addb535 100644
--- a/client/virt/libvirt_xml.py
+++ b/client/virt/libvirt_xml.py
@@ -32,33 +32,102 @@ class LibvirtXMLBase(xml_utils.XMLTreeFile):
Base class for common attributes/methods applying to all sub-classes
"""
- @classmethod
- def generate_uuid(cls):
+ _xml_initialized = False
+
+ def __init__(self, persistent=False, **virsh_dargs):
"""
- Returns generated uuid value
+ Initialize instance's virsh interface
+
+ @param: persistent: Use persistent virsh connection for this instance
+ @param: virsh_dargs: Virsh function API keyword arguments
"""
- try:
- return open("/proc/sys/kernel/random/uuid").read().strip()
- except IOError:
- return "" #assume libvirt will fill in empty uuids
+
+ if persistent:
+ self.virsh = virsh.VirshPersistent(**virsh_func_dargs)
+ else:
+ self.virsh = virsh.Virsh(**virsh_func_dargs)
+
+
+ def init_XML(self, XML):
+ """
+ Secondary Initialization from XML file or string
+ """
+
+ if not _xml_initialized:
+ self.__del__()
+ super(LibvirtXMLBase, self).__init__(XML)
+ self._xml_initialized = True
+
+
+ def check_xml_init(self):
+ """
+ Raises LibvirtXMLError if instance is not initialized
+ """
+
+ if not _xml_initialized:
+ raise LibvirtXMLError("Instance's XML is not initialized")
class LibvirtXML(LibvirtXMLBase):
"""
- Represents capabilities of libvirt
+ Handler of libvirt capabilities and nonspecific item operations.
"""
- # Cache this data upon first __new__ call
- _XML = None
+ def __init__(self, persistent=False, **virsh_dargs):
+ """
+ Initialize libvirt capabilities XML
- def __new__(cls):
- if cls._XML is None:
- cls._XML = virsh.capabilities()
- # older python super doesn't work on class objects
- return LibvirtXMLBase.__new__(cls)
+ @param: persistent: Use persistent virsh connection for this instance
+ @param: virsh_dargs: virsh keyword arguments
+ """
- def __init__(self):
- """Returns copy of libvirtd capabilities XML"""
- # protect against accidental modification
- super(LibvirtXML, self).__init__(self._XML)
+ super(LibvirtXML, self).__init__(None, persistent, **virsh_dargs)
+ self.init_XML(self.virsh.capabilities())
+
+class VMXML(LibvirtXMLBase):
+ """
+ Handler of VM related XML operations
+ """
+
+ def __init__(self, vm_name=None, persistent=False, **virsh_dargs):
+ """
+ Initialize VM's XML, optionally loading from existing
+
+ @param: vm_name: Initialize from virsh dumpxml vm_name
+ @param: persistent: Use persistent virsh connection for this instance
+ @param: virsh_dargs: Keyword virsh arguments
+ """
+
+ super(VMXML, self).__init__(persistent, **virsh_dargs)
+ if vm_name is not None:
+ super(VMXML, self).init_xml(self.virsh.dumpxml(vm_name))
+
+
+ def rename(self, new_name):
+ """
+ Rename VM and destroy UUID, optionally redefining with libvirt.
+
+ @param: new_name: The new name for the vm.
+ @param: commit: If True, redefine domain with virsh
+ """
+
+ self.check_xml_init()
+ old_name = self.find('name').text
+ if old_name == new_name:
+ raise LibvirtXMLError("Can't rename VM to the same name")
+ self.find('name').text = new_name
+ self.remove_by_xpath('uuid')
+ self.write()
+
+
+ def commit(self):
+ """
+ Define VM with virsh from XML
+
+ @param: force: Undefine existing VM if True, or raise LibvirtXMLError
+ """
+ self.check_xml_init()
+ # name is xml filename
+ if not self.virsh.define(self.name):
+ raise LibvirtXMLError("Failed to define VM %s" % vm_name)
--
1.7.4.1
_______________________________________________
Autotest-kernel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/autotest-kernel