On 08/29/2012 10:42 PM, Yu Mingfei wrote:
Signed-off-by: Yu Mingfei<[email protected]>
---
  client/tests/libvirt/tests/virsh_net_create.py |   73
++++++++++++++++++++++++
  1 files changed, 73 insertions(+), 0 deletions(-)
  create mode 100644 client/tests/libvirt/tests/virsh_net_create.py

diff --git a/client/tests/libvirt/tests/virsh_net_create.py
b/client/tests/libvirt/tests/virsh_net_create.py
new file mode 100644
index 0000000..e00ae72
--- /dev/null
+++ b/client/tests/libvirt/tests/virsh_net_create.py
@@ -0,0 +1,73 @@
+import re, logging, tempfile
+from autotest.client.shared import utils, error
+from autotest.client.virt import libvirt_vm, libvirt_xml
+
+
+def run_virsh_net_create(test, params, env):
+    """
+    Test command: virsh net-create.
+
+    1) Create a new network's config file from a source file.
+    2) Check current network environment for new network.
+    3) Stop libvirtd if test need.
+    4) Run test.
+    5) Recover libvirtd and network if test need.
+    6) Check result.
+    """

Good docstring. Need one extra blank line between docstring and beginning of code (comment). I make this mistake all the time too.

+    #Create network's xml file
+    source_file = params.get("source_file",
"/etc/libvirt/qemu/networks/default.xml")
+    net_name = params.get("net_name", "default")
+    net_uuid = params.get("net_uuid", "")
+
+    new_net_xml = tempfile.mktemp(dir="/tmp")

I don't think it's necessary to have another tempfile, just use the NetworkXML (XMLTreeFile). It's already a backup.

+
+    network_xml = libvirt_xml.NetworkXML(source_file)
+    network_xml.general_metadata_config(net_name, net_uuid)
+    #TODO:other configuration
+    network_xml.write(new_net_xml)
+
+    #Prepare network environment
+    list_output = libvirt_vm.virsh_net_list("",
print_info=True).stdout.strip()
+    if re.search(net_name, list_output):
+        libvirt_vm.virsh_net_destroy(net_name, print_info=True)
+
+    #Prepare libvirtd status
+    libvirtd = params.get("libvirtd", "on")
+    if libvirtd == "off":
+        libvirt_vm.service_libvirtd_control("stop")
+
+    #Run test case

Comments are a little out-of-whack here. Anyway, I think we should grab all parameters before 'prepare libvirtd status', just in case an exception gets thrown...it makes trouble for other tests.

+    options_ref = params.get("options_ref", "default")
+    extra = params.get("extra", "")
+    if options_ref == "exist_file":
+        options_ref = new_net_xml + extra
+
+    result = libvirt_vm.virsh_net_create(options_ref, extra,
ignore_status=True, print_info=True)

I'd really like it if a class in the libvirt_xml module could help hide this virsh_() call. Maybe it would be a method of LibvirtXML class that accepts NetworkXML instance as parameter?

I think maybe a lot of the above could be boiled down into (for example) something like:

libvirt = libvirt_xml.LibvirtXML()
libvirt.replace_default_network(libvirt_xml.NetworkXML(source_file))

But I'm fine if you disagree and/or want to leave this for possible future enhancement. Just an idea that came to mind.

+    status = result.exit_status
+    output = result.stdout.strip()
+
+    #Recover libvirtd service start
+    if libvirtd == "off":
+        libvirt_vm.service_libvirtd_control("start")
+
+    #Recover network
+    list_output = libvirt_vm.virsh_net_list("",
print_info=True).stdout.strip()
+    if re.search(net_name, list_output) and net_name != "default":
+        libvirt_vm.virsh_net_destroy(net_name, print_info=True)

This seems to be duplicated code to some above, can we have a little function for this? Do you think it would be useful general function to put into libvirt_vm or virsh module? It's fine if not, just another idea.

+
+    list_output = libvirt_vm.virsh_net_list("",
print_info=True).stdout.strip()
+    if not re.search("default", list_output):
+        libvirt_vm.virsh_net_create(source_file, print_info=True)

Eeek! again. Definitely need a function for these. Suggestion- Maybe make function accept callable parameter, so virsh_net_destroy or virsh_net_create method can be passed in as parameter. Mmmm, maybe that won't work.

+
+    #Check Result
+    status_error = params.get("status_error", "no")
+    addition_status_error = params.get("addition_status_error", "no")
+    status_error = (status_error == "no") and (addition_status_error ==
"no")
+    if not status_error:
+        if status == 0:
+            raise error.TestFail("Run successful with wrong command!")
+    else:
+        if status != 0:
+            raise error.TestFail("Run failed with right command.")
+        if not re.search(net_name, output):
+            raise error.TestFail("Run successful but result is not
expected.")

Hmmm, the above seems to be a repeating pattern in many libvirt tests. Can we make a standardized function/method/class for it somewhere? Just another idea :)


--
Chris Evich, RHCA, RHCE, RHCDS, RHCSS
Quality Assurance Engineer
e-mail: cevich + `@' + redhat.com o: 1-888-RED-HAT1 x44214

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

Reply via email to