Browse Source

Add test for WSGI server, and fix a str/bytes bug that it found

tags/nilmdb-2.0.0
Jim Paris 2 years ago
parent
commit
96eadb0577
4 changed files with 51 additions and 15 deletions
  1. +12
    -14
      nilmdb/server/server.py
  2. +2
    -1
      requirements.txt
  3. +1
    -0
      tests/test.order
  4. +36
    -0
      tests/test_wsgi.py

+ 12
- 14
nilmdb/server/server.py View File

@@ -501,7 +501,7 @@ class Server(object):
# work, the web server must use only a single process and single
# Python interpreter. Multiple threads are OK.
_wsgi_server = None
def wsgi_application(dbpath, basepath): # pragma: no cover
def wsgi_application(dbpath, basepath):
"""Return a WSGI application object with a database at the
specified path.

@@ -526,23 +526,21 @@ def wsgi_application(dbpath, basepath): # pragma: no cover
err = sprintf("Initializing database at path '%s' failed:\n\n",
dbpath)
err += traceback.format_exc()
try:
import pwd
import grp
err += sprintf("\nRunning as: uid=%d (%s), gid=%d (%s) "
"on host %s, pid %d\n",
os.getuid(), pwd.getpwuid(os.getuid())[0],
os.getgid(), grp.getgrgid(os.getgid())[0],
socket.gethostname(), os.getpid())
except ImportError:
pass
import pwd
import grp
err += sprintf("\nRunning as: uid=%d (%s), gid=%d (%s) "
"on host %s, pid %d\n",
os.getuid(), pwd.getpwuid(os.getuid())[0],
os.getgid(), grp.getgrgid(os.getgid())[0],
socket.gethostname(), os.getpid())
err += sprintf("\nEnvironment:\n%s\n", pprint.pformat(environ))
if _wsgi_server is None:
# Serve up the error with our own mini WSGI app.
headers = [ ('Content-type', 'text/plain'),
('Content-length', str(len(err))) ]
err_b = err.encode('utf-8')
headers = [ ('Content-type', 'text/plain; charset=utf-8'),
('Content-length', str(len(err_b))) ]
start_response("500 Internal Server Error", headers)
return [err]
return [err_b]

# Call the normal application
return _wsgi_server.wsgi_application(environ, start_response)


+ 2
- 1
requirements.txt View File

@@ -1,6 +1,6 @@
cython>=0.29.13
CherryPy>=18.1.2
coverage>=4.5.4
cython>=0.29.13
decorator>=4.4.0
fallocate>=1.6.4
nose>=1.3.7
@@ -11,3 +11,4 @@ python-datetime-tz>=0.5.4
python-dateutil>=2.8.0
requests>=2.22.0
tz>=0.2.2
WebTest>=2.0.33

+ 1
- 0
tests/test.order View File

@@ -12,6 +12,7 @@ test_interval.py

test_bulkdata.py
test_nilmdb.py
test_wsgi.py
test_client.py
test_numpyclient.py
test_cmdline.py


+ 36
- 0
tests/test_wsgi.py View File

@@ -0,0 +1,36 @@
from nose.tools import *
from nose.tools import assert_raises
from testutil.helpers import *

import io
import os
import sys
import time

import nilmdb.server
import webtest

testdb = "tests/testdb"

# Test WSGI interface

class TestWSGI(object):
def test_wsgi(self):

# Bad database gives debug info
app = webtest.TestApp(nilmdb.server.wsgi_application("/dev/null", "/"))
resp = app.get('/', expect_errors=True)
eq_(resp.status_int, 500)
eq_(resp.content_type, "text/plain")
body = resp.body.decode('utf-8')
in_("Initializing database at path '/dev/null' failed", body)
in_("Not a directory: b'/dev/null/data'", body)
in_("Running as: uid=", body)
in_("Environment:", body)

# Good database works fine
app = webtest.TestApp(nilmdb.server.wsgi_application(testdb, "/nilmdb"))
resp = app.get('/version', expect_errors=True)
eq_(resp.status_int, 404)
resp = app.get('/nilmdb/version')
eq_(resp.json, nilmdb.__version__)

Loading…
Cancel
Save