Project

General

Profile

0001-tests-insert-log-filter-on-correct-handler-45615.patch

Nicolas Roche, 31 Jul 2020 02:34 PM

Download (2.89 KB)

View differences:

Subject: [PATCH] tests: insert log filter on correct handler (#45615)

 .../test_request_context_filter.py             | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
tests_multitenant/test_request_context_filter.py
1 1
import pytest
2
from _pytest.logging import LogCaptureHandler
2 3

  
3 4
import logging
4 5

  
5 6
from hobo.logger import RequestContextFilter
6 7

  
7 8
from tenant_schemas.utils import tenant_context
8 9

  
9 10
from django.contrib.auth.models import User
......
16 17
        self.calls = []
17 18

  
18 19
    def send(self, *args, **kwargs):
19 20
        self.calls.append((args, kwargs))
20 21

  
21 22

  
22 23
def test_request_context_filter(caplog, settings, tenants, client):
23 24
    root_logger = logging.getLogger()
24
    assert len(root_logger.handlers) == 1
25
    root_logger.handlers[0].addFilter(RequestContextFilter())
25
    for handler in root_logger.handlers:
26
        if handler.__class__ == LogCaptureHandler:
27
            handler.addFilter(RequestContextFilter())
28
            break
29
    else:
30
        assert False, 'No LogCaptureHandler found'
31

  
26 32
    for tenant in tenants:
27 33
        with tenant_context(tenant):
28 34
            user = User.objects.create(first_name='John', last_name='Doe', username='john.doe',
29 35
                                       email='jodn.doe@example.com')
30 36
            user.set_password('john.doe')
31 37
            user.save()
32 38
            user.saml_identifiers.create(name_id='ab' * 16, issuer='https://idp.example.com')
33 39

  
......
57 63
    yield MockSender()
58 64

  
59 65

  
60 66
@pytest.fixture
61 67
def journald_handler(sender):
62 68
    root_logger = logging.getLogger()
63 69
    journald_handler = JournalHandler(sender_function=sender.send)
64 70
    journald_handler.addFilter(RequestContextFilter())
65
    root_logger.handlers.append(journald_handler)
71

  
72
    root_logger.handlers.insert(0, journald_handler)  # head insert
66 73
    try:
67 74
        yield journald_handler
68 75
    finally:
69
        root_logger.handlers.remove(journald_handler)
76
        root_logger.handlers.pop(0)
70 77

  
71 78

  
72 79
def test_systemd(settings, tenants, client, journald_handler, sender):
73
    root_logger = logging.getLogger()
74
    assert len(root_logger.handlers) == 2
75

  
76 80
    for tenant in tenants:
77 81
        with tenant_context(tenant):
78 82
            user = User.objects.create(first_name='John', last_name='Doe', username='john.doe',
79 83
                                       email='jodn.doe@example.com')
80 84
            user.set_password('john.doe')
81 85
            user.save()
82 86
            user.saml_identifiers.create(name_id='ab' * 16, issuer='https://idp.example.com')
83 87

  
84
-