[Libvirt-cim] [PATCH V2 1/4] cimtest - vlan extention - helper

Wenchao Xia xiawenc at linux.vnet.ibm.com
Tue Feb 14 08:59:40 UTC 2012


    This patch added core functions as test units. Generaly, the test process
is: Modify it, Enum it to check the result. All operation were done by
libvirt-cim. In adding a network device, it would automatically delete it if
it exist, before call libvirt-cim.

Signed-off-by: Wenchao Xia <xiawenc at linux.vnet.ibm.com>
---
 suites/libvirt-cim/lib/XenKvmLib/host_network.py |  455 ++++++++++++++++++++++
 1 files changed, 455 insertions(+), 0 deletions(-)
 create mode 100644 suites/libvirt-cim/lib/XenKvmLib/host_network.py

diff --git a/suites/libvirt-cim/lib/XenKvmLib/host_network.py b/suites/libvirt-cim/lib/XenKvmLib/host_network.py
new file mode 100644
index 0000000..cb17f55
--- /dev/null
+++ b/suites/libvirt-cim/lib/XenKvmLib/host_network.py
@@ -0,0 +1,455 @@
+#!/usr/bin/env python
+
+#
+# Copyright 2012 IBM Corp.
+#
+# Authors:
+#   Wenchao Xia (Wayne) <xiawenc at cn.ibm.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+#
+
+#   This file have basic functions for testing of host network in libvirt-cim
+
+
+import sys
+import os
+import pywbem
+
+from CimTest.Globals import logger, CIM_USER, CIM_PASS, CIM_NS
+from XenKvmLib.classes import get_typed_class
+from XenKvmLib.vxml import get_class
+
+network_clas_prefix = "Net_"
+vesssd_parent = "Virt"
+vs_prefix = "VS_"
+ea_prefix = "EA_"
+ec_prefix = "EC_"
+delimiter_System = """:"""
+delimiter_Device = """/"""
+
+vess_cls_name = network_clas_prefix + "VirtualEthernetSwitchSystem"
+vessms_cls_name = network_clas_prefix + "VirtualEthernetSwitchSystemManagementService"
+vesssd_cls_name = network_clas_prefix + "VirtualEthernetSwitchSystemSettingData"
+easd_cls_name = network_clas_prefix + "EthernetPortAllocationSettingData"
+
+vlan_8021q_type = 1
+
+bridge_pNIC = "test_br1"
+bridge_name = """%s%s""" % (vs_prefix, bridge_pNIC)
+bridge_stp = 0
+bridge_delay = 0
+bridge_autostart = 0
+bridge_dhcp = 0
+
+bridge_stp_modify = 1
+
+
+def set_vs_define(inst):
+    return """
+instance of %s {
+    VirtualSystemIdentifier="%s";
+    STP=%d;
+    Delay=%d;
+    AutoStart=%d;
+    DHCP=%d;
+};""" % (vesssd_cls_name,
+        inst["VirtualSystemIdentifier"],
+        inst["STP"],
+        inst["Delay"],
+        inst["AutoStart"],
+        inst["DHCP"],)
+
+def set_vs_modify(inst):
+    return """
+instance of %s {
+    VirtualSystemIdentifier="%s";
+    STP=%d;
+};""" % (vesssd_cls_name,
+        inst["VirtualSystemIdentifier"],
+        inst["STP"],)
+
+
+def set_vs_destroy(inst):
+    inst = pywbem.CIMInstanceName(classname=vess_cls_name, keybindings=pywbem.NocaseDict({'CreationClassName': vess_cls_name, 'Name': inst["Name"]}))
+    return inst
+
+def get_vessms(cim):
+    _class = vessms_cls_name
+    sys_mgmt_service = cim.EnumerateInstanceNames(_class)[0]
+    #it is not supposed to fail here, the enum of VESSMS is very simple
+    logger.info("got vessms as: %s.", sys_mgmt_service)
+    return sys_mgmt_service
+
+def get_vess(cim, targetInst, name_only):
+    _class = vess_cls_name
+    if name_only == 1:
+        vess_list = cim.EnumerateInstanceNames(_class)
+    else :
+        vess_list = cim.EnumerateInstances(_class)
+    targetID = targetInst["Name"]
+    instanceList = ""
+    for inst in vess_list:
+        items = inst.items()
+        instanceList += str(items)+ "\n"
+        cmp_ret = cmp(inst["Name"], targetInst["Name"])
+        if cmp_ret == 0:
+            logger.info("found the device., it is :\n %s", str(items))
+            return inst
+    return None
+
+def try_chang_vess_state(cim, targetInst, state):
+    vess_inst = get_vess(cim, targetInst, 1)
+    if vess_inst == None :
+        logger.error("vess %s not found.", targetInst["Name"])
+    val = pywbem.cim_types.Uint16(state)
+    method = "RequestStateChange"
+    par_name = "RequestedState"
+    param = {par_name: val}
+    logger.info("trying method %s of %s with parameter:\n %s", method, vess_inst, param)
+    ret = cim.InvokeMethod(method, vess_inst, **param)
+    vess_inst = get_vess(cim, targetInst, 0)
+    logger.info("result state is %d, requested is %d.", vess_inst["EnabledState"], state)
+    if vess_inst["EnabledState"] == state :
+        return 1
+    else :
+        return 0
+
+def check_vesssd(cim, targetInst, compareAll = False):
+    #check the result
+    _class = vesssd_cls_name
+    vesssd = cim.EnumerateInstances(_class)
+    targetID = targetInst["Name"]
+    instanceList = ""
+    found = 0
+    for inst in vesssd:
+        items = inst.items()
+        instanceList += str(items)+ "\n"
+        (prefix, ID) = inst["InstanceID"].split(":",1)
+        cmp_ret = cmp(ID, targetID) or cmp(prefix, vesssd_parent)
+        if cmp_ret == 0:
+            logger.info("found the device., it is :\n %s", str(items))
+            if compareAll == False :
+                found = 1
+            else :
+                found = 1
+                if inst["STP"] != targetInst["STP"]:
+                    found = 0
+                if inst["Delay"] != targetInst["Delay"]:
+                    found = 0
+                if inst["AutoStart"] != targetInst["AutoStart"]:
+                    found = 0
+                if inst["DHCP"] != targetInst["DHCP"]:
+                    found = 0
+    #logger.info("search for %s, found is %d, list is: \n%s", _class, found, instanceList)
+    logger.info("target vesssd searching result is %d.", found)
+    return found
+
+def try_delete_vs(cim, sys_mgmt_service, inst):
+    #delete the bridge
+    val = set_vs_destroy(inst)
+    method = "DestroySystem"
+    par_name = "AffectedSystem"
+    param = {par_name: val}
+    logger.info("trying method %s of %s with parameter:\n %s", method, sys_mgmt_service, param)
+    ret = cim.InvokeMethod(method, sys_mgmt_service, **param)
+    found = check_vesssd(cim, inst)
+    if found == 1 :
+        return 0
+    else :
+        return 1
+
+def try_create_vs(cim, sys_mgmt_service, inst):
+    #Check the Env
+    found = check_vesssd(cim, inst)
+    if found == 1:
+        logger.info("device exist, trying delete it");
+        ret = try_delete_vs(cim, sys_mgmt_service, inst)
+        if ret != 1:
+            return 0
+
+    #add the bridge
+    val = set_vs_define(inst)
+    method = "DefineSystem"
+    logger.info("try method %s of %s with parameter %s", method, sys_mgmt_service, val)
+    ret = cim.InvokeMethod(method, sys_mgmt_service, **{"SystemSettings": val})
+    found = check_vesssd(cim, inst)
+    if found == 1 :
+        return 1
+    else :
+        return 0
+
+def try_modify_vs(cim, sys_mgmt_service, inst):
+    #modify the bridge
+    val = set_vs_modify(inst)
+    method = "ModifySystemSettings"
+    par_name = "SystemSettings"
+    param = {par_name: val}
+    logger.info("trying method %s of %s with parameter:\n %s", method, sys_mgmt_service, param)
+    ret = cim.InvokeMethod(method, sys_mgmt_service, **param)
+    found = check_vesssd(cim, inst, True)
+    if found == 1 :
+        return 1
+    else :
+        return 0
+
+
+#inst should be:
+#{'Parent': "eth0", 'VLanID':10, 'ReorderHdr':1, 'Ingress':"1:2 2:3", 'Egress':'2:4'}
+def set_ea_add_8021q(inst):
+    ParentSystem = """%s%s""" % (vs_prefix, inst['Parent'])
+    AffectedID = """%s%s%s""" % (vesssd_parent, delimiter_System, ParentSystem)
+    AffectedConfiguration = pywbem.CIMInstanceName(classname= vesssd_cls_name, keybindings=pywbem.NocaseDict({'InstanceID': AffectedID}))
+    EA_ID = """%s%s.%d""" % (ea_prefix, inst['Parent'], inst['VLanID'])
+    InstanceID_EA = """%s%s%s""" % (ParentSystem, delimiter_Device, EA_ID)
+    ea1 = """
+instance of %s {
+    InstanceID ="%s";
+    VLANType = %d;
+    Connection = {"VLAN%d"};
+    AutoStart=%d;
+    DHCP=%d;
+};""" % (easd_cls_name,
+        InstanceID_EA,
+        vlan_8021q_type,
+        inst["VLanID"],
+        inst["AutoStart"],
+        inst["DHCP"])
+    final_param = {"AffectedConfiguration" : AffectedConfiguration, "ResourceSettings" : [ea1,]}
+    return final_param
+
+def set_ea_mod_8021q(inst):
+    ParentSystem = """%s%s""" % (vs_prefix, inst['Parent'])
+    EA_ID = """%s%s.%d""" % (ea_prefix, inst['Parent'], inst['VLanID'])
+    InstanceID_EA = """%s%s%s""" % (ParentSystem, delimiter_Device, EA_ID)
+    ea1 = """
+instance of %s {
+    InstanceID ="%s";
+    VLANType = %d;
+    Connection = {"VLAN%d"};
+    AutoStart=%d;
+    DHCP=%d;
+};""" % (easd_cls_name,
+        InstanceID_EA,
+        vlan_8021q_type,
+        inst["VLanID"],
+        inst["AutoStart"],
+        inst["DHCP"])
+    final_param = {"ResourceSettings" : [ea1,]}
+    return final_param
+
+def set_ea_del_8021q(inst):
+    ParentSystem = """%s%s""" % (vs_prefix, inst['Parent'])
+    EA_ID = """%s%s.%d""" % (ea_prefix, inst['Parent'], inst['VLanID'])
+    InstanceID_EA = """%s%s%s""" % (ParentSystem, delimiter_Device, EA_ID)
+    del_ref = pywbem.CIMInstanceName(classname= easd_cls_name, keybindings=pywbem.NocaseDict({'InstanceID': InstanceID_EA}))
+    final_param = {"ResourceSettings" : [del_ref,],}
+    return final_param
+
+
+def check_easd_ea(cim, targetInst, compareAll = False):
+    #check the result
+    _class = easd_cls_name
+    easds = cim.EnumerateInstances(_class)
+
+    inst = targetInst
+    ParentSystem = """%s%s""" % (vs_prefix, inst['Parent'])
+    EA_ID = """%s%s.%d""" % (ea_prefix, inst['Parent'], inst['VLanID'])
+    InstanceID_EA = """%s%s%s""" % (ParentSystem, delimiter_Device, EA_ID)
+    vlanstr = """VLAN%d""" % (inst['VLanID'])
+
+    instanceList = ""
+    found = 0
+    for inst in easds:
+        items = inst.items()
+        instanceList += str(items)+ "\n"
+        cmp_ret = cmp(inst["InstanceID"], InstanceID_EA)
+        if cmp_ret == 0:
+            logger.info("found the device., it is :\n %s", str(items))
+            cmp_ret = cmp(inst["Connection"][0], vlanstr)
+            if cmp_ret == 0:
+                    found = 1
+            else :
+                    found = 0
+            if compareAll == False :
+                pass
+            else :
+                if inst["AutoStart"] != targetInst["AutoStart"]:
+                    found = 0
+                if inst["DHCP"] != targetInst["DHCP"]:
+                    found = 0
+            break
+    #logger.info("search for %s, found is %d, list is: \n%s", _class, found, instanceList)
+    logger.info("target easd searching result is %d.", found)
+    return found
+
+def try_del_ea(cim, sys_mgmt_service, inst):
+    param = set_ea_del_8021q(inst)
+    method = "RemoveResourceSettings"
+    logger.info("trying method %s of %s with parameter:\n %s", method, sys_mgmt_service, param)
+    ret = cim.InvokeMethod(method, sys_mgmt_service, **param)
+    found = check_easd_ea(cim, inst)
+    if found == 1 :
+        return 0
+    else :
+        return 1
+
+def try_mod_ea(cim, sys_mgmt_service, inst):
+    param = set_ea_mod_8021q(inst)
+    method = "ModifyResourceSettings"
+    logger.info("trying method %s of %s with parameter:\n %s", method, sys_mgmt_service, param)
+    ret = cim.InvokeMethod(method, sys_mgmt_service, **param)
+    found = check_easd_ea(cim, inst, True)
+    if found == 1 :
+        return 1
+    else :
+        return 0
+
+
+def try_add_ea(cim, sys_mgmt_service, inst):
+    #Check the Env
+    found = check_easd_ea(cim, inst)
+    if found == 1:
+        logger.info("device exist, trying delete it");
+        ret = try_del_ea(cim, sys_mgmt_service, inst)
+        if ret != 1:
+            return 0
+
+    param = set_ea_add_8021q(inst)
+    method = "AddResourceSettings"
+    logger.info("trying method %s of %s with parameter:\n %s", method, sys_mgmt_service, param)
+    ret = cim.InvokeMethod(method, sys_mgmt_service, **param)
+    found = check_easd_ea(cim, inst)
+    if found == 1 :
+        return 1
+    else :
+        return 0
+
+
+#inst should be:
+#{'ParentBridge': "eth0", 'EthPort':eth0.10, 'TargetBridge': "test_br"}
+def set_ec_add(inst):
+    ParentSystem = """%s%s""" % (vs_prefix, inst['ParentBridge'])
+    TargetSystem = """%s%s""" % (vs_prefix, inst['TargetBridge'])
+    AffectedID = """%s%s%s""" % (vesssd_parent, delimiter_System, ParentSystem)
+    AffectedConfiguration = pywbem.CIMInstanceName(classname= vesssd_cls_name, keybindings=pywbem.NocaseDict({'InstanceID': AffectedID}))
+    EA_ID = """%s%s""" % (ea_prefix, inst['EthPort'])
+    EC_ID = """%s%s""" % (ec_prefix, inst['EthPort'])
+    InstanceID_EA = """%s%s%s""" % (ParentSystem, delimiter_Device, EA_ID)
+    InstanceID_EC = """%s%s%s""" % (ParentSystem, delimiter_Device, EC_ID)
+    ea1 = """
+instance of %s {
+    InstanceID ="%s";
+    HostResource = {"%s"};
+    Parent = "%s";
+};""" % (easd_cls_name,
+        InstanceID_EC,
+        TargetSystem,
+        InstanceID_EA)
+    final_param = {"AffectedConfiguration" : AffectedConfiguration, "ResourceSettings" : [ea1,]}
+    return final_param
+
+def set_ec_del(inst):
+    ParentSystem = """%s%s""" % (vs_prefix, inst['ParentBridge'])
+    EC_ID = """%s%s""" % (ec_prefix, inst['EthPort'])
+    InstanceID_EC = """%s%s%s""" % (ParentSystem, delimiter_Device, EC_ID)
+    del_ref = pywbem.CIMInstanceName(classname= easd_cls_name, keybindings=pywbem.NocaseDict({'InstanceID': InstanceID_EC}))
+    final_param = {"ResourceSettings" : [del_ref,],}
+    return final_param
+
+def check_easd_ec(cim, targetInst, compareAll = False):
+    #check the result
+    _class = easd_cls_name
+    easds = cim.EnumerateInstances(_class)
+
+    inst = targetInst
+    ParentSystem = """%s%s""" % (vs_prefix, inst['ParentBridge'])
+    TargetSystem = """%s%s""" % (vs_prefix, inst['TargetBridge'])
+    EC_ID = """%s%s""" % (ec_prefix, inst['EthPort'])
+    InstanceID_EC0 = """%s%s%s""" % (ParentSystem, delimiter_Device, EC_ID)
+    InstanceID_EC1 = """%s%s%s""" % (TargetSystem, delimiter_Device, EC_ID)
+
+    instanceList = ""
+    EC0_found = 0
+    EC1_found = 0
+    for inst in easds:
+        items = inst.items()
+        instanceList += str(items)+ "\n"
+        cmp_ret = cmp(inst["InstanceID"], InstanceID_EC0)
+        if cmp_ret == 0:
+            logger.info("found the ec0., it is :\n %s", str(items))
+            EC0_found = 1
+        else :
+            cmp_ret = cmp(inst["InstanceID"], InstanceID_EC1)
+            if cmp_ret == 0:
+                logger.info("found the ec1., it is :\n %s", str(items))
+                EC1_found = 1
+    #logger.info("search for %s, list is: \n%s", _class, instanceList)
+    found = EC0_found and EC1_found
+    logger.info("target easd_ec searching result is %d and %d.", EC0_found, EC1_found)
+    if EC0_found != EC1_found :
+        logger.info("not all ec found as expected.")
+        found = -1
+    return found
+
+def try_del_ec(cim, sys_mgmt_service, inst):
+    param = set_ec_del(inst)
+    method = "RemoveResourceSettings"
+    logger.info("trying method %s of %s with parameter:\n %s", method, sys_mgmt_service, param)
+    ret = cim.InvokeMethod(method, sys_mgmt_service, **param)
+    found = check_easd_ec(cim, inst)
+    if found == 0 :
+        return 1
+    else :
+        return 0
+
+def try_add_ec(cim, sys_mgmt_service, inst):
+    #Check the Env
+    found = check_easd_ec(cim, inst)
+    if found != 0:
+        logger.info("device exist, trying delete it");
+        ret = try_del_ec(cim, sys_mgmt_service, inst)
+        if ret != 1:
+            return 0
+
+    param = set_ec_add(inst)
+    method = "AddResourceSettings"
+    logger.info("trying method %s of %s with parameter:\n %s", method, sys_mgmt_service, param)
+    ret = cim.InvokeMethod(method, sys_mgmt_service, **param)
+    found = check_easd_ec(cim, inst)
+    if found == 1 :
+        return 1
+    else :
+        return 0
+
+def FoundOnePNIC(cim):
+    #check the result
+    _class = vesssd_cls_name
+    vesssd = cim.EnumerateInstances(_class)
+    EthList = []
+    found = 0
+    for inst in vesssd:
+        (prefix, ID) = inst["InstanceID"].split(":",1)
+        if ID.find(vs_prefix) == 0 :
+            realname = ID[len(vs_prefix):]
+            if realname.find("eth") == 0 and realname.find(".") == -1 :
+                found = 1
+                EthList.append(realname)
+    #logger.info("search for %s, found is %d, list is: \n%s", _class, found, instanceList)
+    ret_name = None
+    if found == 1 :
+        ret_name = EthList[0]
+    logger.info("pNIC searching result is %d, list %s, ret is %s.", found, EthList, ret_name)
+    return ret_name
-- 
1.7.1





More information about the Libvirt-cim mailing list