As I said, virsh domname case should receive different id or uuid
rather than different domain name, so I thinks it's irrelevant with
this case.

In addition, as we discussed, if you want to operate XML, you should
spit it into libvirt_xml relevant module, I think it should be common
function for XML's adding, deleting, searching, modifying.

Alex

----- Original Message -----
From: "Yu Mingfei" <yuming...@cn.fujitsu.com>
To: "Chris Evich" <cev...@redhat.com>, "Lucas Meneghel Rodrigues" 
<l...@redhat.com>, "Alex Jia" <a...@redhat.com>
Cc: autotest@test.kernel.org
Sent: Tuesday, July 10, 2012 8:59:34 AM
Subject: [PATCH 1/3]Add virsh domname testcases for libvirt

This patch add a function rename to VM class.
Rename() changes VM's name to a new name.

Signed-off-by: Yu Mingfei <yuming...@cn.fujitsu.com>
---
  client/virt/libvirt_vm.py |   60 +++++++++++++++++++++++++++++++++++++++++++++
  1 files changed, 60 insertions(+), 0 deletions(-)

diff --git a/client/virt/libvirt_vm.py b/client/virt/libvirt_vm.py
index d97ac0c..e2d0daf 100644
--- a/client/virt/libvirt_vm.py
+++ b/client/virt/libvirt_vm.py
@@ -1597,3 +1597,63 @@ class VM(virt_vm.BaseVM):
          Override BaseVM restore_from_file method
          """
          virsh_restore(self.name, path, uri=self.connect_uri)
+
+
+    def rename(self, newname):
+        """
+        Rename this vm.
+        @param newname:new name of vm.
+        @return:True or False
+        """
+        def rename_xml_utils(xml_path, newname):
+            """
+            Modify name in an xml file.
+            @param xml_path:xml file path
+            """
+            dom = minidom.parse(xml_path)
+            root = dom.documentElement
+            node_name = root.getElementsByTagName("name")[0]
+            node_name.firstChild.data = "%s" % newname
+            node_uuid = root.getElementsByTagName("uuid")[0]
+            root.removeChild(node_uuid)
+            f=open(xml_path, "w")
+            dom.writexml(f)
+            f.close()
+
+        if self.name == newname:
+            logging.error("Error:Please give a different name with current.")
+            return False
+
+        #Create a new xml file for new vm
+        new_vm_tmp_xml = self.backup_xml()
+        try:
+            rename_xml_utils(new_vm_tmp_xml, newname)
+        except Exception, detail:
+            logging.error("Error:Edit xml failed: %s", detail)
+            if os.path.exists(new_vm_tmp_xml):
+                os.remove(new_vm_tmp_xml)
+            return False
+
+        #Undefine old vm and define new vm
+        old_vm_tmp_xml = self.backup_xml()
+        if not self.undefine():
+            logging.error("Error:Undefine %s failed.", self.name)
+            if os.path.exists(new_vm_tmp_xml):
+                os.remove(new_vm_tmp_xml)
+            if os.path.exists(old_vm_tmp_xml):
+                os.remove(old_vm_tmp_xml)
+            return False
+        if not self.define(new_vm_tmp_xml):
+            logging.error("Error:Define %s failed.", newname)
+            self.define(old_vm_tmp_xml)
+            return False
+        logging.debug("Rename %s to %s succeed.", self.name, newname)
+        self.name = newname
+
+        #delete tmp files
+        if os.path.exists(new_vm_tmp_xml):
+            os.remove(new_vm_tmp_xml)
+        if os.path.exists(old_vm_tmp_xml):
+            os.remove(old_vm_tmp_xml)
+        return True
+
-- 1.7.1

_______________________________________________
Autotest mailing list
Autotest@test.kernel.org
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest

Reply via email to