# HG changeset patch
# User Kaitlin Rupert <karupert(a)us.ibm.com>
# Date 1236965462 25200
# Node ID e7a106fadb25acb73ecc8d5ff4137611d942548a
# Parent b1e05c9de638909c5c6a7ba86aa2b3551802d013
[TEST] #4 Make sure network pool is created with a random IP.
This will help prevent overlap with existing networks. However, this won't
completely prevent the issue. We'd need to do test to see if the IP address
is already used by a different network. If the network is in use, then another
IP address should be used.
Update from 3 to 4:
-Remove changes made for debugging purposes:
*Uisng a 192.168.122 value for the subnet instead of 192.168.123
*Overriding addr value
Updates from 2 to 3:
-If IP address is already in use, return from the _init_() call.
Updates from 1 to 2:
-Check to see if pool with the specified IP range exists, if so, return a
meaningful error
-Since the NetXML generates a random IP, creating a NetXML object in the
destroy call will generate an object with a different IP. Instead, use
the virsh call to destroy the network.
Signed-off-by: Kaitlin Rupert <karupert(a)us.ibm.com>
diff -r b1e05c9de638 -r e7a106fadb25 suites/libvirt-cim/lib/XenKvmLib/common_util.py
--- a/suites/libvirt-cim/lib/XenKvmLib/common_util.py Fri Mar 13 10:31:05 2009 -0700
+++ b/suites/libvirt-cim/lib/XenKvmLib/common_util.py Fri Mar 13 10:31:02 2009 -0700
@@ -36,7 +36,7 @@
CIM_ERROR_GETINSTANCE
from CimTest.ReturnCodes import PASS, FAIL, XFAIL_RC
from XenKvmLib.xm_virt_util import diskpool_list, virsh_version, net_list,\
- domain_list, virt2uri
+ domain_list, virt2uri, net_destroy
from XenKvmLib.vxml import PoolXML, NetXML
from VirtLib import utils
from XenKvmLib.const import default_pool_name, default_network_name
@@ -438,11 +438,9 @@
if net_name == None:
return FAIL
- netxml = NetXML(server, virt=virt, networkname=net_name)
- ret = netxml.destroy_vnet()
- if not ret:
- logger.error("Failed to destroy Virtual Network '%s'",
- net_name)
+ ret = net_destroy(net_name, server, virt)
+ if ret != 0:
+ logger.error("Failed to destroy Virtual Network '%s'",
net_name)
return FAIL
return PASS
diff -r b1e05c9de638 -r e7a106fadb25 suites/libvirt-cim/lib/XenKvmLib/vxml.py
--- a/suites/libvirt-cim/lib/XenKvmLib/vxml.py Fri Mar 13 10:31:05 2009 -0700
+++ b/suites/libvirt-cim/lib/XenKvmLib/vxml.py Fri Mar 13 10:31:02 2009 -0700
@@ -32,6 +32,7 @@
# shared by XenXML & KVMXML.
import os
import sys
+import random
import platform
import tempfile
from time import sleep
@@ -39,7 +40,8 @@
from xml.dom import minidom, Node
from xml import xpath
from VirtLib import utils, live
-from XenKvmLib.xm_virt_util import get_bridge_from_network_xml, bootloader
+from XenKvmLib.xm_virt_util import get_bridge_from_network_xml, bootloader, \
+ net_list
from XenKvmLib.test_doms import set_uuid, viruuid
from XenKvmLib import vsms
from XenKvmLib import const
@@ -182,7 +184,6 @@
def get_valid_bridge_name(server):
bridge_list = live.available_bridges(server)
if bridgename in bridge_list:
- import random
vbr = bridgename + str(random.randint(1, 100))
if vbr in bridge_list:
logger.error('Need to give different bridge name '
@@ -207,13 +208,27 @@
self.add_sub_node(network, 'name', self.net_name)
self.add_sub_node(network, 'uuid', set_uuid())
self.add_sub_node(network, 'forward')
- subnet = '192.168.122.'
+ subnet = '192.168.123.'
self.add_sub_node(network, 'bridge', name=self.vbr, stp='on',
forwardDelay='0')
- ip = self.add_sub_node(network, 'ip', address=subnet+'1',
+ ip_base = random.randint(1, 100)
+ addr = subnet+'%d' % ip_base
+
+ n_list = net_list(server, virt)
+ for _net_name in n_list:
+ cmd = "virsh net-dumpxml %s | awk '/ip address/ {print}' | \
+ cut -d ' ' -f 4 | sed 's/address=//'" % _net_name
+ s, in_use_addr = utils.run_remote(server, cmd)
+ in_use_addr = in_use_addr.strip("'")
+ if in_use_addr == addr:
+ logger.error("IP address is in use by a different network")
+ return None
+
+ ip = self.add_sub_node(network, 'ip', address=addr,
netmask='255.255.255.0')
dhcp = self.add_sub_node(ip, 'dhcp')
- self.add_sub_node(dhcp, 'range', start=subnet+'2',
+ range_addr = subnet+'%d' % (ip_base + 1)
+ self.add_sub_node(dhcp, 'range', start=range_addr,
end=subnet+'254')
def create_vnet(self):
diff -r b1e05c9de638 -r e7a106fadb25 suites/libvirt-cim/lib/XenKvmLib/xm_virt_util.py
--- a/suites/libvirt-cim/lib/XenKvmLib/xm_virt_util.py Fri Mar 13 10:31:05 2009 -0700
+++ b/suites/libvirt-cim/lib/XenKvmLib/xm_virt_util.py Fri Mar 13 10:31:02 2009 -0700
@@ -184,6 +184,14 @@
if len(bridge) > 1:
return bridge[1]
+def net_destroy(network, server, virt="Xen"):
+ """Function destroys a given virtual network"""
+
+ cmd = "virsh -c %s net-destroy %s" % (virt2uri(virt), network)
+ ret, out = utils.run_remote(server, cmd)
+
+ return ret
+
def network_by_bridge(bridge, server, virt="Xen"):
"""Function returns virtual network for a given
bridge"""