Skip to content

Commit

Permalink
add tests for eventlog
Browse files Browse the repository at this point in the history
  • Loading branch information
Zsailer committed Aug 22, 2019
1 parent d3282c0 commit acc296a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 8 deletions.
20 changes: 12 additions & 8 deletions jupyterhub/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2078,6 +2078,17 @@ def init_pycurl(self):
e,
)

def init_eventlog(self):
"""Set up the event logging system."""
self.eventlog = EventLog(parent=self)

for dirname, _, files in os.walk(os.path.join(here, 'event-schemas')):
for file in files:
if not file.endswith('.yaml'):
continue
self.eventlog.register_schema_file(os.path.join(dirname, file))


def write_pid_file(self):
pid = os.getpid()
if self.pid_file:
Expand Down Expand Up @@ -2128,14 +2139,7 @@ def _log_cls(name, cls):
_log_cls("Authenticator", self.authenticator_class)
_log_cls("Spawner", self.spawner_class)

self.eventlog = EventLog(parent=self)

for dirname, _, files in os.walk(os.path.join(here, 'event-schemas')):
for file in files:
if not file.endswith('.yaml'):
continue
self.eventlog.register_schema_file(os.path.join(dirname, file))

self.init_eventlog()
self.init_pycurl()
self.init_secrets()
self.init_internal_ssl()
Expand Down
79 changes: 79 additions & 0 deletions jupyterhub/tests/test_eventlog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""Tests for Eventlogging in JupyterHub.
To test a new schema or event, simply add it to the
`valid_events` and `invalid_events` variables below.
You *shouldn't* need to write new tests.
"""
import io
import json
import logging
import jsonschema
import pytest
from .mocking import MockHub
from traitlets.config import Config


# To test new schemas, add them to the `valid_events`
# and `invalid_events` dictionary below.

# To test valid events, add event item with the form:
# { ( '<schema id>', <version> ) : { <event_data> } }
valid_events = [
('hub.jupyter.org/server-action', 1, dict(action='start', username='test-username', servername='test-servername')),
]

# To test invalid events, add event item with the form:
# { ( '<schema id>', <version> ) : { <event_data> } }
invalid_events = [
# Missing required keys
('hub.jupyter.org/server-action', 1, dict(action='start')),
]


@pytest.fixture
def get_hub_and_sink():
"""Get a hub instance with all registered schemas and record an event with it."""
# Get a mockhub.
hub = MockHub.instance()
sink = io.StringIO()
handler = logging.StreamHandler(sink)

def _record_from_hub(schema):
# Update the hub config with handler info.
cfg = Config()
cfg.EventLog.handlers = [handler]
cfg.EventLog.allowed_schemas = [schema]

# Get hub app.
hub.update_config(cfg)
hub.init_eventlog()

# Record an event from the hub.
return hub, sink

yield _record_from_hub

# teardown
hub.clear_instance()
handler.flush()


@pytest.mark.parametrize('schema, version, event', valid_events)
def test_valid_events(get_hub_and_sink, schema, version, event):
hub, sink = get_hub_and_sink(schema)
# Record event.
hub.eventlog.record_event(schema, version, event)
# Inspect consumed event.
output = sink.getvalue()
x = json.loads(output)

assert x is not None

@pytest.mark.parametrize('schema, version, event', invalid_events)
def test_invalid_events(get_hub_and_sink, schema, version, event):
hub, sink = get_hub_and_sink(schema)

# Make sure an error is thrown when bad events are recorded.
with pytest.raises(jsonschema.ValidationError):
recorded_event = hub.eventlog.record_event(schema, version, event)

0 comments on commit acc296a

Please sign in to comment.