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.
 
 
 

257 lines
9.1 KiB

  1. import nilmdb
  2. from nose.tools import *
  3. from nose.tools import assert_raises
  4. import distutils.version
  5. import simplejson as json
  6. import itertools
  7. import os
  8. import sys
  9. import threading
  10. import urllib2
  11. from urllib2 import urlopen, HTTPError
  12. import cStringIO
  13. import time
  14. import requests
  15. from nilmdb.utils import serializer_proxy
  16. testdb = "tests/testdb"
  17. #@atexit.register
  18. #def cleanup():
  19. # os.unlink(testdb)
  20. from testutil.helpers import *
  21. class Test00Nilmdb(object): # named 00 so it runs first
  22. def test_NilmDB(self):
  23. recursive_unlink(testdb)
  24. with assert_raises(IOError):
  25. nilmdb.NilmDB("/nonexistant-db/foo")
  26. db = nilmdb.NilmDB(testdb)
  27. db.close()
  28. db = nilmdb.NilmDB(testdb, sync=False)
  29. db.close()
  30. # test timer, just to get coverage
  31. capture = cStringIO.StringIO()
  32. old = sys.stdout
  33. sys.stdout = capture
  34. with nilmdb.utils.Timer("test"):
  35. time.sleep(0.01)
  36. sys.stdout = old
  37. in_("test: ", capture.getvalue())
  38. def test_stream(self):
  39. db = nilmdb.NilmDB(testdb, sync=False)
  40. eq_(db.stream_list(), [])
  41. # Bad path
  42. with assert_raises(ValueError):
  43. db.stream_create("foo/bar/baz", "PrepData")
  44. with assert_raises(ValueError):
  45. db.stream_create("/foo", "PrepData")
  46. # Bad layout type
  47. with assert_raises(ValueError):
  48. db.stream_create("/newton/prep", "NoSuchLayout")
  49. db.stream_create("/newton/prep", "PrepData")
  50. db.stream_create("/newton/raw", "RawData")
  51. db.stream_create("/newton/zzz/rawnotch", "RawNotchedData")
  52. # Verify we got 3 streams
  53. eq_(db.stream_list(), [ ["/newton/prep", "PrepData"],
  54. ["/newton/raw", "RawData"],
  55. ["/newton/zzz/rawnotch", "RawNotchedData"]
  56. ])
  57. # Match just one type or one path
  58. eq_(db.stream_list(layout="RawData"), [ ["/newton/raw", "RawData"] ])
  59. eq_(db.stream_list(path="/newton/raw"), [ ["/newton/raw", "RawData"] ])
  60. # Verify that columns were made right (pytables specific)
  61. if "h5file" in db.data.__dict__:
  62. h5file = db.data.h5file
  63. eq_(len(h5file.getNode("/newton/prep").cols), 9)
  64. eq_(len(h5file.getNode("/newton/raw").cols), 7)
  65. eq_(len(h5file.getNode("/newton/zzz/rawnotch").cols), 10)
  66. assert(not h5file.getNode("/newton/prep").colindexed["timestamp"])
  67. assert(not h5file.getNode("/newton/prep").colindexed["c1"])
  68. # Set / get metadata
  69. eq_(db.stream_get_metadata("/newton/prep"), {})
  70. eq_(db.stream_get_metadata("/newton/raw"), {})
  71. meta1 = { "description": "The Data",
  72. "v_scale": "1.234" }
  73. meta2 = { "description": "The Data" }
  74. meta3 = { "v_scale": "1.234" }
  75. db.stream_set_metadata("/newton/prep", meta1)
  76. db.stream_update_metadata("/newton/prep", {})
  77. db.stream_update_metadata("/newton/raw", meta2)
  78. db.stream_update_metadata("/newton/raw", meta3)
  79. eq_(db.stream_get_metadata("/newton/prep"), meta1)
  80. eq_(db.stream_get_metadata("/newton/raw"), meta1)
  81. # fill in some test coverage for start >= end
  82. with assert_raises(nilmdb.server.NilmDBError):
  83. db.stream_remove("/newton/prep", 0, 0)
  84. with assert_raises(nilmdb.server.NilmDBError):
  85. db.stream_remove("/newton/prep", 1, 0)
  86. db.stream_remove("/newton/prep", 0, 1)
  87. db.close()
  88. class TestBlockingServer(object):
  89. def setUp(self):
  90. self.db = serializer_proxy(nilmdb.NilmDB)(testdb, sync=False)
  91. def tearDown(self):
  92. self.db.close()
  93. def test_blocking_server(self):
  94. # Server should fail if the database doesn't have a "_thread_safe"
  95. # property.
  96. with assert_raises(KeyError):
  97. nilmdb.Server(object())
  98. # Start web app on a custom port
  99. self.server = nilmdb.Server(self.db, host = "127.0.0.1",
  100. port = 32180, stoppable = True)
  101. # Run it
  102. event = threading.Event()
  103. def run_server():
  104. self.server.start(blocking = True, event = event)
  105. thread = threading.Thread(target = run_server)
  106. thread.start()
  107. if not event.wait(timeout = 10):
  108. raise AssertionError("server didn't start in 10 seconds")
  109. # Send request to exit.
  110. req = urlopen("http://127.0.0.1:32180/exit/", timeout = 1)
  111. # Wait for it
  112. thread.join()
  113. def geturl(path):
  114. req = urlopen("http://127.0.0.1:32180" + path, timeout = 10)
  115. return req.read()
  116. def getjson(path):
  117. return json.loads(geturl(path))
  118. class TestServer(object):
  119. def setUp(self):
  120. # Start web app on a custom port
  121. self.db = serializer_proxy(nilmdb.NilmDB)(testdb, sync=False)
  122. self.server = nilmdb.Server(self.db, host = "127.0.0.1",
  123. port = 32180, stoppable = False)
  124. self.server.start(blocking = False)
  125. def tearDown(self):
  126. # Close web app
  127. self.server.stop()
  128. self.db.close()
  129. def test_server(self):
  130. # Make sure we can't force an exit, and test other 404 errors
  131. for url in [ "/exit", "/", "/favicon.ico" ]:
  132. with assert_raises(HTTPError) as e:
  133. geturl(url)
  134. eq_(e.exception.code, 404)
  135. # Check version
  136. eq_(distutils.version.LooseVersion(getjson("/version")),
  137. distutils.version.LooseVersion(nilmdb.__version__))
  138. def test_stream_list(self):
  139. # Known streams that got populated by an earlier test (test_nilmdb)
  140. streams = getjson("/stream/list")
  141. eq_(streams, [
  142. ['/newton/prep', 'PrepData'],
  143. ['/newton/raw', 'RawData'],
  144. ['/newton/zzz/rawnotch', 'RawNotchedData'],
  145. ])
  146. streams = getjson("/stream/list?layout=RawData")
  147. eq_(streams, [['/newton/raw', 'RawData']])
  148. streams = getjson("/stream/list?layout=NoSuchLayout")
  149. eq_(streams, [])
  150. def test_stream_metadata(self):
  151. with assert_raises(HTTPError) as e:
  152. getjson("/stream/get_metadata?path=foo")
  153. eq_(e.exception.code, 404)
  154. data = getjson("/stream/get_metadata?path=/newton/prep")
  155. eq_(data, {'description': 'The Data', 'v_scale': '1.234'})
  156. data = getjson("/stream/get_metadata?path=/newton/prep"
  157. "&key=v_scale")
  158. eq_(data, {'v_scale': '1.234'})
  159. data = getjson("/stream/get_metadata?path=/newton/prep"
  160. "&key=v_scale&key=description")
  161. eq_(data, {'description': 'The Data', 'v_scale': '1.234'})
  162. data = getjson("/stream/get_metadata?path=/newton/prep"
  163. "&key=v_scale&key=foo")
  164. eq_(data, {'foo': None, 'v_scale': '1.234'})
  165. data = getjson("/stream/get_metadata?path=/newton/prep"
  166. "&key=foo")
  167. eq_(data, {'foo': None})
  168. def test_cors_headers(self):
  169. # Test that CORS headers are being set correctly
  170. # Normal GET should send simple response
  171. url = "http://127.0.0.1:32180/stream/list"
  172. r = requests.get(url, headers = { "Origin": "http://google.com/" })
  173. eq_(r.status_code, 200)
  174. if "access-control-allow-origin" not in r.headers:
  175. raise AssertionError("No Access-Control-Allow-Origin (CORS) "
  176. "header in response:\n", r.headers)
  177. eq_(r.headers["access-control-allow-origin"], "http://google.com/")
  178. # OPTIONS without CORS preflight headers should result in 405
  179. r = requests.options(url, headers = {
  180. "Origin": "http://google.com/",
  181. })
  182. eq_(r.status_code, 405)
  183. # OPTIONS with preflight headers should give preflight response
  184. r = requests.options(url, headers = {
  185. "Origin": "http://google.com/",
  186. "Access-Control-Request-Method": "POST",
  187. "Access-Control-Request-Headers": "X-Custom",
  188. })
  189. eq_(r.status_code, 200)
  190. if "access-control-allow-origin" not in r.headers:
  191. raise AssertionError("No Access-Control-Allow-Origin (CORS) "
  192. "header in response:\n", r.headers)
  193. eq_(r.headers["access-control-allow-methods"], "GET, HEAD")
  194. eq_(r.headers["access-control-allow-headers"], "X-Custom")
  195. def test_post_bodies(self):
  196. # Test JSON post bodies
  197. r = requests.post("http://127.0.0.1:32180/stream/set_metadata",
  198. headers = { "Content-Type": "application/json" },
  199. data = '{"hello": 1}')
  200. eq_(r.status_code, 404) # wrong parameters
  201. r = requests.post("http://127.0.0.1:32180/stream/set_metadata",
  202. headers = { "Content-Type": "application/json" },
  203. data = '["hello"]')
  204. eq_(r.status_code, 415) # not a dict
  205. r = requests.post("http://127.0.0.1:32180/stream/set_metadata",
  206. headers = { "Content-Type": "application/json" },
  207. data = '[hello]')
  208. eq_(r.status_code, 400) # badly formatted JSON