sample_test_run.py 6.5 KB

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