~lazr-developers/lazr.sshserver/trunk

« back to all changes in this revision

Viewing changes to src/lazr/sshserver/tests/test_events.py

  • Committer: Jürgen Gmach
  • Date: 2021-10-31 16:38:55 UTC
  • Revision ID: juergen.gmach@canonical.com-20211031163855-b2brmahmbih8ho37
Moved to git

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2010-2015 Canonical Ltd.  This software is licensed under the
2
 
# GNU Lesser General Public License version 3 (see the file LICENSE).
3
 
 
4
 
"""Tests for the logging events."""
5
 
 
6
 
from __future__ import absolute_import, print_function
7
 
 
8
 
__metaclass__ = type
9
 
 
10
 
import logging
11
 
 
12
 
import testtools
13
 
from zope.component import (
14
 
    adapter,
15
 
    getGlobalSiteManager,
16
 
    provideHandler,
17
 
    )
18
 
# This non-standard import is necessary to hook up the event system.
19
 
import zope.component.event  # noqa: F401
20
 
from zope.event import notify
21
 
 
22
 
from lazr.sshserver.events import (
23
 
    ILoggingEvent,
24
 
    LoggingEvent,
25
 
    )
26
 
 
27
 
 
28
 
class ListHandler(logging.Handler):
29
 
    """Logging handler that just appends records to a list.
30
 
 
31
 
    This handler isn't intended to be used by production code -- memory leak
32
 
    city! -- instead it's useful for unit tests that want to make sure the
33
 
    right events are being logged.
34
 
    """
35
 
 
36
 
    def __init__(self, logging_list):
37
 
        """Construct a `ListHandler`.
38
 
 
39
 
        :param logging_list: A list that will be appended to. The handler
40
 
             mutates this list.
41
 
        """
42
 
        logging.Handler.__init__(self)
43
 
        self._list = logging_list
44
 
 
45
 
    def emit(self, record):
46
 
        """Append 'record' to the list."""
47
 
        self._list.append(record)
48
 
 
49
 
 
50
 
class TestLoggingEvent(testtools.TestCase):
51
 
 
52
 
    def assertLogs(self, records, function, *args, **kwargs):
53
 
        """Assert 'function' logs 'records' when run with the given args."""
54
 
        logged_events = []
55
 
        handler = ListHandler(logged_events)
56
 
        self.logger.addHandler(handler)
57
 
        result = function(*args, **kwargs)
58
 
        self.logger.removeHandler(handler)
59
 
        self.assertEqual(
60
 
            [(record.levelno, record.getMessage())
61
 
             for record in logged_events], records)
62
 
        return result
63
 
 
64
 
    def assertEventLogs(self, record, logging_event):
65
 
        self.assertLogs([record], notify, logging_event)
66
 
 
67
 
    def setUp(self):
68
 
        super(TestLoggingEvent, self).setUp()
69
 
        logger = logging.getLogger(self.getUniqueString())
70
 
        logger.setLevel(logging.DEBUG)
71
 
        self.logger = logger
72
 
 
73
 
        @adapter(ILoggingEvent)
74
 
        def _log_event(event):
75
 
            logger.log(event.level, event.message)
76
 
 
77
 
        provideHandler(_log_event)
78
 
        self.addCleanup(getGlobalSiteManager().unregisterHandler, _log_event)
79
 
 
80
 
    def test_level(self):
81
 
        event = LoggingEvent(logging.CRITICAL, "foo")
82
 
        self.assertEventLogs((logging.CRITICAL, 'foo'), event)
83
 
 
84
 
    def test_formatting(self):
85
 
        event = LoggingEvent(logging.DEBUG, "foo: %(name)s", name="bar")
86
 
        self.assertEventLogs((logging.DEBUG, 'foo: bar'), event)
87
 
 
88
 
    def test_subclass(self):
89
 
        class SomeEvent(LoggingEvent):
90
 
            template = "%(something)s happened."
91
 
            level = logging.INFO
92
 
        self.assertEventLogs(
93
 
            (logging.INFO, 'foo happened.'), SomeEvent(something='foo'))