~bzr-pqm/bzr/bzr.dev

3221.4.2 by Martin Pool
Add in thumpers tests for selection of the right Launchpad instance
1
# Copyright (C) 2008 Canonical Ltd
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
3221.4.2 by Martin Pool
Add in thumpers tests for selection of the right Launchpad instance
16
17
"""Tests for selection of the right Launchpad service by environment"""
18
19
import os
3955.3.9 by Jonathan Lange
Catch errors.
20
import xmlrpclib
3221.4.2 by Martin Pool
Add in thumpers tests for selection of the right Launchpad instance
21
3955.3.9 by Jonathan Lange
Catch errors.
22
from bzrlib import errors
3221.4.2 by Martin Pool
Add in thumpers tests for selection of the right Launchpad instance
23
from bzrlib.plugins.launchpad.lp_registration import (
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
24
    InvalidLaunchpadInstance, LaunchpadService, NotLaunchpadBranch)
3955.3.8 by Jonathan Lange
Support lp URL shortcuts.
25
from bzrlib.plugins.launchpad.test_lp_directory import FakeResolveFactory
26
from bzrlib.tests import TestCase
3221.4.2 by Martin Pool
Add in thumpers tests for selection of the right Launchpad instance
27
28
29
class LaunchpadServiceTests(TestCase):
30
    """Test that the correct Launchpad instance is chosen."""
31
32
    def setUp(self):
33
        super(LaunchpadServiceTests, self).setUp()
34
        # make sure we have a reproducible standard environment
35
        self._captureVar('BZR_LP_XMLRPC_URL', None)
36
37
    def test_default_service(self):
38
        service = LaunchpadService()
39
        self.assertEqual('https://xmlrpc.launchpad.net/bazaar/',
40
                         service.service_url)
41
42
    def test_alter_default_service_url(self):
43
        LaunchpadService.DEFAULT_SERVICE_URL = 'http://example.com/'
44
        try:
45
            service = LaunchpadService()
46
            self.assertEqual('http://example.com/',
47
                             service.service_url)
48
        finally:
49
            LaunchpadService.DEFAULT_SERVICE_URL = \
50
                LaunchpadService.LAUNCHPAD_INSTANCE['production']
51
52
    def test_staging_service(self):
53
        service = LaunchpadService(lp_instance='staging')
54
        self.assertEqual('https://xmlrpc.staging.launchpad.net/bazaar/',
55
                         service.service_url)
56
57
    def test_edge_service(self):
58
        service = LaunchpadService(lp_instance='edge')
59
        self.assertEqual('https://xmlrpc.edge.launchpad.net/bazaar/',
60
                         service.service_url)
61
62
    def test_dev_service(self):
63
        service = LaunchpadService(lp_instance='dev')
3955.3.2 by Jonathan Lange
Tighten up the code a little, changing the dev service to use https,
64
        self.assertEqual('https://xmlrpc.launchpad.dev/bazaar/',
3221.4.2 by Martin Pool
Add in thumpers tests for selection of the right Launchpad instance
65
                         service.service_url)
66
67
    def test_demo_service(self):
68
        service = LaunchpadService(lp_instance='demo')
69
        self.assertEqual('https://xmlrpc.demo.launchpad.net/bazaar/',
70
                         service.service_url)
71
72
    def test_unknown_service(self):
73
        error = self.assertRaises(InvalidLaunchpadInstance,
74
                                  LaunchpadService,
75
                                  lp_instance='fubar')
76
        self.assertEqual('fubar is not a valid Launchpad instance.',
77
                         str(error))
78
79
    def test_environment_overrides_default(self):
80
        os.environ['BZR_LP_XMLRPC_URL'] = 'http://example.com/'
81
        service = LaunchpadService()
82
        self.assertEqual('http://example.com/',
83
                         service.service_url)
84
85
    def test_environment_overrides_specified_service(self):
86
        os.environ['BZR_LP_XMLRPC_URL'] = 'http://example.com/'
87
        service = LaunchpadService(lp_instance='staging')
88
        self.assertEqual('http://example.com/',
89
                         service.service_url)
3955.3.1 by Jonathan Lange
Start doing URL stuff, extracting the domain bit out of LaunchpadService,
90
91
92
class TestURLInference(TestCase):
93
    """Test the way we infer Launchpad web pages from branch URLs."""
94
95
    def test_default_bzr_ssh_url(self):
96
        service = LaunchpadService()
97
        web_url = service.get_web_url_from_branch_url(
98
            'bzr+ssh://bazaar.launchpad.net/~foo/bar/baz')
99
        self.assertEqual(
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
100
            'https://code.edge.launchpad.net/~foo/bar/baz', web_url)
3955.3.1 by Jonathan Lange
Start doing URL stuff, extracting the domain bit out of LaunchpadService,
101
102
    def test_product_bzr_ssh_url(self):
103
        service = LaunchpadService(lp_instance='production')
104
        web_url = service.get_web_url_from_branch_url(
105
            'bzr+ssh://bazaar.launchpad.net/~foo/bar/baz')
106
        self.assertEqual(
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
107
            'https://code.launchpad.net/~foo/bar/baz', web_url)
3955.3.1 by Jonathan Lange
Start doing URL stuff, extracting the domain bit out of LaunchpadService,
108
3955.3.3 by Jonathan Lange
Test a couple more cases.
109
    def test_sftp_branch_url(self):
110
        service = LaunchpadService(lp_instance='production')
111
        web_url = service.get_web_url_from_branch_url(
112
            'sftp://bazaar.launchpad.net/~foo/bar/baz')
113
        self.assertEqual(
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
114
            'https://code.launchpad.net/~foo/bar/baz', web_url)
3955.3.3 by Jonathan Lange
Test a couple more cases.
115
116
    def test_staging_branch_url(self):
117
        service = LaunchpadService(lp_instance='production')
118
        web_url = service.get_web_url_from_branch_url(
119
            'bzr+ssh://bazaar.staging.launchpad.net/~foo/bar/baz')
120
        self.assertEqual(
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
121
            'https://code.launchpad.net/~foo/bar/baz', web_url)
3955.3.3 by Jonathan Lange
Test a couple more cases.
122
3955.3.4 by Jonathan Lange
Some error cases, plus a docstring.
123
    def test_non_launchpad_url(self):
124
        service = LaunchpadService()
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
125
        error = self.assertRaises(
126
            NotLaunchpadBranch, service.get_web_url_from_branch_url,
3955.3.4 by Jonathan Lange
Some error cases, plus a docstring.
127
            'bzr+ssh://example.com/~foo/bar/baz')
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
128
        self.assertEqual(
4031.2.11 by John Arbash Meinel
Turn a bunch of imports into lazy imports.
129
            'bzr+ssh://example.com/~foo/bar/baz is not registered on Launchpad.',
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
130
            str(error))
3955.3.4 by Jonathan Lange
Some error cases, plus a docstring.
131
132
    def test_dodgy_launchpad_url(self):
133
        service = LaunchpadService()
134
        self.assertRaises(
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
135
            NotLaunchpadBranch, service.get_web_url_from_branch_url,
3955.3.4 by Jonathan Lange
Some error cases, plus a docstring.
136
            'bzr+ssh://launchpad.net/~foo/bar/baz')
137
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
138
    def test_lp_branch_url(self):
139
        service = LaunchpadService(lp_instance='production')
3955.3.8 by Jonathan Lange
Support lp URL shortcuts.
140
        factory = FakeResolveFactory(
141
            self, '~foo/bar/baz',
142
            dict(urls=['http://bazaar.launchpad.net/~foo/bar/baz']))
143
        web_url = service.get_web_url_from_branch_url(
144
            'lp:~foo/bar/baz', factory)
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
145
        self.assertEqual(
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
146
            'https://code.launchpad.net/~foo/bar/baz', web_url)
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
147
3955.3.8 by Jonathan Lange
Support lp URL shortcuts.
148
    def test_lp_branch_shortcut(self):
149
        service = LaunchpadService()
150
        factory = FakeResolveFactory(
151
            self, 'foo',
152
            dict(urls=['http://bazaar.launchpad.net/~foo/bar/baz']))
153
        web_url = service.get_web_url_from_branch_url('lp:foo', factory)
154
        self.assertEqual(
155
            'https://code.edge.launchpad.net/~foo/bar/baz', web_url)
156
3955.3.9 by Jonathan Lange
Catch errors.
157
    def test_lp_branch_fault(self):
158
        service = LaunchpadService()
159
        factory = FakeResolveFactory(self, 'foo', None)
160
        def submit(service):
161
            raise xmlrpclib.Fault(42, 'something went wrong')
162
        factory.submit = submit
163
        self.assertRaises(
164
            errors.InvalidURL, service.get_web_url_from_branch_url, 'lp:foo',
165
            factory)
166
3955.3.1 by Jonathan Lange
Start doing URL stuff, extracting the domain bit out of LaunchpadService,
167
    def test_staging_url(self):
168
        service = LaunchpadService(lp_instance='staging')
169
        web_url = service.get_web_url_from_branch_url(
170
            'bzr+ssh://bazaar.launchpad.net/~foo/bar/baz')
171
        self.assertEqual(
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
172
            'https://code.staging.launchpad.net/~foo/bar/baz', web_url)
3955.3.1 by Jonathan Lange
Start doing URL stuff, extracting the domain bit out of LaunchpadService,
173
174
    def test_edge_url(self):
175
        service = LaunchpadService(lp_instance='edge')
176
        web_url = service.get_web_url_from_branch_url(
177
            'bzr+ssh://bazaar.launchpad.net/~foo/bar/baz')
178
        self.assertEqual(
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
179
            'https://code.edge.launchpad.net/~foo/bar/baz', web_url)
3955.3.1 by Jonathan Lange
Start doing URL stuff, extracting the domain bit out of LaunchpadService,
180
181
    def test_dev_url(self):
182
        service = LaunchpadService(lp_instance='dev')
183
        web_url = service.get_web_url_from_branch_url(
184
            'bzr+ssh://bazaar.launchpad.net/~foo/bar/baz')
185
        self.assertEqual(
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
186
            'https://code.launchpad.dev/~foo/bar/baz', web_url)
3955.3.1 by Jonathan Lange
Start doing URL stuff, extracting the domain bit out of LaunchpadService,
187
188
    def test_demo_url(self):
189
        service = LaunchpadService(lp_instance='demo')
190
        web_url = service.get_web_url_from_branch_url(
191
            'bzr+ssh://bazaar.launchpad.net/~foo/bar/baz')
192
        self.assertEqual(
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
193
            'https://code.demo.launchpad.net/~foo/bar/baz', web_url)