sample_test_run.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 argparse
  7. import shlex
  8. import subprocess
  9. import sys
  10. import time
  11. import traceback
  12. import glob
  13. WAMRC_CMD = "../../wamr-compiler/build/wamrc"
  14. def compile_wasm_files_to_aot(wasm_apps_dir):
  15. wasm_files = glob.glob(wasm_apps_dir + "/*.wasm")
  16. print("Compile wasm app into aot files")
  17. for wasm_file in wasm_files:
  18. aot_file = wasm_file[0 : len(wasm_file) - 5] + ".aot";
  19. cmd = [ WAMRC_CMD, "-o", aot_file, wasm_file ]
  20. subprocess.check_call(cmd)
  21. def start_server(cwd):
  22. """
  23. Startup the 'simple' process works in TCP server mode
  24. """
  25. app_server = subprocess.Popen(shlex.split("./simple -s "), cwd=cwd)
  26. return app_server
  27. def query_installed_application(cwd):
  28. """
  29. Query all installed applications
  30. """
  31. qry_prc = subprocess.run(
  32. shlex.split("./host_tool -q"), cwd=cwd, check=False, capture_output=True
  33. )
  34. assert qry_prc.returncode == 69
  35. return qry_prc.returncode, qry_prc.stdout
  36. def install_wasm_application(wasm_name, wasm_file, cwd):
  37. """
  38. Install a wasm application
  39. """
  40. inst_prc = subprocess.run(
  41. shlex.split(f"./host_tool -i {wasm_name} -f {wasm_file}"),
  42. cwd=cwd,
  43. check=False,
  44. capture_output=True,
  45. )
  46. assert inst_prc.returncode == 65
  47. return inst_prc.returncode, inst_prc.stdout
  48. def uninstall_wasm_application(wasm_name, cwd):
  49. """
  50. Uninstall a wasm application
  51. """
  52. unst_prc = subprocess.run(
  53. shlex.split(f"./host_tool -u {wasm_name}"),
  54. cwd=cwd,
  55. check=False,
  56. capture_output=True,
  57. )
  58. assert unst_prc.returncode == 66
  59. return unst_prc.returncode, unst_prc.stdout
  60. def send_get_to_wasm_application(wasm_name, url, cwd):
  61. """
  62. send a request (GET) from host to an applicaton
  63. """
  64. qry_prc = subprocess.run(
  65. shlex.split(f"./host_tool -r /app/{wasm_name}{url} -A GET"),
  66. cwd=cwd,
  67. check=False,
  68. capture_output=True,
  69. )
  70. assert qry_prc.returncode == 69
  71. return qry_prc.returncode, qry_prc.stdout
  72. def main():
  73. """
  74. GO!GO!!GO!!!
  75. """
  76. parser = argparse.ArgumentParser(description="run the sample and examine outputs")
  77. parser.add_argument("working_directory", type=str)
  78. parser.add_argument("--aot", action='store_true', help="Test with AOT")
  79. args = parser.parse_args()
  80. test_aot = False
  81. suffix = ".wasm"
  82. if not args.aot:
  83. print("Test with interpreter mode")
  84. else:
  85. print("Test with AOT mode")
  86. test_aot = True
  87. suffix = ".aot"
  88. wasm_apps_dir = args.working_directory + "/wasm-apps"
  89. compile_wasm_files_to_aot(wasm_apps_dir)
  90. ret = 1
  91. app_server = None
  92. try:
  93. app_server = start_server(args.working_directory)
  94. # wait for a second
  95. time.sleep(1)
  96. print("--> Install timer" + suffix + "...")
  97. install_wasm_application(
  98. "timer", "./wasm-apps/timer" + suffix, args.working_directory
  99. )
  100. # wait for a second
  101. time.sleep(3)
  102. print("--> Query all installed applications...")
  103. query_installed_application(args.working_directory)
  104. print("--> Install event_publisher" + suffix + "...")
  105. install_wasm_application(
  106. "event_publisher",
  107. "./wasm-apps/event_publisher" + suffix,
  108. args.working_directory,
  109. )
  110. print("--> Install event_subscriber" + suffix + "...")
  111. install_wasm_application(
  112. "event_subscriber",
  113. "./wasm-apps/event_subscriber" + suffix,
  114. args.working_directory,
  115. )
  116. print("--> Query all installed applications...")
  117. query_installed_application(args.working_directory)
  118. print("--> Uninstall timer" + suffix + "...")
  119. uninstall_wasm_application("timer", args.working_directory)
  120. print("--> Query all installed applications...")
  121. query_installed_application(args.working_directory)
  122. print("--> Uninstall event_publisher" + suffix + "...")
  123. uninstall_wasm_application(
  124. "event_publisher",
  125. args.working_directory,
  126. )
  127. print("--> Uninstall event_subscriber" + suffix + "...")
  128. uninstall_wasm_application(
  129. "event_subscriber",
  130. args.working_directory,
  131. )
  132. print("--> Query all installed applications...")
  133. query_installed_application(args.working_directory)
  134. print("--> Install request_handler" + suffix + "...")
  135. install_wasm_application(
  136. "request_handler",
  137. "./wasm-apps/request_handler" + suffix,
  138. args.working_directory,
  139. )
  140. print("--> Query again...")
  141. query_installed_application(args.working_directory)
  142. print("--> Install request_sender" + suffix + "...")
  143. install_wasm_application(
  144. "request_sender",
  145. "./wasm-apps/request_sender" + suffix,
  146. args.working_directory,
  147. )
  148. print("--> Send GET to the Wasm application named request_handler...")
  149. send_get_to_wasm_application("request_handler", "/url1", args.working_directory)
  150. print("--> Uninstall request_handler" + suffix + "...")
  151. uninstall_wasm_application(
  152. "request_handler",
  153. args.working_directory,
  154. )
  155. print("--> Uninstall request_sender" + suffix + "...")
  156. uninstall_wasm_application(
  157. "request_sender",
  158. args.working_directory,
  159. )
  160. # Install a wasm app named "__exit_app_manager__" just to make app manager exit
  161. # while the wasm app is uninstalled, so as to collect the code coverage data.
  162. # Only available when collecting code coverage is enabled.
  163. print("--> Install timer" + suffix + "...")
  164. install_wasm_application(
  165. "__exit_app_manager__", "./wasm-apps/timer" + suffix, args.working_directory
  166. )
  167. print("--> Uninstall timer" + suffix + "...")
  168. uninstall_wasm_application(
  169. "__exit_app_manager__",
  170. args.working_directory,
  171. )
  172. # wait for a second
  173. time.sleep(1)
  174. print("--> All pass")
  175. ret = 0
  176. except AssertionError:
  177. traceback.print_exc()
  178. finally:
  179. app_server.kill()
  180. return ret
  181. if __name__ == "__main__":
  182. sys.exit(main())