Signed-off-by: Yu Mingfei <[email protected]>
---
 libvirt/tests/virsh_dumpxml.py | 98 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 98 insertions(+)
 create mode 100644 libvirt/tests/virsh_dumpxml.py

diff --git a/libvirt/tests/virsh_dumpxml.py b/libvirt/tests/virsh_dumpxml.py
new file mode 100644
index 0000000..a4759fb
--- /dev/null
+++ b/libvirt/tests/virsh_dumpxml.py
@@ -0,0 +1,98 @@
+import re, logging
+from autotest.client.shared import error
+from virttest import virsh
+
+
+def run_virsh_dumpxml(test, params, env):
+    """
+    Test command: virsh dumpxml.
+    This version contains a common option(--inactive) only.
+    TODO: to support some options like --security-info, --update-cpu
+
+    1) Prepare parameters.
+    2) Set options of virsh dumpxml.
+    3) Prepare environment: vm_state, etc.
+    4) Run dumpxml command.
+    5) Recover environment.
+    6) Check result.
+    """
+    def is_dumpxml_of_running_vm(dumpxml, domid):
+        """
+        To check whether the dumpxml is got during vm is running.
+
+        @param dumpxml: the output of virsh dumpxml.
+        @param domid: the id of vm
+        """
+        match_string = "<domain.*id='%s'/>" % domid
+        if re.search(dumpxml, match_string):
+            return True
+        return False
+
+
+    # Prepare parameters
+    vm_name = params.get("main_vm")
+    vm = env.get_vm(vm_name)
+
+    vm_ref = params.get("dumpxml_vm_ref", "domname")
+    options_ref = params.get("dumpxml_options_ref", "")
+    options_suffix = params.get("dumpxml_options_suffix", "")
+    vm_state = params.get("dumpxml_vm_state", "running")
+    status_error = params.get("status_error", "no")
+    domuuid = vm.get_uuid()
+    domid = vm.get_id()
+
+    # Prepare vm state for test
+    if vm_state == "shutoff" and vm.is_alive():
+        vm.destroy() # Confirm vm is shutoff
+
+    if vm_ref == "domname":
+        vm_ref = vm_name
+    elif vm_ref == "domid":
+        vm_ref = domid
+    elif vm_ref == "domuuid":
+        vm_ref = domuuid
+    elif vm_ref == "hex_id":
+        if domid == "-":
+            vm_ref = domid
+        else:
+            vm_ref = hex(int(domid))
+
+    if options_ref:
+        vm_ref = "%s %s" % (vm_ref, options_ref)
+
+    if options_suffix:
+        vm_ref = "%s %s" % (vm_ref, options_suffix)
+
+    # Run command
+    logging.info("Command:virsh dumpxml %s", vm_ref)
+    try:
+        output = virsh.dumpxml(vm_ref)
+        status = 0
+    except error.CmdError, detail:
+        status = 1
+        output = detail
+    logging.debug("virsh dumpxml result:\n%s", output)
+
+    # Recover vm state
+    if vm_state == "paused":
+        vm.resume()
+    if vm.is_alive():
+        vm.destroy()
+
+    # Check result
+    if status_error == "yes":
+        if status == 0:
+            raise error.TestFail("Run successfully with wrong command.")
+    elif status_error == "no":
+        if status:
+            raise error.TestFail("Run failed with right command.")
+        else:
+            if options_ref == "--inactive":
+                if is_dumpxml_of_running_vm(output, domid):
+                    raise error.TestFail("Got dumpxml for active vm "
+                                         "with --inactive option!")
+            else:
+                if (vm_state == "shutoff"
+                    and is_dumpxml_of_running_vm(output, domid)):
+                    raise error.TestFail("Got dumpxml for active vm "
+                                         "when vm is shutoff.")
-- 
1.7.11.7

_______________________________________________
Virt-test-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virt-test-devel

Reply via email to