[dts] [PATCH v2 07/19] Move some general functions from dts.py to utils.py and settings.py

Jiajia, Sun sunx.jiajia at intel.com
Fri May 22 11:04:00 CEST 2015


From: sjiajiax <sunx.jiajia at intel.com>

Signed-off-by: sjiajiax <sunx.jiajia at intel.com>
---
 framework/dts.py      |  86 +++++++++----------------------------------
 framework/settings.py |  43 ++++++++++++++++++++++
 framework/utils.py    | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 160 insertions(+), 69 deletions(-)
 create mode 100644 framework/utils.py

diff --git a/framework/dts.py b/framework/dts.py
index c9ecccb..f6a14ad 100644
--- a/framework/dts.py
+++ b/framework/dts.py
@@ -49,6 +49,7 @@ from test_case import TestCase
 from test_result import Result
 from stats_reporter import StatsReporter
 from excel_reporter import ExcelReporter
+from utils import *
 from exception import TimeoutException
 from logger import getLogger
 import logger
@@ -57,6 +58,7 @@ import sys
 reload(sys)
 sys.setdefaultencoding('UTF8')
 
+PROJECT_MODULE_PREFIX = 'project_'
 
 debug_mode = False
 config = None
@@ -73,44 +75,12 @@ result = None
 excel_report = None
 stats = None
 log_handler = None
+Package = ''
+Patches = []
 drivername = ""
 interrupttypr = ""
 
 
-def RED(text):
-    return "\x1B[" + "31;1m" + text + "\x1B[" + "0m"
-
-
-def BLUE(text):
-    return "\x1B[" + "36;1m" + text + "\x1B[" + "0m"
-
-
-def GREEN(text):
-    return "\x1B[" + "32;1m" + text + "\x1B[" + "0m"
-
-
-def regexp(s, to_match, allString=False):
-    """
-    Ensure that the re `to_match' only has one group in it.
-    """
-
-    scanner = re.compile(to_match, re.DOTALL)
-    if allString:
-        return scanner.findall(s)
-    m = scanner.search(s)
-    if m is None:
-        log_handler.warning("Failed to match " + to_match + " in the string " + s)
-        return None
-    return m.group(1)
-
-
-def pprint(some_dict):
-    """
-    Print JSON format dictionary object.
-    """
-    return json.dumps(some_dict, sort_keys=True, indent=4)
-
-
 def report(text, frame=False, annex=False):
     """
     Save report text into rst file.
@@ -130,37 +100,7 @@ def close_crb_sessions():
     if tester is not None:
         tester.close()
     log_handler.info("DTS ended")
-
-
-def get_nic_driver(pci_id):
-    """
-    Return linux driver for specified pci device
-    """
-    driverlist = dict(zip(NICS.values(), DRIVERS.keys()))
-    try:
-        driver = DRIVERS[driverlist[pci_id]]
-    except Exception as e:
-        driver = None
-    return driver
-
-
-def accepted_nic(pci_id):
-    """
-    Return True if the pci_id is a known NIC card in the settings file and if
-    it is selected in the execution file, otherwise it returns False.
-    """
-    if pci_id not in NICS.values():
-        return False
-
-    if nic is 'any':
-        return True
-
-    else:
-        if pci_id == NICS[nic]:
-            return True
-
-    return False
-
+	
 
 def get_crb_os(crb):
     if 'OS' in crb:
@@ -220,9 +160,10 @@ def get_project_obj(project_name, super_class, crbInst, serializer):
     """
     Load project module and return crb instance.
     """
+    global PROJECT_MODULE_PREFIX
     project_obj = None
     try:
-        project_module = __import__("project_" + project_name)
+        project_module = __import__(PROJECT_MODULE_PREFIX + project_name)
 
         for project_subclassname, project_subclass in get_subclasses(project_module, super_class):
             project_obj = project_subclass(crbInst, serializer)
@@ -273,7 +214,7 @@ def dts_crbs_init(crbInst, skip_setup, read_cache, project, base_dir, nic):
     """
     global dut
     global tester
-    serializer.set_serialized_filename('../.%s.cache' % crbInst['IP'])
+    serializer.set_serialized_filename('.%s.cache' % crbInst['IP'])
     serializer.load_from_file()
 
     dut = get_project_obj(project, Dut, crbInst, serializer)
@@ -337,7 +278,6 @@ def dts_run_target(crbInst, targets, test_suites, nic):
         if 'nic_type' not in paramDict:
             paramDict['nic_type'] = 'any'
             nic = 'any'
-        result.nic = nic
 
         dts_run_suite(crbInst, test_suites, target, nic)
 
@@ -359,7 +299,9 @@ def dts_run_suite(crbInst, test_suites, target, nic):
             test_module = __import__('TestSuite_' + test_suite)
             for test_classname, test_class in get_subclasses(test_module, TestCase):
 
-                test_suite = test_class(dut, tester, target)
+                test_suite = test_class(dut, tester, target, test_suite)
+                result.nic = test_suite.nic
+
                 dts_log_testsuite(test_suite, log_handler, test_classname)
 
                 log_handler.info("\nTEST SUITE : " + test_classname)
@@ -400,6 +342,12 @@ def run_all(config_file, pkgName, git, patch, skip_setup,
     global stats
     global log_handler
     global debug_mode
+    global Package
+    global Patches
+
+    # save global variable
+    Package = pkgName
+    Patches = patch
 
     # prepare the output folder
     if not os.path.exists(output_dir):
diff --git a/framework/settings.py b/framework/settings.py
index feb6fa5..2eccc64 100644
--- a/framework/settings.py
+++ b/framework/settings.py
@@ -48,6 +48,7 @@ NICS = {
     'powerville': '8086:1521',
     'ophir': '8086:105e',
     'niantic': '8086:10fb',
+    'niantic_vf': '8086:10ed',
     'ironpond': '8086:151c',
     'twinpond': '8086:1528',
     'twinville': '8086:1512',
@@ -77,6 +78,7 @@ DRIVERS = {
     'powerville': 'igb',
     'ophir': 'igb',
     'niantic': 'ixgbe',
+    'niantic_vf': 'ixgbevf',
     'ironpond': 'ixgbe',
     'twinpond': 'ixgbe',
     'twinville': 'ixgbe',
@@ -137,6 +139,17 @@ Global macro for dts.
 """
 IXIA = "ixia"
 
+"""
+The root path of framework configs.
+"""
+CONFIG_ROOT_PATH = "./conf/"
+
+"""
+The log name seperater.
+"""
+LOG_NAME_SEP = '.'
+
+
 def nic_name_from_type(type):
     """
     strip nic code name by nic type
@@ -145,3 +158,33 @@ def nic_name_from_type(type):
         if nic_type == type:
             return name
     return 'Unknown'
+
+
+def get_nic_driver(pci_id):
+    """
+    Return linux driver for specified pci device
+    """
+    driverlist = dict(zip(NICS.values(), DRIVERS.keys()))
+    try:
+        driver = DRIVERS[driverlist[pci_id]]
+    except Exception as e:
+        driver = None
+    return driver
+
+
+def accepted_nic(pci_id):
+    """
+    Return True if the pci_id is a known NIC card in the settings file and if
+    it is selected in the execution file, otherwise it returns False.
+    """
+    if pci_id not in NICS.values():
+        return False
+
+    if nic is 'any':
+        return True
+
+    else:
+        if pci_id == NICS[nic]:
+            return True
+
+    return False
diff --git a/framework/utils.py b/framework/utils.py
new file mode 100644
index 0000000..dd45e9e
--- /dev/null
+++ b/framework/utils.py
@@ -0,0 +1,100 @@
+# BSD LICENSE
+#
+# Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+#   * Redistributions of source code must retain the above copyright
+#     notice, this list of conditions and the following disclaimer.
+#   * Redistributions in binary form must reproduce the above copyright
+#     notice, this list of conditions and the following disclaimer in
+#     the documentation and/or other materials provided with the
+#     distribution.
+#   * Neither the name of Intel Corporation nor the names of its
+#     contributors may be used to endorse or promote products derived
+#     from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+import json         # json format
+import re
+
+
+def RED(text):
+    return "\x1B[" + "31;1m" + str(text) + "\x1B[" + "0m"
+
+
+def BLUE(text):
+    return "\x1B[" + "36;1m" + str(text) + "\x1B[" + "0m"
+
+
+def GREEN(text):
+    return "\x1B[" + "32;1m" + str(text) + "\x1B[" + "0m"
+
+
+def pprint(some_dict):
+    """
+    Print JSON format dictionary object.
+    """
+    return json.dumps(some_dict, sort_keys=True, indent=4)
+
+
+def regexp(s, to_match, allString=False):
+    """
+    Ensure that the re `to_match' only has one group in it.
+    """
+
+    scanner = re.compile(to_match, re.DOTALL)
+    if allString:
+        return scanner.findall(s)
+    m = scanner.search(s)
+    if m is None:
+        print RED("Failed to match " + to_match + " in the string " + s)
+        return None
+    return m.group(1)
+
+
+def get_obj_funcs(obj, func_name_regex):
+    """
+    Return function list which name matched regex.
+    """
+    for func_name in dir(obj):
+        func = getattr(obj, func_name)
+        if callable(func) and re.match(func_name_regex, func.__name__):
+            yield func
+
+
+def remove_old_rsa_key(crb, ip):
+    """
+    Remove the old RSA key of specified IP on crb.
+    """
+    if ':' not in ip:
+        ip = ip.strip()
+        port = ''
+    else:
+        addr = ip.split(':')
+        ip = addr[0].strip()
+        port = addr[1].strip()
+
+    rsa_key_path = "~/.ssh/known_hosts"
+    if port:
+        remove_rsa_key_cmd = "sed -i '/^\[%s\]:%d/d' %s" % \
+            (ip.strip(), int(
+             port), rsa_key_path)
+    else:
+        remove_rsa_key_cmd = "sed -i '/^%s/d' %s" % \
+            (ip.strip(), rsa_key_path)
+    crb.send_expect(remove_rsa_key_cmd, "# ")
-- 
1.9.3



More information about the dts mailing list