You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

216 lines
7.5 KiB

  1. # -*- coding: utf-8 -*-
  2. import nilmrun.server
  3. from nilmdb.client.httpclient import HTTPClient, ClientError, ServerError
  4. from nilmdb.utils.printf import *
  5. from nose.plugins.skip import SkipTest
  6. from nose.tools import *
  7. from nose.tools import assert_raises
  8. import itertools
  9. import distutils.version
  10. import os
  11. import sys
  12. import threading
  13. import cStringIO
  14. import simplejson as json
  15. import unittest
  16. import warnings
  17. import time
  18. import re
  19. import urllib2
  20. from urllib2 import urlopen, HTTPError
  21. import requests
  22. import pprint
  23. from testutil.helpers import *
  24. testurl = "http://localhost:32181/"
  25. def setup_module():
  26. global test_server
  27. # Start web app on a custom port
  28. test_server = nilmrun.server.Server(host = "127.0.0.1",
  29. port = 32181,
  30. force_traceback = True)
  31. test_server.start(blocking = False)
  32. def teardown_module():
  33. global test_server
  34. # Close web app
  35. test_server.stop()
  36. class TestClient(object):
  37. def test_client_01_basic(self):
  38. client = HTTPClient(baseurl = testurl)
  39. version = client.get("/version")
  40. eq_(distutils.version.LooseVersion(version),
  41. distutils.version.LooseVersion(nilmrun.__version__))
  42. in_("This is NilmRun", client.get("/"))
  43. with assert_raises(ClientError):
  44. client.get("/favicon.ico")
  45. def test_client_02_manager(self):
  46. client = HTTPClient(baseurl = testurl)
  47. eq_(client.get("/process/list"), [])
  48. with assert_raises(ClientError) as e:
  49. client.get("/process/status", { "pid": 12345 })
  50. in_("No such PID", str(e.exception))
  51. with assert_raises(ClientError):
  52. client.get("/process/remove", { "pid": 12345 })
  53. in_("No such PID", str(e.exception))
  54. def test_client_03_process_basic(self):
  55. client = HTTPClient(baseurl = testurl, post_json = True)
  56. # start dummy filter
  57. pid = client.post("/filter/dummy", { "count": 30 })
  58. eq_(client.get("/process/list"), [pid])
  59. time.sleep(1)
  60. # Verify that status looks OK
  61. status = client.get("/process/status", { "pid": pid, "clear": True })
  62. for x in [ "pid", "alive", "exitcode", "name",
  63. "start_time", "parameters", "log" ]:
  64. in_(x, status)
  65. in_("dummy 0\ndummy 1\ndummy 2\ndummy 3\n", status["log"])
  66. eq_(status["alive"], True)
  67. eq_(status["exitcode"], None)
  68. # Check that the log got cleared
  69. status = client.get("/process/status", { "pid": pid })
  70. nin_("dummy 0\ndummy 1\ndummy 2\ndummy 3\n", status["log"])
  71. # See that it ended properly
  72. start = time.time()
  73. while status["alive"] == True and (time.time() - start) < 5:
  74. status = client.get("/process/status", { "pid": pid })
  75. in_("dummy 27\ndummy 28\ndummy 29\n", status["log"])
  76. eq_(status["alive"], False)
  77. eq_(status["exitcode"], 0)
  78. # Remove it
  79. killstatus = client.post("/process/remove", { "pid": pid })
  80. eq_(status, killstatus)
  81. eq_(client.get("/process/list"), [])
  82. with assert_raises(ClientError) as e:
  83. client.post("/process/remove", { "pid": pid })
  84. in_("No such PID", str(e.exception))
  85. def test_client_04_process_terminate(self):
  86. client = HTTPClient(baseurl = testurl, post_json = True)
  87. # Trigger exception in filter
  88. pid = client.post("/filter/dummy", { "count": -1 })
  89. time.sleep(0.5)
  90. status = client.get("/process/status", { "pid": pid })
  91. eq_(status["alive"], False)
  92. eq_(status["exitcode"], 1)
  93. in_("Exception: test exception", status["log"])
  94. client.post("/process/remove", { "pid": pid })
  95. # Kill a running filter by removing it early
  96. newpid = client.post("/filter/dummy", { "count": 30 })
  97. ne_(newpid, pid)
  98. time.sleep(0.5)
  99. status = client.post("/process/remove", { "pid": newpid })
  100. eq_(status["alive"], False)
  101. ne_(status["exitcode"], 0)
  102. # No more
  103. eq_(client.get("/process/list"), [])
  104. # Try to remove a running filter that ignored SIGTERM
  105. # (can't be killed)
  106. pid = client.post("/filter/dummy", { "count": 0 })
  107. with assert_raises(ServerError) as e:
  108. status = client.post("/process/remove", { "pid": pid })
  109. in_("503 Service Unavailable", str(e.exception))
  110. in_("Failed to stop process", str(e.exception))
  111. # Wait for it to die, then remove it
  112. start = time.time()
  113. while (time.time() - start) < 5:
  114. status = client.get("/process/status", { "pid": pid })
  115. if status["alive"] == False:
  116. break
  117. eq_(status["alive"], False)
  118. in_("dummy 39\n", status["log"])
  119. eq_(status["exitcode"], 0)
  120. def test_client_05_trainola_simple(self):
  121. client = HTTPClient(baseurl = testurl, post_json = True)
  122. pid = client.post("/filter/trainola", { "data": {} })
  123. start = time.time()
  124. while (time.time() - start) < 5:
  125. status = client.get("/process/status", { "pid": pid })
  126. if status["alive"] == False:
  127. break
  128. eq_(status["alive"], False)
  129. ne_(status["exitcode"], 0)
  130. @unittest.skip("needs a running nilmdb")
  131. def test_client_06_trainola(self):
  132. client = HTTPClient(baseurl = testurl, post_json = True)
  133. data = { "url": "http://bucket.mit.edu/nilmdb",
  134. "stream": "/sharon/prep-a",
  135. "start": 1366111383280463,
  136. "end": 1366126163457797,
  137. "columns": [ { "name": "P1", "index": 0 },
  138. { "name": "Q1", "index": 1 },
  139. { "name": "P3", "index": 2 } ],
  140. "exemplars": [
  141. { "name": "Boiler Pump ON",
  142. "url": "http://bucket.mit.edu/nilmdb",
  143. "stream": "/sharon/prep-a",
  144. "start": 1366260494269078,
  145. "end": 1366260608185031,
  146. "columns": [ { "name": "P1", "index": 0 },
  147. { "name": "Q1", "index": 1 }
  148. ]
  149. },
  150. { "name": "Boiler Pump OFF",
  151. "url": "http://bucket.mit.edu/nilmdb",
  152. "stream": "/sharon/prep-a",
  153. "start": 1366260864215764,
  154. "end": 1366260870882998,
  155. "columns": [ { "name": "P1", "index": 0 },
  156. { "name": "Q1", "index": 1 }
  157. ]
  158. }
  159. ]
  160. }
  161. # start trainola
  162. pid = client.post("/filter/trainola", { "data": data })
  163. # wait for it to finish
  164. for i in range(60):
  165. time.sleep(1)
  166. if i == 2:
  167. status = client.get("/process/status", { "pid": pid,
  168. "clear": True })
  169. in_("Loading stream data", status['log'])
  170. elif i == 3:
  171. status = client.get("/process/status", { "pid": pid })
  172. nin_("Loading stream data", status['log'])
  173. else:
  174. status = client.get("/process/status", { "pid": pid })
  175. if status["alive"] == False:
  176. break
  177. else:
  178. client.post("/process/remove", {"pid": pid })
  179. raise AssertionError("took too long")
  180. if i < 3:
  181. raise AssertionError("too fast?")