~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/canonical/testing/layers.py

  • Committer: Guilherme Salgado
  • Date: 2008-01-17 20:11:29 UTC
  • mto: This revision was merged to the branch mainline in revision 5578.
  • Revision ID: guilherme.salgado@canonical.com-20080117201129-sobtzx86tawhmfno
Change LaunchpadAccessLogger to log number of sqlstatements, ticks and the request duration.  Also monkey patch HTTPCaller.__call__ when running pagetests to log each request.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
    'LayerConsistencyError', 'LayerIsolationError', 'TwistedLayer'
24
24
    ]
25
25
 
 
26
import logging
26
27
import socket
27
28
import time
28
29
from urllib import urlopen
29
30
 
30
31
import psycopg
31
32
import transaction
 
33
 
 
34
import zope.app.testing.functional
32
35
from zope.component import getUtility, getGlobalSiteManager
33
36
from zope.component.interfaces import ComponentLookupError
34
37
from zope.security.management import getSecurityPolicy
35
38
from zope.security.simplepolicies import PermissiveSecurityPolicy
 
39
from zope.server.logger.pythonlogger import PythonLogger
36
40
 
37
41
from canonical.config import config
38
42
from canonical.database.sqlbase import ZopelessTransactionManager
42
46
from canonical.launchpad.mail.mailbox import TestMailBox
43
47
from canonical.launchpad.scripts import execute_zcml_for_scripts
44
48
from canonical.launchpad.webapp.servers import (
45
 
    register_launchpad_request_publication_factories)
 
49
    LaunchpadAccessLogger, register_launchpad_request_publication_factories)
46
50
from canonical.lp import initZopeless
47
51
from canonical.librarian.ftests.harness import LibrarianTestSetup
48
52
from canonical.testing import reset_logging
49
53
from canonical.testing.profiled import profiled
50
54
 
51
55
 
 
56
orig__call__ = zope.app.testing.functional.HTTPCaller.__call__
 
57
 
 
58
 
52
59
class LayerError(Exception):
53
60
    pass
54
61
 
674
681
        _reconnect_sqlos(database_config_section=database_config_section)
675
682
 
676
683
 
 
684
class MockHTTPTask:
 
685
 
 
686
    class MockHTTPRequestParser:
 
687
        headers = None
 
688
        first_line = None
 
689
 
 
690
    class MockHTTPServerChannel:
 
691
        # This is not important to us, so we can hardcode it here.
 
692
        addr = ['127.0.0.88', 80]
 
693
 
 
694
    request_data = MockHTTPRequestParser()
 
695
    channel = MockHTTPServerChannel()
 
696
 
 
697
    def __init__(self, response, first_line):
 
698
        self.request = response._request
 
699
        # We have no way of knowing when the task started, so we use
 
700
        # the current time here. That shouldn't be a problem since we don't
 
701
        # care about that for our tests.
 
702
        self.start_time = time.time()
 
703
        self.status = response.getStatus()
 
704
        self.bytes_written = int(response.getHeader('Content-length'))
 
705
        self.request_data.headers = self.request.headers
 
706
        self.request_data.first_line = first_line
 
707
 
 
708
    def getCGIEnvironment(self):
 
709
        return self.request._orig_env
 
710
 
 
711
 
677
712
class PageTestLayer(LaunchpadFunctionalLayer):
678
713
    """Environment for page tests.
679
714
    """
686
721
    @classmethod
687
722
    @profiled
688
723
    def setUp(cls):
 
724
        file_handler = logging.FileHandler('pagetests-access.log', 'w')
 
725
        file_handler.setFormatter(logging.Formatter())
 
726
        logger = PythonLogger('pagetests-access')
 
727
        logger.logger.addHandler(file_handler)
 
728
        logger.logger.setLevel(logging.INFO)
 
729
        access_logger = LaunchpadAccessLogger(logger)
 
730
        def my__call__(obj, request_string, handle_errors=True, form=None):
 
731
            """Call HTTPCaller.__call__ and log the page hit."""
 
732
            response = orig__call__(
 
733
                obj, request_string, handle_errors=handle_errors, form=form)
 
734
            first_line = request_string.splitlines()[0]
 
735
            access_logger.log(MockHTTPTask(response._response, first_line))
 
736
            return response
 
737
 
 
738
        zope.app.testing.functional.HTTPCaller.__call__ = my__call__
689
739
        cls.resetBetweenTests(True)
690
740
 
691
741
    @classmethod
692
742
    @profiled
693
743
    def tearDown(cls):
694
744
        cls.resetBetweenTests(True)
 
745
        zope.app.testing.functional.HTTPCaller.__call__ = orig__call__
695
746
 
696
747
    @classmethod
697
748
    @profiled