On 02/22/2013 02:51 PM, Yu Mingfei wrote:
Signed-off-by: Yu Mingfei<[email protected]>
---
libvirt/tests/virsh_net_define.py | 140 ++++++++++++++++++++++++++++++++++++++
1 file changed, 140 insertions(+)
create mode 100644 libvirt/tests/virsh_net_define.py
diff --git a/libvirt/tests/virsh_net_define.py
b/libvirt/tests/virsh_net_define.py
new file mode 100644
index 0000000..7a98c5c
--- /dev/null
+++ b/libvirt/tests/virsh_net_define.py
@@ -0,0 +1,140 @@
+import logging
+from autotest.client.shared import error
+from virttest import virsh, libvirt_vm, xml_utils, libvirt_xml
+
+
+def get_network_xml_instance(virsh_dargs, test_xml, net_name,
+ net_uuid, bridge):
+ test_netxml = libvirt_xml.NetworkXML(
+ virsh_instance=virsh.Virsh(**virsh_dargs))
+ test_netxml.xml = test_xml.name
+
+ # modify XML if called for
+ if net_name is not "":
A shorter code is 'if net_name'.
+ test_netxml.name = net_name
+ else:
+ test_netxml.name = "default"
+ if net_uuid is not "":
Same as above.
+ test_netxml.uuid = net_uuid
+ else:
+ del test_netxml.uuid # let libvirt auto-generate
+ if bridge is not None:
Same as above.
+ test_netxml.bridge = bridge
+
+ # TODO: Test other network parameters
+
+ logging.debug("Modified XML:")
+ test_netxml.debug_xml()
+ return test_netxml
+
+
+def run_virsh_net_define(test, params, env):
+ """
+ Test command: virsh net-define.
+
+ 1) Collect parameters&environment info before test
+ 2) Prepare options for command
+ 3) Execute command for test
+ 4) Check state of defined network
As usual, if you successfully define a virtual network then libvirt will
put the network XML configuration file under the
/etc/libvirt/qemu/networks/,
so you should also check if the $network.xml file exists.
In addition, libvirt supports different kinds of virtual network such as
NAT based network,
Rotuted network, and Isolated network, etc, you should also check if
they can be successfully
defined, for more checkpoints and details, please refer to
http://libvirt.org/formatnetwork.html.
+ 5) Recover environment
+ 6) Check result
+ """
+ uri = libvirt_vm.normalize_connect_uri(params.get("connect_uri",
+ "default"))
+ net_name = params.get("net_define_net_name", "default")
+ net_uuid = params.get("net_define_net_uuid")
+ options_ref = params.get("net_define_options_ref", "default")
+ extra_args = params.get("net_define_extra_arg", "")
+ remove_existing = params.get("net_define_remove_existing", "yes")
+ libvirtd = params.get("libvirtd", "on")
+ status_error = params.get("status_error", "no")
+
+ virsh_dargs = {'uri':uri, 'debug':False, 'ignore_status':True}
+ virsh_instance = virsh.VirshPersistent(**virsh_dargs)
+
+ # Prepare environment and record current net_state_dict
+ backup = libvirt_xml.NetworkXML.new_all_networks_dict(virsh_instance)
+ backup_state = virsh_instance.net_state_dict()
+ logging.debug("Backed up network(s): %s", backup_state)
+
+ # Make some XML to use for testing, for now we just copy 'default'
+ test_xml = xml_utils.TempXMLFile() # temporary file
+ try:
+ # LibvirtXMLBase.__str__ returns XML content
+ test_xml.write(str(backup['default']))
+ test_xml.flush()
+ except (KeyError, AttributeError):
+ raise error.TestNAError("Test requires default network to exist")
+
+ testnet_xml = get_network_xml_instance(virsh_dargs, test_xml, net_name,
+ net_uuid, bridge=None)
+
+ if remove_existing:
+ for netxml in backup.values():
+ netxml.orbital_nuclear_strike()
+
+ if options_ref == "file_arg":
+ options_ref = testnet_xml.xml
+ elif options_ref == "no_file":
+ options_ref = ""
+ elif options_ref == "not_exist_file":
+ options_ref = "/not/exist/file"
+
+ #Run test case
+
+ # Prepare libvirtd status
+ if libvirtd == "off":
+ virsh_instance.close_session() # all virsh commands will probably fail
+ libvirt_vm.service_libvirtd_control("stop")
+
+ define_result = virsh.net_define(options_ref, extra_args, **virsh_dargs)
+ logging.debug(define_result)
+ define_status = define_result.exit_status
+
+ # If defining network succeed, then trying to start it.
+ if define_status == 0:
+ start_result = virsh.net_start(net_name, extra="", **virsh_dargs)
+ logging.debug(start_result)
+ start_status = start_result.exit_status
+
+ # Recover libvirtd service start
+ if libvirtd == "off":
+ libvirt_vm.service_libvirtd_control("start")
+ virsh_instance.new_session()
+
+ # Done with file, cleanup
+ del test_xml
+ del testnet_xml
+
+ # Recover environment
+ leftovers = libvirt_xml.NetworkXML.new_all_networks_dict(virsh_instance)
+ for netxml in leftovers.values():
+ netxml.orbital_nuclear_strike()
+
+ # Recover from backup
+ for netxml in backup.values():
+ netxml.create()
+ # autostart = True requires persistent = True first!
+ for state in ['active', 'persistent', 'autostart']:
+ netxml[state] = backup_state[netxml.name][state]
+
+ # Close down persistent virsh session (including for all netxml copies)
+ if hasattr(virsh_instance, 'close_session'):
+ virsh_instance.close_session()
+
+ #check status_error
+ if status_error == "yes":
+ if define_status == 0:
+ if start_status == 0:
+ raise error.TestFail("Define an unexpected network, "
+ "and start it successfully.")
+ else:
+ raise error.TestFail("Define an unexpected network, "
+ "but start it failed.")
+ elif status_error == "no":
+ if define_status != 0:
+ raise error.TestFail("Run failed with right command")
+ else:
+ if start_status != 0:
+ raise error.TestFail("Network is defined as expected, "
+ "but start it failed.")
_______________________________________________
Virt-test-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virt-test-devel