~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/codehosting/puller/tests/test_scheduler.py

  • Committer: Jelmer Vernooij
  • Date: 2011-09-21 14:28:02 UTC
  • mfrom: (14006 devel)
  • mto: This revision was merged to the branch mainline in revision 14010.
  • Revision ID: jelmer@canonical.com-20110921142802-7ggkc204igsy532w
MergeĀ lp:launchpad

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2009-2010 Canonical Ltd.  This software is licensed under the
 
1
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
3
 
4
4
# pylint: disable-msg=W0222,W0231
5
5
 
6
6
__metaclass__ = type
7
7
 
8
 
from datetime import datetime
9
8
import logging
10
9
import os
11
10
import textwrap
16
15
    format_registry,
17
16
    )
18
17
from bzrlib.urlutils import join as urljoin
19
 
import pytz
20
18
from testtools.deferredruntest import (
21
19
    assert_fails_with,
22
20
    AsynchronousDeferredRunTest,
62
60
    def callRemote(self, method_name, *args):
63
61
        method = getattr(self, '_remote_%s' % method_name, self._default)
64
62
        deferred = method(*args)
 
63
 
65
64
        def append_to_log(pass_through):
66
65
            self.calls.append((method_name,) + tuple(args))
67
66
            return pass_through
 
67
 
68
68
        deferred.addCallback(append_to_log)
69
69
        return deferred
70
70
 
140
140
            self.calls.append(('method',) + args)
141
141
 
142
142
        def do_raise(self):
143
 
            return 1/0
 
143
            return 1 / 0
144
144
 
145
145
        def unexpectedError(self, failure):
146
146
            self.failure = failure
406
406
        # attempt to call mirrorFailed().
407
407
 
408
408
        runtime_error_failure = makeFailure(RuntimeError)
 
409
 
409
410
        class FailingMirrorFailedStubPullerListener(self.StubPullerListener):
410
411
            def mirrorFailed(self, message, oops):
411
412
                return runtime_error_failure
 
413
 
412
414
        self.protocol.listener = FailingMirrorFailedStubPullerListener()
413
415
        self.listener = self.protocol.listener
414
416
        self.protocol.errReceived('traceback')
436
438
        """The puller master logs an OOPS when it receives an unexpected
437
439
        error.
438
440
        """
439
 
        now = datetime.now(pytz.timezone('UTC'))
440
441
        fail = makeFailure(RuntimeError, 'error message')
441
442
        self.eventHandler.unexpectedError(fail)
442
443
        oops = self.oopses[-1]
565
566
        deferred = self.eventHandler.run()
566
567
        # Fake a successful run.
567
568
        deferred.callback(None)
 
569
 
568
570
        def check_available_prefixes(ignored):
569
571
            self.assertEqual(self.available_oops_prefixes, set(['foo']))
 
572
 
570
573
        return deferred.addCallback(check_available_prefixes)
571
574
 
572
575
    def test_restoresOopsPrefixToSetOnFailure(self):
579
582
        except RuntimeError:
580
583
            fail = failure.Failure()
581
584
        deferred.errback(fail)
 
585
 
582
586
        def check_available_prefixes(ignored):
583
587
            self.assertEqual(self.available_oops_prefixes, set(['foo']))
 
588
 
584
589
        return deferred.addErrback(check_available_prefixes)
585
590
 
586
591
    def test_logOopsWhenNoAvailablePrefix(self):
591
596
        self.available_oops_prefixes.clear()
592
597
 
593
598
        unexpected_errors = []
 
599
 
594
600
        def unexpectedError(failure):
595
601
            unexpected_errors.append(failure)
 
602
 
596
603
        self.eventHandler.unexpectedError = unexpectedError
597
604
        self.assertRaises(KeyError, self.eventHandler.run)
598
605
        self.assertEqual(unexpected_errors[0].type, KeyError)
717
724
 
718
725
        old_oops_raising = errorlog.globalErrorUtility.raising
719
726
        errorlog.globalErrorUtility.raising = new_oops_raising
 
727
 
720
728
        def restore_oops():
721
729
            errorlog.globalErrorUtility.raising = old_oops_raising
 
730
 
722
731
        self.addCleanup(restore_oops)
723
732
 
724
733
        expected_output = 'foo\nbar'
756
765
                """Record the lock id on the listener."""
757
766
                self.listener.lock_ids.append(id)
758
767
 
759
 
 
760
768
        class PullerMasterWithLockID(scheduler.PullerMaster):
761
769
            """A subclass of PullerMaster that allows recording of lock ids.
762
770
            """
887
895
 
888
896
        # We need to create a branch at the destination_url, so that the
889
897
        # subprocess can actually create a lock.
890
 
        BzrDir.create_branch_convenience(locking_puller_master.destination_url)
 
898
        BzrDir.create_branch_convenience(
 
899
            locking_puller_master.destination_url)
891
900
 
892
901
        # Because when the deferred returned by 'func' is done we kill the
893
902
        # locking subprocess, we know that when the subprocess is done, the
944
953
            puller_master = self.makePullerMaster(
945
954
                script_text=lower_timeout_script)
946
955
            deferred = puller_master.mirror()
 
956
 
947
957
            def check_mirror_failed(ignored):
948
958
                self.assertEqual(len(self.client.calls), 1)
949
959
                mirror_failed_call = self.client.calls[0]
953
963
                self.assertTrue(
954
964
                    "Could not acquire lock" in mirror_failed_call[2])
955
965
                return ignored
 
966
 
956
967
            deferred.addCallback(check_mirror_failed)
957
968
            return deferred
958
969