~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# Copyright 2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Fixture code for YUITest + XHR integration testing."""

__metaclass__ = type
__all__ = [
    'login_as_person',
    'make_suite',
    'setup',
    'YUITestFixtureControllerView',
]

from fnmatch import fnmatchcase
import os
import sys
from textwrap import dedent
import traceback
import unittest

from lazr.restful import ResourceJSONEncoder
from lazr.restful.utils import get_current_browser_request
import simplejson
from zope.component import getUtility
from zope.exceptions.exceptionformatter import format_exception
from zope.interface import implements
from zope.publisher.interfaces import NotFound
from zope.publisher.interfaces.http import IResult
from zope.security.checker import (
    NamesChecker,
    ProxyFactory,
    )
from zope.security.proxy import removeSecurityProxy
from zope.session.interfaces import IClientIdManager

from lp.app.versioninfo import revno
from lp.services.config import config
from lp.services.webapp.interfaces import (
    IOpenLaunchBag,
    IPlacelessAuthUtility,
    )
from lp.services.webapp.login import logInPrincipal
from lp.services.webapp.publisher import LaunchpadView
from lp.testing import AbstractYUITestCase
from lp.testing.layers import (
    DatabaseLayer,
    LaunchpadLayer,
    LayerProcessController,
    LibrarianLayer,
    YUIAppServerLayer,
    )


EXPLOSIVE_ERRORS = (SystemExit, MemoryError, KeyboardInterrupt)


class setup:
    """Decorator to mark a function as a fixture available from JavaScript.

    This makes the function available to call from JS integration tests over
    XHR.  The fixture setup can have one or more cleanups tied to it with
    ``add_cleanup`` decorator/callable and can be composed with another
    function with the ``extend`` decorator/callable.
    """
    def __init__(self, function, extends=None):
        self._cleanups = []
        self._function = function
        self._extends = extends
        # We can't use locals because we want to affect the function's module,
        # not this one.
        module = sys.modules[function.__module__]
        fixtures = getattr(module, '_fixtures_', None)
        if fixtures is None:
            fixtures = module._fixtures_ = {}
        fixtures[function.__name__] = self

    def __call__(self, request, data):
        """Call the originally decorated setup function."""
        if self._extends is not None:
            self._extends(request, data)
        self._function(request, data)

    def add_cleanup(self, function):
        """Add a cleanup function to be executed on teardown, FILO."""
        self._cleanups.append(function)
        return self

    def teardown(self, request, data):
        """Run all registered cleanups.  If no cleanups, a no-op."""
        for f in reversed(self._cleanups):
            f(request, data)
        if self._extends is not None:
            self._extends.teardown(request, data)

    def extend(self, function):
        return setup(function, self)


def login_as_person(person):
    """This is a helper function designed to be used within a fixture.

    Provide a person, such as one generated by LaunchpadObjectFactory, and
    the browser will become logged in as this person.

    Explicit tear-down is unnecessary because the database is reset at the end
    of every test, and the cookie is discarded.
    """
    if person.is_team:
        raise AssertionError("Please do not try to login as a team")
    email = removeSecurityProxy(person.preferredemail).email
    request = get_current_browser_request()
    assert request is not None, "We do not have a browser request."
    authutil = getUtility(IPlacelessAuthUtility)
    principal = authutil.getPrincipalByLogin(email, want_password=False)
    launchbag = getUtility(IOpenLaunchBag)
    launchbag.setLogin(email)
    logInPrincipal(request, principal, email)


class CloseDbResult:
    implements(IResult)

    # This is machinery, not content.  We specify our security checker here
    # directly for clarity.
    __Security_checker__ = NamesChecker(['next', '__iter__'])

    def __iter__(self):
        try:
            # Reset the session.
            LaunchpadLayer.resetSessionDb()
            # Yield control to asyncore for a second, just to be a
            # little bit nice.  We could be even nicer by moving this
            # whole teardown/setup dance to a thread and waiting for
            # it to be done, but there's not a (known) compelling need
            # for that right now, and doing it this way is slightly
            # simpler.
            yield ''
            DatabaseLayer.testSetUp()
            yield ''
            # Reset the librarian.
            LibrarianLayer.testTearDown()
            yield ''
            # Reset the database.
            DatabaseLayer.testTearDown()
            yield ''
            LibrarianLayer.testSetUp()
        except (SystemExit, KeyboardInterrupt):
            raise
        except:
            print "Hm, serious error when trying to clean up the test."
            traceback.print_exc()
        # We're done, so we can yield the body.
        yield '\n'


class YUITestFixtureControllerView(LaunchpadView):
    """Dynamically loads YUI test along their fixtures run over an app server.
    """

    JAVASCRIPT = 'JAVASCRIPT'
    HTML = 'HTML'
    SETUP = 'SETUP'
    TEARDOWN = 'TEARDOWN'
    INDEX = 'INDEX'

    page_template = dedent("""\
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
          "http://www.w3.org/TR/html4/strict.dtd">
        <html>
          <head>
          <title>Test</title>
          <script type="text/javascript"
            src="/+icing/rev%(revno)s/build/launchpad.js"></script>
          <link rel="stylesheet"
            href="/+icing/yui/assets/skins/sam/skin.css"/>
          <link rel="stylesheet" href="/+icing/rev%(revno)s/combo.css"/>
          <style>
          /* Taken and customized from testlogger.css */
          .yui-console-entry-src { display:none; }
          .yui-console-entry.yui-console-entry-pass .yui-console-entry-cat {
            background-color: green;
            font-weight: bold;
            color: white;
          }
          .yui-console-entry.yui-console-entry-fail .yui-console-entry-cat {
            background-color: red;
            font-weight: bold;
            color: white;
          }
          .yui-console-entry.yui-console-entry-ignore .yui-console-entry-cat {
            background-color: #666;
            font-weight: bold;
            color: white;
          }
          </style>
          <script type="text/javascript" src="%(test_module)s"></script>
        </head>
        <body class="yui3-skin-sam">
          <div id="log"></div>
          <p>Want to re-run your test?</p>
          <ul>
            <li><a href="?">Reload test JS</a></li>
            <li><a href="?reload=1">Reload test JS and the associated
                                    Python fixtures</a></li>
          </ul>
          <p>Don't forget to run <code>make jsbuild</code> and then do a
             hard reload of this page if you change a file that is built
             into launchpad.js!</p>
          <p>If you change Python code other than the fixtures, you must
             restart the server.  Sorry.</p>
        </body>
        </html>
        """)

    index_template = dedent("""\
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
          "http://www.w3.org/TR/html4/strict.dtd">
        <html>
          <head>
          <title>YUI XHR Tests</title>
          <script type="text/javascript"
            src="/+icing/rev%(revno)s/build/launchpad.js"></script>
          <link rel="stylesheet"
            href="/+icing/yui/assets/skins/sam/skin.css"/>
          <link rel="stylesheet" href="/+icing/rev%(revno)s/combo.css"/>
          <style>
          ul {
            text-align: left;
          }
          body, ul, h1 {
            margin: 0.3em;
            padding: 0.3em;
          }
        </style>
        </head>
        <body class="yui3-skin-sam">
          <h1>YUI XHR Tests</h1>
          <ul>%(tests)s</ul>
        </body>
        </html>
        """)

    def __init__(self, context, request):
        super(YUITestFixtureControllerView, self).__init__(context, request)
        self.names = []
        self.action = None
        self.fixtures = []

    @property
    def traversed_path(self):
        return os.path.join(*self.names)

    def initialize(self):
        if not self.names:
            self.action = self.INDEX
            return
        path, ext = os.path.splitext(self.traversed_path)
        full_path = os.path.join(config.root, 'lib', path)
        if not os.path.exists(full_path + '.py'):
            raise NotFound(self, full_path + '.py', self.request)
        if not os.path.exists(full_path + '.js'):
            raise NotFound(self, full_path + '.js', self.request)

        if ext == '.js':
            self.action = self.JAVASCRIPT
        else:
            if self.request.method == 'GET':
                self.action = self.HTML
            else:
                self.fixtures = self.request.form['fixtures'].split(',')
                if self.request.form['action'] == 'setup':
                    self.action = self.SETUP
                else:
                    self.action = self.TEARDOWN

    # The following two zope methods publishTraverse and browserDefault
    # allow this view class to take control of traversal from this point
    # onwards.  Traversed names just end up in self.names.
    def publishTraverse(self, request, name):
        """Traverse to the given name."""
        # The two following constraints are enforced by the publisher.
        assert os.path.sep not in name, (
            'traversed name contains os.path.sep: %s' % name)
        assert name != '..', 'traversing to ..'
        if name:
            self.names.append(name)
        return self

    def browserDefault(self, request):
        return self, ()

    @property
    def module_name(self):
        return '.'.join(self.names)

    def get_fixtures(self):
        module = __import__(
            self.module_name, globals(), locals(), ['_fixtures_'], 0)
        return module._fixtures_

    def renderINDEX(self):
        root = os.path.join(config.root, 'lib')
        test_lines = []
        for path in find_tests(root):
            test_path = '/+yuitest/' + '/'.join(path)
            module_name = '.'.join(path)
            try:
                module = __import__(
                    module_name, globals(), locals(), ['test_suite'], 0)
            except ImportError:
                warning = 'cannot import Python fixture file'
            else:
                try:
                    suite_factory = module.test_suite
                except AttributeError:
                    warning = 'cannot find test_suite'
                else:
                    try:
                        suite = suite_factory()
                    except EXPLOSIVE_ERRORS:
                        raise
                    except:
                        warning = 'test_suite raises errors'
                    else:
                        case = None
                        for case in suite:
                            if isinstance(case, YUIAppServerTestCase):
                                root_url = config.appserver_root_url(
                                    case.facet)
                                if root_url != 'None':
                                    test_path = root_url + test_path
                                warning = ''
                                break
                        else:
                            warning = (
                                'test suite is not instance of '
                                'YUIAppServerTestCase')
            link = '<a href="%s">%s</a>' % (test_path, test_path)
            if warning:
                warning = ' <span class="warning">%s</span>' % warning
            test_lines.append('<li>%s%s</li>' % (link, warning))
        return self.index_template % {
            'revno': revno,
            'tests': '\n'.join(test_lines)}

    def renderJAVASCRIPT(self):
        self.request.response.setHeader('Content-Type', 'text/javascript')
        self.request.response.setHeader('Cache-Control', 'no-cache')
        return open(
            os.path.join(config.root, 'lib', self.traversed_path))

    def renderHTML(self):
        self.request.response.setHeader('Content-Type', 'text/html')
        self.request.response.setHeader('Cache-Control', 'no-cache')
        if ('INTERACTIVE_TESTS' in os.environ and
            'reload' in self.request.form):
            # We should try to reload the module.
            module = sys.modules.get(self.module_name)
            if module is not None:
                del module._fixtures_
                reload(module)
        return self.page_template % dict(
            test_module='/+yuitest/%s.js' % self.traversed_path,
            revno=revno)

    def renderSETUP(self):
        data = {}
        fixtures = self.get_fixtures()
        try:
            for fixture_name in self.fixtures:
                __traceback_info__ = (fixture_name, data)
                fixtures[fixture_name](self.request, data)
        except EXPLOSIVE_ERRORS:
            raise
        except:
            self.request.response.setStatus(500)
            result = ''.join(format_exception(*sys.exc_info()))
        else:
            self.request.response.setHeader(
                'Content-Type', 'application/json')
            # We use the ProxyFactory so that the restful
            # redaction code is always used.
            result = simplejson.dumps(
                ProxyFactory(data), cls=ResourceJSONEncoder)
        return result

    def renderTEARDOWN(self):
        data = simplejson.loads(self.request.form['data'])
        fixtures = self.get_fixtures()
        try:
            for fixture_name in reversed(self.fixtures):
                __traceback_info__ = (fixture_name, data)
                fixtures[fixture_name].teardown(self.request, data)
        except EXPLOSIVE_ERRORS:
            raise
        except:
            self.request.response.setStatus(500)
            result = ''.join(format_exception(*sys.exc_info()))
        else:
            # Remove the session cookie, in case we have one.
            self.request.response.expireCookie(
                getUtility(IClientIdManager).namespace)
            # Blow up the database once we are out of this transaction
            # by passing a result that will do so when it is iterated
            # through in asyncore.
            self.request.response.setHeader('Content-Length', 1)
            result = CloseDbResult()
        return result

    def render(self):
        return getattr(self, 'render' + self.action)()


def find_tests(root):
    for dirpath, dirnames, filenames in os.walk(root):
        dirpath = os.path.relpath(dirpath, root)
        for filename in filenames:
            if fnmatchcase(filename, 'test_*.js'):
                name, ext = os.path.splitext(filename)
                if name + '.py' in filenames:
                    names = dirpath.split(os.path.sep)
                    names.append(name)
                    yield names


# This class cannot be imported directly into a test suite because
# then the test loader will sniff and (try to) run it.  Use make_suite
# instead (or import this module rather than this class).
class YUIAppServerTestCase(AbstractYUITestCase):
    "Instantiate this test case with the Python fixture module name."

    layer = YUIAppServerLayer
    _testMethodName = 'runTest'
    # 5 minutes for the suite.  Hopefully we never get close to this.
    suite_timeout = 300000
    # 12 seconds for each test.  Hopefully they are three or less for
    # yuixhr tests, and less than one for pure JS tests, but
    # occasionally buildbot runs over six seconds even for tests that
    # are well-behaved locally and on ec2, so we up the limit to 12..
    incremental_timeout = 12000
    # 45 seconds for the first test, to include warmup time.  These times
    # are wildly large, and they are only necessary on buildbot.  ec2 and
    # local instances are much, much faster.  We have not yet investigated
    # why buildbot is so slow for these.
    initial_timeout = 45000

    def __init__(self, module_name, facet='mainsite'):
        self.module_name = module_name
        self.facet = facet
        # This needs to be done early so the "id" is set correctly.
        self.test_path = self.module_name.replace('.', '/')
        super(YUIAppServerTestCase, self).__init__()

    def setUp(self):
        config = LayerProcessController.appserver_config
        root_url = config.appserver_root_url(self.facet)
        self.html_uri = '%s/+yuitest/%s' % (root_url, self.test_path)
        super(YUIAppServerTestCase, self).setUp()

    runTest = AbstractYUITestCase.checkResults


def make_suite(module_name, facet='mainsite'):
    return unittest.TestSuite([YUIAppServerTestCase(module_name, facet)])