~bzr-pqm/bzr/bzr.dev

5557.1.7 by John Arbash Meinel
Merge in the bzr.dev 5582
1
# Copyright (C) 2007-2011 Canonical Ltd
2245.8.1 by Martin Pool
Start adding tests for launchpad indirection
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2245.8.1 by Martin Pool
Start adding tests for launchpad indirection
16
3251.4.3 by Aaron Bentley
More renames and cleanups
17
"""Tests for directory lookup through Launchpad.net"""
2245.8.1 by Martin Pool
Start adding tests for launchpad indirection
18
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
19
import os
2898.4.4 by James Henstridge
Changes to account for modifications to the XMLRPC API.
20
import xmlrpclib
21
2245.8.3 by Martin Pool
Start adding indirection transport
22
from bzrlib import (
5609.24.11 by John Arbash Meinel
If someone uses -Dlaunchpad, use the 'official' URL instead of the local one.
23
    debug,
2245.8.4 by Martin Pool
lp:/// indirection works
24
    errors,
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
25
    tests,
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
26
    transport,
2245.8.3 by Martin Pool
Start adding indirection transport
27
    )
2898.4.17 by James Henstridge
Add a test that the redirect actually occurs when opening an lp: URL.
28
from bzrlib.branch import Branch
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
29
from bzrlib.directory_service import directories
3777.1.18 by Aaron Bentley
Fix None handling wrt auth upgrades
30
from bzrlib.tests import (
5967.12.5 by Martin Pool
Update launchpad plugin for features under tests.features
31
    features,
3777.1.18 by Aaron Bentley
Fix None handling wrt auth upgrades
32
    TestCaseInTempDir,
33
    TestCaseWithMemoryTransport
34
)
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
35
from bzrlib.plugins.launchpad import (
36
    _register_directory,
37
    lp_registration,
38
    )
3251.4.3 by Aaron Bentley
More renames and cleanups
39
from bzrlib.plugins.launchpad.lp_directory import (
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
40
    LaunchpadDirectory)
5361.1.1 by John Arbash Meinel
Handle a simple ~ in lp: urls.
41
from bzrlib.plugins.launchpad.account import get_lp_login, set_lp_login
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
42
from bzrlib.tests import http_server
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
43
44
45
def load_tests(standard_tests, module, loader):
46
    result = loader.suiteClass()
47
    t_tests, remaining_tests = tests.split_suite_by_condition(
48
        standard_tests, tests.condition_isinstance((
4776.2.6 by Vincent Ladeuil
Fixed as per review comments.
49
                TestXMLRPCTransport,
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
50
                )))
51
    transport_scenarios = [
52
        ('http', dict(server_class=PreCannedHTTPServer,)),
53
        ]
5967.12.5 by Martin Pool
Update launchpad plugin for features under tests.features
54
    if features.HTTPSServerFeature.available():
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
55
        transport_scenarios.append(
56
            ('https', dict(server_class=PreCannedHTTPSServer,)),
57
            )
58
    tests.multiply_tests(t_tests, transport_scenarios, result)
59
60
    # No parametrization for the remaining tests
61
    result.addTests(remaining_tests)
62
63
    return result
2898.4.3 by James Henstridge
Make launchpad_transport_indirect() use XMLRPC to resolve the lp: URL.
64
65
66
class FakeResolveFactory(object):
5609.24.9 by John Arbash Meinel
A bunch more tests and bug fixes for the local resolution.
67
2898.4.3 by James Henstridge
Make launchpad_transport_indirect() use XMLRPC to resolve the lp: URL.
68
    def __init__(self, test, expected_path, result):
69
        self._test = test
70
        self._expected_path = expected_path
71
        self._result = result
5609.24.9 by John Arbash Meinel
A bunch more tests and bug fixes for the local resolution.
72
        self._submitted = False
2898.4.3 by James Henstridge
Make launchpad_transport_indirect() use XMLRPC to resolve the lp: URL.
73
74
    def __call__(self, path):
75
        self._test.assertEqual(self._expected_path, path)
76
        return self
77
78
    def submit(self, service):
3193.5.2 by Tim Penhey
Updated the tests to handle unknown launchpad instances.
79
        self._service_url = service.service_url
5609.24.9 by John Arbash Meinel
A bunch more tests and bug fixes for the local resolution.
80
        self._submitted = True
2898.4.3 by James Henstridge
Make launchpad_transport_indirect() use XMLRPC to resolve the lp: URL.
81
        return self._result
2245.8.1 by Martin Pool
Start adding tests for launchpad indirection
82
2245.8.7 by Martin Pool
small review cleanups
83
5609.24.9 by John Arbash Meinel
A bunch more tests and bug fixes for the local resolution.
84
class LocalDirectoryURLTests(TestCaseInTempDir):
85
    """Tests for branch urls that we try to pass through local resolution."""
86
87
    def assertResolve(self, expected, url, submitted=False):
88
        path = url[url.index(':')+1:].lstrip('/')
89
        factory = FakeResolveFactory(self, path,
90
                    dict(urls=['bzr+ssh://fake-resolved']))
91
        directory = LaunchpadDirectory()
92
        self.assertEqual(expected,
93
            directory._resolve(url, factory, _lp_login='user'))
94
        # We are testing local resolution, and the fallback when necessary.
95
        self.assertEqual(submitted, factory._submitted)
96
97
    def test_short_form(self):
98
        self.assertResolve('bzr+ssh://bazaar.launchpad.net/+branch/apt',
99
                           'lp:apt')
100
101
    def test_two_part_form(self):
102
        self.assertResolve('bzr+ssh://bazaar.launchpad.net/+branch/apt/2.2',
103
                           'lp:apt/2.2')
104
105
    def test_two_part_plus_subdir(self):
106
        # We allow you to pass more than just what resolves. That way you can
107
        # do things like "bzr log lp:apt/2.2/BUGS"
108
        # Though the virtual FS implementation currently aborts when given a
109
        # URL like this, rather than letting you recurse upwards to find the
110
        # real branch at lp:apt/2.2
111
        self.assertResolve('bzr+ssh://bazaar.launchpad.net/+branch/apt/2.2/BUGS',
112
                           'lp:apt/2.2/BUGS')
113
114
    def test_user_expansion(self):
115
        self.assertResolve('bzr+ssh://bazaar.launchpad.net/~user/apt/foo',
116
                           'lp:~/apt/foo')
117
118
    def test_ubuntu(self):
119
        # Confirmed against xmlrpc. If you don't have a ~user, xmlrpc doesn't
120
        # care that you are asking for 'ubuntu'
121
        self.assertResolve('bzr+ssh://bazaar.launchpad.net/+branch/ubuntu',
122
                           'lp:ubuntu')
123
124
    def test_ubuntu_apt(self):
125
        self.assertResolve('bzr+ssh://bazaar.launchpad.net/+branch/ubuntu/apt',
126
                           'lp:ubuntu/apt')
127
128
    def test_ubuntu_natty_apt(self):
129
        self.assertResolve(
130
            'bzr+ssh://bazaar.launchpad.net/+branch/ubuntu/natty/apt',
131
            'lp:ubuntu/natty/apt')
132
133
    def test_ubuntu_natty_apt_filename(self):
134
        self.assertResolve(
135
            'bzr+ssh://bazaar.launchpad.net/+branch/ubuntu/natty/apt/filename',
136
            'lp:ubuntu/natty/apt/filename')
137
138
    def test_user_two_part(self):
139
        # We fall back to the ResolveFactory. The real Launchpad one will raise
140
        # InvalidURL for this case.
141
        self.assertResolve('bzr+ssh://fake-resolved', 'lp:~jameinel/apt',
142
                           submitted=True)
143
144
    def test_user_three_part(self):
145
        self.assertResolve('bzr+ssh://bazaar.launchpad.net/~jameinel/apt/foo',
146
                           'lp:~jameinel/apt/foo')
147
148
    def test_user_three_part_plus_filename(self):
149
        self.assertResolve(
150
            'bzr+ssh://bazaar.launchpad.net/~jameinel/apt/foo/fname',
151
            'lp:~jameinel/apt/foo/fname')
152
153
    def test_user_ubuntu_two_part(self):
154
        self.assertResolve('bzr+ssh://fake-resolved', 'lp:~jameinel/ubuntu',
155
                           submitted=True)
156
        self.assertResolve('bzr+ssh://fake-resolved', 'lp:~jameinel/debian',
157
                           submitted=True)
158
159
    def test_user_ubuntu_three_part(self):
160
        self.assertResolve('bzr+ssh://fake-resolved',
161
                           'lp:~jameinel/ubuntu/natty', submitted=True)
162
        self.assertResolve('bzr+ssh://fake-resolved',
163
                           'lp:~jameinel/debian/sid', submitted=True)
164
165
    def test_user_ubuntu_four_part(self):
166
        self.assertResolve('bzr+ssh://fake-resolved',
167
                           'lp:~jameinel/ubuntu/natty/project', submitted=True)
168
        self.assertResolve('bzr+ssh://fake-resolved',
169
                           'lp:~jameinel/debian/sid/project', submitted=True)
170
171
    def test_user_ubuntu_five_part(self):
172
        self.assertResolve(
173
            'bzr+ssh://bazaar.launchpad.net/~jameinel/ubuntu/natty/apt/branch',
174
            'lp:~jameinel/ubuntu/natty/apt/branch')
175
        self.assertResolve(
176
            'bzr+ssh://bazaar.launchpad.net/~jameinel/debian/sid/apt/branch',
177
            'lp:~jameinel/debian/sid/apt/branch')
178
179
    def test_user_ubuntu_five_part_plus_subdir(self):
180
        self.assertResolve(
181
            'bzr+ssh://bazaar.launchpad.net/~jameinel/ubuntu/natty/apt/branch/f',
182
            'lp:~jameinel/ubuntu/natty/apt/branch/f')
183
        self.assertResolve(
184
            'bzr+ssh://bazaar.launchpad.net/~jameinel/debian/sid/apt/branch/f',
185
            'lp:~jameinel/debian/sid/apt/branch/f')
186
187
    def test_handles_special_lp(self):
188
        self.assertResolve('bzr+ssh://bazaar.launchpad.net/+branch/apt', 'lp:apt')
189
        self.assertResolve('bzr+ssh://bazaar.launchpad.net/+branch/apt',
190
                           'lp:///apt')
191
        self.assertResolve('bzr+ssh://bazaar.launchpad.net/+branch/apt',
192
                           'lp://production/apt')
193
        self.assertResolve('bzr+ssh://bazaar.launchpad.dev/+branch/apt',
194
                           'lp://dev/apt')
195
        self.assertResolve('bzr+ssh://bazaar.staging.launchpad.net/+branch/apt',
196
                           'lp://staging/apt')
197
        self.assertResolve('bzr+ssh://bazaar.qastaging.launchpad.net/+branch/apt',
198
                           'lp://qastaging/apt')
199
        self.assertResolve('bzr+ssh://bazaar.demo.launchpad.net/+branch/apt',
200
                           'lp://demo/apt')
201
5609.24.11 by John Arbash Meinel
If someone uses -Dlaunchpad, use the 'official' URL instead of the local one.
202
    def test_debug_launchpad_uses_resolver(self):
203
        self.assertResolve('bzr+ssh://bazaar.launchpad.net/+branch/bzr',
204
                           'lp:bzr', submitted=False)
205
        debug.debug_flags.add('launchpad')
206
        self.addCleanup(debug.debug_flags.discard, 'launchpad')
207
        self.assertResolve('bzr+ssh://fake-resolved', 'lp:bzr', submitted=True)
5609.24.9 by John Arbash Meinel
A bunch more tests and bug fixes for the local resolution.
208
209
3777.1.18 by Aaron Bentley
Fix None handling wrt auth upgrades
210
class DirectoryUrlTests(TestCaseInTempDir):
3251.4.3 by Aaron Bentley
More renames and cleanups
211
    """Tests for branch urls through Launchpad.net directory"""
2245.8.1 by Martin Pool
Start adding tests for launchpad indirection
212
2245.8.5 by Martin Pool
Add short-form lp:PRODUCT url form
213
    def test_short_form(self):
214
        """A launchpad url should map to a http url"""
2898.4.3 by James Henstridge
Make launchpad_transport_indirect() use XMLRPC to resolve the lp: URL.
215
        factory = FakeResolveFactory(
2898.4.4 by James Henstridge
Changes to account for modifications to the XMLRPC API.
216
            self, 'apt', dict(urls=[
217
                    'http://bazaar.launchpad.net/~apt/apt/devel']))
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
218
        directory = LaunchpadDirectory()
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
219
        self.assertEquals('http://bazaar.launchpad.net/~apt/apt/devel',
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
220
                          directory._resolve('lp:apt', factory))
3193.5.2 by Tim Penhey
Updated the tests to handle unknown launchpad instances.
221
        # Make sure that resolve went to the production server.
5243.1.1 by Martin
Use the production server in the launchpad plugin rather than edge
222
        self.assertEquals('https://xmlrpc.launchpad.net/bazaar/',
3193.5.2 by Tim Penhey
Updated the tests to handle unknown launchpad instances.
223
                          factory._service_url)
224
5615.2.1 by Jelmer Vernooij
Support the 'qastaging' instance of Launchpad.
225
    def test_qastaging(self):
226
        """A launchpad url should map to a http url"""
227
        factory = FakeResolveFactory(
228
            self, 'apt', dict(urls=[
229
                    'http://bazaar.qastaging.launchpad.net/~apt/apt/devel']))
230
        url = 'lp://qastaging/apt'
231
        directory = LaunchpadDirectory()
232
        self.assertEquals('http://bazaar.qastaging.launchpad.net/~apt/apt/devel',
233
                          directory._resolve(url, factory))
234
        # Make sure that resolve went to the qastaging server.
235
        self.assertEquals('https://xmlrpc.qastaging.launchpad.net/bazaar/',
236
                          factory._service_url)
237
3193.5.2 by Tim Penhey
Updated the tests to handle unknown launchpad instances.
238
    def test_staging(self):
239
        """A launchpad url should map to a http url"""
240
        factory = FakeResolveFactory(
241
            self, 'apt', dict(urls=[
242
                    'http://bazaar.staging.launchpad.net/~apt/apt/devel']))
243
        url = 'lp://staging/apt'
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
244
        directory = LaunchpadDirectory()
3193.5.2 by Tim Penhey
Updated the tests to handle unknown launchpad instances.
245
        self.assertEquals('http://bazaar.staging.launchpad.net/~apt/apt/devel',
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
246
                          directory._resolve(url, factory))
3193.5.2 by Tim Penhey
Updated the tests to handle unknown launchpad instances.
247
        # Make sure that resolve went to the staging server.
248
        self.assertEquals('https://xmlrpc.staging.launchpad.net/bazaar/',
249
                          factory._service_url)
2245.8.5 by Martin Pool
Add short-form lp:PRODUCT url form
250
3251.4.3 by Aaron Bentley
More renames and cleanups
251
    def test_url_from_directory(self):
2245.8.3 by Martin Pool
Start adding indirection transport
252
        """A launchpad url should map to a http url"""
2898.4.3 by James Henstridge
Make launchpad_transport_indirect() use XMLRPC to resolve the lp: URL.
253
        factory = FakeResolveFactory(
2898.4.4 by James Henstridge
Changes to account for modifications to the XMLRPC API.
254
            self, 'apt', dict(urls=[
255
                    'http://bazaar.launchpad.net/~apt/apt/devel']))
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
256
        directory = LaunchpadDirectory()
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
257
        self.assertEquals('http://bazaar.launchpad.net/~apt/apt/devel',
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
258
                          directory._resolve('lp:///apt', factory))
2898.4.3 by James Henstridge
Make launchpad_transport_indirect() use XMLRPC to resolve the lp: URL.
259
3251.4.3 by Aaron Bentley
More renames and cleanups
260
    def test_directory_skip_bad_schemes(self):
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
261
        factory = FakeResolveFactory(
262
            self, 'apt', dict(urls=[
263
                    'bad-scheme://bazaar.launchpad.net/~apt/apt/devel',
264
                    'http://bazaar.launchpad.net/~apt/apt/devel',
265
                    'http://another/location']))
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
266
        directory = LaunchpadDirectory()
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
267
        self.assertEquals('http://bazaar.launchpad.net/~apt/apt/devel',
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
268
                          directory._resolve('lp:///apt', factory))
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
269
3251.4.3 by Aaron Bentley
More renames and cleanups
270
    def test_directory_no_matching_schemes(self):
2898.4.3 by James Henstridge
Make launchpad_transport_indirect() use XMLRPC to resolve the lp: URL.
271
        # If the XMLRPC call does not return any protocols we support,
272
        # invalidURL is raised.
273
        factory = FakeResolveFactory(
2898.4.4 by James Henstridge
Changes to account for modifications to the XMLRPC API.
274
            self, 'apt', dict(urls=[
275
                    'bad-scheme://bazaar.launchpad.net/~apt/apt/devel']))
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
276
        directory = LaunchpadDirectory()
2898.4.11 by James Henstridge
Switch back to RedirectRequested based implementation.
277
        self.assertRaises(errors.InvalidURL,
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
278
                          directory._resolve, 'lp:///apt', factory)
2898.4.4 by James Henstridge
Changes to account for modifications to the XMLRPC API.
279
3251.4.3 by Aaron Bentley
More renames and cleanups
280
    def test_directory_fault(self):
2898.4.4 by James Henstridge
Changes to account for modifications to the XMLRPC API.
281
        # Test that XMLRPC faults get converted to InvalidURL errors.
282
        factory = FakeResolveFactory(self, 'apt', None)
283
        def submit(service):
284
            raise xmlrpclib.Fault(42, 'something went wrong')
285
        factory.submit = submit
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
286
        directory = LaunchpadDirectory()
2898.4.11 by James Henstridge
Switch back to RedirectRequested based implementation.
287
        self.assertRaises(errors.InvalidURL,
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
288
                          directory._resolve, 'lp:///apt', factory)
2245.8.4 by Martin Pool
lp:/// indirection works
289
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
290
    def test_skip_bzr_ssh_launchpad_net_when_anonymous(self):
291
        # Test that bzr+ssh://bazaar.launchpad.net gets skipped if
292
        # Bazaar does not know the user's Launchpad ID:
293
        self.assertEqual(None, get_lp_login())
294
        factory = FakeResolveFactory(
295
            self, 'apt', dict(urls=[
296
                    'bzr+ssh://bazaar.launchpad.net/~apt/apt/devel',
297
                    'http://bazaar.launchpad.net/~apt/apt/devel']))
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
298
        directory = LaunchpadDirectory()
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
299
        self.assertEquals('http://bazaar.launchpad.net/~apt/apt/devel',
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
300
                          directory._resolve('lp:///apt', factory))
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
301
3031.2.2 by jml at canonical
Failing test for skipping SFTP.
302
    def test_skip_sftp_launchpad_net_when_anonymous(self):
303
        # Test that sftp://bazaar.launchpad.net gets skipped if
304
        # Bazaar does not know the user's Launchpad ID:
305
        self.assertEqual(None, get_lp_login())
306
        factory = FakeResolveFactory(
307
            self, 'apt', dict(urls=[
308
                    'sftp://bazaar.launchpad.net/~apt/apt/devel',
309
                    'http://bazaar.launchpad.net/~apt/apt/devel']))
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
310
        directory = LaunchpadDirectory()
3031.2.2 by jml at canonical
Failing test for skipping SFTP.
311
        self.assertEquals('http://bazaar.launchpad.net/~apt/apt/devel',
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
312
                          directory._resolve('lp:///apt', factory))
3031.2.2 by jml at canonical
Failing test for skipping SFTP.
313
5609.24.8 by John Arbash Meinel
Fix the launchpad-login tests. And fix a bad commit typo.
314
    def test_with_login_avoid_resolve_factory(self):
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
315
        # Test that bzr+ssh URLs get rewritten to include the user's
316
        # Launchpad ID (assuming we know the Launchpad ID).
317
        factory = FakeResolveFactory(
318
            self, 'apt', dict(urls=[
5609.24.8 by John Arbash Meinel
Fix the launchpad-login tests. And fix a bad commit typo.
319
                    'bzr+ssh://my-super-custom/special/devel',
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
320
                    'http://bazaar.launchpad.net/~apt/apt/devel']))
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
321
        directory = LaunchpadDirectory()
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
322
        self.assertEquals(
5609.24.8 by John Arbash Meinel
Fix the launchpad-login tests. And fix a bad commit typo.
323
            'bzr+ssh://bazaar.launchpad.net/+branch/apt',
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
324
            directory._resolve('lp:///apt', factory, _lp_login='username'))
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
325
326
    def test_no_rewrite_of_other_bzr_ssh(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
327
        # Test that we don't rewrite bzr+ssh URLs for other
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
328
        self.assertEqual(None, get_lp_login())
329
        factory = FakeResolveFactory(
330
            self, 'apt', dict(urls=[
331
                    'bzr+ssh://example.com/~apt/apt/devel',
332
                    'http://bazaar.launchpad.net/~apt/apt/devel']))
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
333
        directory = LaunchpadDirectory()
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
334
        self.assertEquals('bzr+ssh://example.com/~apt/apt/devel',
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
335
                          directory._resolve('lp:///apt', factory))
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
336
2245.8.4 by Martin Pool
lp:/// indirection works
337
    # TODO: check we get an error if the url is unreasonable
3251.4.3 by Aaron Bentley
More renames and cleanups
338
    def test_error_for_bad_url(self):
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
339
        directory = LaunchpadDirectory()
2245.8.4 by Martin Pool
lp:/// indirection works
340
        self.assertRaises(errors.InvalidURL,
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
341
            directory._resolve, 'lp://ratotehunoahu')
2898.4.12 by James Henstridge
Add tests that redirects get issued by appropriate transport methods.
342
5361.1.1 by John Arbash Meinel
Handle a simple ~ in lp: urls.
343
    def test_resolve_tilde_to_user(self):
344
        factory = FakeResolveFactory(
345
            self, '~username/apt/test', dict(urls=[
5609.24.9 by John Arbash Meinel
A bunch more tests and bug fixes for the local resolution.
346
                'bzr+ssh://bazaar.launchpad.net/~username/apt/test']))
5361.1.1 by John Arbash Meinel
Handle a simple ~ in lp: urls.
347
        directory = LaunchpadDirectory()
348
        self.assertEquals(
5609.24.9 by John Arbash Meinel
A bunch more tests and bug fixes for the local resolution.
349
            'bzr+ssh://bazaar.launchpad.net/~username/apt/test',
5361.1.1 by John Arbash Meinel
Handle a simple ~ in lp: urls.
350
            directory._resolve('lp:~/apt/test', factory, _lp_login='username'))
351
        # Should also happen when the login is just set by config
352
        set_lp_login('username')
353
        self.assertEquals(
5609.24.9 by John Arbash Meinel
A bunch more tests and bug fixes for the local resolution.
354
            'bzr+ssh://bazaar.launchpad.net/~username/apt/test',
5361.1.1 by John Arbash Meinel
Handle a simple ~ in lp: urls.
355
            directory._resolve('lp:~/apt/test', factory))
356
357
    def test_tilde_fails_no_login(self):
358
        factory = FakeResolveFactory(
359
            self, '~username/apt/test', dict(urls=[
360
                    'bzr+ssh://bazaar.launchpad.net/~username/apt/test']))
361
        self.assertIs(None, get_lp_login())
362
        directory = LaunchpadDirectory()
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
363
        self.assertRaises(errors.InvalidURL,
364
                          directory._resolve, 'lp:~/apt/test', factory)
5361.1.1 by John Arbash Meinel
Handle a simple ~ in lp: urls.
365
2898.4.17 by James Henstridge
Add a test that the redirect actually occurs when opening an lp: URL.
366
3251.4.3 by Aaron Bentley
More renames and cleanups
367
class DirectoryOpenBranchTests(TestCaseWithMemoryTransport):
2898.4.17 by James Henstridge
Add a test that the redirect actually occurs when opening an lp: URL.
368
3251.4.3 by Aaron Bentley
More renames and cleanups
369
    def test_directory_open_branch(self):
2898.4.17 by James Henstridge
Add a test that the redirect actually occurs when opening an lp: URL.
370
        # Test that opening an lp: branch redirects to the real location.
371
        target_branch = self.make_branch('target')
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
372
        class FooService(object):
373
            """A directory service that maps the name to a FILE url"""
374
375
            def look_up(self, name, url):
376
                if 'lp:///apt' == url:
377
                    return target_branch.base.rstrip('/')
3251.4.2 by Aaron Bentley
Clean up Launchpad directory service code
378
                return '!unexpected look_up value!'
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
379
380
        directories.remove('lp:')
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
381
        directories.remove('ubuntu:')
382
        directories.remove('debianlp:')
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
383
        directories.register('lp:', FooService, 'Map lp URLs to local urls')
3251.4.2 by Aaron Bentley
Clean up Launchpad directory service code
384
        self.addCleanup(_register_directory)
4985.2.1 by Vincent Ladeuil
Deploy addAttrCleanup on the whole test suite.
385
        self.addCleanup(directories.remove, 'lp:')
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
386
        t = transport.get_transport('lp:///apt')
387
        branch = Branch.open_from_transport(t)
2898.4.17 by James Henstridge
Add a test that the redirect actually occurs when opening an lp: URL.
388
        self.assertEqual(target_branch.base, branch.base)
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
389
390
391
class PredefinedRequestHandler(http_server.TestingHTTPRequestHandler):
392
    """Request handler for a unique and pre-defined request.
393
394
    The only thing we care about here is that we receive a connection. But
395
    since we want to dialog with a real http client, we have to send it correct
396
    responses.
397
398
    We expect to receive a *single* request nothing more (and we won't even
399
    check what request it is), the tests will recognize us from our response.
400
    """
401
402
    def handle_one_request(self):
403
        tcs = self.server.test_case_server
404
        requestline = self.rfile.readline()
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
405
        self.MessageClass(self.rfile, 0)
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
406
        if requestline.startswith('POST'):
407
            # The body should be a single line (or we don't know where it ends
408
            # and we don't want to issue a blocking read)
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
409
            self.rfile.readline()
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
410
411
        self.wfile.write(tcs.canned_response)
412
4776.2.6 by Vincent Ladeuil
Fixed as per review comments.
413
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
414
class PreCannedServerMixin(object):
415
416
    def __init__(self):
417
        super(PreCannedServerMixin, self).__init__(
418
            request_handler=PredefinedRequestHandler)
419
        # Bytes read and written by the server
420
        self.bytes_read = 0
421
        self.bytes_written = 0
422
        self.canned_response = None
423
424
425
class PreCannedHTTPServer(PreCannedServerMixin, http_server.HttpServer):
426
    pass
427
428
5967.12.5 by Martin Pool
Update launchpad plugin for features under tests.features
429
if features.HTTPSServerFeature.available():
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
430
    from bzrlib.tests import https_server
431
    class PreCannedHTTPSServer(PreCannedServerMixin, https_server.HTTPSServer):
432
        pass
433
434
4776.2.6 by Vincent Ladeuil
Fixed as per review comments.
435
class TestXMLRPCTransport(tests.TestCase):
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
436
437
    # set by load_tests
438
    server_class = None
439
440
    def setUp(self):
441
        tests.TestCase.setUp(self)
442
        self.server = self.server_class()
4934.3.3 by Martin Pool
Rename Server.setUp to Server.start_server
443
        self.server.start_server()
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
444
        # Ensure we don't clobber env
5570.3.6 by Vincent Ladeuil
Get rid of all _captureVar() calls, no test failures, pfew.
445
        self.overrideEnv('BZR_LP_XMLRPC_URL', None)
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
446
447
    def tearDown(self):
4934.3.1 by Martin Pool
Rename Server.tearDown to .stop_server
448
        self.server.stop_server()
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
449
        tests.TestCase.tearDown(self)
450
451
    def set_canned_response(self, server, path):
452
        response_format = '''HTTP/1.1 200 OK\r
453
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
454
Server: Apache/2.0.54 (Fedora)\r
455
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
456
ETag: "56691-23-38e9ae00"\r
457
Accept-Ranges: bytes\r
458
Content-Length: %(length)d\r
459
Connection: close\r
460
Content-Type: text/plain; charset=UTF-8\r
461
\r
462
<?xml version='1.0'?>
463
<methodResponse>
464
<params>
465
<param>
466
<value><struct>
467
<member>
468
<name>urls</name>
469
<value><array><data>
470
<value><string>bzr+ssh://bazaar.launchpad.net/%(path)s</string></value>
471
<value><string>http://bazaar.launchpad.net/%(path)s</string></value>
472
</data></array></value>
473
</member>
474
</struct></value>
475
</param>
476
</params>
477
</methodResponse>
478
'''
479
        length = 334 + 2 * len(path)
480
        server.canned_response = response_format % dict(length=length,
481
                                                        path=path)
482
4776.2.3 by Vincent Ladeuil
Add NEWS entry.
483
    def do_request(self, server_url):
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
484
        os.environ['BZR_LP_XMLRPC_URL'] = self.server.get_url()
485
        service = lp_registration.LaunchpadService()
486
        resolve = lp_registration.ResolveLaunchpadPathRequest('bzr')
487
        result = resolve.submit(service)
4776.2.3 by Vincent Ladeuil
Add NEWS entry.
488
        return result
489
490
    def test_direct_request(self):
491
        self.set_canned_response(self.server, '~bzr-pqm/bzr/bzr.dev')
492
        result = self.do_request(self.server.get_url())
4776.2.2 by Vincent Ladeuil
Start testing the XMLRPC transport re-implemented on top of _urllib2_wrappers.
493
        urls = result.get('urls', None)
494
        self.assertIsNot(None, urls)
495
        self.assertEquals(
496
            ['bzr+ssh://bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev',
497
             'http://bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev'],
498
            urls)
4776.2.3 by Vincent Ladeuil
Add NEWS entry.
499
    # FIXME: we need to test with a real proxy, I can't find a way so simulate
500
    # CONNECT without leaving one server hanging the test :-/ Since that maybe
501
    # related to the leaking tests problems, I'll punt for now -- vila 20091030
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
502
503
504
class TestDebuntuExpansions(TestCaseInTempDir):
505
    """Test expansions for ubuntu: and debianlp: schemes."""
506
507
    def setUp(self):
5558.1.1 by Vincent Ladeuil
Catch the fugitive TestDebuntuExpansions tests and bring them back in the isolation jail
508
        super(TestDebuntuExpansions, self).setUp()
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
509
        self.directory = LaunchpadDirectory()
510
511
    def _make_factory(self, package='foo', distro='ubuntu', series=None):
512
        if series is None:
513
            path = '%s/%s' % (distro, package)
514
            url_suffix = '~branch/%s/%s' % (distro, package)
515
        else:
516
            path = '%s/%s/%s' % (distro, series, package)
517
            url_suffix = '~branch/%s/%s/%s' % (distro, series, package)
518
        return FakeResolveFactory(
519
            self, path, dict(urls=[
520
                'http://bazaar.launchpad.net/' + url_suffix]))
521
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
522
    def assertURL(self, expected_url, shortcut, package='foo', distro='ubuntu',
523
                  series=None):
524
        factory = self._make_factory(package=package, distro=distro,
525
                                     series=series)
526
        self.assertEqual('http://bazaar.launchpad.net/~branch/' + expected_url,
527
                         self.directory._resolve(shortcut, factory))
528
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
529
    # Bogus distro.
530
531
    def test_bogus_distro(self):
532
        self.assertRaises(errors.InvalidURL,
533
                          self.directory._resolve, 'gentoo:foo')
534
535
    def test_trick_bogus_distro_u(self):
536
        self.assertRaises(errors.InvalidURL,
537
                          self.directory._resolve, 'utube:foo')
538
539
    def test_trick_bogus_distro_d(self):
540
        self.assertRaises(errors.InvalidURL,
541
                          self.directory._resolve, 'debuntu:foo')
542
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
543
    def test_missing_ubuntu_distroseries_without_project(self):
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
544
        # Launchpad does not hold source packages for Intrepid.  Missing or
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
545
        # bogus distroseries with no project name is treated like a project.
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
546
        self.assertURL('ubuntu/intrepid', 'ubuntu:intrepid', package='intrepid')
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
547
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
548
    def test_missing_ubuntu_distroseries_with_project(self):
549
        # Launchpad does not hold source packages for Intrepid.  Missing or
550
        # bogus distroseries with a project name is treated like an unknown
551
        # series (i.e. we keep it verbatim).
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
552
        self.assertURL('ubuntu/intrepid/foo',
553
                       'ubuntu:intrepid/foo', series='intrepid')
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
554
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
555
    def test_missing_debian_distroseries(self):
556
        # Launchpad does not hold source packages for unstable.  Missing or
557
        # bogus distroseries is treated like a project.
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
558
        self.assertURL('debian/sid',
559
                       'debianlp:sid', package='sid', distro='debian')
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
560
561
    # Ubuntu Default distro series.
562
563
    def test_ubuntu_default_distroseries_expansion(self):
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
564
        self.assertURL('ubuntu/foo', 'ubuntu:foo')
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
565
5462.4.2 by Barry Warsaw
Might as well support natty.
566
    def test_ubuntu_natty_distroseries_expansion(self):
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
567
        self.assertURL('ubuntu/natty/foo', 'ubuntu:natty/foo', series='natty')
5462.4.2 by Barry Warsaw
Might as well support natty.
568
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
569
    def test_ubuntu_n_distroseries_expansion(self):
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
570
        self.assertURL('ubuntu/natty/foo', 'ubuntu:n/foo', series='natty')
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
571
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
572
    def test_ubuntu_maverick_distroseries_expansion(self):
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
573
        self.assertURL('ubuntu/maverick/foo', 'ubuntu:maverick/foo',
574
                       series='maverick')
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
575
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
576
    def test_ubuntu_m_distroseries_expansion(self):
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
577
        self.assertURL('ubuntu/maverick/foo', 'ubuntu:m/foo', series='maverick')
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
578
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
579
    def test_ubuntu_lucid_distroseries_expansion(self):
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
580
        self.assertURL('ubuntu/lucid/foo', 'ubuntu:lucid/foo', series='lucid')
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
581
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
582
    def test_ubuntu_l_distroseries_expansion(self):
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
583
        self.assertURL('ubuntu/lucid/foo', 'ubuntu:l/foo', series='lucid')
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
584
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
585
    def test_ubuntu_karmic_distroseries_expansion(self):
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
586
        self.assertURL('ubuntu/karmic/foo', 'ubuntu:karmic/foo',
587
                       series='karmic')
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
588
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
589
    def test_ubuntu_k_distroseries_expansion(self):
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
590
        self.assertURL('ubuntu/karmic/foo', 'ubuntu:k/foo', series='karmic')
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
591
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
592
    def test_ubuntu_jaunty_distroseries_expansion(self):
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
593
        self.assertURL('ubuntu/jaunty/foo', 'ubuntu:jaunty/foo',
594
                       series='jaunty')
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
595
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
596
    def test_ubuntu_j_distroseries_expansion(self):
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
597
        self.assertURL('ubuntu/jaunty/foo', 'ubuntu:j/foo', series='jaunty')
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
598
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
599
    def test_ubuntu_hardy_distroseries_expansion(self):
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
600
        self.assertURL('ubuntu/hardy/foo', 'ubuntu:hardy/foo', series='hardy')
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
601
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
602
    def test_ubuntu_h_distroseries_expansion(self):
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
603
        self.assertURL('ubuntu/hardy/foo', 'ubuntu:h/foo', series='hardy')
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
604
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
605
    def test_ubuntu_dapper_distroseries_expansion(self):
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
606
        self.assertURL('ubuntu/dapper/foo', 'ubuntu:dapper/foo',
607
                       series='dapper')
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
608
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
609
    def test_ubuntu_d_distroseries_expansion(self):
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
610
        self.assertURL('ubuntu/dapper/foo', 'ubuntu:d/foo', series='dapper')
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
611
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
612
    # Debian default distro series.
613
614
    def test_debian_default_distroseries_expansion(self):
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
615
        self.assertURL('debian/foo', 'debianlp:foo', distro='debian')
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
616
617
    def test_debian_squeeze_distroseries_expansion(self):
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
618
        self.assertURL('debian/squeeze/foo', 'debianlp:squeeze/foo',
619
                       distro='debian', series='squeeze')
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
620
621
    def test_debian_lenny_distroseries_expansion(self):
5462.4.6 by Vincent Ladeuil
Add helper to simplify tests
622
        self.assertURL('debian/lenny/foo', 'debianlp:lenny/foo',
623
                       distro='debian', series='lenny')