/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: Canonical.com Patch Queue Manager
  • Date: 2011-07-22 12:51:48 UTC
  • mfrom: (6024.3.12 2.5-verbosity-knob-812928)
  • Revision ID: pqm@pqm.ubuntu.com-20110722125148-wbtsysvbmhy5velq
(jameinel) Bug #812928,
 allow configuring how verbose the package freshness checks are. (John A
 Meinel)

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