IDFApp.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. """ IDF Test Applications """
  15. import subprocess
  16. import hashlib
  17. import json
  18. import os
  19. import sys
  20. from tiny_test_fw import App
  21. from . import CIAssignExampleTest
  22. try:
  23. import gitlab_api
  24. except ImportError:
  25. gitlab_api = None
  26. def parse_encrypted_flag(args, offs, binary):
  27. # Find partition entries (e.g. the entries with an offset and a file)
  28. for _, entry in args.items():
  29. # If the current entry is a partition, we have to check whether it is
  30. # the one we are looking for or not
  31. try:
  32. if (entry["offset"], entry["file"]) == (offs, binary):
  33. return entry["encrypted"] == "true"
  34. except (TypeError, KeyError):
  35. # TypeError occurs if the entry is a list, which is possible in JSON
  36. # data structure.
  37. # KeyError occurs if the entry doesn't have "encrypted" field.
  38. continue
  39. # The entry was not found, return None. The caller will have to check
  40. # CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT macro
  41. return None
  42. def parse_flash_settings(path, default_encryption=False):
  43. file_name = os.path.basename(path)
  44. # For compatibility reasons, this list contains all the files to be
  45. # flashed
  46. flash_files = []
  47. # The following list only contains the files that need encryption
  48. encrypt_files = []
  49. if file_name == "flasher_args.json":
  50. # CMake version using build metadata file
  51. with open(path, "r") as f:
  52. args = json.load(f)
  53. for (offs, binary) in args["flash_files"].items():
  54. if offs:
  55. flash_files.append((offs, binary))
  56. encrypted = parse_encrypted_flag(args, offs, binary)
  57. # default_encryption should be taken into account if and only if
  58. # encrypted flag is not provided in the JSON file.
  59. if (encrypted is None and default_encryption) or encrypted:
  60. encrypt_files.append((offs, binary))
  61. flash_settings = args["flash_settings"]
  62. app_name = os.path.splitext(args["app"]["file"])[0]
  63. else:
  64. # GNU Make version uses download.config arguments file
  65. with open(path, "r") as f:
  66. args = f.readlines()[-1].split(" ")
  67. flash_settings = {}
  68. for idx in range(0, len(args), 2): # process arguments in pairs
  69. if args[idx].startswith("--"):
  70. # strip the -- from the command line argument
  71. flash_settings[args[idx][2:]] = args[idx + 1]
  72. else:
  73. # offs, filename
  74. flash_files.append((args[idx], args[idx + 1]))
  75. # Parameter default_encryption tells us if the files need encryption
  76. if default_encryption:
  77. encrypt_files = flash_files
  78. # we can only guess app name in download.config.
  79. for p in flash_files:
  80. if not os.path.dirname(p[1]) and "partition" not in p[1]:
  81. # app bin usually in the same dir with download.config and it's not partition table
  82. app_name = os.path.splitext(p[1])[0]
  83. break
  84. else:
  85. app_name = None
  86. return flash_files, encrypt_files, flash_settings, app_name
  87. class Artifacts(object):
  88. def __init__(self, dest_root_path, artifact_index_file, app_path, config_name, target):
  89. assert gitlab_api
  90. # at least one of app_path or config_name is not None. otherwise we can't match artifact
  91. assert app_path or config_name
  92. assert os.path.exists(artifact_index_file)
  93. self.gitlab_inst = gitlab_api.Gitlab(os.getenv("CI_PROJECT_ID"))
  94. self.dest_root_path = dest_root_path
  95. with open(artifact_index_file, "r") as f:
  96. artifact_index = json.load(f)
  97. self.artifact_info = self._find_artifact(artifact_index, app_path, config_name, target)
  98. @staticmethod
  99. def _find_artifact(artifact_index, app_path, config_name, target):
  100. for artifact_info in artifact_index:
  101. match_result = True
  102. if app_path:
  103. # We use endswith here to avoid issue like:
  104. # examples_protocols_mqtt_ws but return a examples_protocols_mqtt_wss failure
  105. match_result = artifact_info["app_dir"].endswith(app_path)
  106. if config_name:
  107. match_result = match_result and config_name == artifact_info["config"]
  108. if target:
  109. match_result = match_result and target == artifact_info["target"]
  110. if match_result:
  111. ret = artifact_info
  112. break
  113. else:
  114. ret = None
  115. return ret
  116. def download_artifacts(self):
  117. if self.artifact_info:
  118. base_path = os.path.join(self.artifact_info["work_dir"], self.artifact_info["build_dir"])
  119. job_id = self.artifact_info["ci_job_id"]
  120. # 1. download flash args file
  121. if self.artifact_info["build_system"] == "cmake":
  122. flash_arg_file = os.path.join(base_path, "flasher_args.json")
  123. else:
  124. flash_arg_file = os.path.join(base_path, "download.config")
  125. self.gitlab_inst.download_artifact(job_id, [flash_arg_file], self.dest_root_path)
  126. # 2. download all binary files
  127. flash_files, _, _, app_name = parse_flash_settings(os.path.join(self.dest_root_path, flash_arg_file))
  128. artifact_files = [os.path.join(base_path, p[1]) for p in flash_files]
  129. artifact_files.append(os.path.join(base_path, app_name + ".elf"))
  130. self.gitlab_inst.download_artifact(job_id, artifact_files, self.dest_root_path)
  131. # 3. download sdkconfig file
  132. self.gitlab_inst.download_artifact(job_id, [os.path.join(os.path.dirname(base_path), "sdkconfig")],
  133. self.dest_root_path)
  134. else:
  135. base_path = None
  136. return base_path
  137. def download_artifact_files(self, file_names):
  138. if self.artifact_info:
  139. base_path = os.path.join(self.artifact_info["work_dir"], self.artifact_info["build_dir"])
  140. job_id = self.artifact_info["ci_job_id"]
  141. # download all binary files
  142. artifact_files = [os.path.join(base_path, fn) for fn in file_names]
  143. self.gitlab_inst.download_artifact(job_id, artifact_files, self.dest_root_path)
  144. # download sdkconfig file
  145. self.gitlab_inst.download_artifact(job_id, [os.path.join(os.path.dirname(base_path), "sdkconfig")],
  146. self.dest_root_path)
  147. else:
  148. base_path = None
  149. return base_path
  150. class IDFApp(App.BaseApp):
  151. """
  152. Implements common esp-idf application behavior.
  153. idf applications should inherent from this class and overwrite method get_binary_path.
  154. """
  155. IDF_DOWNLOAD_CONFIG_FILE = "download.config"
  156. IDF_FLASH_ARGS_FILE = "flasher_args.json"
  157. def __init__(self, app_path, config_name=None, target=None):
  158. super(IDFApp, self).__init__(app_path)
  159. self.config_name = config_name
  160. self.target = target
  161. self.idf_path = self.get_sdk_path()
  162. self.binary_path = self.get_binary_path(app_path, config_name, target)
  163. self.elf_file = self._get_elf_file_path(self.binary_path)
  164. self._elf_file_sha256 = None
  165. assert os.path.exists(self.binary_path)
  166. if self.IDF_DOWNLOAD_CONFIG_FILE not in os.listdir(self.binary_path):
  167. if self.IDF_FLASH_ARGS_FILE not in os.listdir(self.binary_path):
  168. msg = ("Neither {} nor {} exists. "
  169. "Try to run 'make print_flash_cmd | tail -n 1 > {}/{}' "
  170. "or 'idf.py build' "
  171. "for resolving the issue."
  172. "").format(self.IDF_DOWNLOAD_CONFIG_FILE, self.IDF_FLASH_ARGS_FILE,
  173. self.binary_path, self.IDF_DOWNLOAD_CONFIG_FILE)
  174. raise AssertionError(msg)
  175. # In order to keep backward compatibility, flash_files is unchanged.
  176. # However, we now have a new attribute encrypt_files.
  177. self.flash_files, self.encrypt_files, self.flash_settings = self._parse_flash_download_config()
  178. self.partition_table = self._parse_partition_table()
  179. @classmethod
  180. def get_sdk_path(cls):
  181. # type: () -> str
  182. idf_path = os.getenv("IDF_PATH")
  183. assert idf_path
  184. assert os.path.exists(idf_path)
  185. return idf_path
  186. def _get_sdkconfig_paths(self):
  187. """
  188. returns list of possible paths where sdkconfig could be found
  189. Note: could be overwritten by a derived class to provide other locations or order
  190. """
  191. return [os.path.join(self.binary_path, "sdkconfig"), os.path.join(self.binary_path, "..", "sdkconfig")]
  192. def get_sdkconfig(self):
  193. """
  194. reads sdkconfig and returns a dictionary with all configuredvariables
  195. :raise: AssertionError: if sdkconfig file does not exist in defined paths
  196. """
  197. d = {}
  198. sdkconfig_file = None
  199. for i in self._get_sdkconfig_paths():
  200. if os.path.exists(i):
  201. sdkconfig_file = i
  202. break
  203. assert sdkconfig_file is not None
  204. with open(sdkconfig_file) as f:
  205. for line in f:
  206. configs = line.split('=')
  207. if len(configs) == 2:
  208. d[configs[0]] = configs[1].rstrip()
  209. return d
  210. def get_binary_path(self, app_path, config_name=None, target=None):
  211. # type: (str, str, str) -> str
  212. """
  213. get binary path according to input app_path.
  214. subclass must overwrite this method.
  215. :param app_path: path of application
  216. :param config_name: name of the application build config. Will match any config if None
  217. :param target: target name. Will match for target if None
  218. :return: abs app binary path
  219. """
  220. pass
  221. @staticmethod
  222. def _get_elf_file_path(binary_path):
  223. ret = ""
  224. file_names = os.listdir(binary_path)
  225. for fn in file_names:
  226. if os.path.splitext(fn)[1] == ".elf":
  227. ret = os.path.join(binary_path, fn)
  228. return ret
  229. def _int_offs_abs_paths(self, files_list):
  230. return [(int(offs, 0),
  231. os.path.join(self.binary_path, file_path.strip()))
  232. for (offs, file_path) in files_list]
  233. def _parse_flash_download_config(self):
  234. """
  235. Parse flash download config from build metadata files
  236. Sets self.flash_files, self.flash_settings
  237. (Called from constructor)
  238. Returns (flash_files, encrypt_files, flash_settings)
  239. """
  240. if self.IDF_FLASH_ARGS_FILE in os.listdir(self.binary_path):
  241. # CMake version using build metadata file
  242. path = os.path.join(self.binary_path, self.IDF_FLASH_ARGS_FILE)
  243. else:
  244. # GNU Make version uses download.config arguments file
  245. path = os.path.join(self.binary_path, self.IDF_DOWNLOAD_CONFIG_FILE)
  246. # If the JSON doesn't find the encrypted flag for our files, provide
  247. # a default encrpytion flag: the macro
  248. # CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT
  249. sdkconfig_dict = self.get_sdkconfig()
  250. default_encryption = "CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT" in sdkconfig_dict
  251. flash_files, encrypt_files, flash_settings, _ = parse_flash_settings(path, default_encryption)
  252. # Flash setting "encrypt" only and only if all the files to flash
  253. # must be encrypted. Else, this parameter should be False.
  254. # All files must be encrypted is both file lists are the same
  255. flash_settings["encrypt"] = sorted(flash_files) == sorted(encrypt_files)
  256. return self._int_offs_abs_paths(flash_files), self._int_offs_abs_paths(encrypt_files), flash_settings
  257. def _parse_partition_table(self):
  258. """
  259. Parse partition table contents based on app binaries
  260. Returns partition_table data
  261. (Called from constructor)
  262. """
  263. partition_tool = os.path.join(self.idf_path,
  264. "components",
  265. "partition_table",
  266. "gen_esp32part.py")
  267. assert os.path.exists(partition_tool)
  268. errors = []
  269. # self.flash_files is sorted based on offset in order to have a consistent result with different versions of
  270. # Python
  271. for (_, path) in sorted(self.flash_files, key=lambda elem: elem[0]):
  272. if 'partition' in os.path.split(path)[1]:
  273. partition_file = os.path.join(self.binary_path, path)
  274. process = subprocess.Popen([sys.executable, partition_tool, partition_file],
  275. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  276. (raw_data, raw_error) = process.communicate()
  277. if isinstance(raw_error, bytes):
  278. raw_error = raw_error.decode()
  279. if 'Traceback' in raw_error:
  280. # Some exception occured. It is possible that we've tried the wrong binary file.
  281. errors.append((path, raw_error))
  282. continue
  283. if isinstance(raw_data, bytes):
  284. raw_data = raw_data.decode()
  285. break
  286. else:
  287. traceback_msg = os.linesep.join(['{} {}:{}{}'.format(partition_tool,
  288. p,
  289. os.linesep,
  290. msg) for p, msg in errors])
  291. raise ValueError("No partition table found for IDF binary path: {}{}{}".format(self.binary_path,
  292. os.linesep,
  293. traceback_msg))
  294. partition_table = dict()
  295. for line in raw_data.splitlines():
  296. if line[0] != "#":
  297. try:
  298. _name, _type, _subtype, _offset, _size, _flags = line.split(",")
  299. if _size[-1] == "K":
  300. _size = int(_size[:-1]) * 1024
  301. elif _size[-1] == "M":
  302. _size = int(_size[:-1]) * 1024 * 1024
  303. else:
  304. _size = int(_size)
  305. _offset = int(_offset, 0)
  306. except ValueError:
  307. continue
  308. partition_table[_name] = {
  309. "type": _type,
  310. "subtype": _subtype,
  311. "offset": _offset,
  312. "size": _size,
  313. "flags": _flags
  314. }
  315. return partition_table
  316. def get_elf_sha256(self):
  317. if self._elf_file_sha256:
  318. return self._elf_file_sha256
  319. sha256 = hashlib.sha256()
  320. with open(self.elf_file, 'rb') as f:
  321. sha256.update(f.read())
  322. self._elf_file_sha256 = sha256.hexdigest()
  323. return self._elf_file_sha256
  324. class Example(IDFApp):
  325. def _get_sdkconfig_paths(self):
  326. """
  327. overrides the parent method to provide exact path of sdkconfig for example tests
  328. """
  329. return [os.path.join(self.binary_path, "..", "sdkconfig")]
  330. def _try_get_binary_from_local_fs(self, app_path, config_name=None, target=None, local_build_dir="build_examples"):
  331. # build folder of example path
  332. path = os.path.join(self.idf_path, app_path, "build")
  333. if os.path.exists(path):
  334. return path
  335. if not config_name:
  336. config_name = "default"
  337. if not target:
  338. target = "esp32"
  339. # Search for CI build folders.
  340. # Path format: $IDF_PATH/build_examples/app_path_with_underscores/config/target
  341. # (see tools/ci/build_examples_cmake.sh)
  342. # For example: $IDF_PATH/build_examples/examples_get-started_blink/default/esp32
  343. app_path_underscored = app_path.replace(os.path.sep, "_")
  344. example_path = os.path.join(self.idf_path, local_build_dir)
  345. for dirpath in os.listdir(example_path):
  346. if os.path.basename(dirpath) == app_path_underscored:
  347. path = os.path.join(example_path, dirpath, config_name, target, "build")
  348. if os.path.exists(path):
  349. return path
  350. else:
  351. return None
  352. def get_binary_path(self, app_path, config_name=None, target=None):
  353. path = self._try_get_binary_from_local_fs(app_path, config_name, target)
  354. if path:
  355. return path
  356. else:
  357. artifacts = Artifacts(self.idf_path,
  358. CIAssignExampleTest.get_artifact_index_file(case_group=CIAssignExampleTest.ExampleGroup),
  359. app_path, config_name, target)
  360. path = artifacts.download_artifacts()
  361. if path:
  362. return os.path.join(self.idf_path, path)
  363. else:
  364. raise OSError("Failed to find example binary")
  365. class UT(IDFApp):
  366. def get_binary_path(self, app_path, config_name=None, target=None):
  367. if not config_name:
  368. config_name = "default"
  369. path = os.path.join(self.idf_path, app_path)
  370. default_build_path = os.path.join(path, "build")
  371. if os.path.exists(default_build_path):
  372. return default_build_path
  373. # first try to get from build folder of unit-test-app
  374. path = os.path.join(self.idf_path, "tools", "unit-test-app", "build")
  375. if os.path.exists(path):
  376. # found, use bin in build path
  377. return path
  378. # ``make ut-build-all-configs`` or ``make ut-build-CONFIG`` will copy binary to output folder
  379. path = os.path.join(self.idf_path, "tools", "unit-test-app", "output", target, config_name)
  380. if os.path.exists(path):
  381. return path
  382. raise OSError("Failed to get unit-test-app binary path")
  383. class TestApp(Example):
  384. def get_binary_path(self, app_path, config_name=None, target=None):
  385. path = self._try_get_binary_from_local_fs(app_path, config_name, target, local_build_dir="build_test_apps")
  386. if path:
  387. return path
  388. else:
  389. artifacts = Artifacts(self.idf_path,
  390. CIAssignExampleTest.get_artifact_index_file(case_group=CIAssignExampleTest.TestAppsGroup),
  391. app_path, config_name, target)
  392. path = artifacts.download_artifacts()
  393. if path:
  394. return os.path.join(self.idf_path, path)
  395. else:
  396. raise OSError("Failed to find example binary")
  397. class LoadableElfTestApp(TestApp):
  398. def __init__(self, app_path, app_files, config_name=None, target=None):
  399. # add arg `app_files` for loadable elf test_app.
  400. # Such examples only build elf files, so it doesn't generate flasher_args.json.
  401. # So we can't get app files from config file. Test case should pass it to application.
  402. super(IDFApp, self).__init__(app_path)
  403. self.app_files = app_files
  404. self.config_name = config_name
  405. self.target = target
  406. self.idf_path = self.get_sdk_path()
  407. self.binary_path = self.get_binary_path(app_path, config_name, target)
  408. self.elf_file = self._get_elf_file_path(self.binary_path)
  409. assert os.path.exists(self.binary_path)
  410. def get_binary_path(self, app_path, config_name=None, target=None):
  411. path = self._try_get_binary_from_local_fs(app_path, config_name, target, local_build_dir="build_test_apps")
  412. if path:
  413. return path
  414. else:
  415. artifacts = Artifacts(self.idf_path,
  416. CIAssignExampleTest.get_artifact_index_file(case_group=CIAssignExampleTest.TestAppsGroup),
  417. app_path, config_name, target)
  418. path = artifacts.download_artifact_files(self.app_files)
  419. if path:
  420. return os.path.join(self.idf_path, path)
  421. else:
  422. raise OSError("Failed to find the loadable ELF file")
  423. class SSC(IDFApp):
  424. def get_binary_path(self, app_path, config_name=None, target=None):
  425. # TODO: to implement SSC get binary path
  426. return app_path
  427. class AT(IDFApp):
  428. def get_binary_path(self, app_path, config_name=None, target=None):
  429. # TODO: to implement AT get binary path
  430. return app_path