run.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (C) 2019 Intel Corporation. All rights reserved.
  4. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5. #
  6. import json
  7. import os
  8. import subprocess
  9. import glob
  10. import re
  11. from typing import Dict
  12. WORK_DIR = os.getcwd()
  13. TEST_WASM_COMMAND = (
  14. "./build/build-{runtime}/iwasm {running_options} {running_mode} {file} {argument}"
  15. )
  16. COMPILE_AOT_COMMAND = "./build/build-wamrc/{compiler} {options} -o {out_file} {in_file}"
  17. TEST_AOT_COMMAND = "./build/build-{runtime}/iwasm {running_options} {file} {argument}"
  18. LOG_FILE = "issues_tests.log"
  19. LOG_ENTRY = """
  20. =======================================================
  21. Failing issue id: {}.
  22. run with command_lists: {}
  23. {}
  24. {}
  25. =======================================================
  26. """
  27. # Function to read and parse JSON file
  28. def read_json_file(file_path):
  29. with open(file_path, "r") as file:
  30. return json.load(file)
  31. return None
  32. def dump_error_log(failing_issue_id, command_lists, exit_code_cmp, stdout_cmp):
  33. with open(LOG_FILE, "a") as file:
  34. file.write(
  35. LOG_ENTRY.format(failing_issue_id, command_lists, exit_code_cmp, stdout_cmp)
  36. )
  37. def get_issue_ids_should_test():
  38. # Define the path pattern
  39. path_pattern = "issues/issue-*"
  40. # Regular expression to extract the number
  41. pattern = r"issue-(\d+)"
  42. # Initialize a set to store the issue numbers
  43. issue_numbers = set()
  44. # Use glob to find directories matching the pattern
  45. for dir_path in glob.glob(path_pattern):
  46. # Extract the issue number using regular expression
  47. match = re.search(pattern, dir_path)
  48. if match:
  49. issue_number = match.group(1)
  50. issue_numbers.add(int(issue_number))
  51. # Print the set of issue numbers
  52. return issue_numbers
  53. def get_and_check(d, key, default=None, nullable=False):
  54. element = d.get(key, default)
  55. if not nullable and element is None:
  56. raise Exception(f"Missing {key} in {d}")
  57. return element
  58. def run_and_compare_results(
  59. passed_ids, failed_ids, issue_id, cmd, description, ret_code, stdout_content
  60. ):
  61. print(f"####################################")
  62. print(f"test BA issue #{issue_id} `{description}`: {cmd}")
  63. command_list = cmd.split()
  64. result = subprocess.run(
  65. command_list,
  66. stdout=subprocess.PIPE,
  67. stderr=subprocess.PIPE,
  68. text=True,
  69. errors="ignore",
  70. )
  71. actual_exit_code = result.returncode
  72. actual_output = result.stdout.rstrip("\n")
  73. exit_code_cmp = f"exit code (actual, expected) : {actual_exit_code, ret_code}"
  74. stdout_cmp = f"stdout (actual, expected) : {actual_output, stdout_content}"
  75. print(exit_code_cmp)
  76. print(stdout_cmp)
  77. if actual_exit_code == ret_code and (
  78. actual_output == stdout_content
  79. or (stdout_content == "Compile success"
  80. and actual_output.find(stdout_content) != -1)
  81. or (len(stdout_content) > 30 and actual_output.find(stdout_content) != -1)
  82. ):
  83. passed_ids.add(issue_id)
  84. print("== PASS ==")
  85. else:
  86. failed_ids.add(issue_id)
  87. print(f"== FAILED: {issue_id} ==")
  88. dump_error_log(
  89. issue_id,
  90. command_list,
  91. exit_code_cmp,
  92. stdout_cmp,
  93. )
  94. print("")
  95. def run_issue_test_wamrc(
  96. passed_ids, failed_ids, issue_id, compile_options, stdout_only_cmp_last_line=False
  97. ):
  98. compiler = get_and_check(compile_options, "compiler")
  99. only_compile = get_and_check(compile_options, "only compile")
  100. in_file = get_and_check(compile_options, "in file")
  101. out_file = get_and_check(compile_options, "out file")
  102. options = get_and_check(compile_options, "options")
  103. expected_return = get_and_check(compile_options, "expected return")
  104. ret_code = get_and_check(expected_return, "ret code")
  105. stdout_content = get_and_check(expected_return, "stdout content")
  106. description = get_and_check(expected_return, "description")
  107. issue_path = os.path.join(WORK_DIR, f"issues/issue-{issue_id}/")
  108. # file maybe *.wasm or *.aot, needs to the match the exact file name
  109. actual_file = glob.glob(issue_path + in_file)
  110. assert len(actual_file) == 1
  111. # the absolute file path
  112. in_file_path = os.path.join(issue_path, actual_file[0])
  113. out_file_path = os.path.join(issue_path, out_file)
  114. cmd = COMPILE_AOT_COMMAND.format(
  115. compiler=compiler, options=options, out_file=out_file_path, in_file=in_file_path
  116. )
  117. run_and_compare_results(
  118. passed_ids, failed_ids, issue_id, cmd, description, ret_code, stdout_content
  119. )
  120. return only_compile
  121. def run_issue_test_iwasm(passed_ids, failed_ids, issue_id, test_case):
  122. runtime = get_and_check(test_case, "runtime")
  123. mode = get_and_check(test_case, "mode")
  124. file = get_and_check(test_case, "file")
  125. options = get_and_check(test_case, "options")
  126. argument = get_and_check(test_case, "argument")
  127. expected_return = get_and_check(test_case, "expected return")
  128. ret_code = get_and_check(expected_return, "ret code")
  129. stdout_content = get_and_check(expected_return, "stdout content")
  130. description = get_and_check(expected_return, "description")
  131. issue_path = os.path.join(WORK_DIR, f"issues/issue-{issue_id}/")
  132. # file maybe *.wasm or *.aot, needs to the match the exact file name
  133. actual_file = glob.glob(issue_path + file)
  134. assert len(actual_file) == 1
  135. # the absolute file path
  136. file_path = os.path.join(issue_path, actual_file[0])
  137. if mode == "aot":
  138. cmd = TEST_AOT_COMMAND.format(
  139. runtime=runtime,
  140. file=file_path,
  141. running_options=options,
  142. argument=argument,
  143. )
  144. else:
  145. if mode == "classic-interp":
  146. running_mode = "--interp"
  147. elif mode == "fast-interp":
  148. running_mode = ""
  149. else:
  150. running_mode = f"--{mode}"
  151. cmd = TEST_WASM_COMMAND.format(
  152. runtime=runtime,
  153. running_mode=running_mode,
  154. file=file_path,
  155. running_options=options,
  156. argument=argument,
  157. )
  158. run_and_compare_results(
  159. passed_ids, failed_ids, issue_id, cmd, description, ret_code, stdout_content
  160. )
  161. def process_and_run_test_cases(data: Dict[str, Dict]):
  162. issue_ids_should_test = get_issue_ids_should_test()
  163. passed_ids = set()
  164. failed_ids = set()
  165. for test_case in data.get("test cases", []):
  166. is_deprecated = get_and_check(test_case, "deprecated")
  167. issue_ids = get_and_check(test_case, "ids", default=[])
  168. if is_deprecated:
  169. print(f"test case {issue_ids} are deprecated, continue running nest one(s)")
  170. continue
  171. compile_options = get_and_check(test_case, "compile_options", nullable=True)
  172. for issue_id in issue_ids:
  173. only_compile = False
  174. # if this issue needs to test wamrc to compile the test case first
  175. if compile_options:
  176. only_compile = compile_options["only compile"]
  177. run_issue_test_wamrc(passed_ids, failed_ids, issue_id, compile_options)
  178. # if this issue requires to test iwasm to run the test case
  179. if not only_compile:
  180. run_issue_test_iwasm(passed_ids, failed_ids, issue_id, test_case)
  181. # cross out the this issue_id in the should test set
  182. issue_ids_should_test.remove(issue_id)
  183. total = len(passed_ids) + len(failed_ids)
  184. passed = len(passed_ids)
  185. failed = len(failed_ids)
  186. issue_ids_should_test = (
  187. issue_ids_should_test if issue_ids_should_test else "no more"
  188. )
  189. print(f"==== Test results ====")
  190. print(f" Total: {total}")
  191. print(f" Passed: {passed}")
  192. print(f" Failed: {failed}")
  193. def main():
  194. # Path to the JSON file
  195. file_path = "running_config.json"
  196. # Read and parse the JSON file
  197. data = read_json_file(file_path)
  198. # Check if data is successfully read
  199. if data is None:
  200. assert 0, "No data to process."
  201. # Remove the log file from last run if it exists
  202. if os.path.exists(LOG_FILE):
  203. os.remove(LOG_FILE)
  204. # Process the data
  205. process_and_run_test_cases(data)
  206. if __name__ == "__main__":
  207. main()