~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/canonical/testing/ftests/test_layers.py

Refactored AppServerLayer to only start up the web app server using runlaunchpad.py.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
__metaclass__ = type
8
8
 
9
9
from cStringIO import StringIO
 
10
import os
 
11
import signal
10
12
from urllib import urlopen
11
13
import unittest
12
14
 
15
17
from zope.component import getUtility, ComponentLookupError
16
18
 
17
19
from canonical.config import config, dbconfig
 
20
from canonical.pidfile import pidfile_path
18
21
from canonical.librarian.client import LibrarianClient, UploadFailed
19
22
from canonical.librarian.interfaces import ILibrarianClient
20
 
from canonical.testing import (
21
 
    BaseLayer, DatabaseLayer, FunctionalLayer, LaunchpadFunctionalLayer,
22
 
    LaunchpadLayer, LaunchpadScriptLayer, LaunchpadZopelessLayer,
 
23
from canonical.launchpad.ftests.harness import LaunchpadTestSetup
 
24
from canonical.testing.layers import (
 
25
    AppServerLayer, BaseLayer, DatabaseLayer, FunctionalLayer,
 
26
    LaunchpadFunctionalLayer, LaunchpadLayer, LaunchpadScriptLayer,
 
27
    LaunchpadZopelessLayer, LayerInvariantError, LayerIsolationError,
23
28
    LibrarianLayer, ZopelessLayer)
24
29
 
25
30
 
320
325
        self.assertEqual(user, 'librarian')
321
326
 
322
327
 
 
328
class AppServerTestCase(BaseTestCase):
 
329
    layer = AppServerLayer
 
330
 
 
331
    want_component_architecture = True
 
332
    want_launchpad_database = True
 
333
    want_librarian_running = True
 
334
    want_functional_flag = True
 
335
    want_zopeless_flag = False
 
336
 
 
337
    def testAppServerIsAvailable(self):
 
338
        # Test that the app server is up and running.
 
339
        root_url = AppServerLayer.appserver_config.vhost.mainsite.rooturl
 
340
        home_page = urlopen(root_url).read()
 
341
        self.failUnless(
 
342
            'What is Launchpad?' in home_page,
 
343
            "Home page couldn't be retrieved:\n%s" % home_page)
 
344
 
 
345
    def testSetUpTwiceRaisesInvariantError(self):
 
346
        # Calling setUp another time should raises an isolation error.
 
347
        self.assertRaises(LayerInvariantError, AppServerLayer.setUp)
 
348
 
 
349
 
 
350
class AppServerSetupTestCase(unittest.TestCase):
 
351
    """Tests for the setUp and tearDown components of AppServerLayer."""
 
352
    # We need the layer below AppServerLayer
 
353
    layer = LaunchpadFunctionalLayer
 
354
 
 
355
    def tearDown(self):
 
356
        # If the app server was started, kill it.
 
357
        if AppServerLayer.appserver is not None:
 
358
            AppServerLayer.tearDown()
 
359
 
 
360
    def test_tearDown(self):
 
361
        # Test that tearDown kills the app server and remove the PID file.
 
362
        AppServerLayer.setUp()
 
363
        pid = AppServerLayer.appserver.pid
 
364
        pid_file = pidfile_path('launchpad', AppServerLayer.appserver_config)
 
365
        AppServerLayer.tearDown()
 
366
        self.assertRaises(OSError, os.kill, pid, 0)
 
367
        self.failIf(os.path.exists(pid_file), "PID file wasn't removed")
 
368
        self.failUnless(AppServerLayer.appserver is None,
 
369
            "appserver class attribute wasn't reset")
 
370
 
 
371
    def test_testTearDownRaisesIsolationError(self):
 
372
        # A LayerIsolationError should be raised if the app server dies.
 
373
        AppServerLayer.setUp()
 
374
        AppServerLayer.testSetUp()
 
375
        os.kill(AppServerLayer.appserver.pid, signal.SIGTERM)
 
376
        AppServerLayer.appserver.wait()
 
377
        self.assertRaises(LayerIsolationError, AppServerLayer.testTearDown)
 
378
 
 
379
    def test_testTearDownResetsDB(self):
 
380
        # The database should be reset after each test since 
 
381
        AppServerLayer.setUp()
 
382
        AppServerLayer.testSetUp()
 
383
        AppServerLayer.testTearDown()
 
384
        self.assertEquals(True, LaunchpadTestSetup()._reset_db)
 
385
 
 
386
 
323
387
class TestNameTestCase(unittest.TestCase):
324
388
    layer = BaseLayer
325
389
    def testTestName(self):