Signed-off-by: Yu Mingfei <[email protected]>
---
libvirt/tests/virsh_net_autostart.py | 96 ++++++++++++++++++++++++++++++++++++
1 file changed, 96 insertions(+)
create mode 100644 libvirt/tests/virsh_net_autostart.py
diff --git a/libvirt/tests/virsh_net_autostart.py
b/libvirt/tests/virsh_net_autostart.py
new file mode 100644
index 0000000..ef2aed6
--- /dev/null
+++ b/libvirt/tests/virsh_net_autostart.py
@@ -0,0 +1,96 @@
+import logging
+from autotest.client.shared import error
+from virttest import virsh, libvirt_vm
+from virttest.libvirt_xml import network_xml
+
+
+def run_virsh_net_autostart(test, params, env):
+ """
+ Test command: virsh net-autostart.
+ """
+ # Gather test parameters
+ uri = libvirt_vm.normalize_connect_uri(params.get("connect_uri",
+ "default"))
+ status_error = "yes" == params.get("status_error", "no")
+ net_ref = params.get("net_autostart_net_ref", "netname")
+ disable = "yes" == params.get("net_autostart_disable", "no")
+ extra = params.get("net_autostart_extra", "") # extra cmd-line params.
+
+ # make easy to maintain
+ virsh_dargs = {'uri':uri, 'debug':False, 'ignore_status':True}
+ virsh_instance = virsh.VirshPersistent(**virsh_dargs)
+
+ # Prepare environment and record current net_state_dict
+ backup = network_xml.NetworkXML.new_all_networks_dict(virsh_instance)
+ backup_state = virsh_instance.net_state_dict()
+ logging.debug("Backed up network(s): %s", backup_state)
+
+ try:
+ default_xml = backup['default']
+ except (KeyError, AttributeError):
+ raise error.TestNAError("Test requires default network to exist")
+
+ # Prepare default property for test
+ default_xml['persistent'] = True
+ netname = "default"
+ netuuid = default_xml.uuid
+ # disable autostart for test firstly
+ default_xml['active'] = False
+
+ # reopen it after command is been executed
+ del default_xml
+ virsh_instance.close_session()
+
+ # Prepare options and arguments
+ if net_ref == "netname":
+ net_ref = netname
+ elif net_ref == "netuuid":
+ net_ref = netuuid
+
+ if disable:
+ net_ref += " --disable"
+
+ # Run test case
+ result = virsh.net_autostart(net_ref, extra, **virsh_dargs)
+ logging.debug(result)
+ status = result.exit_status
+
+ # Check if autostart or disable is successful with libvirtd restart.
+ libvirt_vm.libvirtd_restart()
+ virsh_instance = virsh.VirshPersistent(**virsh_dargs)
+
+ # reopen default_xml
+ currents = network_xml.NetworkXML.new_all_networks_dict(virsh_instance)
+ current_state = virsh_instance.net_state_dict()
+ logging.debug("Current network(s): %s", current_state)
+ default_xml = currents['default']
+ is_active = default_xml['active']
+
+ del default_xml
+
+ # Recover environment
+ leftovers = network_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)
+ virsh_instance.close_session()
+
+ # Check Result
+ if status_error:
+ if status == 0:
+ raise error.TestFail("Run successfully with wrong command!")
+ else:
+ if disable:
+ if status or is_active:
+ raise error.TestFail("Disable autostart failed.")
+ else:
+ if status or (not is_active):
+ raise error.TestFail("Set network autostart failed.")
--
1.7.11.7
_______________________________________________
Virt-test-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virt-test-devel