Test below 2 APIs in this case: networkLookupByUUIDString/networkLookupByUUID --- repos/network/network_uuid.py | 68 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 10 deletions(-) diff --git a/repos/network/network_uuid.py b/repos/network/network_uuid.py index 02a104c..8f2ca83 100644 --- a/repos/network/network_uuid.py +++ b/repos/network/network_uuid.py @@ -1,13 +1,18 @@ #!/usr/bin/env python -# To test "virsh net-uuid" command +#To test "virsh net-uuid" command and related APIs +#To test 2 APIs in this case: +# networkLookupByUUIDString +# networkLookupByUUID import os import sys import re import commands - +import binascii import libvirt + from libvirt import libvirtError +from xml.dom import minidom from src import sharedmod @@ -15,6 +20,7 @@ required_params = ('networkname',) optional_params = {} VIRSH_NETUUID = "virsh net-uuid" +NWPATH = "/etc/libvirt/qemu/networks/" def check_network_exists(conn, networkname, logger): """ check if the network exists, may or may not be active """ @@ -29,25 +35,43 @@ def check_network_exists(conn, networkname, logger): def check_network_uuid(networkname, UUIDString, logger): """ check UUID String of a network """ - status, ret = commands.getstatusoutput(VIRSH_NETUUID + ' %s' % networkname) + status, ret = commands.getstatusoutput(VIRSH_NETUUID + ' %s' \ +% networkname) if status: - logger.error("executing "+ "\"" + VIRSH_NETUUID + ' %s' % networkname + "\"" + " failed") + logger.error("executing "+ "\"" + VIRSH_NETUUID + ' %s' % networkname\ + + "\"" + " failed") logger.error(ret) return False else: UUIDString_virsh = ret[:-1] logger.debug("UUIDString from API is %s" % UUIDString) - logger.debug("UUIDString from " + "\"" + VIRSH_NETUUID + "\"" " is %s" % UUIDString_virsh) + logger.debug("UUIDString from " + "\"" + VIRSH_NETUUID + "\"" " is %s"\ + % UUIDString_virsh) if UUIDString_virsh == UUIDString: return True else: return False +def checking_uuid(logger,nwname,nwuuid): + """ compare two UUIDs, one is from API, another is from network XML""" + global NWPATH + NWPATH = NWPATH + nwname + ".xml" + xml = minidom.parse(NWPATH) + network = xml.getElementsByTagName('network')[0] + uuid = network.getElementsByTagName('uuid')[0].childNodes[0].data + if uuid == nwuuid: + return True + else: + return False -def netuuid(params): - """ call appropriate API to generate the UUIDStirng - of a network , then compared to the output of command - virsh net-uuid +def network_uuid(params): + """ 1.call appropriate API to generate the UUIDStirng + of a network , then compared to the output of command + virsh net-uuid + 2.check below 2 new APIs: + networkLookupByUUIDString + networkLookupByUUID """ + global NWPATH logger = params['logger'] networkname = params['networkname'] @@ -61,7 +85,31 @@ def netuuid(params): try: UUIDString = netobj.UUIDString() - logger.info("the UUID string of network %s is %s" % (networkname, UUIDString)) + + #For a transient network, set another path + if not netobj.isPersistent() == 1: + NWPATH = "/var/run/libvirt/network/" + + logger.info("the UUID string of network \"%s\" is \"%s\""\ + % (networkname, UUIDString)) + #allowing '-' and ' ' anywhere between character pairs, just + #check one of them. + UUIDString1 = UUIDString.replace("-"," ") + network1 = conn.networkLookupByUUIDString(UUIDString1) + nw_name1 = network1.name() + logger.debug("The given UUID is \"%s\", the network is \"%s\" using\ + networkLookupByUUIDString" %(UUIDString1,nw_name1)) + + UUIDString2 = UUIDString.replace("-","") + UUID_ascii = binascii.a2b_hex(UUIDString2) + network2 = conn.networkLookupByUUID(UUID_ascii) + nw_name2 = network2.name() + logger.debug("The given UUID is \"%s\", the network is \"%s\" using \ +networkLookupByUUID" %(UUIDString2,nw_name2)) + + if nw_name1 == nw_name2 and checking_uuid(logger,nw_name1,UUIDString): + logger.info("Successed to get network name \"%s\" using \"%s\""\ + %(nw_name1,UUIDString)) if check_network_uuid(networkname, UUIDString, logger): logger.info(VIRSH_NETUUID + " test succeeded.") -- 1.8.3.1 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list