/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5050.79.2 by John Arbash Meinel
Start refactoring lp_api_lite and making it testable.
1
# Copyright (C) 2011 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
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
"""Tools for dealing with the Launchpad API without using launchpadlib.
18
"""
19
20
import socket
21
22
from bzrlib import tests
23
from bzrlib.plugins.launchpad import lp_api_lite
24
5050.79.4 by John Arbash Meinel
Put several tests behind a Feature object.
25
class _JSONParserFeature(tests.Feature):
26
27
    def _probe(self):
28
        return lp_api_lite.json is not None
29
30
    def feature_name(self):
31
        return 'simplejson or json'
32
33
JSONParserFeature = _JSONParserFeature()
34
5050.79.2 by John Arbash Meinel
Start refactoring lp_api_lite and making it testable.
35
_example_response = r"""
36
{
37
    "total_size": 2,
38
    "start": 0,
39
    "next_collection_link": "https://api.launchpad.net/1.0/ubuntu/+archive/primary?distro_series=%2Fubuntu%2Flucid&exact_match=true&source_name=%22bzr%22&status=Published&ws.op=getPublishedSources&ws.start=1&ws.size=1",
40
    "entries": [
41
        {
42
            "package_creator_link": "https://api.launchpad.net/1.0/~maxb",
43
            "package_signer_link": "https://api.launchpad.net/1.0/~jelmer",
44
            "source_package_name": "bzr",
45
            "removal_comment": null,
46
            "display_name": "bzr 2.1.4-0ubuntu1 in lucid",
47
            "date_made_pending": null,
48
            "source_package_version": "2.1.4-0ubuntu1",
49
            "date_superseded": null,
50
            "http_etag": "\"9ba966152dec474dc0fe1629d0bbce2452efaf3b-5f4c3fbb3eaf26d502db4089777a9b6a0537ffab\"",
51
            "self_link": "https://api.launchpad.net/1.0/ubuntu/+archive/primary/+sourcepub/1750327",
52
            "distro_series_link": "https://api.launchpad.net/1.0/ubuntu/lucid",
53
            "component_name": "main",
54
            "status": "Published",
55
            "date_removed": null,
56
            "pocket": "Updates",
57
            "date_published": "2011-05-30T06:09:58.653984+00:00",
58
            "removed_by_link": null,
59
            "section_name": "devel",
60
            "resource_type_link": "https://api.launchpad.net/1.0/#source_package_publishing_history",
61
            "archive_link": "https://api.launchpad.net/1.0/ubuntu/+archive/primary",
62
            "package_maintainer_link": "https://api.launchpad.net/1.0/~ubuntu-devel-discuss-lists",
63
            "date_created": "2011-05-30T05:19:12.233621+00:00",
64
            "scheduled_deletion_date": null
65
        }
66
    ]
67
}"""
68
5050.79.3 by John Arbash Meinel
All the bits are now hooked up. The slow tests are disabled,
69
_no_versions_response = '{"total_size": 0, "start": 0, "entries": []}'
70
71
5050.79.2 by John Arbash Meinel
Start refactoring lp_api_lite and making it testable.
72
class TestLatestPublication(tests.TestCase):
73
74
    def make_latest_publication(self, archive='ubuntu', series='natty',
75
                                project='bzr'):
76
        return lp_api_lite.LatestPublication(archive, series, project)
77
78
    def test_init(self):
79
        latest_pub = self.make_latest_publication()
80
        self.assertEqual('ubuntu', latest_pub._archive)
81
        self.assertEqual('natty', latest_pub._series)
82
        self.assertEqual('bzr', latest_pub._project)
5050.79.8 by John Arbash Meinel
We should supply pocket=Release when none is supplied.
83
        self.assertEqual('Release', latest_pub._pocket)
5050.79.2 by John Arbash Meinel
Start refactoring lp_api_lite and making it testable.
84
85
    def test__archive_URL(self):
86
        latest_pub = self.make_latest_publication()
87
        self.assertEqual(
88
            'https://api.launchpad.net/1.0/ubuntu/+archive/primary',
89
            latest_pub._archive_URL())
90
91
    def test__publication_status_for_ubuntu(self):
92
        latest_pub = self.make_latest_publication()
93
        self.assertEqual('Published', latest_pub._publication_status())
94
95
    def test__publication_status_for_debian(self):
96
        latest_pub = self.make_latest_publication(archive='debian')
97
        self.assertEqual('Pending', latest_pub._publication_status())
98
99
    def test_pocket(self):
100
        latest_pub = self.make_latest_publication(series='natty-proposed')
101
        self.assertEqual('natty', latest_pub._series)
102
        self.assertEqual('Proposed', latest_pub._pocket)
103
104
    def test_series_None(self):
105
        latest_pub = self.make_latest_publication(series=None)
106
        self.assertEqual('ubuntu', latest_pub._archive)
107
        self.assertEqual(None, latest_pub._series)
108
        self.assertEqual('bzr', latest_pub._project)
5050.79.8 by John Arbash Meinel
We should supply pocket=Release when none is supplied.
109
        self.assertEqual('Release', latest_pub._pocket)
5050.79.2 by John Arbash Meinel
Start refactoring lp_api_lite and making it testable.
110
111
    def test__query_params(self):
112
        latest_pub = self.make_latest_publication()
113
        self.assertEqual({'ws.op': 'getPublishedSources',
114
                          'exact_match': 'true',
115
                          'source_name': '"bzr"',
116
                          'status': 'Published',
117
                          'ws.size': '1',
118
                          'distro_series': '/ubuntu/natty',
5050.79.8 by John Arbash Meinel
We should supply pocket=Release when none is supplied.
119
                          'pocket': 'Release',
5050.79.2 by John Arbash Meinel
Start refactoring lp_api_lite and making it testable.
120
                         }, latest_pub._query_params())
121
122
    def test__query_params_no_series(self):
123
        latest_pub = self.make_latest_publication(series=None)
124
        self.assertEqual({'ws.op': 'getPublishedSources',
125
                          'exact_match': 'true',
126
                          'source_name': '"bzr"',
127
                          'status': 'Published',
128
                          'ws.size': '1',
5050.79.8 by John Arbash Meinel
We should supply pocket=Release when none is supplied.
129
                          'pocket': 'Release',
5050.79.2 by John Arbash Meinel
Start refactoring lp_api_lite and making it testable.
130
                         }, latest_pub._query_params())
131
132
    def test__query_params_pocket(self):
133
        latest_pub = self.make_latest_publication(series='natty-proposed')
134
        self.assertEqual({'ws.op': 'getPublishedSources',
135
                          'exact_match': 'true',
136
                          'source_name': '"bzr"',
137
                          'status': 'Published',
138
                          'ws.size': '1',
139
                          'distro_series': '/ubuntu/natty',
140
                          'pocket': 'Proposed',
141
                         }, latest_pub._query_params())
142
143
    def test__query_URL(self):
144
        latest_pub = self.make_latest_publication()
145
        # we explicitly sort params, so we can be sure this URL matches exactly
146
        self.assertEqual(
147
            'https://api.launchpad.net/1.0/ubuntu/+archive/primary'
148
            '?distro_series=%2Fubuntu%2Fnatty&exact_match=true'
5050.79.8 by John Arbash Meinel
We should supply pocket=Release when none is supplied.
149
            '&pocket=Release&source_name=%22bzr%22&status=Published'
5050.79.2 by John Arbash Meinel
Start refactoring lp_api_lite and making it testable.
150
            '&ws.op=getPublishedSources&ws.size=1',
151
            latest_pub._query_URL())
152
153
    def DONT_test__gracefully_handle_failed_rpc_connection(self):
154
        # TODO: This test kind of sucks. We intentionally create an arbitrary
155
        #       port and don't listen to it, because we want the request to fail.
156
        #       However, it seems to take 1s for it to timeout. Is there a way
157
        #       to make it fail faster?
158
        latest_pub = self.make_latest_publication()
159
        s = socket.socket()
160
        s.bind(('127.0.0.1', 0))
161
        addr, port = s.getsockname()
162
        latest_pub.LP_API_ROOT = 'http://%s:%s/' % (addr, port)
163
        s.close()
164
        self.assertIs(None, latest_pub._get_lp_info())
165
5050.79.3 by John Arbash Meinel
All the bits are now hooked up. The slow tests are disabled,
166
    def DONT_test__query_launchpad(self):
5050.79.2 by John Arbash Meinel
Start refactoring lp_api_lite and making it testable.
167
        # TODO: This is a test that we are making a valid request against
168
        #       launchpad. This seems important, but it is slow, requires net
169
        #       access, and requires launchpad to be up and running. So for
170
        #       now, it is commented out for production tests.
171
        latest_pub = self.make_latest_publication()
172
        json_txt = latest_pub._get_lp_info()
173
        self.assertIsNot(None, json_txt)
174
        if lp_api_lite.json is None:
175
            # We don't have a way to parse the text
176
            return
177
        # The content should be a valid json result
178
        content = lp_api_lite.json.loads(json_txt)
179
        entries = content['entries'] # It should have an 'entries' field.
180
        # ws.size should mean we get 0 or 1, and there should be something
181
        self.assertEqual(1, len(entries))
182
        entry = entries[0]
183
        self.assertEqual('bzr', entry['source_package_name'])
184
        version = entry['source_package_version']
185
        self.assertIsNot(None, version)
5050.79.3 by John Arbash Meinel
All the bits are now hooked up. The slow tests are disabled,
186
187
    def test__get_lp_info_no_json(self):
188
        # If we can't parse the json, we don't make the query.
189
        self.overrideAttr(lp_api_lite, 'json', None)
190
        latest_pub = self.make_latest_publication()
191
        self.assertIs(None, latest_pub._get_lp_info())
192
193
    def test__parse_json_info_no_module(self):
194
        # If a json parsing module isn't available, we just return None here.
195
        self.overrideAttr(lp_api_lite, 'json', None)
196
        latest_pub = self.make_latest_publication()
197
        self.assertIs(None, latest_pub._parse_json_info(_example_response))
198
199
    def test__parse_json_example_response(self):
5050.79.4 by John Arbash Meinel
Put several tests behind a Feature object.
200
        self.requireFeature(JSONParserFeature)
5050.79.3 by John Arbash Meinel
All the bits are now hooked up. The slow tests are disabled,
201
        latest_pub = self.make_latest_publication()
202
        content = latest_pub._parse_json_info(_example_response)
203
        self.assertIsNot(None, content)
204
        self.assertEqual(2, content['total_size'])
205
        entries = content['entries']
206
        self.assertEqual(1, len(entries))
207
        entry = entries[0]
208
        self.assertEqual('bzr', entry['source_package_name'])
209
        self.assertEqual("2.1.4-0ubuntu1", entry["source_package_version"])
210
5050.79.4 by John Arbash Meinel
Put several tests behind a Feature object.
211
    def test__parse_json_not_json(self):
212
        self.requireFeature(JSONParserFeature)
213
        latest_pub = self.make_latest_publication()
214
        self.assertIs(None, latest_pub._parse_json_info('Not_valid_json'))
215
5050.79.3 by John Arbash Meinel
All the bits are now hooked up. The slow tests are disabled,
216
    def test_get_latest_version_no_response(self):
217
        latest_pub = self.make_latest_publication()
218
        latest_pub._get_lp_info = lambda: None
219
        self.assertEqual(None, latest_pub.get_latest_version())
220
221
    def test_get_latest_version_no_json(self):
222
        self.overrideAttr(lp_api_lite, 'json', None)
223
        latest_pub = self.make_latest_publication()
224
        self.assertEqual(None, latest_pub.get_latest_version())
225
5050.79.4 by John Arbash Meinel
Put several tests behind a Feature object.
226
    def test_get_latest_version_invalid_json(self):
227
        self.requireFeature(JSONParserFeature)
228
        latest_pub = self.make_latest_publication()
229
        latest_pub._get_lp_info = lambda: "not json"
230
        self.assertEqual(None, latest_pub.get_latest_version())
231
5050.79.3 by John Arbash Meinel
All the bits are now hooked up. The slow tests are disabled,
232
    def test_get_latest_version_no_versions(self):
5050.79.4 by John Arbash Meinel
Put several tests behind a Feature object.
233
        self.requireFeature(JSONParserFeature)
5050.79.3 by John Arbash Meinel
All the bits are now hooked up. The slow tests are disabled,
234
        latest_pub = self.make_latest_publication()
235
        latest_pub._get_lp_info = lambda: _no_versions_response
236
        self.assertEqual(None, latest_pub.get_latest_version())
237
5050.79.4 by John Arbash Meinel
Put several tests behind a Feature object.
238
    def test_get_latest_version_missing_entries(self):
239
        # Launchpad's no-entries response does have an empty entries value.
240
        # However, lets test that we handle other failures without tracebacks
241
        self.requireFeature(JSONParserFeature)
242
        latest_pub = self.make_latest_publication()
243
        latest_pub._get_lp_info = lambda: '{}'
244
        self.assertEqual(None, latest_pub.get_latest_version())
245
246
    def test_get_latest_version_invalid_entries(self):
247
        # Make sure we sanely handle a json response we don't understand
248
        self.requireFeature(JSONParserFeature)
249
        latest_pub = self.make_latest_publication()
250
        latest_pub._get_lp_info = lambda: '{"entries": {"a": 1}}'
251
        self.assertEqual(None, latest_pub.get_latest_version())
252
5050.79.3 by John Arbash Meinel
All the bits are now hooked up. The slow tests are disabled,
253
    def test_get_latest_version_example(self):
5050.79.4 by John Arbash Meinel
Put several tests behind a Feature object.
254
        self.requireFeature(JSONParserFeature)
5050.79.3 by John Arbash Meinel
All the bits are now hooked up. The slow tests are disabled,
255
        latest_pub = self.make_latest_publication()
256
        latest_pub._get_lp_info = lambda: _example_response
257
        self.assertEqual("2.1.4-0ubuntu1", latest_pub.get_latest_version())
258
259
    def DONT_test_get_latest_version_from_launchpad(self):
5050.79.4 by John Arbash Meinel
Put several tests behind a Feature object.
260
        self.requireFeature(JSONParserFeature)
5050.79.3 by John Arbash Meinel
All the bits are now hooked up. The slow tests are disabled,
261
        latest_pub = self.make_latest_publication()
262
        self.assertIsNot(None, latest_pub.get_latest_version())