~bzr-pqm/bzr/bzr.dev

5557.1.7 by John Arbash Meinel
Merge in the bzr.dev 5582
1
# Copyright (C) 2008-2011 Canonical Ltd
3221.4.2 by Martin Pool
Add in thumpers tests for selection of the right Launchpad instance
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
5570.3.6 by Vincent Ladeuil
Get rid of all _captureVar() calls, no test failures, pfew.
35
        self.overrideEnv('BZR_LP_XMLRPC_URL', None)
3221.4.2 by Martin Pool
Add in thumpers tests for selection of the right Launchpad instance
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_dev_service(self):
58
        service = LaunchpadService(lp_instance='dev')
3955.3.2 by Jonathan Lange
Tighten up the code a little, changing the dev service to use https,
59
        self.assertEqual('https://xmlrpc.launchpad.dev/bazaar/',
3221.4.2 by Martin Pool
Add in thumpers tests for selection of the right Launchpad instance
60
                         service.service_url)
61
62
    def test_demo_service(self):
63
        service = LaunchpadService(lp_instance='demo')
64
        self.assertEqual('https://xmlrpc.demo.launchpad.net/bazaar/',
65
                         service.service_url)
66
67
    def test_unknown_service(self):
68
        error = self.assertRaises(InvalidLaunchpadInstance,
69
                                  LaunchpadService,
70
                                  lp_instance='fubar')
71
        self.assertEqual('fubar is not a valid Launchpad instance.',
72
                         str(error))
73
74
    def test_environment_overrides_default(self):
75
        os.environ['BZR_LP_XMLRPC_URL'] = 'http://example.com/'
76
        service = LaunchpadService()
77
        self.assertEqual('http://example.com/',
78
                         service.service_url)
79
80
    def test_environment_overrides_specified_service(self):
81
        os.environ['BZR_LP_XMLRPC_URL'] = 'http://example.com/'
82
        service = LaunchpadService(lp_instance='staging')
83
        self.assertEqual('http://example.com/',
84
                         service.service_url)
3955.3.1 by Jonathan Lange
Start doing URL stuff, extracting the domain bit out of LaunchpadService,
85
86
87
class TestURLInference(TestCase):
88
    """Test the way we infer Launchpad web pages from branch URLs."""
89
90
    def test_default_bzr_ssh_url(self):
91
        service = LaunchpadService()
92
        web_url = service.get_web_url_from_branch_url(
93
            'bzr+ssh://bazaar.launchpad.net/~foo/bar/baz')
94
        self.assertEqual(
5243.1.1 by Martin
Use the production server in the launchpad plugin rather than edge
95
            '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,
96
97
    def test_product_bzr_ssh_url(self):
98
        service = LaunchpadService(lp_instance='production')
99
        web_url = service.get_web_url_from_branch_url(
100
            'bzr+ssh://bazaar.launchpad.net/~foo/bar/baz')
101
        self.assertEqual(
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
102
            '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,
103
3955.3.3 by Jonathan Lange
Test a couple more cases.
104
    def test_sftp_branch_url(self):
105
        service = LaunchpadService(lp_instance='production')
106
        web_url = service.get_web_url_from_branch_url(
107
            'sftp://bazaar.launchpad.net/~foo/bar/baz')
108
        self.assertEqual(
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
109
            'https://code.launchpad.net/~foo/bar/baz', web_url)
3955.3.3 by Jonathan Lange
Test a couple more cases.
110
111
    def test_staging_branch_url(self):
112
        service = LaunchpadService(lp_instance='production')
113
        web_url = service.get_web_url_from_branch_url(
114
            'bzr+ssh://bazaar.staging.launchpad.net/~foo/bar/baz')
115
        self.assertEqual(
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
116
            'https://code.launchpad.net/~foo/bar/baz', web_url)
3955.3.3 by Jonathan Lange
Test a couple more cases.
117
3955.3.4 by Jonathan Lange
Some error cases, plus a docstring.
118
    def test_non_launchpad_url(self):
119
        service = LaunchpadService()
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
120
        error = self.assertRaises(
121
            NotLaunchpadBranch, service.get_web_url_from_branch_url,
3955.3.4 by Jonathan Lange
Some error cases, plus a docstring.
122
            'bzr+ssh://example.com/~foo/bar/baz')
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
123
        self.assertEqual(
4031.2.11 by John Arbash Meinel
Turn a bunch of imports into lazy imports.
124
            '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.
125
            str(error))
3955.3.4 by Jonathan Lange
Some error cases, plus a docstring.
126
127
    def test_dodgy_launchpad_url(self):
128
        service = LaunchpadService()
129
        self.assertRaises(
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
130
            NotLaunchpadBranch, service.get_web_url_from_branch_url,
3955.3.4 by Jonathan Lange
Some error cases, plus a docstring.
131
            'bzr+ssh://launchpad.net/~foo/bar/baz')
132
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
133
    def test_lp_branch_url(self):
134
        service = LaunchpadService(lp_instance='production')
3955.3.8 by Jonathan Lange
Support lp URL shortcuts.
135
        factory = FakeResolveFactory(
136
            self, '~foo/bar/baz',
137
            dict(urls=['http://bazaar.launchpad.net/~foo/bar/baz']))
138
        web_url = service.get_web_url_from_branch_url(
139
            'lp:~foo/bar/baz', factory)
3955.3.5 by Jonathan Lange
Add an untested plugin, make the error handling a little nicer.
140
        self.assertEqual(
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
141
            '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.
142
3955.3.8 by Jonathan Lange
Support lp URL shortcuts.
143
    def test_lp_branch_shortcut(self):
144
        service = LaunchpadService()
145
        factory = FakeResolveFactory(
146
            self, 'foo',
147
            dict(urls=['http://bazaar.launchpad.net/~foo/bar/baz']))
148
        web_url = service.get_web_url_from_branch_url('lp:foo', factory)
149
        self.assertEqual(
5243.1.1 by Martin
Use the production server in the launchpad plugin rather than edge
150
            'https://code.launchpad.net/~foo/bar/baz', web_url)
3955.3.8 by Jonathan Lange
Support lp URL shortcuts.
151
3955.3.9 by Jonathan Lange
Catch errors.
152
    def test_lp_branch_fault(self):
153
        service = LaunchpadService()
154
        factory = FakeResolveFactory(self, 'foo', None)
155
        def submit(service):
156
            raise xmlrpclib.Fault(42, 'something went wrong')
157
        factory.submit = submit
158
        self.assertRaises(
159
            errors.InvalidURL, service.get_web_url_from_branch_url, 'lp:foo',
160
            factory)
161
3955.3.1 by Jonathan Lange
Start doing URL stuff, extracting the domain bit out of LaunchpadService,
162
    def test_staging_url(self):
163
        service = LaunchpadService(lp_instance='staging')
164
        web_url = service.get_web_url_from_branch_url(
165
            'bzr+ssh://bazaar.launchpad.net/~foo/bar/baz')
166
        self.assertEqual(
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
167
            '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,
168
169
    def test_dev_url(self):
170
        service = LaunchpadService(lp_instance='dev')
171
        web_url = service.get_web_url_from_branch_url(
172
            'bzr+ssh://bazaar.launchpad.net/~foo/bar/baz')
173
        self.assertEqual(
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
174
            '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,
175
176
    def test_demo_url(self):
177
        service = LaunchpadService(lp_instance='demo')
178
        web_url = service.get_web_url_from_branch_url(
179
            'bzr+ssh://bazaar.launchpad.net/~foo/bar/baz')
180
        self.assertEqual(
3955.3.7 by Jonathan Lange
Test the launchpad-open command. Fix up some minor bugs.
181
            'https://code.demo.launchpad.net/~foo/bar/baz', web_url)