~launchpad-pqm/launchpad/devel

« back to all changes in this revision

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

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-06-27 23:10:34 UTC
  • mfrom: (13299.1.18 windmill-death)
  • Revision ID: launchpad@pqm.canonical.com-20110627231034-rykdadu5vb2uqzpz
[r=sinzui,
 stevenk][no-qa] Remove Windmill test infrastructure and annotate old
 tests for refactoring.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
__all__ = [
26
26
    'AppServerLayer',
27
27
    'BaseLayer',
28
 
    'BaseWindmillLayer',
29
28
    'DatabaseFunctionalLayer',
30
29
    'DatabaseLayer',
31
30
    'ExperimentalLaunchpadZopelessLayer',
80
79
import psycopg2
81
80
from storm.zope.interfaces import IZStorm
82
81
import transaction
83
 
from windmill.bin.admin_lib import (
84
 
    start_windmill,
85
 
    teardown as windmill_teardown,
86
 
    )
87
82
import wsgi_intercept
88
83
from wsgi_intercept import httplib2_intercept
89
84
from zope.app.publication.httpfactory import chooseClasses
2048
2043
        LayerProcessController.postTestInvariants()
2049
2044
 
2050
2045
 
2051
 
class BaseWindmillLayer(AppServerLayer):
2052
 
    """Layer for Windmill tests.
2053
 
 
2054
 
    This layer shouldn't be used directly. A subclass needs to be
2055
 
    created specifying which base URL to use (e.g.
2056
 
    http://bugs.launchpad.dev:8085/).
2057
 
    """
2058
 
 
2059
 
    facet = None
2060
 
    base_url = None
2061
 
    shell_objects = None
2062
 
    config_file = None
2063
 
 
2064
 
    @classmethod
2065
 
    @profiled
2066
 
    def setUp(cls):
2067
 
        if cls.base_url is None:
2068
 
            # Only do the setup if we're in a subclass that defines
2069
 
            # base_url. With no base_url, we can't create the config
2070
 
            # file windmill needs.
2071
 
            return
2072
 
 
2073
 
        cls._fixStandardInputFileno()
2074
 
        cls._configureWindmillLogging()
2075
 
        cls._configureWindmillStartup()
2076
 
 
2077
 
        # Tell windmill to start its browser and server.  Our testrunner will
2078
 
        # keep going, passing commands to the server for execution.
2079
 
        cls.shell_objects = start_windmill()
2080
 
 
2081
 
        # Patch the config to provide the port number and not use https.
2082
 
        sites = (
2083
 
            (('vhost.%s' % sitename,
2084
 
            'rooturl: %s/' % cls.appserver_root_url(sitename))
2085
 
            for sitename in ['mainsite', 'answers', 'blueprints', 'bugs',
2086
 
                            'code', 'testopenid', 'translations']))
2087
 
        for site in sites:
2088
 
            config.push('windmillsettings', "\n[%s]\n%s\n" % site)
2089
 
        allvhosts.reload()
2090
 
 
2091
 
    @classmethod
2092
 
    @profiled
2093
 
    def tearDown(cls):
2094
 
        if cls.shell_objects is not None:
2095
 
            windmill_teardown(cls.shell_objects)
2096
 
        if cls.config_file is not None:
2097
 
            # Close the file so that it gets deleted.
2098
 
            cls.config_file.close()
2099
 
        config.reloadConfig()
2100
 
        reset_logging()
2101
 
        # XXX: deryck 2011-01-28 bug=709438
2102
 
        # Windmill mucks about with the default timeout and this is
2103
 
        # a fix until the library itself can be cleaned up.
2104
 
        socket.setdefaulttimeout(None)
2105
 
 
2106
 
    @classmethod
2107
 
    @profiled
2108
 
    def testSetUp(cls):
2109
 
        # Left-over threads should be harmless, since they should all
2110
 
        # belong to Windmill, which will be cleaned up on layer
2111
 
        # tear down.
2112
 
        BaseLayer.disable_thread_check = True
2113
 
        socket.setdefaulttimeout(120)
2114
 
 
2115
 
    @classmethod
2116
 
    @profiled
2117
 
    def testTearDown(cls):
2118
 
        # To play nice with Windmill layers, we need to reset
2119
 
        # the socket timeout default in this method, too.
2120
 
        socket.setdefaulttimeout(None)
2121
 
 
2122
 
    @classmethod
2123
 
    def _fixStandardInputFileno(cls):
2124
 
        """Patch the STDIN fileno so Windmill doesn't break."""
2125
 
        # If we're running in a bin/test sub-process, sys.stdin is
2126
 
        # replaced by FakeInputContinueGenerator, which doesn't have a
2127
 
        # fileno method. When Windmill starts Firefox,
2128
 
        # sys.stdin.fileno() is called, so we add such a method here, to
2129
 
        # prevent it from breaking. By returning None, we should ensure
2130
 
        # that it doesn't try to use the return value for anything.
2131
 
        if not safe_hasattr(sys.stdin, 'fileno'):
2132
 
            assert isinstance(sys.stdin, FakeInputContinueGenerator), (
2133
 
                "sys.stdin (%r) doesn't have a fileno method." % sys.stdin)
2134
 
            sys.stdin.fileno = lambda: None
2135
 
 
2136
 
    @classmethod
2137
 
    def _configureWindmillLogging(cls):
2138
 
        """Override the default windmill log handling."""
2139
 
        if not config.windmill.debug_log:
2140
 
            return
2141
 
 
2142
 
        # Add a new log handler to capture all of the windmill testrunner
2143
 
        # output. This overrides windmill's own log handling, which we do not
2144
 
        # have direct access to.
2145
 
        # We'll overwrite the previous log contents to keep the disk usage
2146
 
        # low, and because the contents are only meant as an in-situ debugging
2147
 
        # aid.
2148
 
        filehandler = logging.FileHandler(config.windmill.debug_log, mode='w')
2149
 
        filehandler.setLevel(logging.NOTSET)
2150
 
        filehandler.setFormatter(
2151
 
            logging.Formatter(
2152
 
                "%(asctime)s - %(name)s - %(levelname)s - %(message)s"))
2153
 
        logging.getLogger('windmill').addHandler(filehandler)
2154
 
 
2155
 
        # Make sure that everything sent to the windmill logger is captured.
2156
 
        # This works because windmill configures the root logger for its
2157
 
        # purposes, and we are pre-empting that by inserting a new logger one
2158
 
        # level higher in the logger chain.
2159
 
        logging.getLogger('windmill').setLevel(logging.NOTSET)
2160
 
 
2161
 
    @classmethod
2162
 
    def _configureWindmillStartup(cls):
2163
 
        """Pass our startup parameters to the windmill server."""
2164
 
        # Windmill needs a config file on disk to load its settings from.
2165
 
        # There is no way to directly pass settings to the windmill test
2166
 
        # driver from out here.
2167
 
        config_text = dedent("""\
2168
 
            START_FIREFOX = True
2169
 
            TEST_URL = '%s/'
2170
 
            CONSOLE_LOG_LEVEL = %d
2171
 
            """ % (cls.base_url, logging.NOTSET))
2172
 
        cls.config_file = tempfile.NamedTemporaryFile(suffix='.py')
2173
 
        cls.config_file.write(config_text)
2174
 
        # Flush the file so that windmill can read it.
2175
 
        cls.config_file.flush()
2176
 
        os.environ['WINDMILL_CONFIG_FILE'] = cls.config_file.name
2177
 
 
2178
 
 
2179
2046
class YUITestLayer(FunctionalLayer):
2180
2047
    """The base class for all YUITests cases."""