CaseConfig.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http:#www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. Processing case config files.
  16. This is mainly designed for CI, we need to auto create and assign test jobs.
  17. Template Config File::
  18. TestConfig:
  19. app:
  20. path: Users/Test/TinyTestFW/IDF/IDFApp.py
  21. class: Example
  22. dut:
  23. path:
  24. class:
  25. config_file: /somewhere/config_file_for_runner
  26. test_name: CI_test_job_1
  27. Filter:
  28. chip: ESP32
  29. env_tag: default
  30. CaseConfig:
  31. - name: test_examples_protocol_https_request
  32. # optional
  33. extra_data: some extra data passed to case with kwarg extra_data
  34. overwrite: # overwrite test configs
  35. app:
  36. path: Users/Test/TinyTestFW/IDF/IDFApp.py
  37. class: Example
  38. - name: xxx
  39. """
  40. # TODO: add a function to use suitable import lib for python2 and python3
  41. import imp
  42. import yaml
  43. import TestCase
  44. def _filter_one_case(test_method, case_filter):
  45. """ Apply filter for one case (the filter logic is the same as described in ``filter_test_cases``) """
  46. filter_result = True
  47. for key in case_filter:
  48. if key in test_method.case_info:
  49. # the filter key is both in case and filter
  50. # we need to check if they match
  51. filter_item, accepted_item = case_filter[key], test_method.case_info[key]
  52. if isinstance(filter_item, (tuple, list)) \
  53. and isinstance(accepted_item, (tuple, list)):
  54. # both list/tuple, check if they have common item
  55. filter_result = True if set(filter_item) & set(accepted_item) else False
  56. elif isinstance(filter_item, (tuple, list)):
  57. # filter item list/tuple, check if case accepted value in filter item list/tuple
  58. filter_result = True if accepted_item in filter_item else False
  59. elif isinstance(accepted_item, (tuple, list)):
  60. # accepted item list/tuple, check if case filter value is in accept item list/tuple
  61. filter_result = True if filter_item in accepted_item else False
  62. else:
  63. # both string/int, just do string compare
  64. filter_result = (filter_item == accepted_item)
  65. else:
  66. # key in filter only, which means the case supports all values for this filter key, match succeed
  67. pass
  68. if not filter_result:
  69. # match failed
  70. break
  71. return filter_result
  72. def filter_test_cases(test_methods, case_filter):
  73. """
  74. filter test case. filter logic:
  75. 1. if filter key both in case attribute and filter:
  76. * if both value is string/int, then directly compare
  77. * if one is list/tuple, the other one is string/int, then check if string/int is in list/tuple
  78. * if both are list/tuple, then check if they have common item
  79. 2. if only case attribute or filter have the key, filter succeed
  80. for example, the following are match succeed scenarios
  81. (the rule is symmetric, result is same if exchange values for user filter and case attribute):
  82. * user case filter is ``chip: ["esp32", "esp32c"]``, case doesn't have ``chip`` attribute
  83. * user case filter is ``chip: ["esp32", "esp32c"]``, case attribute is ``chip: "esp32"``
  84. * user case filter is ``chip: "esp32"``, case attribute is ``chip: "esp32"``
  85. :param test_methods: a list of test methods functions
  86. :param case_filter: case filter
  87. :return: filtered test methods
  88. """
  89. filtered_test_methods = []
  90. for test_method in test_methods:
  91. if _filter_one_case(test_method, case_filter):
  92. filtered_test_methods.append(test_method)
  93. return filtered_test_methods
  94. class Parser(object):
  95. DEFAULT_CONFIG = {
  96. "TestConfig": dict(),
  97. "Filter": dict(),
  98. "CaseConfig": [{"extra_data": None}],
  99. }
  100. @classmethod
  101. def parse_config_file(cls, config_file):
  102. """
  103. parse from config file and then update to default config.
  104. :param config_file: config file path
  105. :return: configs
  106. """
  107. configs = cls.DEFAULT_CONFIG.copy()
  108. if config_file:
  109. with open(config_file, "r") as f:
  110. configs.update(yaml.load(f))
  111. return configs
  112. @classmethod
  113. def handle_overwrite_args(cls, overwrite):
  114. """
  115. handle overwrite configs. import module from path and then get the required class.
  116. :param overwrite: overwrite args
  117. :return: dict of (original key: class)
  118. """
  119. output = dict()
  120. for key in overwrite:
  121. _path = overwrite[key]["path"]
  122. # TODO: add a function to use suitable import lib for python2 and python3
  123. _module = imp.load_source(str(hash(_path)), overwrite[key]["path"])
  124. output[key] = _module.__getattribute__(overwrite[key]["class"])
  125. return output
  126. @classmethod
  127. def apply_config(cls, test_methods, config_file):
  128. """
  129. apply config for test methods
  130. :param test_methods: a list of test methods functions
  131. :param config_file: case filter file
  132. :return: filtered cases
  133. """
  134. configs = cls.parse_config_file(config_file)
  135. test_case_list = []
  136. for _config in configs["CaseConfig"]:
  137. _filter = configs["Filter"].copy()
  138. _filter.update(_config)
  139. _overwrite = cls.handle_overwrite_args(_filter.pop("overwrite", dict()))
  140. _extra_data = _filter.pop("extra_data", None)
  141. for test_method in test_methods:
  142. if _filter_one_case(test_method, _filter):
  143. test_case_list.append(TestCase.TestCase(test_method, _extra_data, **_overwrite))
  144. return test_case_list
  145. class Generator(object):
  146. """ Case config file generator """
  147. def __init__(self):
  148. self.default_config = {
  149. "TestConfig": dict(),
  150. "Filter": dict(),
  151. }
  152. def set_default_configs(self, test_config, case_filter):
  153. """
  154. :param test_config: "TestConfig" value
  155. :param case_filter: "Filter" value
  156. :return: None
  157. """
  158. self.default_config = {"TestConfig": test_config, "Filter": case_filter}
  159. def generate_config(self, case_configs, output_file):
  160. """
  161. :param case_configs: "CaseConfig" value
  162. :param output_file: output file path
  163. :return: None
  164. """
  165. config = self.default_config.copy()
  166. config.update({"CaseConfig": case_configs})
  167. with open(output_file, "w") as f:
  168. yaml.dump(config, f)