2017-10-09 22:44:55 -04:00
|
|
|
# Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http:#www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2020-04-20 03:12:03 -04:00
|
|
|
import functools
|
2020-05-29 00:39:58 -04:00
|
|
|
import json
|
|
|
|
import logging
|
2017-11-07 23:27:57 -05:00
|
|
|
import os
|
|
|
|
import re
|
2017-10-09 22:44:55 -04:00
|
|
|
|
2020-08-25 04:42:36 -04:00
|
|
|
import junit_xml
|
|
|
|
|
2019-11-26 22:21:33 -05:00
|
|
|
from tiny_test_fw import TinyFW, Utility
|
2020-05-19 08:27:31 -04:00
|
|
|
from .DebugUtils import OCDBackend, GDBBackend, CustomProcess # noqa: export DebugUtils for users
|
2020-08-25 04:42:36 -04:00
|
|
|
from .IDFApp import IDFApp, Example, LoadableElfTestApp, UT, TestApp, ComponentUTApp # noqa: export all Apps for users
|
|
|
|
from .IDFDUT import IDFDUT, ESP32DUT, ESP32S2DUT, ESP8266DUT, ESP32QEMUDUT # noqa: export DUTs for users
|
|
|
|
from .unity_test_parser import TestResults, TestFormat
|
2017-10-09 22:44:55 -04:00
|
|
|
|
2020-04-03 04:16:40 -04:00
|
|
|
# pass TARGET_DUT_CLS_DICT to Env.py to avoid circular dependency issue.
|
|
|
|
TARGET_DUT_CLS_DICT = {
|
|
|
|
'ESP32': ESP32DUT,
|
|
|
|
'ESP32S2': ESP32S2DUT,
|
|
|
|
}
|
|
|
|
|
2017-10-09 22:44:55 -04:00
|
|
|
|
2020-05-29 00:39:58 -04:00
|
|
|
def format_case_id(target, case_name):
|
|
|
|
return "{}.{}".format(target, case_name)
|
2019-04-01 03:16:47 -04:00
|
|
|
|
|
|
|
|
2020-04-29 23:49:51 -04:00
|
|
|
try:
|
|
|
|
string_type = basestring
|
|
|
|
except NameError:
|
|
|
|
string_type = str
|
|
|
|
|
|
|
|
|
2020-05-29 00:39:58 -04:00
|
|
|
def upper_list_or_str(text):
|
|
|
|
"""
|
|
|
|
Return the uppercase of list of string or string. Return itself for other
|
|
|
|
data types
|
|
|
|
:param text: list or string, other instance will be returned immediately
|
|
|
|
:return: uppercase of list of string
|
|
|
|
"""
|
2020-04-29 23:49:51 -04:00
|
|
|
if isinstance(text, string_type):
|
2020-05-29 00:39:58 -04:00
|
|
|
return [text.upper()]
|
|
|
|
elif isinstance(text, list):
|
|
|
|
return [item.upper() for item in text]
|
|
|
|
else:
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
|
|
def local_test_check(decorator_target):
|
|
|
|
# Try to get the sdkconfig.json to read the IDF_TARGET value.
|
|
|
|
# If not set, will set to ESP32.
|
|
|
|
# For CI jobs, this is a fake procedure, the true target and dut will be
|
|
|
|
# overwritten by the job config YAML file.
|
|
|
|
idf_target = 'ESP32' # default if sdkconfig not found or not readable
|
|
|
|
if os.getenv('CI_JOB_ID'): # Only auto-detect target when running locally
|
|
|
|
return idf_target
|
|
|
|
|
2020-07-21 04:00:05 -04:00
|
|
|
decorator_target = upper_list_or_str(decorator_target)
|
2020-05-29 00:39:58 -04:00
|
|
|
expected_json_path = os.path.join('build', 'config', 'sdkconfig.json')
|
|
|
|
if os.path.exists(expected_json_path):
|
|
|
|
sdkconfig = json.load(open(expected_json_path))
|
|
|
|
try:
|
|
|
|
idf_target = sdkconfig['IDF_TARGET'].upper()
|
|
|
|
except KeyError:
|
|
|
|
logging.warning('IDF_TARGET not in {}. IDF_TARGET set to esp32'.format(os.path.abspath(expected_json_path)))
|
|
|
|
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(decorator_target, list):
|
|
|
|
if idf_target not in decorator_target:
|
|
|
|
raise ValueError('IDF_TARGET set to {}, not in decorator target value'.format(idf_target))
|
2020-04-20 03:12:03 -04:00
|
|
|
else:
|
2020-05-29 00:39:58 -04:00
|
|
|
if idf_target != decorator_target:
|
|
|
|
raise ValueError('IDF_TARGET set to {}, not equal to decorator target value'.format(idf_target))
|
|
|
|
return idf_target
|
|
|
|
|
|
|
|
|
|
|
|
def get_dut_class(target, erase_nvs=None):
|
|
|
|
if target not in TARGET_DUT_CLS_DICT:
|
|
|
|
raise Exception('target can only be {%s} (case insensitive)' % ', '.join(TARGET_DUT_CLS_DICT.keys()))
|
|
|
|
|
|
|
|
dut = TARGET_DUT_CLS_DICT[target.upper()]
|
|
|
|
if erase_nvs:
|
|
|
|
dut.ERASE_NVS = 'erase_nvs'
|
|
|
|
return dut
|
2020-04-20 03:12:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
def ci_target_check(func):
|
|
|
|
@functools.wraps(func)
|
|
|
|
def wrapper(**kwargs):
|
2020-05-29 00:39:58 -04:00
|
|
|
target = upper_list_or_str(kwargs.get('target', []))
|
|
|
|
ci_target = upper_list_or_str(kwargs.get('ci_target', []))
|
2020-04-20 03:12:03 -04:00
|
|
|
if not set(ci_target).issubset(set(target)):
|
|
|
|
raise ValueError('ci_target must be a subset of target')
|
|
|
|
|
|
|
|
return func(**kwargs)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2020-08-25 04:42:36 -04:00
|
|
|
def test_func_generator(func, app, target, ci_target, module, execution_time, level, erase_nvs, drop_kwargs_dut=False, **kwargs):
|
|
|
|
test_target = local_test_check(target)
|
|
|
|
dut = get_dut_class(test_target, erase_nvs)
|
|
|
|
if drop_kwargs_dut and 'dut' in kwargs: # panic_test() will inject dut, resolve conflicts here
|
|
|
|
dut = kwargs['dut']
|
|
|
|
del kwargs['dut']
|
|
|
|
original_method = TinyFW.test_method(
|
|
|
|
app=app, dut=dut, target=upper_list_or_str(target), ci_target=upper_list_or_str(ci_target),
|
|
|
|
module=module, execution_time=execution_time, level=level, erase_nvs=erase_nvs,
|
|
|
|
dut_dict=TARGET_DUT_CLS_DICT, **kwargs
|
|
|
|
)
|
|
|
|
test_func = original_method(func)
|
|
|
|
test_func.case_info["ID"] = format_case_id(target, test_func.case_info["name"])
|
|
|
|
return test_func
|
|
|
|
|
|
|
|
|
2020-04-20 03:12:03 -04:00
|
|
|
@ci_target_check
|
2020-04-09 01:42:13 -04:00
|
|
|
def idf_example_test(app=Example, target="ESP32", ci_target=None, module="examples", execution_time=1,
|
2019-10-15 08:15:06 -04:00
|
|
|
level="example", erase_nvs=True, config_name=None, **kwargs):
|
2017-10-09 22:44:55 -04:00
|
|
|
"""
|
|
|
|
decorator for testing idf examples (with default values for some keyword args).
|
|
|
|
|
|
|
|
:param app: test application class
|
2020-04-09 01:42:13 -04:00
|
|
|
: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
|
2017-10-09 22:44:55 -04:00
|
|
|
:param module: module, string
|
|
|
|
:param execution_time: execution time in minutes, int
|
2018-06-15 05:45:24 -04:00
|
|
|
:param level: test level, could be used to filter test cases, string
|
2018-07-26 03:07:32 -04:00
|
|
|
:param erase_nvs: if need to erase_nvs in DUT.start_app()
|
2019-10-15 08:15:06 -04:00
|
|
|
:param config_name: if specified, name of the app configuration
|
2017-10-09 22:44:55 -04:00
|
|
|
:param kwargs: other keyword args
|
|
|
|
:return: test method
|
|
|
|
"""
|
2019-04-01 03:16:47 -04:00
|
|
|
def test(func):
|
2020-08-25 04:42:36 -04:00
|
|
|
return test_func_generator(func, app, target, ci_target, module, execution_time, level, erase_nvs, **kwargs)
|
2019-04-01 03:16:47 -04:00
|
|
|
return test
|
2018-07-26 03:07:32 -04:00
|
|
|
|
|
|
|
|
2020-04-20 03:12:03 -04:00
|
|
|
@ci_target_check
|
2020-04-09 01:42:13 -04:00
|
|
|
def idf_unit_test(app=UT, target="ESP32", ci_target=None, module="unit-test", execution_time=1,
|
2018-07-26 03:07:32 -04:00
|
|
|
level="unit", erase_nvs=True, **kwargs):
|
|
|
|
"""
|
|
|
|
decorator for testing idf unit tests (with default values for some keyword args).
|
|
|
|
|
|
|
|
:param app: test application class
|
2020-04-09 01:42:13 -04:00
|
|
|
: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
|
2018-07-26 03:07:32 -04:00
|
|
|
:param module: module, string
|
|
|
|
:param execution_time: execution time in minutes, int
|
|
|
|
:param level: test level, could be used to filter test cases, string
|
|
|
|
:param erase_nvs: if need to erase_nvs in DUT.start_app()
|
|
|
|
:param kwargs: other keyword args
|
|
|
|
:return: test method
|
2019-10-20 14:55:11 -04:00
|
|
|
"""
|
|
|
|
def test(func):
|
2020-08-25 04:42:36 -04:00
|
|
|
return test_func_generator(func, app, target, ci_target, module, execution_time, level, erase_nvs, **kwargs)
|
2019-10-20 14:55:11 -04:00
|
|
|
return test
|
|
|
|
|
|
|
|
|
2020-04-20 03:12:03 -04:00
|
|
|
@ci_target_check
|
2020-04-09 01:42:13 -04:00
|
|
|
def idf_custom_test(app=TestApp, target="ESP32", ci_target=None, module="misc", execution_time=1,
|
2020-08-25 04:42:36 -04:00
|
|
|
level="integration", erase_nvs=True, config_name=None, **kwargs):
|
|
|
|
"""
|
|
|
|
decorator for idf custom tests (with default values for some keyword args).
|
|
|
|
|
|
|
|
:param app: test application class
|
|
|
|
: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 execution_time: execution time in minutes, int
|
|
|
|
:param level: test level, could be used to filter test cases, string
|
|
|
|
:param erase_nvs: if need to erase_nvs in DUT.start_app()
|
|
|
|
:param config_name: if specified, name of the app configuration
|
|
|
|
:param kwargs: other keyword args
|
|
|
|
:return: test method
|
|
|
|
"""
|
|
|
|
def test(func):
|
|
|
|
return test_func_generator(func, app, target, ci_target, module, execution_time, level, erase_nvs, drop_kwargs_dut=True, **kwargs)
|
|
|
|
return test
|
|
|
|
|
|
|
|
|
|
|
|
@ci_target_check
|
|
|
|
def idf_component_unit_test(app=ComponentUTApp, target="ESP32", ci_target=None, module="misc", execution_time=1,
|
|
|
|
level="integration", erase_nvs=True, config_name=None, **kwargs):
|
2019-10-20 14:55:11 -04:00
|
|
|
"""
|
2020-01-27 06:12:49 -05:00
|
|
|
decorator for idf custom tests (with default values for some keyword args).
|
2019-10-20 14:55:11 -04:00
|
|
|
|
|
|
|
:param app: test application class
|
2020-04-09 01:42:13 -04:00
|
|
|
: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
|
2019-10-20 14:55:11 -04:00
|
|
|
:param module: module, string
|
|
|
|
:param execution_time: execution time in minutes, int
|
|
|
|
:param level: test level, could be used to filter test cases, string
|
|
|
|
:param erase_nvs: if need to erase_nvs in DUT.start_app()
|
2020-01-27 06:12:49 -05:00
|
|
|
:param config_name: if specified, name of the app configuration
|
2019-10-20 14:55:11 -04:00
|
|
|
:param kwargs: other keyword args
|
|
|
|
:return: test method
|
2018-07-26 03:07:32 -04:00
|
|
|
"""
|
2019-04-01 03:16:47 -04:00
|
|
|
|
|
|
|
def test(func):
|
2020-08-25 04:42:36 -04:00
|
|
|
return test_func_generator(func, app, target, ci_target, module, execution_time, level, erase_nvs, **kwargs)
|
2019-04-01 03:16:47 -04:00
|
|
|
|
|
|
|
return test
|
2017-11-07 23:27:57 -05:00
|
|
|
|
|
|
|
|
2020-08-25 04:42:36 -04:00
|
|
|
class ComponentUTResult:
|
|
|
|
"""
|
|
|
|
Function Class, parse component unit test results
|
|
|
|
"""
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def parse_result(stdout):
|
|
|
|
try:
|
|
|
|
results = TestResults(stdout, TestFormat.UNITY_FIXTURE_VERBOSE)
|
|
|
|
except (ValueError, TypeError) as e:
|
|
|
|
raise ValueError('Error occurs when parsing the component unit test stdout to JUnit report: ' + str(e))
|
|
|
|
|
|
|
|
group_name = results.tests()[0].group()
|
|
|
|
with open(os.path.join(os.getenv('LOG_PATH', ''), '{}_XUNIT_RESULT.xml'.format(group_name)), 'w') as fw:
|
|
|
|
junit_xml.to_xml_report_file(fw, [results.to_junit()])
|
|
|
|
|
|
|
|
if results.num_failed():
|
|
|
|
# raise exception if any case fails
|
|
|
|
err_msg = 'Failed Cases:\n'
|
|
|
|
for test_case in results.test_iter():
|
|
|
|
if test_case.result() == 'FAIL':
|
|
|
|
err_msg += '\t{}: {}'.format(test_case.name(), test_case.message())
|
|
|
|
raise AssertionError(err_msg)
|
|
|
|
|
|
|
|
|
2017-11-07 23:27:57 -05:00
|
|
|
def log_performance(item, value):
|
|
|
|
"""
|
|
|
|
do print performance with pre-defined format to console
|
|
|
|
|
|
|
|
:param item: performance item name
|
|
|
|
:param value: performance value
|
|
|
|
"""
|
2018-07-13 04:47:42 -04:00
|
|
|
performance_msg = "[Performance][{}]: {}".format(item, value)
|
|
|
|
Utility.console_log(performance_msg, "orange")
|
|
|
|
# update to junit test report
|
|
|
|
current_junit_case = TinyFW.JunitReport.get_current_test_case()
|
2018-07-13 04:48:43 -04:00
|
|
|
current_junit_case.stdout += performance_msg + "\r\n"
|
2017-11-07 23:27:57 -05:00
|
|
|
|
|
|
|
|
2020-02-28 10:12:11 -05:00
|
|
|
def check_performance(item, value, target):
|
2017-11-07 23:27:57 -05:00
|
|
|
"""
|
|
|
|
check if idf performance meet pass standard
|
|
|
|
|
|
|
|
:param item: performance item name
|
|
|
|
:param value: performance item value
|
2020-02-28 10:12:11 -05:00
|
|
|
:param target: target chip
|
2017-11-07 23:27:57 -05:00
|
|
|
:raise: AssertionError: if check fails
|
|
|
|
"""
|
|
|
|
|
2020-02-28 10:12:11 -05:00
|
|
|
def _find_perf_item(path):
|
|
|
|
with open(path, 'r') as f:
|
2017-11-07 23:27:57 -05:00
|
|
|
data = f.read()
|
2020-02-28 10:12:11 -05:00
|
|
|
match = re.search(r'#define\s+IDF_PERFORMANCE_(MIN|MAX)_{}\s+([\d.]+)'.format(item.upper()), data)
|
|
|
|
return match.group(1), float(match.group(2))
|
|
|
|
|
|
|
|
def _check_perf(op, standard_value):
|
|
|
|
if op == 'MAX':
|
|
|
|
ret = value <= standard_value
|
|
|
|
else:
|
|
|
|
ret = value >= standard_value
|
|
|
|
if not ret:
|
|
|
|
raise AssertionError("[Performance] {} value is {}, doesn't meet pass standard {}"
|
|
|
|
.format(item, value, standard_value))
|
|
|
|
|
|
|
|
path_prefix = os.path.join(IDFApp.get_sdk_path(), 'components', 'idf_test', 'include')
|
|
|
|
performance_files = (os.path.join(path_prefix, target, 'idf_performance_target.h'),
|
|
|
|
os.path.join(path_prefix, 'idf_performance.h'))
|
|
|
|
|
|
|
|
for performance_file in performance_files:
|
|
|
|
try:
|
2020-08-10 07:44:34 -04:00
|
|
|
op, standard = _find_perf_item(performance_file)
|
2020-02-28 10:12:11 -05:00
|
|
|
except (IOError, AttributeError):
|
|
|
|
# performance file doesn't exist or match is not found in it
|
|
|
|
continue
|
|
|
|
|
2020-08-10 07:44:34 -04:00
|
|
|
_check_perf(op, standard)
|
2020-02-28 10:12:11 -05:00
|
|
|
# if no exception was thrown then the performance is met and no need to continue
|
|
|
|
break
|
2020-08-10 07:44:34 -04:00
|
|
|
else:
|
|
|
|
raise AssertionError("Failed to get performance standard for {}".format(item))
|
2020-07-21 04:00:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
MINIMUM_FREE_HEAP_SIZE_RE = re.compile(r'Minimum free heap size: (\d+) bytes')
|
|
|
|
|
|
|
|
|
|
|
|
def print_heap_size(app_name, config_name, target, minimum_free_heap_size):
|
|
|
|
"""
|
|
|
|
Do not change the print output in case you really need to.
|
|
|
|
The result is parsed by ci-dashboard project
|
|
|
|
"""
|
|
|
|
print('------ heap size info ------\n'
|
|
|
|
'[app_name] {}\n'
|
|
|
|
'[config_name] {}\n'
|
|
|
|
'[target] {}\n'
|
|
|
|
'[minimum_free_heap_size] {} Bytes\n'
|
|
|
|
'------ heap size end ------'.format(app_name,
|
|
|
|
'' if not config_name else config_name,
|
|
|
|
target,
|
|
|
|
minimum_free_heap_size))
|