[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [test-API][PATCH] Modify repos/network/network_list.py and add network_list case to conf



Modify the old network_list.py. The new network_list.py covers all
flags of listAllNetworks and the following api. and add network_list to
basic_network.conf.
virNetwork:
name()
bridgeName()
isActive()
isPersistent()
virConnect:
listAllNetworks()
---
 cases/basic_network.conf      |   37 +++++-
 repos/network/network_list.py |  279 ++++++++++++++++++-----------------------
 2 files changed, 151 insertions(+), 165 deletions(-)

diff --git a/cases/basic_network.conf b/cases/basic_network.conf
index 991ad99..805cfd0 100644
--- a/cases/basic_network.conf
+++ b/cases/basic_network.conf
@@ -14,6 +14,16 @@ network:define
     netmode
         nat
 
+#VIR_CONNECT_LIST_NETWORKS_INACTIVE = 1
+#VIR_CONNECT_LIST_NETWORKS_ACTIVE = 2
+#VIR_CONNECT_LIST_NETWORKS_PERSISTENT = 4
+#VIR_CONNECT_LIST_NETWORKS_TRANSIENT = 8
+#VIR_CONNECT_LIST_NETWORKS_AUTOSTART = 16
+#VIR_CONNECT_LIST_NETWORKS_NO_AUTOSTART = 32
+network:network_list
+    flags
+	1
+
 network:start
     networkname
         $defaultnetname
@@ -24,10 +34,18 @@ network:autostart
     autostart
         enable
 
+network:network_list
+    flags
+	3
+
 network:destroy
     networkname
         $defaultnetname
 
+network:network_list
+    flags
+	4
+
 network:undefine
     networkname
         $defaultnetname
@@ -48,6 +66,10 @@ network:create
     netmode
         nat
 
+network:network_list
+    flags
+	8
+
 network:destroy
     networkname
         $defaultnetname
@@ -78,6 +100,10 @@ network:autostart
     autostart
         enable
 
+network:network_list
+    flags
+	16
+
 network:destroy
     networkname
         $defaultnetname
@@ -102,11 +128,14 @@ network:create
     netmode
         route
 
+network:network_list
+    flags
+	32
+
 network:destroy
     networkname
         $defaultnetname
 
-
 network:define
     networkname
         $defaultnetname
@@ -141,7 +170,6 @@ network:undefine
     networkname
         $defaultnetname
 
-
 network:create
     networkname
         $defaultnetname
@@ -162,8 +190,3 @@ network:destroy
     networkname
         $defaultnetname
 
-
-
-
-
-
diff --git a/repos/network/network_list.py b/repos/network/network_list.py
index 7c34f69..647da82 100644
--- a/repos/network/network_list.py
+++ b/repos/network/network_list.py
@@ -1,184 +1,147 @@
 #!/usr/bin/env python
 # To test "virsh net-list" command
 
-import os
-import sys
-import re
-import commands
-
 import libvirt
 from libvirt import libvirtError
 
 from src import sharedmod
 from utils import utils
 
-required_params = ('netlistopt',)
+required_params = ('flags',)
 optional_params = {}
 
-VIRSH_QUIET_NETLIST = "virsh --quiet net-list %s|awk '{print $1}'"
-VIRSH_NETLIST = "virsh net-list %s"
-GET_BRIDGE_IP = "/sbin/ifconfig %s | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'"
-CONFIG_DIR = "/etc/libvirt/qemu/networks/"
-
-def get_option_list(params):
-    """return options we need to test
-    """
-    logger = params['logger']
-    option_list=[]
-
-    value = params['netlistopt']
-
-    if value == 'all':
-        option_list = [' ', '--all', '--inactive']
-    elif value == '--all' or value == '--inactive':
-        option_list.append(value)
+VIRSH_NETWORK_LIST = "virsh net-list %s|sed -n '3,$'p|awk '{print $1}'"
+GET_BRIDGE_IP = "/sbin/ifconfig %s | grep 'inet addr:' | cut -d: -f2 | awk \
+                '{print $1}'"
+FLAGDICT = {1:" --inactive", 2:"", 4:" --persistent",\
+                8:" --transient", 16:" --autostart", 32:" --no-autostart" }
+CONFIG_DIR = "ls /etc/libvirt/qemu/networks/"
+
+def check_bridge_ip(bridgename):
+    """ Check if the bridge has ip """
+
+    (status, output) = utils.exec_cmd(GET_BRIDGE_IP % bridgename,\
+                                       shell=True)
+    if not status and utils.do_ping(output[0], 50):
+        logger.info("Bridge %s is active" % bridgename)
+        logger.info("%s has ip: %s" % (bridgename, output[0]))
+        return True
     else:
-        logger.error("value %s is not supported" % value)
-        return 1, option_list
-
-    return 0, option_list
-
-def get_output(logger, command, flag):
-    """execute shell command
-    """
-    status, ret = commands.getstatusoutput(command)
-    if not flag and status:
-        logger.error("executing "+ "\"" +  command  + "\"" + " failed")
-        logger.error(ret)
-    return status, ret
-
-def check_all_option(conn, logger):
-    """check the output of virsh net-list with --all option
-    """
-    all_network = []
-    entries = os.listdir(CONFIG_DIR)
-    logger.debug("%s in %s" % (entries, CONFIG_DIR))
-    status, network_names = get_output(logger, VIRSH_QUIET_NETLIST % '--all', 0)
-    if not status:
-        all_network = network_names.split('\n')
-        logger.info("all network is %s" % all_network)
+        logger.error("Bridge %s has no ip or fails to ping" % bridgename)
+        return False
+
+def check_persistent_netxml(networkname):
+    """ Check if the network is persistent via checking network xml dir """
+
+    (status, output) = utils.exec_cmd(CONFIG_DIR, shell=True)
+    network_list_dir = []
+    if status:
+        logger.error("Executing " + CONFIG_DIR + " failed")
+        logger.error(output)
+        return False
     else:
-        return 1
-
-    if all_network == ['']:
-        return 0
-
-    for entry in entries:
-        if not entry.endswith('.xml'):
-            continue
+        for i in range(len(output)):
+            network_list_dir.append(output[i][:-4])
+        del network_list_dir[0]
+        logger.info("Get persistent network name list under dir: %s" \
+                    % network_list_dir)
+        if networkname in network_list_dir:
+            return True
         else:
-            network = entry[:-4]
-            if network not in all_network:
-                logger.error("network %s not in the output of virsh net-list" % network)
-                return 1
-    return 0
-
-def check_inactive_option(conn, logger):
-    """check the output of virsh net-list with --inactive option
-    """
-    inactive_network = []
-    status, network_names = get_output(logger, VIRSH_QUIET_NETLIST % '--inactive', 0)
-    if not status:
-        inactive_network = network_names.split('\n')
-        logger.info("inactive network: %s" % inactive_network)
-    else:
+            logger.error("Failed to get the persistent %s" % networkname)
+            return False
+
+def get_network_list_virsh(flaglist):
+    """ Get the network name list through virsh command """
+
+    flagstr = ""
+    # Convert the flags that be passed to API to VIRSH flags
+    for flag_key in flaglist:
+        if FLAGDICT.has_key(int(flag_key)):
+            flagstr += FLAGDICT.get(int(flag_key))
+    logger.info("Execute virsh net-list" + flagstr)
+
+    network_list_virsh = []
+    (status, output) = utils.exec_cmd(VIRSH_NETWORK_LIST % flagstr,\
+                                       shell=True)
+    if status:
+        logger.error("Executing " + VIRSH_NETWORK_LIST + " failed")
         return 1
-
-    if inactive_network == ['']:
-        return 0
-
-    for network in inactive_network:
-        try:
-            netobj = conn.networkLookupByName(network)
-            bridgename = netobj.bridgeName()
-            status, ip = get_output(logger, GET_BRIDGE_IP % bridgename, 1)
-
-            if not status:
-                logger.info("network %s is inactive as we expected" % network)
-            else:
-                logger.error("network %s is not inactive, wrong" % network)
-                return 1
-        except libvirtError, e:
-            logger.error("API error message: %s, error code is %s" \
-                         % (e.message, e.get_error_code()))
-            return 1
-
-    return 0
-
-def check_default_option(conn, logger):
-    """check the output of virsh net-list
-    """
-    active_network = []
-    status, network_names = get_output(logger, VIRSH_QUIET_NETLIST % '', 0)
-    if not status:
-        active_network = network_names.split('\n')
-        logger.info("running network: %s" % active_network)
     else:
-        return 1
-
-    if active_network == ['']:
-        return 0
-
-    for network in active_network:
-        try:
-            netobj = conn.networkLookupByName(network)
-            bridgename = netobj.bridgeName()
-            status, ip = get_output(logger, GET_BRIDGE_IP % bridgename, 0)
-            if not status and utils.do_ping(ip, 0):
-                logger.info("network %s is active as we expected" % network)
-                logger.debug("%s has ip: %s" % (bridgename, ip))
-            else:
-                logger.error("network %s has no ip or fails to ping" % network)
-                return 1
-        except libvirtError, e:
-            logger.error("API error message: %s, error code is %s" \
-                         % (e.message, e.get_error_code()))
-            return 1
+        network_list_virsh = output[:-1]
+        logger.info("Get network name list via VIRSH: %s" \
+                    % network_list_virsh)
+
+def convert_flags(flags):
+    """ Bitwise-OR of flags in conf and convert them to the readable flags """
+
+    flaglist = []
+    flagstr = ""
+    logger.info("The given flags are %s " % flags)
+    if not '|' in flags:
+        flagn = int(flags)
+        flaglist.append(flagn)
+    else:
+        # bitwise-OR of flags of net-list
+        flaglist = flags.split('|')
+        flagn = 0
+        for flag in flaglist:
+            flagn |= int(flag)
 
-    return 0
+    # Convert the flags in conf file to readable flag
+    for flag_key in flaglist:
+        if FLAGDICT.has_key(int(flag_key)):
+            flagstr += FLAGDICT.get(int(flag_key))
+    logger.info("List network with flags:" + flagstr)
 
-def execute_virsh_netlist(option, logger):
-    """execute virsh net-list command with appropriate option given
-    """
-    status, ret = get_output(logger, VIRSH_NETLIST % option, 0)
-    if not status:
-        logger.info(ret)
+    return (flaglist, flagn)
 
 def network_list(params):
-    """test net-list command to virsh with default, --all, --inactive
-    """
-    logger = params['logger']
-    ret, option_list = get_option_list(params)
-
-    if ret:
-        return 1
+    """ List network with all filters """
 
+    global logger
+    logger = params['logger']
     conn = sharedmod.libvirtobj['conn']
-
-    for option in option_list:
-        if option == ' ':
-            logger.info("check the output of virsh net-list")
-            if not check_default_option(conn, logger):
-                logger.info("virsh net-list checking succeeded")
-                execute_virsh_netlist(option, logger)
-            else:
-                logger.error("virsh net-list checking failed")
-                return 1
-        elif option == '--inactive':
-            logger.info("check the output of virsh net-list --inactive")
-            if not check_inactive_option(conn, logger):
-                logger.info("virsh net-list --inactive checking succeeded")
-                execute_virsh_netlist(option, logger)
+    flags = params['flags']
+    (flaglist, flagn) = convert_flags(flags)
+
+    try:
+        logger.info("Flag list %s " % flaglist)
+        logger.info("Bitwise OR value of flags is %s" % flagn)
+
+        network_list_api = conn.listAllNetworks(flagn)
+        network_list_virsh = get_network_list_virsh(flaglist)
+        network_namelist_api = []
+        for network in network_list_api:
+
+            networkname = network.name()
+            network_namelist_api.append(networkname)
+            bridgename = network.bridgeName()
+            logger.info("Network name: %s " % networkname)
+            logger.info("Network %s 's bridge name: %s " % (networkname ,\
+                                                            bridgename))
+            # Check if the network is active
+            if network.isActive() and check_bridge_ip(bridgename):
+                logger.info("The %s network is active" % networkname)
             else:
-                logger.error("virsh net-list --inactive checking failed")
-                return 1
-        elif option == '--all':
-            logger.info("check the output of virsh net-list --all")
-            if not check_all_option(conn, logger):
-                logger.info("virsh net-list --all checking succeeded")
-                execute_virsh_netlist(option, logger)
+                logger.info("The %s network isn't active" % networkname)
+
+            # Check if the network is persistent
+            if network.isPersistent() and check_persistent_netxml(networkname):
+                logger.info("The %s network is persistent" % networkname)
             else:
-                logger.error("virsh net-list --all checking failed")
-                return 1
+                logger.info("The %s network isn't persistent" % networkname)
+
+            logger.info("Network list through API: %s " % network_namelist_api)
+        if cmp(network_namelist_api,network_list_virsh):
+            logger.info("Successfully get network list through API")
+        else:
+            logger.error("Failed to get network list through API")
+            return 1
+
+    except libvirtError, e:
+        logger.error("API error message: %s" % e.message)
+        return 1
 
     return 0
+
-- 
1.7.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]