sample_test_run.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. def start_server(cwd):
  13. """
  14. Startup the 'simple' process works in TCP server mode
  15. """
  16. app_server = subprocess.Popen(shlex.split("./simple -s "), cwd=cwd)
  17. return app_server
  18. def query_installed_application(cwd):
  19. """
  20. Query all installed applications
  21. """
  22. qry_prc = subprocess.run(
  23. shlex.split("./host_tool -q"), cwd=cwd, check=False, capture_output=True
  24. )
  25. assert qry_prc.returncode == 69
  26. return qry_prc.returncode, qry_prc.stdout
  27. def install_wasm_application(wasm_name, wasm_file, cwd):
  28. """
  29. Install a wasm application
  30. """
  31. inst_prc = subprocess.run(
  32. shlex.split(f"./host_tool -i {wasm_name} -f {wasm_file}"),
  33. cwd=cwd,
  34. check=False,
  35. capture_output=True,
  36. )
  37. assert inst_prc.returncode == 65
  38. return inst_prc.returncode, inst_prc.stdout
  39. def uninstall_wasm_application(wasm_name, cwd):
  40. """
  41. Uninstall a wasm application
  42. """
  43. unst_prc = subprocess.run(
  44. shlex.split(f"./host_tool -u {wasm_name}"),
  45. cwd=cwd,
  46. check=False,
  47. capture_output=True,
  48. )
  49. assert unst_prc.returncode == 66
  50. return unst_prc.returncode, unst_prc.stdout
  51. def send_get_to_wasm_application(wasm_name, url, cwd):
  52. """
  53. send a request (GET) from host to an applicaton
  54. """
  55. qry_prc = subprocess.run(
  56. shlex.split(f"./host_tool -r /app/{wasm_name}{url} -A GET"),
  57. cwd=cwd,
  58. check=False,
  59. capture_output=True,
  60. )
  61. assert qry_prc.returncode == 69
  62. return qry_prc.returncode, qry_prc.stdout
  63. def main():
  64. """
  65. GO!GO!!GO!!!
  66. """
  67. parser = argparse.ArgumentParser(description="run the sample and examine outputs")
  68. parser.add_argument("working_directory", type=str)
  69. args = parser.parse_args()
  70. ret = 1
  71. app_server = None
  72. try:
  73. app_server = start_server(args.working_directory)
  74. # wait for a second
  75. time.sleep(1)
  76. print("--> Install timer.wasm...")
  77. install_wasm_application(
  78. "timer", "./wasm-apps/timer.wasm", args.working_directory
  79. )
  80. print("--> Install event_publisher.wasm...")
  81. install_wasm_application(
  82. "event_publisher",
  83. "./wasm-apps/event_publisher.wasm",
  84. args.working_directory,
  85. )
  86. print("--> Install event_subscriber.wasm...")
  87. install_wasm_application(
  88. "event_subscriber",
  89. "./wasm-apps/event_subscriber.wasm",
  90. args.working_directory,
  91. )
  92. print("--> Uninstall timer.wasm...")
  93. uninstall_wasm_application("timer", args.working_directory)
  94. print("--> Uninstall event_publisher.wasm...")
  95. uninstall_wasm_application(
  96. "event_publisher",
  97. args.working_directory,
  98. )
  99. print("--> Uninstall event_subscriber.wasm...")
  100. uninstall_wasm_application(
  101. "event_subscriber",
  102. args.working_directory,
  103. )
  104. print("--> Query all installed applications...")
  105. query_installed_application(args.working_directory)
  106. print("--> Install request_handler.wasm...")
  107. install_wasm_application(
  108. "request_handler",
  109. "./wasm-apps/request_handler.wasm",
  110. args.working_directory,
  111. )
  112. print("--> Query again...")
  113. query_installed_application(args.working_directory)
  114. print("--> Install request_sender.wasm...")
  115. install_wasm_application(
  116. "request_sender",
  117. "./wasm-apps/request_sender.wasm",
  118. args.working_directory,
  119. )
  120. print("--> Send GET to the Wasm application named request_handler...")
  121. send_get_to_wasm_application("request_handler", "/url1", args.working_directory)
  122. print("--> All pass")
  123. ret = 0
  124. except AssertionError:
  125. traceback.print_exc()
  126. finally:
  127. app_server.kill()
  128. return ret
  129. if __name__ == "__main__":
  130. sys.exit(main())