__init__.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #
  2. # Copyright (c) 2021 Project CHIP Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import json
  17. import os
  18. import subprocess
  19. from dataclasses import dataclass
  20. from pathlib import Path
  21. from typing import Iterator, Set
  22. from . import linux, runner
  23. from .test_definition import ApplicationPaths, TestDefinition, TestTag, TestTarget
  24. _DEFAULT_CHIP_ROOT = os.path.abspath(
  25. os.path.join(os.path.dirname(__file__), "..", "..", ".."))
  26. _YAML_TEST_SUITE_PATH = os.path.abspath(
  27. os.path.join(_DEFAULT_CHIP_ROOT, "src/app/tests/suites"))
  28. @dataclass(eq=True, frozen=True)
  29. class ManualTest:
  30. yaml: str
  31. reason: str
  32. INVALID_TESTS = {
  33. "tests.yaml", # certification/tests.yaml is not a real test
  34. "PICS.yaml", # certification/PICS.yaml is not a real test
  35. # The items below are examples and will never work (likely)
  36. # completely exclude them
  37. "Config_Example.yaml",
  38. "Config_Variables_Example.yaml",
  39. "PICS_Example.yaml",
  40. "Response_Example.yaml",
  41. "Test_Example.yaml",
  42. "Test_Example_1.yaml",
  43. "Test_Example_2.yaml",
  44. "Test_Example_3.yaml",
  45. }
  46. def _IsValidYamlTest(name: str) -> bool:
  47. """Check if the given file name is a valid YAML test.
  48. This returns invalid for examples, simulated and other specific tests.
  49. """
  50. # Simulated tests are not runnable by repl tests, need
  51. # separate infrastructure. Exclude them completely (they are
  52. # not even manual)
  53. if name.endswith('_Simulated.yaml'):
  54. return False
  55. return name not in INVALID_TESTS
  56. def _LoadManualTestsJson(json_file_path: str) -> Iterator[str]:
  57. with open(json_file_path, 'rt') as f:
  58. data = json.load(f)
  59. for c in data["collection"]:
  60. for name in data[c]:
  61. yield f"{name}.yaml"
  62. def _GetManualTests() -> Set[str]:
  63. manualtests = set()
  64. # Flagged as manual from: src/app/tests/suites/manualTests.json
  65. for item in _LoadManualTestsJson(os.path.join(_YAML_TEST_SUITE_PATH, "manualTests.json")):
  66. manualtests.add(item)
  67. return manualtests
  68. def _GetFlakyTests() -> Set[str]:
  69. """List of flaky tests.
  70. While this list is empty, it remains here in case we need to quickly add a new test
  71. that is flaky.
  72. """
  73. return set()
  74. def _GetSlowTests() -> Set[str]:
  75. """Generally tests using sleep() a bit too freely.
  76. 10s seems like a good threshold to consider something slow
  77. """
  78. return {
  79. "DL_LockUnlock.yaml", # ~ 10 seconds
  80. "TestSubscribe_AdministratorCommissioning.yaml", # ~ 15 seconds
  81. "Test_TC_CC_5_1.yaml", # ~ 30 seconds
  82. "Test_TC_CC_5_2.yaml", # ~ 30 seconds
  83. "Test_TC_CC_5_3.yaml", # ~ 25 seconds
  84. "Test_TC_CC_6_1.yaml", # ~ 35 seconds
  85. "Test_TC_CC_6_2.yaml", # ~ 60 seconds
  86. "Test_TC_CC_6_3.yaml", # ~ 50 seconds
  87. "Test_TC_CC_7_2.yaml", # ~ 65 seconds
  88. "Test_TC_CC_7_3.yaml", # ~ 70 seconds
  89. "Test_TC_CC_7_4.yaml", # ~ 25 seconds
  90. "Test_TC_CC_8_1.yaml", # ~ 60 seconds
  91. "Test_TC_DRLK_2_4.yaml", # ~ 60 seconds
  92. "Test_TC_I_2_2.yaml", # ~ 15 seconds
  93. "Test_TC_LVL_3_1.yaml", # ~ 35 seconds
  94. "Test_TC_LVL_4_1.yaml", # ~ 55 seconds
  95. "Test_TC_LVL_5_1.yaml", # ~ 35 seconds
  96. "Test_TC_LVL_6_1.yaml", # ~ 10 seconds
  97. "Test_TC_WNCV_3_1.yaml", # ~ 20 seconds
  98. "Test_TC_WNCV_3_2.yaml", # ~ 20 seconds
  99. "Test_TC_WNCV_3_3.yaml", # ~ 15 seconds
  100. "Test_TC_WNCV_3_4.yaml", # ~ 10 seconds
  101. "Test_TC_WNCV_3_5.yaml", # ~ 10 seconds
  102. "Test_TC_WNCV_4_1.yaml", # ~ 20 seconds
  103. "Test_TC_WNCV_4_2.yaml", # ~ 20 seconds
  104. "Test_TC_WNCV_4_5.yaml", # ~ 12 seconds
  105. }
  106. def _GetExtraSlowTests() -> Set[str]:
  107. """Generally tests using sleep() so much they should never run in CI.
  108. 1 minute seems like a good threshold to consider something extra slow
  109. """
  110. return {
  111. "Test_TC_DGGEN_2_1.yaml", # > 2 hours
  112. }
  113. def _GetInDevelopmentTests() -> Set[str]:
  114. """Tests that fail in YAML for some reason."""
  115. return {
  116. "Test_TC_TIMESYNC_1_1.yaml", # Time sync SDK is not yet ready
  117. "Test_TC_TIMESYNC_2_3.yaml", # Time sync SDK is not yet ready
  118. "Test_TC_PSCFG_1_1.yaml", # Power source configuration cluster is deprecated and removed from all-clusters
  119. "Test_TC_PSCFG_2_1.yaml", # Power source configuration cluster is deprecated and removed from all-clusters
  120. "Test_TC_PSCFG_2_2.yaml", # Power source configuration cluster is deprecated and removed from all-clusters
  121. "Test_TC_SMOKECO_2_2.yaml", # chip-repl does not support local timeout (07/20/2023) and test assumes
  122. # TestEventTriggersEnabled is true, which it's not in CI.
  123. "Test_TC_SMOKECO_2_3.yaml", # chip-repl does not support local timeout (07/20/2023) and test assumes
  124. # TestEventTriggersEnabled is true, which it's not in CI.
  125. "Test_TC_SMOKECO_2_4.yaml", # chip-repl does not support local timeout (07/20/2023) and test assumes
  126. # TestEventTriggersEnabled is true, which it's not in CI.
  127. "Test_TC_SMOKECO_2_5.yaml", # chip-repl does not support local timeout (07/20/2023) and test assumes
  128. # TestEventTriggersEnabled is true, which it's not in CI.
  129. "Test_TC_SMOKECO_2_6.yaml", # chip-repl does not support local timeout (07/20/2023) and test assumes
  130. # TestEventTriggersEnabled is true, which it's not in CI.
  131. "Test_TC_IDM_1_2.yaml", # Broken harness: https://github.com/project-chip/connectedhomeip/issues/29115
  132. "Test_TC_S_2_4.yaml", # https://github.com/project-chip/connectedhomeip/issues/29117
  133. }
  134. def _GetChipReplUnsupportedTests() -> Set[str]:
  135. """Tests that fail in chip-repl for some reason"""
  136. return {
  137. "Test_AddNewFabricFromExistingFabric.yaml", # chip-repl does not support GetCommissionerRootCertificate and IssueNocChain command
  138. "Test_TC_OPCREDS_3_7.yaml", # chip-repl does not support GetCommissionerRootCertificate and IssueNocChain command
  139. "TestEqualities.yaml", # chip-repl does not support pseudo-cluster commands that return a value
  140. "TestExampleCluster.yaml", # chip-repl does not load custom pseudo clusters
  141. "TestAttributesById.yaml", # chip-repl does not support AnyCommands (06/06/2023)
  142. "TestCommandsById.yaml", # chip-repl does not support AnyCommands (06/06/2023)
  143. "TestEventsById.yaml", # chip-repl does not support AnyCommands (06/06/2023)
  144. "TestReadNoneSubscribeNone.yaml", # chip-repl does not support AnyCommands (07/27/2023)
  145. "Test_TC_DRLK_2_8.yaml", # Test fails only in chip-repl: Refer--> https://github.com/project-chip/connectedhomeip/pull/27011#issuecomment-1593339855
  146. "Test_TC_ACE_1_6.yaml", # Test fails only in chip-repl: Refer--> https://github.com/project-chip/connectedhomeip/pull/27910#issuecomment-1632485584
  147. "Test_TC_IDM_1_2.yaml", # chip-repl does not support AnyCommands (19/07/2023)
  148. "TestGroupKeyManagementCluster.yaml", # chip-repl does not support EqualityCommands (2023-08-04)
  149. "Test_TC_S_2_2.yaml", # chip-repl does not support scenes cluster commands
  150. "Test_TC_S_2_3.yaml", # chip-repl does not support scenes cluster commands
  151. "Test_TC_S_2_4.yaml", # chip-repl does not support scenes cluster commands
  152. "Test_TC_MOD_3_1.yaml", # chip-repl does not support EqualityCommands pseudo-cluster
  153. "Test_TC_MOD_3_2.yaml", # chip-repl does not support EqualityCommands pseudo-cluster
  154. "Test_TC_MOD_3_3.yaml", # chip-repl does not support EqualityCommands pseudo-cluster
  155. "Test_TC_MOD_3_4.yaml", # chip-repl does not support EqualityCommands pseudo-cluster
  156. "Test_TC_BRBINFO_2_1.yaml", # chip-repl does not support EqualityCommands pseudo-cluster
  157. "Test_TC_DGGEN_2_1.yaml", # chip-repl does not support EqualityCommands pseudo-cluster
  158. "Test_TC_DGGEN_2_3.yaml", # chip-repl does not support EqualityCommands pseudo-cluster
  159. "Test_TC_LWM_3_1.yaml", # chip-repl does not support EqualityCommands pseudo-cluster
  160. "Test_TC_G_2_4.yaml", # chip-repl does not support EqualityCommands pseudo-cluster
  161. "Test_TC_RVCRUNM_3_1.yaml", # chip-repl does not support EqualityCommands pseudo-cluster
  162. "Test_TC_RVCCLEANM_3_1.yaml", # chip-repl does not support EqualityCommands pseudo-cluster
  163. "Test_TC_TCCM_3_1.yaml", # chip-repl does not support EqualityCommands pseudo-cluster
  164. "Test_TC_TCTL_2_1.yaml", # chip-repl does not support EqualityCommands pseudo-cluster
  165. # chip-repl and chip-tool disagree on what the YAML here should look like: https://github.com/project-chip/connectedhomeip/issues/29110
  166. "TestClusterMultiFabric.yaml",
  167. "Test_TC_ACL_2_5.yaml", # chip-repl does not support LastReceivedEventNumber : https://github.com/project-chip/connectedhomeip/issues/28884
  168. "Test_TC_ACL_2_6.yaml", # chip-repl does not support LastReceivedEventNumber : https://github.com/project-chip/connectedhomeip/issues/28884
  169. }
  170. def _GetPurposefulFailureTests() -> Set[str]:
  171. """Tests that fail in YAML on purpose."""
  172. return {
  173. "TestPurposefulFailureEqualities.yaml"
  174. }
  175. def _AllYamlTests():
  176. yaml_test_suite_path = Path(_YAML_TEST_SUITE_PATH)
  177. if not yaml_test_suite_path.exists():
  178. raise FileNotFoundError(
  179. f"Expected directory {_YAML_TEST_SUITE_PATH} to exist")
  180. for path in yaml_test_suite_path.rglob("*.yaml"):
  181. if not path.is_file():
  182. continue
  183. yield path
  184. def target_for_name(name: str):
  185. if (name.startswith("TV_") or name.startswith("Test_TC_MC_") or
  186. name.startswith("Test_TC_LOWPOWER_") or name.startswith("Test_TC_KEYPADINPUT_") or
  187. name.startswith("Test_TC_APPLAUNCHER_") or name.startswith("Test_TC_MEDIAINPUT_") or
  188. name.startswith("Test_TC_WAKEONLAN_") or name.startswith("Test_TC_CHANNEL_") or
  189. name.startswith("Test_TC_MEDIAPLAYBACK_") or name.startswith("Test_TC_AUDIOOUTPUT_") or
  190. name.startswith("Test_TC_TGTNAV_") or name.startswith("Test_TC_APBSC_") or
  191. name.startswith("Test_TC_CONTENTLAUNCHER_") or name.startswith("Test_TC_ALOGIN_")):
  192. return TestTarget.TV
  193. if name.startswith("DL_") or name.startswith("Test_TC_DRLK_"):
  194. return TestTarget.LOCK
  195. if name.startswith("OTA_"):
  196. return TestTarget.OTA
  197. if name.startswith("Test_TC_BRBINFO_") or name.startswith("Test_TC_ACT_"):
  198. return TestTarget.BRIDGE
  199. return TestTarget.ALL_CLUSTERS
  200. def tests_with_command(chip_tool: str, is_manual: bool):
  201. """Executes `chip_tool` binary to see what tests are available, using cmd
  202. to get the list.
  203. """
  204. cmd = "list"
  205. if is_manual:
  206. cmd += "-manual"
  207. result = subprocess.run([chip_tool, "tests", cmd], capture_output=True)
  208. result.check_returncode()
  209. test_tags = set()
  210. if is_manual:
  211. test_tags.add(TestTag.MANUAL)
  212. in_development_tests = [s.replace(".yaml", "") for s in _GetInDevelopmentTests()]
  213. for name in result.stdout.decode("utf8").split("\n"):
  214. if not name:
  215. continue
  216. target = target_for_name(name)
  217. tags = test_tags.copy()
  218. if name in in_development_tests:
  219. tags.add(TestTag.IN_DEVELOPMENT)
  220. yield TestDefinition(
  221. run_name=name, name=name, target=target, tags=tags
  222. )
  223. def _AllFoundYamlTests(treat_repl_unsupported_as_in_development: bool, use_short_run_name: bool):
  224. """
  225. use_short_run_name should be true if we want the run_name to be "Test_ABC" instead of "some/path/Test_ABC.yaml"
  226. """
  227. manual_tests = _GetManualTests()
  228. flaky_tests = _GetFlakyTests()
  229. slow_tests = _GetSlowTests()
  230. extra_slow_tests = _GetExtraSlowTests()
  231. in_development_tests = _GetInDevelopmentTests()
  232. chip_repl_unsupported_tests = _GetChipReplUnsupportedTests()
  233. purposeful_failure_tests = _GetPurposefulFailureTests()
  234. for path in _AllYamlTests():
  235. if not _IsValidYamlTest(path.name):
  236. continue
  237. tags = set()
  238. if path.name in manual_tests:
  239. tags.add(TestTag.MANUAL)
  240. if path.name in flaky_tests:
  241. tags.add(TestTag.FLAKY)
  242. if path.name in slow_tests:
  243. tags.add(TestTag.SLOW)
  244. if path.name in extra_slow_tests:
  245. tags.add(TestTag.EXTRA_SLOW)
  246. if path.name in in_development_tests:
  247. tags.add(TestTag.IN_DEVELOPMENT)
  248. if path.name in purposeful_failure_tests:
  249. tags.add(TestTag.PURPOSEFUL_FAILURE)
  250. if treat_repl_unsupported_as_in_development and path.name in chip_repl_unsupported_tests:
  251. tags.add(TestTag.IN_DEVELOPMENT)
  252. if use_short_run_name:
  253. run_name = path.stem # `path.stem` converts "some/path/Test_ABC_1.2.yaml" to "Test_ABC.1.2"
  254. else:
  255. run_name = str(path)
  256. yield TestDefinition(
  257. run_name=run_name,
  258. name=path.stem, # `path.stem` converts "some/path/Test_ABC_1.2.yaml" to "Test_ABC.1.2"
  259. target=target_for_name(path.name),
  260. tags=tags,
  261. )
  262. def AllReplYamlTests():
  263. for test in _AllFoundYamlTests(treat_repl_unsupported_as_in_development=True, use_short_run_name=False):
  264. yield test
  265. def AllChipToolYamlTests():
  266. for test in _AllFoundYamlTests(treat_repl_unsupported_as_in_development=False, use_short_run_name=True):
  267. yield test
  268. def AllChipToolTests(chip_tool: str):
  269. for test in tests_with_command(chip_tool, is_manual=False):
  270. yield test
  271. for test in tests_with_command(chip_tool, is_manual=True):
  272. yield test
  273. __all__ = [
  274. "TestTarget",
  275. "TestDefinition",
  276. "AllTests",
  277. "ApplicationPaths",
  278. "linux",
  279. "runner",
  280. ]