From: liyang <[email protected]>

Signed-off-by: Li Yang <[email protected]>
---
 libvirt/tests/cfg/virsh_cpu_compare.cfg | 31 ++++++++++++++
 libvirt/tests/virsh_cpu_compare.py      | 72 +++++++++++++++++++++++++++++++++
 2 files changed, 103 insertions(+)
 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..7fd9184
--- /dev/null
+++ b/libvirt/tests/cfg/virsh_cpu_compare.cfg
@@ -0,0 +1,31 @@
+- virsh_cpu_compare:
+    type = virsh_cpu_compare
+    vms = ""
+    main_vm = ""
+    kill_vm = "no"
+    kill_unresponsive_vms = "no"
+    encode_video_files = "no"
+    skip_image_processing = "yes"
+    take_regular_screendumps = "no"
+    cpu_compare_ref = "file"
+    cpu_compare_extra = ""
+    cpu_compare_mode = ""
+    cpu_compare_file_name = "cpu.xml"
+    variants:
+        - expected_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"
diff --git a/libvirt/tests/virsh_cpu_compare.py 
b/libvirt/tests/virsh_cpu_compare.py
new file mode 100644
index 0000000..7b36bd2
--- /dev/null
+++ b/libvirt/tests/virsh_cpu_compare.py
@@ -0,0 +1,72 @@
+import commands, 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 net-compare operation.
+    4.Confirm the result.
+
+    """
+
+    def get_cpu_xml(mode, tmp_file):
+        """
+        Get CPU infomation and put it into a file.
+
+        @param: mode: Test mode, decides file's detail.
+        @param: tmp_file: File saves CPU infomation.
+        """
+        file = codecs.open(tmp_file, 'wb', encoding='utf-8')
+        domxml = virsh.capabilities()
+        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", "")
+    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)
+
+    # Prepare temp file.
+    get_cpu_xml(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.8.2

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

Reply via email to