/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/plugins/launchpad/test_lp_api_lite.py

  • Committer: Martin Pool
  • Date: 2011-07-25 02:51:30 UTC
  • mfrom: (6042 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6043.
  • Revision ID: mbp@canonical.com-20110725025130-s0j1lxfw8wljp420
resolve conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""Tools for dealing with the Launchpad API without using launchpadlib.
18
18
"""
19
19
 
 
20
import doctest
20
21
import socket
21
22
 
22
23
from bzrlib import tests
23
24
from bzrlib.tests import features
24
25
from bzrlib.plugins import launchpad
25
26
from bzrlib.plugins.launchpad import lp_api_lite
 
27
from testtools.matchers import DocTestMatches
26
28
 
27
29
 
28
30
class _JSONParserFeature(features.Feature):
80
82
                                project='bzr'):
81
83
        return lp_api_lite.LatestPublication(archive, series, project)
82
84
 
 
85
    def assertPlace(self, place, archive, series, project):
 
86
        lp = lp_api_lite.LatestPublication(archive, series, project)
 
87
        self.assertEqual(place, lp.place())
 
88
 
83
89
    def test_init(self):
84
90
        latest_pub = self.make_latest_publication()
85
91
        self.assertEqual('ubuntu', latest_pub._archive)
181
187
            return
182
188
        # The content should be a valid json result
183
189
        content = lp_api_lite.json.loads(json_txt)
184
 
        entries = content['entries'] # It should have an 'entries' field.
 
190
        entries = content['entries']  # It should have an 'entries' field.
185
191
        # ws.size should mean we get 0 or 1, and there should be something
186
192
        self.assertEqual(1, len(entries))
187
193
        entry = entries[0]
266
272
        latest_pub = self.make_latest_publication()
267
273
        self.assertIsNot(None, latest_pub.get_latest_version())
268
274
 
 
275
    def test_place(self):
 
276
        self.assertPlace('Ubuntu', 'ubuntu', None, 'bzr')
 
277
        self.assertPlace('Ubuntu Natty', 'ubuntu', 'natty', 'bzr')
 
278
        self.assertPlace('Ubuntu Natty Proposed', 'ubuntu', 'natty-proposed',
 
279
                         'bzr')
 
280
        self.assertPlace('Debian', 'debian', None, 'bzr')
 
281
        self.assertPlace('Debian Sid', 'debian', 'sid', 'bzr')
 
282
 
269
283
 
270
284
class TestIsUpToDate(tests.TestCase):
271
285
 
352
366
        self.assertEqual('B', b.last_revision())
353
367
        self.assertEqual('tip-1.0',
354
368
                         lp_api_lite.get_most_recent_tag(tag_dict, b))
 
369
 
 
370
 
 
371
class StubLatestPublication(object):
 
372
 
 
373
    def __init__(self, latest):
 
374
        self.called = False
 
375
        self.latest = latest
 
376
 
 
377
    def get_latest_version(self):
 
378
        self.called = True
 
379
        return self.latest
 
380
 
 
381
    def place(self):
 
382
        return 'Ubuntu Natty'
 
383
 
 
384
 
 
385
class TestReportFreshness(tests.TestCaseWithMemoryTransport):
 
386
 
 
387
    def setUp(self):
 
388
        super(TestReportFreshness, self).setUp()
 
389
        builder = self.make_branch_builder('tip')
 
390
        builder.build_snapshot('A', [], [
 
391
            ('add', ('', 'root-id', 'directory', None))])
 
392
        self.branch = builder.get_branch()
 
393
 
 
394
    def assertFreshnessReports(self, verbosity, latest_version, content):
 
395
        """Assert that lp_api_lite.report_freshness reports the given content.
 
396
 
 
397
        :param verbosity: The reporting level
 
398
        :param latest_version: The version reported by StubLatestPublication
 
399
        :param content: The expected content. This should be in DocTest form.
 
400
        """
 
401
        orig_log_len = len(self.get_log())
 
402
        lp_api_lite.report_freshness(self.branch, verbosity,
 
403
            StubLatestPublication(latest_version))
 
404
        new_content = self.get_log()[orig_log_len:]
 
405
        # Strip out lines that have LatestPublication.get_* because those are
 
406
        # timing related lines. While interesting to log for now, they aren't
 
407
        # something we want to be testing
 
408
        new_content = new_content.split('\n')
 
409
        for i in range(2):
 
410
            if (len(new_content) > 0
 
411
                and 'LatestPublication.get_' in new_content[0]):
 
412
                new_content = new_content[1:]
 
413
        new_content = '\n'.join(new_content)
 
414
        self.assertThat(new_content,
 
415
            DocTestMatches(content,
 
416
                doctest.ELLIPSIS | doctest.REPORT_UDIFF))
 
417
 
 
418
    def test_verbosity_off_skips_check(self):
 
419
        # We force _get_package_branch_info so that we know it would otherwise
 
420
        # try to connect to launcphad
 
421
        self.overrideAttr(launchpad, '_get_package_branch_info',
 
422
            lambda x: ('ubuntu', 'natty', 'bzr'))
 
423
        self.overrideAttr(lp_api_lite, 'LatestPublication',
 
424
            lambda *args: self.fail('Tried to query launchpad'))
 
425
        c = self.branch.get_config()
 
426
        c.set_user_option('launchpad.packaging_verbosity', 'off')
 
427
        orig_log_len = len(self.get_log())
 
428
        launchpad._check_is_up_to_date(self.branch)
 
429
        new_content = self.get_log()[orig_log_len:]
 
430
        self.assertContainsRe(new_content,
 
431
            'not checking memory.*/tip/ because verbosity is turned off')
 
432
 
 
433
    def test_verbosity_off(self):
 
434
        latest_pub = StubLatestPublication('1.0-1ubuntu2')
 
435
        lp_api_lite.report_freshness(self.branch, 'off', latest_pub)
 
436
        self.assertFalse(latest_pub.called)
 
437
 
 
438
    def test_verbosity_all_out_of_date_smoke(self):
 
439
        self.branch.tags.set_tag('1.0-1ubuntu1', 'A')
 
440
        self.assertFreshnessReports('all', '1.0-1ubuntu2',
 
441
             '    INFO  Most recent Ubuntu Natty version: 1.0-1ubuntu2\n'
 
442
             'Packaging branch version: 1.0-1ubuntu1\n'
 
443
             'Packaging branch status: OUT-OF-DATE\n')
 
444
 
 
445
 
 
446
class Test_GetNewestVersions(tests.TestCaseWithMemoryTransport):
 
447
 
 
448
    def setUp(self):
 
449
        super(Test_GetNewestVersions, self).setUp()
 
450
        builder = self.make_branch_builder('tip')
 
451
        builder.build_snapshot('A', [], [
 
452
            ('add', ('', 'root-id', 'directory', None))])
 
453
        self.branch = builder.get_branch()
 
454
 
 
455
    def assertLatestVersions(self, latest_branch_version, pub_version):
 
456
        if latest_branch_version is not None:
 
457
            self.branch.tags.set_tag(latest_branch_version, 'A')
 
458
        latest_pub = StubLatestPublication(pub_version)
 
459
        self.assertEqual((pub_version, latest_branch_version),
 
460
            lp_api_lite._get_newest_versions(self.branch, latest_pub))
 
461
 
 
462
    def test_no_tags(self):
 
463
        self.assertLatestVersions(None, '1.0-1ubuntu2')
 
464
 
 
465
    def test_out_of_date(self):
 
466
        self.assertLatestVersions('1.0-1ubuntu1', '1.0-1ubuntu2')
 
467
 
 
468
    def test_up_to_date(self):
 
469
        self.assertLatestVersions('1.0-1ubuntu2', '1.0-1ubuntu2')
 
470
 
 
471
    def test_missing(self):
 
472
        self.assertLatestVersions(None, None)
 
473
 
 
474
 
 
475
class Test_ReportFreshness(tests.TestCase):
 
476
 
 
477
    def assertReportedFreshness(self, verbosity, latest_ver, branch_latest_ver,
 
478
                               content, place='Ubuntu Natty'):
 
479
        """Assert that lp_api_lite.report_freshness reports the given content.
 
480
        """
 
481
        reported = []
 
482
        def report_func(value):
 
483
            reported.append(value)
 
484
 
 
485
        lp_api_lite._report_freshness(latest_ver, branch_latest_ver, place,
 
486
                                      verbosity, report_func)
 
487
        new_content = '\n'.join(reported)
 
488
        self.assertThat(new_content,
 
489
            DocTestMatches(content,
 
490
                doctest.ELLIPSIS | doctest.REPORT_UDIFF))
 
491
 
 
492
    def test_verbosity_minimal_no_tags(self):
 
493
        self.assertReportedFreshness('minimal', '1.0-1ubuntu2', None,
 
494
            'Branch is OUT-OF-DATE, Ubuntu Natty has 1.0-1ubuntu2\n')
 
495
 
 
496
    def test_verbosity_minimal_out_of_date(self):
 
497
        self.assertReportedFreshness('minimal', '1.0-1ubuntu2', '1.0-1ubuntu1',
 
498
            '1.0-1ubuntu1 is OUT-OF-DATE,'
 
499
            ' Ubuntu Natty has 1.0-1ubuntu2\n')
 
500
 
 
501
    def test_verbosity_minimal_up_to_date(self):
 
502
        self.assertReportedFreshness('minimal', '1.0-1ubuntu2', '1.0-1ubuntu2',
 
503
             '')
 
504
 
 
505
    def test_verbosity_minimal_missing(self):
 
506
        self.assertReportedFreshness('minimal', None, None,
 
507
             '')
 
508
 
 
509
    def test_verbosity_short_out_of_date(self):
 
510
        self.assertReportedFreshness('short', '1.0-1ubuntu2', '1.0-1ubuntu1',
 
511
            '1.0-1ubuntu1 is OUT-OF-DATE,'
 
512
            ' Ubuntu Natty has 1.0-1ubuntu2\n')
 
513
 
 
514
    def test_verbosity_short_up_to_date(self):
 
515
        self.assertReportedFreshness('short', '1.0-1ubuntu2', '1.0-1ubuntu2',
 
516
             '1.0-1ubuntu2 is CURRENT in Ubuntu Natty')
 
517
 
 
518
    def test_verbosity_short_missing(self):
 
519
        self.assertReportedFreshness('short', None, None,
 
520
             'Ubuntu Natty is MISSING a version')
 
521
 
 
522
    def test_verbosity_all_no_tags(self):
 
523
        self.assertReportedFreshness('all', '1.0-1ubuntu2', None,
 
524
             'Most recent Ubuntu Natty version: 1.0-1ubuntu2\n'
 
525
             'Packaging branch version: None\n'
 
526
             'Packaging branch status: OUT-OF-DATE\n')
 
527
 
 
528
    def test_verbosity_all_out_of_date(self):
 
529
        self.assertReportedFreshness('all', '1.0-1ubuntu2', '1.0-1ubuntu1',
 
530
             'Most recent Ubuntu Natty version: 1.0-1ubuntu2\n'
 
531
             'Packaging branch version: 1.0-1ubuntu1\n'
 
532
             'Packaging branch status: OUT-OF-DATE\n')
 
533
 
 
534
    def test_verbosity_all_up_to_date(self):
 
535
        self.assertReportedFreshness('all', '1.0-1ubuntu2', '1.0-1ubuntu2',
 
536
             'Most recent Ubuntu Natty version: 1.0-1ubuntu2\n'
 
537
             'Packaging branch status: CURRENT\n')
 
538
 
 
539
    def test_verbosity_all_missing(self):
 
540
        self.assertReportedFreshness('all', None, None,
 
541
             'Most recent Ubuntu Natty version: MISSING\n')
 
542
 
 
543
    def test_verbosity_None_is_all(self):
 
544
        self.assertReportedFreshness(None, '1.0-1ubuntu2', '1.0-1ubuntu2',
 
545
             'Most recent Ubuntu Natty version: 1.0-1ubuntu2\n'
 
546
             'Packaging branch status: CURRENT\n')