utils.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import unittest
  2. import sys
  3. import os
  4. import sys
  5. import time
  6. import json
  7. import urllib
  8. import urllib2
  9. import subprocess
  10. import signal
  11. username = "test_user"
  12. def serialize(value):
  13. if isinstance(value, str):
  14. return '"%s"' % value
  15. else:
  16. return str(value)
  17. def execute(scriptname, parameters=[], wait=False):
  18. if os.name == "nt":
  19. command = ["%s.bat" % scriptname] + parameters
  20. elif os.name == "posix":
  21. command = ["./%s.sh" % scriptname] + parameters
  22. else:
  23. raise Exception("Unknown OS: " + str(os.name))
  24. if wait:
  25. return subprocess.call(command, shell=False)
  26. else:
  27. return subprocess.Popen(command, shell=False)
  28. def kill(process):
  29. if os.name == "nt":
  30. subprocess.call(["taskkill", "/F", "/T", "/PID", "%i" % process.pid])
  31. elif os.name == "posix":
  32. subprocess.call(["pkill", "-P", "%i" % process.pid])
  33. def flush_data(data):
  34. if data:
  35. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": username})), timeout=10).read()
  36. return []
  37. def run_file(files, parameters, expected, mode):
  38. # Resolve file
  39. import os.path
  40. time.sleep(0.01)
  41. try:
  42. # Run Modelverse server
  43. proc = execute("run_local_modelverse", ["bootstrap/bootstrap.m"], wait=False)
  44. try:
  45. for filename in files:
  46. if filename == "--fast":
  47. continue
  48. if os.path.isfile("integration/code/%s" % filename):
  49. mod_filename = "integration/code/%s" % filename
  50. elif os.path.isfile("bootstrap/%s" % filename):
  51. mod_filename = "bootstrap/%s" % filename
  52. else:
  53. raise Exception("File not found: %s" % filename)
  54. print("Found file " + str(mod_filename))
  55. # Load in the file required
  56. timeout_val = 120
  57. while 1:
  58. proc2 = execute("compile", [mod_filename, username, filename, mode], wait=False)
  59. while proc2.returncode is None:
  60. time.sleep(0.01)
  61. proc2.poll()
  62. timeout_val -= 0.01
  63. if timeout_val < 0:
  64. kill(proc2)
  65. print("Compilation timeout expired!")
  66. return False
  67. if proc2.returncode != 7:
  68. break
  69. # Make sure everything stopped correctly
  70. assert proc2.returncode == 0
  71. if proc2.returncode != 0:
  72. return False
  73. if mode[-1] == "O":
  74. # Fire up the linker
  75. val = execute("link_and_load", [username] + files, wait=True)
  76. if val != 0:
  77. raise Exception("Linking error")
  78. # Send in the actual request and wait for replies
  79. data = []
  80. for p in parameters:
  81. if isinstance(p, tuple):
  82. for v in p:
  83. data.append(["V", serialize(v)])
  84. else:
  85. data.append(["V", serialize(p)])
  86. flush_data(data)
  87. for e in expected:
  88. c = len(e) if isinstance(e, set) else 1
  89. for _ in range(c):
  90. val = urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "get_output", "username": username})), timeout=10).read()
  91. val = val.split("=", 2)[2]
  92. print("Got %s, expect %s" % (val, e))
  93. if isinstance(e, set):
  94. assert str(val) in e
  95. if str(val) not in e:
  96. return False
  97. else:
  98. assert str(val) == e
  99. if str(val) != e:
  100. return False
  101. # All passed!
  102. return True
  103. except:
  104. raise
  105. finally:
  106. try:
  107. kill(proc2)
  108. except UnboundLocalError:
  109. pass
  110. except:
  111. raise
  112. finally:
  113. try:
  114. kill(proc)
  115. except UnboundLocalError:
  116. pass
  117. def run_barebone(parameters, expected, interface="0", timeout=False, wait=False, link=None):
  118. try:
  119. # Run Modelverse server
  120. proc = execute("run_local_modelverse", ["bootstrap/bootstrap.m"], wait=False)
  121. # Create user and set interface
  122. timeout_val = 15
  123. start = time.time()
  124. while 1:
  125. try:
  126. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % username, "username": "user_manager"})), timeout=1).read()
  127. if interface is not None:
  128. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": interface, "username": username})), timeout=1).read()
  129. break
  130. except:
  131. time.sleep(0.01)
  132. if time.time() - start > timeout_val:
  133. raise
  134. # Send in the actual request and wait for replies
  135. var_list = {}
  136. data = []
  137. for p in parameters:
  138. if isinstance(p, int):
  139. if p not in var_list:
  140. data = flush_data(data)
  141. val = urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "get_output", "username": username})), timeout=10).read()
  142. val = val.split("=", 2)[1].split("&", 1)[0]
  143. var_list[p] = val
  144. continue
  145. else:
  146. val = var_list[p]
  147. t = "R"
  148. else:
  149. val = p
  150. t = "V"
  151. data.append([t, val])
  152. data = flush_data(data)
  153. # Now do linking and loading
  154. if link is not None:
  155. # Execute linker
  156. timeout_val = 10
  157. proc2 = execute("link_and_load", [username] + link, wait=False)
  158. while proc2.returncode is None:
  159. time.sleep(0.01)
  160. proc2.poll()
  161. timeout_val -= 0.01
  162. if timeout_val < 0:
  163. kill(proc2)
  164. print("Linking timeout expired!")
  165. return False
  166. counter = 0
  167. for e in expected:
  168. print("Expect " + str(e))
  169. c = len(e) if isinstance(e, set) else 1
  170. for _ in range(c):
  171. try:
  172. val = urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "get_output", "username": username})), timeout=15).read()
  173. except:
  174. if timeout:
  175. return True
  176. else:
  177. raise
  178. val = val.split("=", 2)[2]
  179. print("Got %s, expect %s" % (val, e))
  180. if isinstance(e, set):
  181. assert str(val) in e
  182. if str(val) not in e:
  183. return False
  184. else:
  185. assert str(val) == e
  186. if str(val) != e:
  187. return False
  188. # All passed!
  189. return not timeout
  190. finally:
  191. kill(proc)