On 07/17/2013 04:46 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()
bridgeName()
isActive()
isPersistent()
virConnect:
listAllNetworks()
Changes from V2 to V1 as follows
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
---
cases/basic_network.conf | 35 ++++-
repos/network/network_list.py | 277 ++++++++++++++++++-----------------------
2 files changed, 148 insertions(+), 164 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..175d6fa 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}'"
+LS_NETWORK_DIR = "ls /etc/libvirt/qemu/networks/"
+LS_AUTOSTART_NET = "ls /etc/libvirt/qemu/networks/autostart/"
+
+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]))
GET_BRIDGE_IP is not a common solution to extract ip
In fedora, it is inet xxx.xxx.xxx.xxx which is not the same as inet addr: xxx.xxx.xxx.xxx on RHEL
+ return True
else:
- logger.error("value %s is not supported" % value)
- return 1, option_list
-
- return 0, option_list
+ logger.error("Bridge %s has no ip or fails to ping" % bridgename)
+ return False
-def get_output(logger, command, flag):
- """execute shell command
+def check_persistent_netxml(networkname):
"""
- 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
+ Check if the network is persistent via checking network xml dir
+ if the network is persistent, return True, or return False
"""
- 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 persistent network name list under dir: %s" \
+ % autostart_list_dir)
s/Get persistent/Get autostart
+ 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 are %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()
+ bridgename = network.bridgeName()
Not all of virtual network have bridge name like using macvtap direct
connection
<network>
<name>virtualnet</name>
<forward dev='eth1' mode='bridge'>
<interface dev='eth1'/>
</forward>
</network>
Guannan
--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list