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.

test_misc.py 4.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. from nose.tools import *
  2. from nose.tools import assert_raises
  3. from testutil.helpers import *
  4. import io
  5. import os
  6. import sys
  7. import time
  8. import socket
  9. import cherrypy
  10. import nilmdb.server
  11. from nilmdb.utils import timer, lock
  12. class TestMisc(object):
  13. def test_timer(self):
  14. capture = io.StringIO()
  15. old = sys.stdout
  16. sys.stdout = capture
  17. with nilmdb.utils.Timer("test"):
  18. time.sleep(0.01)
  19. with nilmdb.utils.Timer("test syslog", tosyslog=True):
  20. time.sleep(0.01)
  21. sys.stdout = old
  22. in_("test: ", capture.getvalue())
  23. def test_lock(self):
  24. with open("/dev/null") as f:
  25. eq_(nilmdb.utils.lock.exclusive_lock(f), True)
  26. nilmdb.utils.lock.exclusive_unlock(f)
  27. # Test error conditions
  28. class FakeFile():
  29. def __init__(self, fileno):
  30. self._fileno = fileno
  31. def fileno(self):
  32. return self._fileno
  33. with assert_raises(TypeError):
  34. nilmdb.utils.lock.exclusive_lock(FakeFile('none'))
  35. with assert_raises(ValueError):
  36. nilmdb.utils.lock.exclusive_lock(FakeFile(-1))
  37. with assert_raises(IOError):
  38. nilmdb.utils.lock.exclusive_lock(FakeFile(12345))
  39. # Lock failure is tested in test_bulkdata
  40. def test_replace_file(self):
  41. fn = b"tests/misc-testdb/file"
  42. try:
  43. os.mkdir(os.path.dirname(fn))
  44. except FileExistsError:
  45. pass
  46. with open(fn, "wb") as f:
  47. f.write(b"hello, world")
  48. nilmdb.utils.atomic.replace_file(fn, b"goodbye, world")
  49. with open(fn, "rb") as f:
  50. eq_(f.read(), b"goodbye, world")
  51. def test_punch(self):
  52. fn = b"tests/misc-testdb/punchit"
  53. try:
  54. os.mkdir(os.path.dirname(fn))
  55. except FileExistsError:
  56. pass
  57. with open(fn, "wb") as f:
  58. f.write(b"hello, world")
  59. nilmdb.utils.fallocate.punch_hole(fn, 3, 5)
  60. with open(fn, "rb") as f:
  61. eq_(f.read(), b"hel\0\0\0\0\0orld")
  62. with assert_raises(OSError):
  63. nilmdb.utils.fallocate.punch_hole(fn, 1, -1, False)
  64. with assert_raises(OSError):
  65. nilmdb.utils.fallocate.punch_hole("/", 1, 1, False)
  66. # no exception because we ignore errors by default
  67. nilmdb.utils.fallocate.punch_hole(fn, 1, -1)
  68. def test_diskusage(self):
  69. hs = nilmdb.utils.diskusage.human_size
  70. eq_(hs(0), "0 bytes")
  71. eq_(hs(1), "1 byte")
  72. eq_(hs(1023), "1023 bytes")
  73. eq_(hs(1024), "1 kiB")
  74. eq_(hs(1048575), "1024 kiB")
  75. eq_(hs(1048576), "1.0 MiB")
  76. eq_(hs(1073741823), "1024.0 MiB")
  77. eq_(hs(1073741824), "1.00 GiB")
  78. eq_(hs(1099511627775), "1024.00 GiB")
  79. eq_(hs(1099511627776), "1.00 TiB")
  80. eq_(hs(1099511627776 * 5000.1234), "5000.12 TiB")
  81. nilmdb.utils.diskusage.du("/dev")
  82. with assert_raises(OSError):
  83. nilmdb.utils.diskusage.du("/dev/null/bogus")
  84. nilmdb.utils.diskusage.du("super-bogus-does-not-exist")
  85. def test_cors_allow(self):
  86. # Just to get some test coverage; these code paths aren't actually
  87. # used in current code
  88. cpy = nilmdb.server.serverutil.cherrypy
  89. (req, resp) = (cpy.request, cpy.response)
  90. cpy.request.method = "DELETE"
  91. with assert_raises(cpy.HTTPError):
  92. nilmdb.server.serverutil.CORS_allow(methods="POST")
  93. with assert_raises(cpy.HTTPError):
  94. nilmdb.server.serverutil.CORS_allow(methods=["POST"])
  95. with assert_raises(cpy.HTTPError):
  96. nilmdb.server.serverutil.CORS_allow(methods=["GET"])
  97. with assert_raises(cpy.HTTPError):
  98. nilmdb.server.serverutil.CORS_allow(methods=[])
  99. (cpy.request, cpy.response) = (req, resp)
  100. def test_cherrypy_failure(self):
  101. # Test failure of cherrypy to start up because the port is
  102. # already in use. This also tests the functionality of
  103. # serverutil:cherrypy_patch_exit()
  104. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  105. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  106. try:
  107. sock.bind(("127.0.0.1", 32180))
  108. sock.listen(1)
  109. except OSError:
  110. raise AssertionError("port 32180 must be free for tests")
  111. nilmdb.server.serverutil.cherrypy_patch_exit()
  112. cherrypy.config.update({
  113. 'environment': 'embedded',
  114. 'server.socket_host': '127.0.0.1',
  115. 'server.socket_port': 32180,
  116. 'engine.autoreload.on': False,
  117. })
  118. with assert_raises(Exception) as e:
  119. cherrypy.engine.start()
  120. in_("Address already in use", str(e.exception))
  121. sock.close()