~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Copyright (C) 2008 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Tests for selection of the right Launchpad service by environment"""

import os
import xmlrpclib

from bzrlib import errors
from bzrlib.plugins.launchpad.lp_registration import (
    InvalidLaunchpadInstance, LaunchpadService, NotLaunchpadBranch)
from bzrlib.plugins.launchpad.test_lp_directory import FakeResolveFactory
from bzrlib.tests import TestCase


class LaunchpadServiceTests(TestCase):
    """Test that the correct Launchpad instance is chosen."""

    def setUp(self):
        super(LaunchpadServiceTests, self).setUp()
        # make sure we have a reproducible standard environment
        self._captureVar('BZR_LP_XMLRPC_URL', None)

    def test_default_service(self):
        service = LaunchpadService()
        self.assertEqual('https://xmlrpc.launchpad.net/bazaar/',
                         service.service_url)

    def test_alter_default_service_url(self):
        LaunchpadService.DEFAULT_SERVICE_URL = 'http://example.com/'
        try:
            service = LaunchpadService()
            self.assertEqual('http://example.com/',
                             service.service_url)
        finally:
            LaunchpadService.DEFAULT_SERVICE_URL = \
                LaunchpadService.LAUNCHPAD_INSTANCE['production']

    def test_staging_service(self):
        service = LaunchpadService(lp_instance='staging')
        self.assertEqual('https://xmlrpc.staging.launchpad.net/bazaar/',
                         service.service_url)

    def test_dev_service(self):
        service = LaunchpadService(lp_instance='dev')
        self.assertEqual('https://xmlrpc.launchpad.dev/bazaar/',
                         service.service_url)

    def test_demo_service(self):
        service = LaunchpadService(lp_instance='demo')
        self.assertEqual('https://xmlrpc.demo.launchpad.net/bazaar/',
                         service.service_url)

    def test_unknown_service(self):
        error = self.assertRaises(InvalidLaunchpadInstance,
                                  LaunchpadService,
                                  lp_instance='fubar')
        self.assertEqual('fubar is not a valid Launchpad instance.',
                         str(error))

    def test_environment_overrides_default(self):
        os.environ['BZR_LP_XMLRPC_URL'] = 'http://example.com/'
        service = LaunchpadService()
        self.assertEqual('http://example.com/',
                         service.service_url)

    def test_environment_overrides_specified_service(self):
        os.environ['BZR_LP_XMLRPC_URL'] = 'http://example.com/'
        service = LaunchpadService(lp_instance='staging')
        self.assertEqual('http://example.com/',
                         service.service_url)


class TestURLInference(TestCase):
    """Test the way we infer Launchpad web pages from branch URLs."""

    def test_default_bzr_ssh_url(self):
        service = LaunchpadService()
        web_url = service.get_web_url_from_branch_url(
            'bzr+ssh://bazaar.launchpad.net/~foo/bar/baz')
        self.assertEqual(
            'https://code.launchpad.net/~foo/bar/baz', web_url)

    def test_product_bzr_ssh_url(self):
        service = LaunchpadService(lp_instance='production')
        web_url = service.get_web_url_from_branch_url(
            'bzr+ssh://bazaar.launchpad.net/~foo/bar/baz')
        self.assertEqual(
            'https://code.launchpad.net/~foo/bar/baz', web_url)

    def test_sftp_branch_url(self):
        service = LaunchpadService(lp_instance='production')
        web_url = service.get_web_url_from_branch_url(
            'sftp://bazaar.launchpad.net/~foo/bar/baz')
        self.assertEqual(
            'https://code.launchpad.net/~foo/bar/baz', web_url)

    def test_staging_branch_url(self):
        service = LaunchpadService(lp_instance='production')
        web_url = service.get_web_url_from_branch_url(
            'bzr+ssh://bazaar.staging.launchpad.net/~foo/bar/baz')
        self.assertEqual(
            'https://code.launchpad.net/~foo/bar/baz', web_url)

    def test_non_launchpad_url(self):
        service = LaunchpadService()
        error = self.assertRaises(
            NotLaunchpadBranch, service.get_web_url_from_branch_url,
            'bzr+ssh://example.com/~foo/bar/baz')
        self.assertEqual(
            'bzr+ssh://example.com/~foo/bar/baz is not registered on Launchpad.',
            str(error))

    def test_dodgy_launchpad_url(self):
        service = LaunchpadService()
        self.assertRaises(
            NotLaunchpadBranch, service.get_web_url_from_branch_url,
            'bzr+ssh://launchpad.net/~foo/bar/baz')

    def test_lp_branch_url(self):
        service = LaunchpadService(lp_instance='production')
        factory = FakeResolveFactory(
            self, '~foo/bar/baz',
            dict(urls=['http://bazaar.launchpad.net/~foo/bar/baz']))
        web_url = service.get_web_url_from_branch_url(
            'lp:~foo/bar/baz', factory)
        self.assertEqual(
            'https://code.launchpad.net/~foo/bar/baz', web_url)

    def test_lp_branch_shortcut(self):
        service = LaunchpadService()
        factory = FakeResolveFactory(
            self, 'foo',
            dict(urls=['http://bazaar.launchpad.net/~foo/bar/baz']))
        web_url = service.get_web_url_from_branch_url('lp:foo', factory)
        self.assertEqual(
            'https://code.launchpad.net/~foo/bar/baz', web_url)

    def test_lp_branch_fault(self):
        service = LaunchpadService()
        factory = FakeResolveFactory(self, 'foo', None)
        def submit(service):
            raise xmlrpclib.Fault(42, 'something went wrong')
        factory.submit = submit
        self.assertRaises(
            errors.InvalidURL, service.get_web_url_from_branch_url, 'lp:foo',
            factory)

    def test_staging_url(self):
        service = LaunchpadService(lp_instance='staging')
        web_url = service.get_web_url_from_branch_url(
            'bzr+ssh://bazaar.launchpad.net/~foo/bar/baz')
        self.assertEqual(
            'https://code.staging.launchpad.net/~foo/bar/baz', web_url)

    def test_dev_url(self):
        service = LaunchpadService(lp_instance='dev')
        web_url = service.get_web_url_from_branch_url(
            'bzr+ssh://bazaar.launchpad.net/~foo/bar/baz')
        self.assertEqual(
            'https://code.launchpad.dev/~foo/bar/baz', web_url)

    def test_demo_url(self):
        service = LaunchpadService(lp_instance='demo')
        web_url = service.get_web_url_from_branch_url(
            'bzr+ssh://bazaar.launchpad.net/~foo/bar/baz')
        self.assertEqual(
            'https://code.demo.launchpad.net/~foo/bar/baz', web_url)