add ci_target filter for target and local target check

This commit is contained in:
Fu Hanxi 2020-04-09 13:42:13 +08:00
parent 9f8b63da38
commit bc026133c5
2 changed files with 61 additions and 20 deletions

View File

@ -13,6 +13,8 @@
# limitations under the License. # limitations under the License.
""" Interface for test cases. """ """ Interface for test cases. """
import json
import logging
import os import os
import time import time
import traceback import traceback
@ -163,6 +165,22 @@ def test_method(**kwargs):
case_info = MANDATORY_INFO.copy() case_info = MANDATORY_INFO.copy()
case_info["name"] = case_info["ID"] = test_func.__name__ case_info["name"] = case_info["ID"] = test_func.__name__
case_info["junit_report_by_case"] = False case_info["junit_report_by_case"] = False
def _filter_ci_target(target, ci_target):
if not ci_target:
return target
if isinstance(target, str):
if isinstance(ci_target, str) and target == ci_target:
return ci_target
else:
if isinstance(ci_target, str) and ci_target in target:
return ci_target
elif isinstance(ci_target, list) and set(ci_target).issubset(set(target)):
return ci_target
raise ValueError('ci_target must be a subset of target')
if os.getenv('CI_JOB_NAME') and 'ci_target' in kwargs:
kwargs['target'] = _filter_ci_target(kwargs['target'], kwargs['ci_target'])
case_info.update(kwargs) case_info.update(kwargs)
@functools.wraps(test_func) @functools.wraps(test_func)
@ -183,15 +201,32 @@ def test_method(**kwargs):
# Runner.py should overwrite target with the current target. # Runner.py should overwrite target with the current target.
env_config.update(overwrite) env_config.update(overwrite)
# This code block is used to run test script locally without # if target not in the default_config or the overwrite, then take it from kwargs passed from the decorator
# Runner.py
target = env_config['target'] if 'target' in env_config else kwargs['target'] target = env_config['target'] if 'target' in env_config else kwargs['target']
if isinstance(target, list):
target = target[0] # This code block is used to do run local test script target set check
elif isinstance(target, str): if not os.getenv('CI_JOB_NAME'):
target = target idf_target = 'ESP32' # default if sdkconfig not found or not readable
else: expected_json_path = os.path.join('build', 'config', 'sdkconfig.json')
raise TypeError('keyword targets can only be list or str') if os.path.exists(expected_json_path):
sdkconfig = json.load(open(expected_json_path))
try:
idf_target = sdkconfig['IDF_TARGET'].upper()
except KeyError:
pass
else:
logging.info('IDF_TARGET: {}'.format(idf_target))
else:
logging.warning('{} not found. IDF_TARGET set to esp32'.format(os.path.abspath(expected_json_path)))
if isinstance(target, list):
if idf_target in target:
target = idf_target
else:
raise ValueError('IDF_TARGET set to {}, not in decorator target value'.format(idf_target))
else:
if idf_target != target:
raise ValueError('IDF_TARGET set to {}, not equal to decorator target value'.format(idf_target))
dut_dict = kwargs['dut_dict'] dut_dict = kwargs['dut_dict']
if target not in dut_dict: if target not in dut_dict:

View File

@ -30,13 +30,14 @@ def format_case_id(chip, case_name):
return "{}.{}".format(chip, case_name) return "{}.{}".format(chip, case_name)
def idf_example_test(app=Example, target="ESP32", module="examples", execution_time=1, def idf_example_test(app=Example, target="ESP32", ci_target=None, module="examples", execution_time=1,
level="example", erase_nvs=True, config_name=None, **kwargs): level="example", erase_nvs=True, config_name=None, **kwargs):
""" """
decorator for testing idf examples (with default values for some keyword args). decorator for testing idf examples (with default values for some keyword args).
:param app: test application class :param app: test application class
:param target: target supported, string or iterable :param target: target supported, string or list
:param ci_target: target auto run in CI, if None than all target will be tested, None, string or list
:param module: module, string :param module: module, string
:param execution_time: execution time in minutes, int :param execution_time: execution time in minutes, int
:param level: test level, could be used to filter test cases, string :param level: test level, could be used to filter test cases, string
@ -47,8 +48,9 @@ def idf_example_test(app=Example, target="ESP32", module="examples", execution_t
""" """
def test(func): def test(func):
original_method = TinyFW.test_method(app=app, target=target, module=module, execution_time=execution_time, original_method = TinyFW.test_method(app=app, target=target, ci_target=ci_target, module=module,
level=level, dut_dict=TARGET_DUT_CLS_DICT, erase_nvs=erase_nvs, **kwargs) execution_time=execution_time, level=level, dut_dict=TARGET_DUT_CLS_DICT,
erase_nvs=erase_nvs, **kwargs)
test_func = original_method(func) test_func = original_method(func)
test_func.case_info["ID"] = format_case_id(target, test_func.case_info["name"]) test_func.case_info["ID"] = format_case_id(target, test_func.case_info["name"])
return test_func return test_func
@ -56,13 +58,14 @@ def idf_example_test(app=Example, target="ESP32", module="examples", execution_t
return test return test
def idf_unit_test(app=UT, target="ESP32", module="unit-test", execution_time=1, def idf_unit_test(app=UT, target="ESP32", ci_target=None, module="unit-test", execution_time=1,
level="unit", erase_nvs=True, **kwargs): level="unit", erase_nvs=True, **kwargs):
""" """
decorator for testing idf unit tests (with default values for some keyword args). decorator for testing idf unit tests (with default values for some keyword args).
:param app: test application class :param app: test application class
:param target: target supported, string or iterable :param target: target supported, string or list
:param ci_target: target auto run in CI, if None than all target will be tested, None, string or list
:param module: module, string :param module: module, string
:param execution_time: execution time in minutes, int :param execution_time: execution time in minutes, int
:param level: test level, could be used to filter test cases, string :param level: test level, could be used to filter test cases, string
@ -72,8 +75,9 @@ def idf_unit_test(app=UT, target="ESP32", module="unit-test", execution_time=1,
""" """
def test(func): def test(func):
original_method = TinyFW.test_method(app=app, target=target, module=module, execution_time=execution_time, original_method = TinyFW.test_method(app=app, target=target, ci_target=ci_target, module=module,
level=level, dut_dict=TARGET_DUT_CLS_DICT, erase_nvs=erase_nvs, **kwargs) execution_time=execution_time, level=level, dut_dict=TARGET_DUT_CLS_DICT,
erase_nvs=erase_nvs, **kwargs)
test_func = original_method(func) test_func = original_method(func)
test_func.case_info["ID"] = format_case_id(target, test_func.case_info["name"]) test_func.case_info["ID"] = format_case_id(target, test_func.case_info["name"])
return test_func return test_func
@ -81,13 +85,14 @@ def idf_unit_test(app=UT, target="ESP32", module="unit-test", execution_time=1,
return test return test
def idf_custom_test(app=TestApp, target="ESP32", module="misc", execution_time=1, def idf_custom_test(app=TestApp, target="ESP32", ci_target=None, module="misc", execution_time=1,
level="integration", erase_nvs=True, config_name=None, group="test-apps", **kwargs): level="integration", erase_nvs=True, config_name=None, group="test-apps", **kwargs):
""" """
decorator for idf custom tests (with default values for some keyword args). decorator for idf custom tests (with default values for some keyword args).
:param app: test application class :param app: test application class
:param target: target supported, string or iterable :param target: target supported, string or list
:param ci_target: target auto run in CI, if None than all target will be tested, None, string or list
:param module: module, string :param module: module, string
:param execution_time: execution time in minutes, int :param execution_time: execution time in minutes, int
:param level: test level, could be used to filter test cases, string :param level: test level, could be used to filter test cases, string
@ -99,8 +104,9 @@ def idf_custom_test(app=TestApp, target="ESP32", module="misc", execution_time=1
""" """
def test(func): def test(func):
original_method = TinyFW.test_method(app=app, target=target, module=module, execution_time=execution_time, original_method = TinyFW.test_method(app=app, target=target, ci_target=ci_target, module=module,
level=level, dut_dict=TARGET_DUT_CLS_DICT, erase_nvs=erase_nvs, **kwargs) execution_time=execution_time, level=level, dut_dict=TARGET_DUT_CLS_DICT,
erase_nvs=erase_nvs, **kwargs)
test_func = original_method(func) test_func = original_method(func)
test_func.case_info["ID"] = format_case_id(target, test_func.case_info["name"]) test_func.case_info["ID"] = format_case_id(target, test_func.case_info["name"])
return test_func return test_func