~launchpad-pqm/launchpad/devel

11634.2.3 by Robert Collins
Move the librarian test instance code from librarian.ftests.harness to librarian.testing.server.
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
4
from __future__ import with_statement
5
11634.2.3 by Robert Collins
Move the librarian test instance code from librarian.ftests.harness to librarian.testing.server.
6
"""Test the LibrarianServerFixture."""
7
8
__metaclass__ = type
9
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
10
import os
11
import socket
12
from textwrap import dedent
14606.2.5 by William Grant
Move the rest of canonical.librarian to lp.services.librarianserver.
13
from urllib import urlopen
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
14
15
from testtools.content import Content
16
from testtools.content_type import UTF8_TEXT
17
14605.1.1 by Curtis Hovey
Moved canonical.config to lp.services.
18
from lp.services.config import config
19
from lp.services.config.fixture import ConfigFixture
14606.2.5 by William Grant
Move the rest of canonical.librarian to lp.services.librarianserver.
20
from lp.services.librarianserver.testing.server import LibrarianServerFixture
21
from lp.testing import TestCase
22
from lp.testing.layers import (
23
    BaseLayer,
24
    DatabaseLayer,
25
    )
14604.1.1 by Curtis Hovey
Separate test-authoring classes from test-running classes.
26
from lp.testing.tests.test_layers_functional import (
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
27
    BaseLayerIsolator,
28
    LayerFixture,
29
    )
11634.2.3 by Robert Collins
Move the librarian test instance code from librarian.ftests.harness to librarian.testing.server.
30
31
32
class TestLibrarianServerFixture(TestCase):
33
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
34
    layer = DatabaseLayer
35
36
    def skip_if_persistent(self, fixture):
11634.2.3 by Robert Collins
Move the librarian test instance code from librarian.ftests.harness to librarian.testing.server.
37
        if fixture._persistent_servers():
38
            self.skip('persistent server running.')
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
39
40
    def test_on_init_no_pid(self):
3691.57.74 by Stuart Bishop
Test fixes
41
        fixture = LibrarianServerFixture(BaseLayer.config_fixture)
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
42
        self.skip_if_persistent(fixture)
11634.2.3 by Robert Collins
Move the librarian test instance code from librarian.ftests.harness to librarian.testing.server.
43
        self.assertEqual(None, fixture.pid)
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
44
45
    def test_setUp_allocates_resources(self):
3691.57.74 by Stuart Bishop
Test fixes
46
        # We need a new ConfigFixture, and create a
47
        # LibrarianServerFixture using it. We can then confirm that new
48
        # resources have been allocated by comparing with the currently
49
        # in use ConfigFixture and config.
50
        config_fixture = ConfigFixture(
51
            'foo', BaseLayer.config_fixture.instance_name)
52
        self.addCleanup(config_fixture.cleanUp)
53
        config_fixture.setUp()
54
        fixture = LibrarianServerFixture(config_fixture)
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
55
        self.skip_if_persistent(fixture)
56
        with fixture:
57
            try:
58
                self.assertNotEqual(config.librarian_server.root, fixture.root)
59
                self.assertNotEqual(
60
                    config.librarian.download_port,
61
                    fixture.download_port)
62
                self.assertNotEqual(
63
                    config.librarian.upload_port,
64
                    fixture.upload_port)
65
                self.assertNotEqual(
66
                    config.librarian.restricted_download_port,
67
                    fixture.restricted_download_port)
68
                self.assertNotEqual(
69
                    config.librarian.restricted_upload_port,
70
                    fixture.restricted_upload_port)
11737.4.7 by Robert Collins
Clarity.
71
                # And it exposes a config fragment (but it is not activated).
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
72
                expected_config = dedent("""\
73
                    [librarian_server]
74
                    root: %s
75
                    [librarian]
76
                    download_port: %s
77
                    upload_port: %s
3691.57.76 by Stuart Bishop
Fixture should use correct hostname
78
                    download_url: http://%s:%s/
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
79
                    restricted_download_port: %s
80
                    restricted_upload_port: %s
3691.57.76 by Stuart Bishop
Fixture should use correct hostname
81
                    restricted_download_url: http://%s:%s/
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
82
                    """) % (
83
                        fixture.root,
84
                        fixture.download_port,
85
                        fixture.upload_port,
3691.57.76 by Stuart Bishop
Fixture should use correct hostname
86
                        config.librarian.download_host,
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
87
                        fixture.download_port,
88
                        fixture.restricted_download_port,
89
                        fixture.restricted_upload_port,
3691.57.76 by Stuart Bishop
Fixture should use correct hostname
90
                        config.librarian.restricted_download_host,
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
91
                        fixture.restricted_download_port,
92
                        )
93
                self.assertEqual(expected_config, fixture.service_config)
94
            except:
95
                self.attachLibrarianLog(fixture)
96
                raise
97
11737.4.7 by Robert Collins
Clarity.
98
    def test_getLogChunks(self):
3691.57.74 by Stuart Bishop
Test fixes
99
        fixture = LibrarianServerFixture(BaseLayer.config_fixture)
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
100
        with fixture:
11737.4.7 by Robert Collins
Clarity.
101
            chunks = fixture.getLogChunks()
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
102
            self.assertIsInstance(chunks, list)
103
        found_started = False
104
        for chunk in chunks:
105
            if 'daemon ready' in chunk:
106
                found_started = True
107
        self.assertTrue(found_started)
108
109
    def test_smoke_test(self):
110
        # Avoid indefinite hangs:
111
        self.addCleanup(socket.setdefaulttimeout, socket.getdefaulttimeout())
112
        socket.setdefaulttimeout(1)
3691.57.74 by Stuart Bishop
Test fixes
113
        fixture = LibrarianServerFixture(BaseLayer.config_fixture)
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
114
        with fixture:
115
            librarian_url = "http://%s:%d" % (
116
                config.librarian.download_host,
117
                fixture.download_port)
118
            restricted_librarian_url = "http://%s:%d" % (
119
                config.librarian.restricted_download_host,
120
                fixture.restricted_download_port)
121
            # Both download ports work:
11737.4.7 by Robert Collins
Clarity.
122
            self.assertIn('Copyright', urlopen(librarian_url).read())
123
            self.assertIn(
124
                'Copyright', urlopen(restricted_librarian_url).read())
11737.4.6 by Robert Collins
Make the librarian use dynamically allocated ports and root dir (except when in production / persistent test services).
125
            os.path.isdir(fixture.root)
126
        # Ports are closed on cleanUp.
127
        self.assertRaises(IOError, urlopen, librarian_url)
128
        self.assertRaises(IOError, urlopen, restricted_librarian_url)
129
        self.assertFalse(os.path.exists(fixture.root))
130
        # We can use the fixture again (gets a new URL):
131
        with fixture:
132
            librarian_url = "http://%s:%d" % (
133
                config.librarian.download_host,
134
                fixture.download_port)
11737.4.7 by Robert Collins
Clarity.
135
            self.assertIn('Copyright', urlopen(librarian_url).read())