Signed-off-by: Li Yang <[email protected]>
---
.../libvirt/tests/virsh_domxml_from_native.py | 112 ++++++++++++++++++++
 1 files changed, 112 insertions(+), 0 deletions(-)
 create mode 100644 client/tests/libvirt/tests/virsh_domxml_from_native.py

diff --git a/client/tests/libvirt/tests/virsh_domxml_from_native.py b/client/tests/libvirt/tests/virsh_domxml_from_native.py
new file mode 100644
index 0000000..2175af5
--- /dev/null
+++ b/client/tests/libvirt/tests/virsh_domxml_from_native.py
@@ -0,0 +1,112 @@
+import re, os, os.path, logging, commands
+from autotest.client.shared import utils, error
+from autotest.client.virt import  libvirt_vm, virsh
+
+
+def run_virsh_domxml_from_native(test, params, env):
+    """
+    Test command: virsh domxml-from-native.
+
+    Convert native guest configuration format to domain XML format.
+    1.Prepare test environment.
+    2.When the ibvirtd == "off", stop the libvirtd service.
+    3.Run the autotest.
+    4.Recover test environment.(If the libvirtd service is stopped ,start
+      the libvirtd service.)
+    5.Confirm the test result.
+    """
+    vm_name = params.get("main_vm", "vm1")
+    vm = env.get_vm(params["main_vm"])
+    vm.verify_alive()
+
+    def virsh_convxml(vm_name, guest_args):
+        """
+        Put dumpxml vm'infomation to a file
+
+        @param: vm_name : VM's name
+        @param: guest_args : File which will save config infomation.
+        """
+        # using command instance because code is shorter
+ grep_command = "ps ax | grep qemu |grep -v grep | grep %s" % vm_name
+        sed_command = "| sed -e  's/^\s\+//g' | cut -d' ' -f1"
+        command = grep_command + sed_command
+        guest, PID = commands.getstatusoutput(command)
+        command = "ps -p %s -o args | sed -e 's/COMMAND//g'" % PID
+        guest, cmdline = commands.getstatusoutput(command)
+        # File will be created if not exist
+        file = open(guest_args,'w')
+        file.write(cmdline)
+        file.close()
+        command = "sed -i 's/ -drive [^ ]* / /g' %s" % guest_args
+        commands.getstatusoutput(command)
+
+    def put_vm_info_to_file(vm_name, vm_ref, guest_args,
+                            virsh_domxml_file_exists):
+        """
+        Put vm'infomation to a file when file exists.
+
+        @param: vm_name : VM's name.
+        @param: vm_ref : Decide 'domxml-from-native' conmand's options.
+        @param: guest_args : File which will save config infomation.
+        @param: virsh_domxml_file_exists : Judge file exists or not.
+        """
+ if vm_ref == "file_guest_args" and virsh_domxml_file_exists != "no":
+            virsh_convxml(vm_name, guest_args)
+
+    def modify_ref(vm_ref, guest_args, invalid_guest_args,
+                   virsh_domxml_file_exists, status_error):
+        """
+        Motify vm_ref(virsh_domxml_from_native's options) according to
+        vm_ref(input) and return motified vm_ref
+
+        @param: vm_ref : Decide 'domxml-from-native' conmand's options.
+        @param: guest_args: 'domxml-from-native' conmand's options(file).
+        @param: args: 'domxml-from-native' conmand's invalid options.
+        @param: virsh_domxml_file_exists: Decide file exists or not.
+        @param: status_error: Test mode."no":normal test."yes":except test.
+        """
+ if vm_ref == "file_guest_args" and (virsh_domxml_file_exists != "" or
+           status_error == "no"):
+            vm_ref = "qemu-argv %s %s" % (guest_args, invalid_guest_args)
+ elif vm_ref == "file_guest_args" and virsh_domxml_file_exists == "":
+            vm_ref = "qemu-argv  %s"  % invalid_guest_args
+        return vm_ref
+
+ virsh_domxml_file_exists = params.get("virsh_domxml_file_exists","null")
+    libvirtd = params.get("libvirtd")
+    vm_ref = params.get("vm_ref")
+    guest_args = params.get("guest_args")
+    args = params.get("invalid_guest_args")
+    status_error = params.get("status_error")
+    #run test case
+
+    #put vm's infomation to a file
+ put_vm_info_to_file(vm_name, vm_ref, guest_args, virsh_domxml_file_exists)
+
+    #modify domxml-from-native's options
+    vm_ref = modify_ref(vm_ref, guest_args, args, virsh_domxml_file_exists,
+                        status_error)
+
+    #libvirtd off
+    if libvirtd == "off":
+        libvirt_vm.libvirtd_stop()
+        vm_ref = "qemu-argv %s" % guest_args
+
+    #Ignore exception with ignore_status=True.
+    status  = virsh.domxml_from_native(vm_ref, ignore_status=True)
+
+    #recover libvirtd service start
+    if libvirtd == "off":
+        libvirt_vm.libvirtd_start()
+
+    #clean up
+    if os.path.exists(guest_args):
+        os.remove(guest_args)
+
+    #check status_error
+    if status_error == "yes":
+        if status == 0:
+            raise error.TestFail("Run successfully with wrong command!")
+    elif status_error == "no":
+        if status != 0:
+            raise error.TestFail("Run failed with right command")
--
1.7.1

--
Regards,
--------------------------------------------------
Li Yang
TEL:+86+25-86630566-8526
EMail:[email protected]
--------------------------------------------------



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

Reply via email to