2018-09-10 13:04:30 +00:00
|
|
|
from __future__ import print_function
|
2017-03-24 10:38:06 +00:00
|
|
|
import yaml
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
from copy import deepcopy
|
|
|
|
import CreateSectionTable
|
|
|
|
|
|
|
|
TEST_CASE_PATTERN = {
|
|
|
|
"initial condition": "UTINIT1",
|
|
|
|
"SDK": "ESP32_IDF",
|
|
|
|
"level": "Unit",
|
|
|
|
"execution time": 0,
|
|
|
|
"auto test": "Yes",
|
|
|
|
"category": "Function",
|
|
|
|
"test point 1": "basic function",
|
|
|
|
"version": "v1 (2016-12-06)",
|
|
|
|
"test environment": "UT_T1_1",
|
2017-09-20 09:17:51 +00:00
|
|
|
"reset": "",
|
2018-01-31 10:59:10 +00:00
|
|
|
"expected result": "1. set succeed",
|
2018-05-15 03:51:56 +00:00
|
|
|
"cmd set": "test_unit_test_case",
|
|
|
|
"Test App": "UT",
|
2017-03-24 10:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class Parser(object):
|
|
|
|
""" parse unit test cases from build files and create files for test bench """
|
|
|
|
|
|
|
|
TAG_PATTERN = re.compile("([^=]+)(=)?(.+)?")
|
2018-12-04 12:46:48 +00:00
|
|
|
DESCRIPTION_PATTERN = re.compile("\[([^]\[]+)\]") # noqa: W605 - regular expression
|
2018-09-27 07:23:44 +00:00
|
|
|
CONFIG_PATTERN = re.compile(r"{([^}]+)}")
|
2018-11-13 11:48:31 +00:00
|
|
|
TEST_GROUPS_PATTERN = re.compile(r"TEST_GROUPS=(.*)$")
|
2017-03-24 10:38:06 +00:00
|
|
|
|
2017-09-13 12:39:43 +00:00
|
|
|
# file path (relative to idf path)
|
|
|
|
TAG_DEF_FILE = os.path.join("tools", "unit-test-app", "tools", "TagDefinition.yml")
|
|
|
|
MODULE_DEF_FILE = os.path.join("tools", "unit-test-app", "tools", "ModuleDefinition.yml")
|
2018-05-15 03:51:56 +00:00
|
|
|
CONFIG_DEPENDENCY_FILE = os.path.join("tools", "unit-test-app", "tools", "ConfigDependency.yml")
|
2017-09-13 12:39:43 +00:00
|
|
|
MODULE_ARTIFACT_FILE = os.path.join("components", "idf_test", "ModuleDefinition.yml")
|
|
|
|
TEST_CASE_FILE = os.path.join("components", "idf_test", "unit_test", "TestCaseAll.yml")
|
2018-05-15 03:51:56 +00:00
|
|
|
UT_BIN_FOLDER = os.path.join("tools", "unit-test-app", "output")
|
2018-11-13 11:48:31 +00:00
|
|
|
UT_CONFIG_FOLDER = os.path.join("tools", "unit-test-app", "configs")
|
2017-09-13 12:39:43 +00:00
|
|
|
ELF_FILE = "unit-test-app.elf"
|
2018-05-15 03:51:56 +00:00
|
|
|
SDKCONFIG_FILE = "sdkconfig"
|
2017-09-13 12:39:43 +00:00
|
|
|
|
2017-03-24 10:38:06 +00:00
|
|
|
def __init__(self, idf_path=os.getenv("IDF_PATH")):
|
|
|
|
self.test_env_tags = {}
|
|
|
|
self.unit_jobs = {}
|
|
|
|
self.file_name_cache = {}
|
|
|
|
self.idf_path = idf_path
|
2017-09-13 12:39:43 +00:00
|
|
|
self.tag_def = yaml.load(open(os.path.join(idf_path, self.TAG_DEF_FILE), "r"))
|
|
|
|
self.module_map = yaml.load(open(os.path.join(idf_path, self.MODULE_DEF_FILE), "r"))
|
2018-09-27 07:23:44 +00:00
|
|
|
self.config_dependencies = yaml.load(open(os.path.join(idf_path, self.CONFIG_DEPENDENCY_FILE), "r"))
|
2017-09-13 12:39:43 +00:00
|
|
|
# used to check if duplicated test case names
|
|
|
|
self.test_case_names = set()
|
|
|
|
self.parsing_errors = []
|
2017-03-24 10:38:06 +00:00
|
|
|
|
2018-11-13 11:48:31 +00:00
|
|
|
def parse_test_cases_for_one_config(self, configs_folder, config_output_folder, config_name):
|
2017-03-24 10:38:06 +00:00
|
|
|
"""
|
2017-09-13 12:39:43 +00:00
|
|
|
parse test cases from elf and save test cases need to be executed to unit test folder
|
2018-11-13 11:48:31 +00:00
|
|
|
:param configs_folder: folder where per-config sdkconfig framents are located (i.e. tools/unit-test-app/configs)
|
2018-05-15 03:51:56 +00:00
|
|
|
:param config_output_folder: build folder of this config
|
|
|
|
:param config_name: built unit test config name
|
2017-03-24 10:38:06 +00:00
|
|
|
"""
|
2018-11-13 11:48:31 +00:00
|
|
|
test_groups = self.get_test_groups(os.path.join(configs_folder, config_name))
|
|
|
|
|
2018-05-15 03:51:56 +00:00
|
|
|
elf_file = os.path.join(config_output_folder, self.ELF_FILE)
|
2017-11-23 08:51:28 +00:00
|
|
|
subprocess.check_output('xtensa-esp32-elf-objdump -t {} | grep test_desc > case_address.tmp'.format(elf_file),
|
2017-03-24 10:38:06 +00:00
|
|
|
shell=True)
|
|
|
|
subprocess.check_output('xtensa-esp32-elf-objdump -s {} > section_table.tmp'.format(elf_file), shell=True)
|
|
|
|
|
|
|
|
table = CreateSectionTable.SectionTable("section_table.tmp")
|
2018-05-15 03:51:56 +00:00
|
|
|
tags = self.parse_tags(os.path.join(config_output_folder, self.SDKCONFIG_FILE))
|
2017-03-24 10:38:06 +00:00
|
|
|
test_cases = []
|
2018-10-10 11:19:31 +00:00
|
|
|
with open("case_address.tmp", "rb") as f:
|
2017-03-24 10:38:06 +00:00
|
|
|
for line in f:
|
|
|
|
# process symbol table like: "3ffb4310 l O .dram0.data 00000018 test_desc_33$5010"
|
|
|
|
line = line.split()
|
|
|
|
test_addr = int(line[0], 16)
|
|
|
|
section = line[3]
|
|
|
|
|
|
|
|
name_addr = table.get_unsigned_int(section, test_addr, 4)
|
|
|
|
desc_addr = table.get_unsigned_int(section, test_addr + 4, 4)
|
|
|
|
file_name_addr = table.get_unsigned_int(section, test_addr + 12, 4)
|
2018-12-04 12:46:48 +00:00
|
|
|
function_count = table.get_unsigned_int(section, test_addr + 20, 4)
|
2017-03-24 10:38:06 +00:00
|
|
|
name = table.get_string("any", name_addr)
|
|
|
|
desc = table.get_string("any", desc_addr)
|
|
|
|
file_name = table.get_string("any", file_name_addr)
|
2018-05-15 03:51:56 +00:00
|
|
|
tc = self.parse_one_test_case(name, desc, file_name, config_name, tags)
|
2017-09-13 12:39:43 +00:00
|
|
|
|
|
|
|
# check if duplicated case names
|
|
|
|
# we need to use it to select case,
|
|
|
|
# if duplicated IDs, Unity could select incorrect case to run
|
|
|
|
# and we need to check all cases no matter if it's going te be executed by CI
|
|
|
|
# also add app_name here, we allow same case for different apps
|
2018-05-15 03:51:56 +00:00
|
|
|
if (tc["summary"] + config_name) in self.test_case_names:
|
2017-09-13 12:39:43 +00:00
|
|
|
self.parsing_errors.append("duplicated test case ID: " + tc["summary"])
|
|
|
|
else:
|
2018-05-15 03:51:56 +00:00
|
|
|
self.test_case_names.add(tc["summary"] + config_name)
|
2017-09-13 12:39:43 +00:00
|
|
|
|
2018-11-13 11:48:31 +00:00
|
|
|
test_group_included = True
|
|
|
|
if test_groups is not None and tc["group"] not in test_groups:
|
|
|
|
test_group_included = False
|
|
|
|
|
|
|
|
if tc["CI ready"] == "Yes" and test_group_included:
|
2017-03-24 10:38:06 +00:00
|
|
|
# update test env list and the cases of same env list
|
|
|
|
if tc["test environment"] in self.test_env_tags:
|
|
|
|
self.test_env_tags[tc["test environment"]].append(tc["ID"])
|
|
|
|
else:
|
2017-04-10 07:08:15 +00:00
|
|
|
self.test_env_tags.update({tc["test environment"]: [tc["ID"]]})
|
2018-01-31 10:59:10 +00:00
|
|
|
|
|
|
|
if function_count > 1:
|
2018-03-09 06:50:34 +00:00
|
|
|
tc.update({"child case num": function_count})
|
2018-01-31 10:59:10 +00:00
|
|
|
|
|
|
|
# only add cases need to be executed
|
2017-09-13 12:39:43 +00:00
|
|
|
test_cases.append(tc)
|
2017-03-24 10:38:06 +00:00
|
|
|
|
|
|
|
os.remove("section_table.tmp")
|
|
|
|
os.remove("case_address.tmp")
|
|
|
|
|
2017-09-13 12:39:43 +00:00
|
|
|
return test_cases
|
2017-03-24 10:38:06 +00:00
|
|
|
|
|
|
|
def parse_case_properities(self, tags_raw):
|
|
|
|
"""
|
|
|
|
parse test case tags (properities) with the following rules:
|
|
|
|
* first tag is always group of test cases, it's mandatory
|
2017-09-13 12:39:43 +00:00
|
|
|
* the rest tags should be [type=value].
|
2017-03-24 10:38:06 +00:00
|
|
|
* if the type have default value, then [type] equal to [type=default_value].
|
|
|
|
* if the type don't don't exist, then equal to [type=omitted_value]
|
|
|
|
default_value and omitted_value are defined in TagDefinition.yml
|
|
|
|
:param tags_raw: raw tag string
|
|
|
|
:return: tag dict
|
|
|
|
"""
|
|
|
|
tags = self.DESCRIPTION_PATTERN.findall(tags_raw)
|
|
|
|
assert len(tags) > 0
|
|
|
|
p = dict([(k, self.tag_def[k]["omitted"]) for k in self.tag_def])
|
|
|
|
p["module"] = tags[0]
|
|
|
|
|
2018-11-13 11:48:31 +00:00
|
|
|
# Use the original value of the first tag as test group name
|
|
|
|
p["group"] = p["module"]
|
|
|
|
|
2017-03-24 10:38:06 +00:00
|
|
|
if p["module"] not in self.module_map:
|
|
|
|
p["module"] = "misc"
|
|
|
|
|
|
|
|
# parsing rest tags, [type=value], =value is optional
|
|
|
|
for tag in tags[1:]:
|
|
|
|
match = self.TAG_PATTERN.search(tag)
|
|
|
|
assert match is not None
|
|
|
|
tag_type = match.group(1)
|
|
|
|
tag_value = match.group(3)
|
|
|
|
if match.group(2) == "=" and tag_value is None:
|
|
|
|
# [tag_type=] means tag_value is empty string
|
|
|
|
tag_value = ""
|
|
|
|
if tag_type in p:
|
|
|
|
if tag_value is None:
|
|
|
|
p[tag_type] = self.tag_def[tag_type]["default"]
|
|
|
|
else:
|
|
|
|
p[tag_type] = tag_value
|
|
|
|
else:
|
|
|
|
# ignore not defined tag type
|
|
|
|
pass
|
|
|
|
return p
|
|
|
|
|
2018-09-27 07:23:44 +00:00
|
|
|
@staticmethod
|
|
|
|
def parse_tags_internal(sdkconfig, config_dependencies, config_pattern):
|
|
|
|
required_tags = []
|
|
|
|
|
|
|
|
def compare_config(config):
|
|
|
|
return config in sdkconfig
|
|
|
|
|
|
|
|
def process_condition(condition):
|
|
|
|
matches = config_pattern.findall(condition)
|
|
|
|
if matches:
|
|
|
|
for config in matches:
|
|
|
|
compare_result = compare_config(config)
|
|
|
|
# replace all configs in condition with True or False according to compare result
|
|
|
|
condition = re.sub(config_pattern, str(compare_result), condition, count=1)
|
|
|
|
# Now the condition is a python condition, we can use eval to compute its value
|
|
|
|
ret = eval(condition)
|
|
|
|
else:
|
|
|
|
# didn't use complex condition. only defined one condition for the tag
|
|
|
|
ret = compare_config(condition)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
for tag in config_dependencies:
|
|
|
|
if process_condition(config_dependencies[tag]):
|
|
|
|
required_tags.append(tag)
|
|
|
|
|
|
|
|
return required_tags
|
|
|
|
|
2018-05-15 03:51:56 +00:00
|
|
|
def parse_tags(self, sdkconfig_file):
|
|
|
|
"""
|
|
|
|
Some test configs could requires different DUTs.
|
2019-04-30 10:51:55 +00:00
|
|
|
For example, if CONFIG_ESP32_SPIRAM_SUPPORT is enabled, we need WROVER-Kit to run test.
|
2018-05-15 03:51:56 +00:00
|
|
|
This method will get tags for runners according to ConfigDependency.yml(maps tags to sdkconfig).
|
|
|
|
|
2018-09-27 07:23:44 +00:00
|
|
|
We support to the following syntax::
|
|
|
|
|
|
|
|
# define the config which requires the tag
|
|
|
|
'tag_a': 'config_a="value_a"'
|
|
|
|
# define the condition for the tag
|
|
|
|
'tag_b': '{config A} and (not {config B} or (not {config C} and {config D}))'
|
|
|
|
|
|
|
|
:param sdkconfig_file: sdk config file of the unit test config
|
2018-05-15 03:51:56 +00:00
|
|
|
:return: required tags for runners
|
|
|
|
"""
|
2018-09-27 07:23:44 +00:00
|
|
|
|
2018-05-15 03:51:56 +00:00
|
|
|
with open(sdkconfig_file, "r") as f:
|
|
|
|
configs_raw_data = f.read()
|
2018-09-27 07:23:44 +00:00
|
|
|
|
2018-05-15 03:51:56 +00:00
|
|
|
configs = configs_raw_data.splitlines(False)
|
2018-09-27 07:23:44 +00:00
|
|
|
|
|
|
|
return self.parse_tags_internal(configs, self.config_dependencies, self.CONFIG_PATTERN)
|
2018-05-15 03:51:56 +00:00
|
|
|
|
2018-11-13 11:48:31 +00:00
|
|
|
def get_test_groups(self, config_file):
|
|
|
|
"""
|
|
|
|
If the config file includes TEST_GROUPS variable, return its value as a list of strings.
|
|
|
|
:param config_file file under configs/ directory for given configuration
|
|
|
|
:return: list of test groups, or None if TEST_GROUPS wasn't set
|
|
|
|
"""
|
|
|
|
with open(config_file, "r") as f:
|
|
|
|
for line in f:
|
|
|
|
match = self.TEST_GROUPS_PATTERN.match(line)
|
|
|
|
if match is not None:
|
|
|
|
return match.group(1).split(' ')
|
|
|
|
return None
|
|
|
|
|
2018-05-15 03:51:56 +00:00
|
|
|
def parse_one_test_case(self, name, description, file_name, config_name, tags):
|
2017-03-24 10:38:06 +00:00
|
|
|
"""
|
|
|
|
parse one test case
|
|
|
|
:param name: test case name (summary)
|
|
|
|
:param description: test case description (tag string)
|
|
|
|
:param file_name: the file defines this test case
|
2018-05-15 03:51:56 +00:00
|
|
|
:param config_name: built unit test app name
|
|
|
|
:param tags: tags to select runners
|
2017-03-24 10:38:06 +00:00
|
|
|
:return: parsed test case
|
|
|
|
"""
|
|
|
|
prop = self.parse_case_properities(description)
|
2017-09-13 12:39:43 +00:00
|
|
|
|
2017-03-24 10:38:06 +00:00
|
|
|
test_case = deepcopy(TEST_CASE_PATTERN)
|
2018-05-15 03:51:56 +00:00
|
|
|
test_case.update({"config": config_name,
|
2017-09-13 12:39:43 +00:00
|
|
|
"module": self.module_map[prop["module"]]['module'],
|
2018-11-13 11:48:31 +00:00
|
|
|
"group": prop["group"],
|
2017-03-24 10:38:06 +00:00
|
|
|
"CI ready": "No" if prop["ignore"] == "Yes" else "Yes",
|
2018-07-03 14:00:09 +00:00
|
|
|
"ID": name,
|
2017-03-24 10:38:06 +00:00
|
|
|
"test point 2": prop["module"],
|
|
|
|
"steps": name,
|
|
|
|
"test environment": prop["test_env"],
|
2017-09-20 09:17:51 +00:00
|
|
|
"reset": prop["reset"],
|
2017-03-24 10:38:06 +00:00
|
|
|
"sub module": self.module_map[prop["module"]]['sub module'],
|
2018-03-09 06:50:34 +00:00
|
|
|
"summary": name,
|
|
|
|
"multi_device": prop["multi_device"],
|
2018-05-21 04:50:27 +00:00
|
|
|
"multi_stage": prop["multi_stage"],
|
2018-05-15 03:51:56 +00:00
|
|
|
"timeout": int(prop["timeout"]),
|
|
|
|
"tags": tags})
|
2017-03-24 10:38:06 +00:00
|
|
|
return test_case
|
|
|
|
|
|
|
|
def dump_test_cases(self, test_cases):
|
|
|
|
"""
|
|
|
|
dump parsed test cases to YAML file for test bench input
|
|
|
|
:param test_cases: parsed test cases
|
|
|
|
"""
|
2018-10-10 11:19:31 +00:00
|
|
|
with open(os.path.join(self.idf_path, self.TEST_CASE_FILE), "w+") as f:
|
2017-03-24 10:38:06 +00:00
|
|
|
yaml.dump({"test cases": test_cases}, f, allow_unicode=True, default_flow_style=False)
|
|
|
|
|
|
|
|
def copy_module_def_file(self):
|
|
|
|
""" copy module def file to artifact path """
|
2017-09-13 12:39:43 +00:00
|
|
|
src = os.path.join(self.idf_path, self.MODULE_DEF_FILE)
|
|
|
|
dst = os.path.join(self.idf_path, self.MODULE_ARTIFACT_FILE)
|
2017-03-24 10:38:06 +00:00
|
|
|
shutil.copy(src, dst)
|
|
|
|
|
2017-09-13 12:39:43 +00:00
|
|
|
def parse_test_cases(self):
|
|
|
|
""" parse test cases from multiple built unit test apps """
|
|
|
|
test_cases = []
|
|
|
|
|
2018-05-15 03:51:56 +00:00
|
|
|
output_folder = os.path.join(self.idf_path, self.UT_BIN_FOLDER)
|
2018-11-13 11:48:31 +00:00
|
|
|
configs_folder = os.path.join(self.idf_path, self.UT_CONFIG_FOLDER)
|
2018-05-15 03:51:56 +00:00
|
|
|
test_configs = os.listdir(output_folder)
|
|
|
|
for config in test_configs:
|
|
|
|
config_output_folder = os.path.join(output_folder, config)
|
|
|
|
if os.path.exists(config_output_folder):
|
2018-11-13 11:48:31 +00:00
|
|
|
test_cases.extend(self.parse_test_cases_for_one_config(configs_folder, config_output_folder, config))
|
2018-07-16 09:44:38 +00:00
|
|
|
test_cases.sort(key=lambda x: x["config"] + x["summary"])
|
2017-09-13 12:39:43 +00:00
|
|
|
self.dump_test_cases(test_cases)
|
|
|
|
|
2017-03-24 10:38:06 +00:00
|
|
|
|
|
|
|
def test_parser():
|
|
|
|
parser = Parser()
|
|
|
|
# test parsing tags
|
|
|
|
# parsing module only and module in module list
|
|
|
|
prop = parser.parse_case_properities("[esp32]")
|
|
|
|
assert prop["module"] == "esp32"
|
|
|
|
# module not in module list
|
|
|
|
prop = parser.parse_case_properities("[not_in_list]")
|
|
|
|
assert prop["module"] == "misc"
|
|
|
|
# parsing a default tag, a tag with assigned value
|
|
|
|
prop = parser.parse_case_properities("[esp32][ignore][test_env=ABCD][not_support1][not_support2=ABCD]")
|
|
|
|
assert prop["ignore"] == "Yes" and prop["test_env"] == "ABCD" \
|
|
|
|
and "not_support1" not in prop and "not_supported2" not in prop
|
|
|
|
# parsing omitted value
|
|
|
|
prop = parser.parse_case_properities("[esp32]")
|
|
|
|
assert prop["ignore"] == "No" and prop["test_env"] == "UT_T1_1"
|
|
|
|
# parsing with incorrect format
|
|
|
|
try:
|
|
|
|
parser.parse_case_properities("abcd")
|
|
|
|
assert False
|
|
|
|
except AssertionError:
|
|
|
|
pass
|
|
|
|
# skip invalid data parse, [type=] assigns empty string to type
|
|
|
|
prop = parser.parse_case_properities("[esp32]abdc aaaa [ignore=]")
|
|
|
|
assert prop["module"] == "esp32" and prop["ignore"] == ""
|
|
|
|
# skip mis-paired []
|
|
|
|
prop = parser.parse_case_properities("[esp32][[ignore=b]][]][test_env=AAA]]")
|
|
|
|
assert prop["module"] == "esp32" and prop["ignore"] == "b" and prop["test_env"] == "AAA"
|
|
|
|
|
2018-09-27 07:23:44 +00:00
|
|
|
config_dependency = {
|
|
|
|
'a': '123',
|
|
|
|
'b': '456',
|
|
|
|
'c': 'not {123}',
|
|
|
|
'd': '{123} and not {456}',
|
|
|
|
'e': '{123} and not {789}',
|
|
|
|
'f': '({123} and {456}) or ({123} and {789})'
|
|
|
|
}
|
|
|
|
sdkconfig = ["123", "789"]
|
|
|
|
tags = parser.parse_tags_internal(sdkconfig, config_dependency, parser.CONFIG_PATTERN)
|
2018-12-04 12:46:48 +00:00
|
|
|
assert sorted(tags) == ['a', 'd', 'f'] # sorted is required for older Python3, e.g. 3.4.8
|
2018-09-27 07:23:44 +00:00
|
|
|
|
2017-03-24 10:38:06 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
test_parser()
|
|
|
|
|
|
|
|
idf_path = os.getenv("IDF_PATH")
|
|
|
|
|
|
|
|
parser = Parser(idf_path)
|
2017-09-13 12:39:43 +00:00
|
|
|
parser.parse_test_cases()
|
2017-03-24 10:38:06 +00:00
|
|
|
parser.copy_module_def_file()
|
2017-09-13 12:39:43 +00:00
|
|
|
if len(parser.parsing_errors) > 0:
|
|
|
|
for error in parser.parsing_errors:
|
2018-09-10 13:04:30 +00:00
|
|
|
print(error)
|
2017-09-13 12:39:43 +00:00
|
|
|
exit(-1)
|
2017-03-24 10:38:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|