+ 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.")