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

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



On 07/22/2013 03:53 PM, hongming zhang wrote:
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()
isActive()
isPersistent()
virConnect:
listAllNetworks()
V1-> V2
1.Change the flag in conf from digit to string
2.Remove the checking method using the result of virsh with flags
3.Add checking method via checking network autostart dir
4.Modify the basic_network.conf
V2 -> V3
1.Remove check_bridge_ip method
2.Remove network.bridgeName() call
3.Fix a typo in log
---
  cases/basic_network.conf      |   35 +++++-
  repos/network/network_list.py |  260 ++++++++++++++++-------------------------
  2 files changed, 130 insertions(+), 165 deletions(-)

diff --git a/cases/basic_network.conf b/cases/basic_network.conf
index 991ad99..91d7f21 100644
--- a/cases/basic_network.conf
+++ b/cases/basic_network.conf
@@ -14,10 +14,18 @@ network:define
      netmode
          nat
+network:network_list
+    flags
+	inactive
+
  network:start
      networkname
          $defaultnetname
+network:network_list
+    flags
+	active
+
  network:autostart
      networkname
  	$defaultnetname
@@ -28,6 +36,10 @@ network:destroy
      networkname
          $defaultnetname
+network:network_list
+    flags
+	default
+
  network:undefine
      networkname
          $defaultnetname
@@ -48,6 +60,10 @@ network:create
      netmode
          nat
+network:network_list
+    flags
+        transient
+
  network:destroy
      networkname
          $defaultnetname
@@ -68,6 +84,10 @@ network:define
      netmode
          route
+network:network_list
+    flags
+	persistent
+
  network:start
      networkname
          $defaultnetname
@@ -106,7 +126,6 @@ network:destroy
      networkname
          $defaultnetname
-
  network:define
      networkname
          $defaultnetname
@@ -127,12 +146,20 @@ network:start
      networkname
          $defaultnetname
+network:network_list
+    flags
+	noautostart
+
  network:autostart
      networkname
          $defaultnetname
      autostart
          enable
+network:network_list
+    flags
+	autostart
+
  network:destroy
      networkname
          $defaultnetname
@@ -141,7 +168,6 @@ network:undefine
      networkname
          $defaultnetname
-
  network:create
      networkname
          $defaultnetname
@@ -162,8 +188,3 @@ network:destroy
      networkname
          $defaultnetname
-
-
-
-
-
diff --git a/repos/network/network_list.py b/repos/network/network_list.py
index 7c34f69..b94b505 100644
--- a/repos/network/network_list.py
+++ b/repos/network/network_list.py
@@ -1,184 +1,128 @@
  #!/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/"
+LS_NETWORK_DIR = "ls /etc/libvirt/qemu/networks/"
+LS_AUTOSTART_NET = "ls /etc/libvirt/qemu/networks/autostart/"
-def get_option_list(params):
-    """return options we need to test
+def check_persistent_netxml(networkname):
      """
-    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)
-    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
+        Check if the network is persistent via checking network xml dir
+        if the network is persistent, return True, or return False
      """
-    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)
-    else:
-        return 1
-
-    if all_network == ['']:
-        return 0
- for entry in entries:
-        if not entry.endswith('.xml'):
-            continue
+    (status, output) = utils.exec_cmd(LS_NETWORK_DIR, shell=True)
+    network_list_dir = []
+    if status:
+        logger.error("Executing " + LS_NETWORK_DIR + " failed")
+        logger.error(output)
+        return False
+    else:
+        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
+            return False
-def check_inactive_option(conn, logger):
-    """check the output of virsh net-list with --inactive option
+def check_autostart_netxml(networkname):
      """
-    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:
-        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
+        Check if the network is autostart via checking network autostart dir
+        if the network is autostart , return True, or return False.
      """
-    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
-
-    return 0
-
-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)
+    (status, output) = utils.exec_cmd(LS_AUTOSTART_NET, shell=True)
+    autostart_list_dir = []
+    if status:
+        logger.error("Executing " + LS_AUTOSTART_NET + " failed")
+        logger.error(output)
+        return False
+    else:
+        for i in range(len(output)):
+            autostart_list_dir.append(output[i][:-4])
+        logger.info("Get autostart network name list under dir: %s" \
+                    % autostart_list_dir)
+        if networkname in autostart_list_dir:
+            return True
+        else:
+            return False
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 flag """
+ 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)
-            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)
-            else:
-                logger.error("virsh net-list --all checking failed")
-                return 1
+    flags = params['flags']
+    logger.info("The given flags is %s " % flags)
+    flag = -1
+    if flags == "default":
+        flag = 0
+    elif flags == "inactive":
+        flag = 1
+    elif flags == "active":
+        flag = 2
+    elif flags == "persistent":
+        flag = 4
+    elif flags == "transient":
+        flag = 8
+    elif flags == "autostart":
+        flag = 16
+    elif flags == "noautostart":
+        flag = 32
+    try:
+        network_list_api = conn.listAllNetworks(flag)
+        network_namelist_api = []
+        logger.debug("Traverse the network object list %s" % \
+                    network_list_api)
+        for network in network_list_api:
+            networkname = network.name()
+            logger.info("Network name: %s " % networkname)
+            # Check if the network is active
+            if cmp(flags,"active") == 0:
+                if network.isActive():
+                    logger.info("The %s network is active" % networkname)
+                else:
+                    logger.error("Failed ,the %s network isn't active" % \
+                                 networkname)
+                    return 1
+
+            # Check if the network is persistent
+            if cmp(flags,"persistent") == 0:
+                if network.isPersistent() and \
+                check_persistent_netxml(networkname):
+                    logger.info("The %s network is persistent" % networkname)
+                else:
+                    logger.error("Failed ,the %s network isn't persistent" % \
+                                 networkname)
+                    return 1
+
+            # Check if the network is auto start
+            if cmp(flags,"autostart") == 0:
+                if check_autostart_netxml(networkname):
+                    logger.info("The %s network is autostart" % networkname)
+                else:
+                    logger.error("Failed ,the %s network isn't autostart" % \
+                                 networkname)
+                    return 1
+
+            network_namelist_api.append(networkname)
+        logger.info("The network list %s" % network_namelist_api)
+
+    except libvirtError, e:
+        logger.error("API error message: %s" % e.message)
+        return 1
return 0
+

ACK and pushed.
Thanks

Guannan


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