~free.ekanayaka/landscape-client/amp-cleanup-7

« back to all changes in this revision

Viewing changes to landscape/tests/helpers.py

  • Committer: Free Ekanayaka
  • Date: 2013-04-15 07:32:54 UTC
  • Revision ID: free.ekanayaka@canonical.com-20130415073254-c3ecmarprf6p5u1w
Drop out-of-date Twisted trial hacks (fixed since Twisted 2.5, in hardy) [trivial] [r=cglass]

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
from logging import Handler, ERROR, Formatter
13
13
from twisted.trial.unittest import TestCase
 
14
from twisted.python.failure import Failure
14
15
from twisted.internet.defer import Deferred
15
16
 
16
17
from landscape.tests.subunit import run_isolated
121
122
            self.fail(
122
123
                "Success result expected on %r, found no result instead" % (
123
124
                    deferred,))
124
 
        elif isinstance(result[0], failure.Failure):
 
125
        elif isinstance(result[0], Failure):
125
126
            self.fail(
126
127
                "Success result expected on %r, "
127
128
                "found failure result (%r) instead" % (deferred, result[0]))
140
141
            self.fail(
141
142
                "Failure result expected on %r, found no result instead" % (
142
143
                    deferred,))
143
 
        elif not isinstance(result[0], failure.Failure):
 
144
        elif not isinstance(result[0], Failure):
144
145
            self.fail(
145
146
                "Failure result expected on %r, "
146
147
                "found success result (%r) instead" % (deferred, result[0]))
638
639
        """Remove sample data for the process that matches C{process_id}."""
639
640
        process_dir = os.path.join(self._sample_dir, str(process_id))
640
641
        shutil.rmtree(process_dir)
641
 
 
642
 
 
643
 
from twisted.python import log
644
 
from twisted.python import failure
645
 
from twisted.trial import reporter
646
 
 
647
 
 
648
 
def install_trial_hack():
649
 
    """
650
 
    Trial's TestCase in Twisted 2.2 had a bug which would prevent
651
 
    certain errors from being reported when being run in a non-trial
652
 
    test runner. This function monkeypatches trial to fix the bug, and
653
 
    only takes effect if using Twisted 2.2.
654
 
    """
655
 
    from twisted.trial.itrial import IReporter
656
 
    if "addError" in IReporter:
657
 
        # We have no need for this monkey patch with newer versions of Twisted.
658
 
        return
659
 
 
660
 
    def run(self, result):
661
 
        """
662
 
        Copied from twisted.trial.unittest.TestCase.run, but some
663
 
        lines from Twisted 2.5.
664
 
        """
665
 
        log.msg("--> %s <--" % (self.id()))
666
 
 
667
 
        # From Twisted 2.5
668
 
        if not isinstance(result, reporter.TestResult):
669
 
            result = PyUnitResultAdapter(result)
670
 
        # End from Twisted 2.5
671
 
 
672
 
        self._timedOut = False
673
 
        if self._shared and self not in self.__class__._instances:
674
 
            self.__class__._instances.add(self)
675
 
        result.startTest(self)
676
 
        if self.getSkip():  # don't run test methods that are marked as .skip
677
 
            result.addSkip(self, self.getSkip())
678
 
            result.stopTest(self)
679
 
            return
680
 
        # From twisted 2.5
681
 
        if hasattr(self, "_installObserver"):
682
 
            self._installObserver()
683
 
        # End from Twisted 2.5
684
 
        self._passed = False
685
 
        first = False
686
 
        if self._shared:
687
 
            first = self._isFirst()
688
 
            self.__class__._instancesRun.add(self)
689
 
        if first:
690
 
            d = self.deferSetUpClass(result)
691
 
        else:
692
 
            d = self.deferSetUp(None, result)
693
 
        try:
694
 
            self._wait(d)
695
 
        finally:
696
 
            self._cleanUp(result)
697
 
            result.stopTest(self)
698
 
            if self._shared and self._isLast():
699
 
                self._initInstances()
700
 
                self._classCleanUp(result)
701
 
            if not self._shared:
702
 
                self._classCleanUp(result)
703
 
    TestCase.run = run
704
 
 
705
 
### Copied from Twisted, to fix a bug in trial in Twisted 2.2! ###
706
 
 
707
 
 
708
 
class UnsupportedTrialFeature(Exception):
709
 
    """A feature of twisted.trial was used that pyunit cannot support."""
710
 
 
711
 
 
712
 
class PyUnitResultAdapter(object):
713
 
    """
714
 
    Wrap a C{TestResult} from the standard library's C{unittest} so that it
715
 
    supports the extended result types from Trial, and also supports
716
 
    L{twisted.python.failure.Failure}s being passed to L{addError} and
717
 
    L{addFailure}.
718
 
 
719
 
    @param original: A C{TestResult} instance from C{unittest}.
720
 
    """
721
 
 
722
 
    def __init__(self, original):
723
 
        self.original = original
724
 
 
725
 
    def _exc_info(self, err):
726
 
        if isinstance(err, failure.Failure):
727
 
            # Unwrap the Failure into a exc_info tuple.
728
 
            err = (err.type, err.value, err.tb)
729
 
        return err
730
 
 
731
 
    def startTest(self, method):
732
 
        # We'll need this later in cleanupErrors.
733
 
        self.__currentTest = method
734
 
        self.original.startTest(method)
735
 
 
736
 
    def stopTest(self, method):
737
 
        self.original.stopTest(method)
738
 
 
739
 
    def addFailure(self, test, fail):
740
 
        self.original.addFailure(test, self._exc_info(fail))
741
 
 
742
 
    def addError(self, test, error):
743
 
        self.original.addError(test, self._exc_info(error))
744
 
 
745
 
    def _unsupported(self, test, feature, info):
746
 
        self.original.addFailure(
747
 
            test,
748
 
            (UnsupportedTrialFeature,
749
 
             UnsupportedTrialFeature(feature, info),
750
 
             None))
751
 
 
752
 
    def addSkip(self, test, reason):
753
 
        """
754
 
        Report the skip as a failure.
755
 
        """
756
 
        self._unsupported(test, 'skip', reason)
757
 
 
758
 
    def addUnexpectedSuccess(self, test, todo):
759
 
        """
760
 
        Report the unexpected success as a failure.
761
 
        """
762
 
        self._unsupported(test, 'unexpected success', todo)
763
 
 
764
 
    def addExpectedFailure(self, test, error):
765
 
        """
766
 
        Report the expected failure (i.e. todo) as a failure.
767
 
        """
768
 
        self._unsupported(test, 'expected failure', error)
769
 
 
770
 
    def addSuccess(self, test):
771
 
        self.original.addSuccess(test)
772
 
 
773
 
    def upDownError(self, method, error, warn, printStatus):
774
 
        pass
775
 
 
776
 
    def cleanupErrors(self, errs):
777
 
        # Let's consider cleanupErrors as REAL errors. In recent
778
 
        # Twisted this is the default behavior, and cleanupErrors
779
 
        # isn't even called.
780
 
        self.addError(self.__currentTest, errs)
781
 
 
782
 
    def startSuite(self, name):
783
 
        pass
784
 
 
785
 
### END COPY FROM TWISTED ###
786
 
 
787
 
install_trial_hack()