Signed-off-by: Li Yang <[email protected]>
---
 libvirt/tests/cfg/virsh_cpu_compare.cfg |   38 ++++++++++++++
libvirt/tests/virsh_cpu_compare.py | 82 +++++++++++++++++++++++++++++++
 2 files changed, 120 insertions(+), 0 deletions(-)
 create mode 100644 libvirt/tests/cfg/virsh_cpu_compare.cfg
 create mode 100644 libvirt/tests/virsh_cpu_compare.py

diff --git a/libvirt/tests/cfg/virsh_cpu_compare.cfg b/libvirt/tests/cfg/virsh_cpu_compare.cfg
new file mode 100644
index 0000000..7b78f3d
--- /dev/null
+++ b/libvirt/tests/cfg/virsh_cpu_compare.cfg
@@ -0,0 +1,38 @@
+- virsh_cpu_compare:
+    type = virsh_cpu_compare
+    take_regular_screendumps = "no"
+    cpu_compare_ref = "file"
+    cpu_compare_extra = ""
+    cpu_compare_mode = ""
+    cpu_compare_file_name = cpu.xml
+    variants:
+        - normal_test:
+            status_error = "no"
+        - error_test:
+            status_error = "yes"
+            variants:
+                - no_option:
+                    cpu_compare_ref = ""
+                - modified_option:
+                    cpu_compare_mode = "modify"
+                - no_detail_option:
+                    cpu_compare_mode = "clear"
+                - extra_option:
+                    cpu_compare_extra = "xyz"
+                - invalid_option:
+                    cpu_compare_extra = "--xyz"
+                - invalid_ref_option:
+                    cpu_compare_ref = "xyz"
+    variants:
+        - host_cpu:
+            cpu_compare_target = "host"
+            vms = ""
+            main_vm = ""
+            kill_vm = "no"
+            kill_unresponsive_vms = "no"
+            encode_video_files = "no"
+            skip_image_processing = "yes"
+        - guest_cpu:
+            cpu_compare_target = "guest"
+            start_vm = "no"
+            kill_vm = "yes"
diff --git a/libvirt/tests/virsh_cpu_compare.py b/libvirt/tests/virsh_cpu_compare.py
new file mode 100644
index 0000000..2654d49
--- /dev/null
+++ b/libvirt/tests/virsh_cpu_compare.py
@@ -0,0 +1,82 @@
+import codecs, os
+from autotest.client.shared import error
+from virttest import virsh
+from xml.dom.minidom import parseString
+
+
+def run_virsh_cpu_compare(test, params, env):
+    """
+    Test command: virsh cpu-compare.
+
+    Compare host CPU with a CPU described by an XML file.
+    1.Get all parameters from configuration.
+    2.Prepare temp file saves  CPU infomation.
+    3.Perform virsh cpu-compare operation.
+    4.Confirm the result.
+
+    """
+
+    def get_cpu_xml(target, mode, tmp_file):
+        """
+        Get CPU infomation and put it into a file.
+
+        @param: target: Test target, host or guest's cpu description.
+        @param: mode: Test mode, decides file's detail.
+        @param: tmp_file: File saves CPU infomation.
+        """
+        file = codecs.open(tmp_file, 'wb', encoding='utf-8')
+        if target == "host":
+            domxml = virsh.capabilities()
+        else:
+            domxml = virsh.dumpxml(vm_name)
+        dom = parseString(domxml)
+        cpu_node = dom.getElementsByTagName('cpu')[0]
+        if mode == "modify":
+            vendor = cpu_node.getElementsByTagName('vendor')[0]
+            for node in vendor.childNodes:
+                if node.nodeType == node.TEXT_NODE:
+                    vendor.removeChild(node)
+                    break
+            text_node = dom.createTextNode('test_vendor')
+            vendor.appendChild(text_node)
+            cpu_node.writexml(file)
+        elif mode == "clear":
+            # Clear up file detail
+            file.truncate(0)
+        else:
+            cpu_node.writexml(file)
+        file.close()
+        dom.unlink()
+
+
+    # Get all parameters.
+    ref = params.get("cpu_compare_ref")
+    mode = params.get("cpu_compare_mode", "")
+    target = params.get("cpu_compare_target", "host")
+    status_error = params.get("status_error", "no")
+    extra = params.get("cpu_compare_extra", "")
+    file_name = params.get("cpu_compare_file_name", "cpu.xml")
+    tmp_file = os.path.join(test.tmpdir, file_name)
+    if target == "guest":
+        vm_name = params.get("main_vm")
+        vm = env.get_vm(vm_name)
+        if vm.is_alive():
+            vm.destroy()
+
+    # Prepare temp file.
+    get_cpu_xml(target, mode, tmp_file)
+
+    if ref == "file":
+        ref = tmp_file
+    ref = "%s %s" % (ref, extra)
+
+    # Perform virsh cpu-compare operation.
+ status = virsh.cpu_compare(ref, ignore_status=True, debug=True).exit_status
+
+    # 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]
--------------------------------------------------



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

Reply via email to