~ubuntu-branches/ubuntu/precise/landscape-client/precise-proposed

« back to all changes in this revision

Viewing changes to landscape/tests/helpers.py

  • Committer: Package Import Robot
  • Author(s): Christopher Glass (Canonical)
  • Date: 2013-09-20 10:10:28 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130920101028-hldi7uo5wudxurmt
Tags: 13.07.3-0ubuntu0.12.04
* New upstream version (LP: #1190510)
 - New annotations exchange mechanism allows clients to send any key-value
   data to the landscape server (LP: #1123932)
 - Network devices now report their maximum theoretical speeds, and duplex
   status to landscape-server (LP: #1126331, LP: #1130733)
 - Landscape.client is now HA aware when HA is implemented using juju
   charms (LP: #1122508)
 - The landscape client will now trigger a reboot if server sends a
   reboot-required message. (LP: #1133005)
 - Big AMP code cleanup and refactoring in order to improve testing, improve
   performance and ease future maintainability (LP: #1165047, LP: #1169102,
   LP: #1170669)
 - Added logic to detect cloned (virtual) computers (LP: #1161856)
 - The landscape-client and landscape-common packages do not use or depend
   on dbus code anymore, and the dependencies to python-gi and gudev are
   dropped. The hardware info plugin now looks at /proc instead of querying
   DBus (LP: #1175553, LP: #1180691)
 - The ceph manager plugin is now a monitor plugin and thus does not require
   root privileges anymore. (LP: #1186973)
 - The detection logic for virtual machine was changed to account for the
   different semantics between Openstack Folsom and Grizzly, and was
   expanded to detect more hypervisors (LP: #1191843)
 - Removed legacy upgrader code from postinst since support for it was
   dropped.
 - The /etc/dbus-1/system.d/landscape.conf file was moved from the
   landscape-common package to the landscape-client-uii as part of
   LP: #1175553, LP: #1180691. No "Breaks" rule was added since the
   landscape-client-ui package requires the exact same version of
   landscape-common, which avoids the case outlined in
   http://www.debian.org/doc/debian-policy/footnotes.html#f53 .
* Removed the hardwareinfo patch since the changes were merged upstream
* Changed dh_clean -K to dh_prep (dh_clean -K is deprecated)
* Removed the packages arguments to dh_installman since all pacakges have
  manpages now.

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
109
110
        HelperTestCase.tearDown(self)
110
111
        MockerTestCase.tearDown(self)
111
112
 
 
113
    def successResultOf(self, deferred):
 
114
        """See C{twisted.trial._synctest._Assertions.successResultOf}.
 
115
 
 
116
        This is a copy of the original method, which is available only
 
117
        since Twisted 12.3.0 (from 2012-12-20).
 
118
        """
 
119
        result = []
 
120
        deferred.addBoth(result.append)
 
121
        if not result:
 
122
            self.fail(
 
123
                "Success result expected on %r, found no result instead" % (
 
124
                    deferred,))
 
125
        elif isinstance(result[0], Failure):
 
126
            self.fail(
 
127
                "Success result expected on %r, "
 
128
                "found failure result (%r) instead" % (deferred, result[0]))
 
129
        else:
 
130
            return result[0]
 
131
 
 
132
    def failureResultOf(self, deferred):
 
133
        """See C{twisted.trial._synctest._Assertions.failureResultOf}.
 
134
 
 
135
        This is a copy of the original method, which is available only
 
136
        since Twisted 12.3.0 (from 2012-12-20).
 
137
        """
 
138
        result = []
 
139
        deferred.addBoth(result.append)
 
140
        if not result:
 
141
            self.fail(
 
142
                "Failure result expected on %r, found no result instead" % (
 
143
                    deferred,))
 
144
        elif not isinstance(result[0], Failure):
 
145
            self.fail(
 
146
                "Failure result expected on %r, "
 
147
                "found success result (%r) instead" % (deferred, result[0]))
 
148
        else:
 
149
            return result[0]
 
150
 
 
151
    def assertNoResult(self, deferred):
 
152
        """See C{twisted.trial._synctest._Assertions.assertNoResult}.
 
153
 
 
154
        This is a copy of the original method, which is available only
 
155
        since Twisted 12.3.0 (from 2012-12-20).
 
156
        """
 
157
        result = []
 
158
        deferred.addBoth(result.append)
 
159
        if result:
 
160
            self.fail(
 
161
                "No result expected on %r, found %r instead" % (
 
162
                    deferred, result[0]))
 
163
 
112
164
    def assertDeferredSucceeded(self, deferred):
113
165
        self.assertTrue(isinstance(deferred, Deferred))
114
166
        called = []
306
358
            reactor_factory = FakeReactor
307
359
            transport_factory = FakeTransport
308
360
 
309
 
            def stopService(service):
310
 
                # We need to explictely stop listening to the socket
311
 
                # because the reactor would still have active selectables
312
 
                # at the end of the test otherwise
313
 
                if os.path.exists(service.port.port):
314
 
                    service.port.connectionLost(None)
315
 
                super(FakeBrokerService, service).stopService()
316
 
 
317
361
        test_case.broker_service = FakeBrokerService(config)
318
362
        test_case.remote = FakeRemoteBroker(
319
363
            test_case.broker_service.exchanger,
320
 
            test_case.broker_service.message_store)
 
364
            test_case.broker_service.message_store,
 
365
            test_case.broker_service.broker)
321
366
 
322
367
 
323
368
class BrokerServiceHelper(FakeBrokerServiceHelper):
331
376
 
332
377
    def set_up(self, test_case):
333
378
        super(BrokerServiceHelper, self).set_up(test_case)
 
379
        test_case.broker_service.startService()
334
380
        # Use different reactor to simulate separate processes
335
381
        self._connector = RemoteBrokerConnector(
336
382
            FakeReactor(), test_case.broker_service.config)
337
 
 
338
 
        def set_remote(remote):
339
 
            test_case.remote = remote
340
 
            return remote
341
 
 
342
 
        test_case.broker_service.startService()
343
 
        connected = self._connector.connect()
344
 
        return connected.addCallback(set_remote)
 
383
        deferred = self._connector.connect()
 
384
        test_case.remote = test_case.successResultOf(deferred)
345
385
 
346
386
    def tear_down(self, test_case):
347
387
        self._connector.disconnect()
599
639
        """Remove sample data for the process that matches C{process_id}."""
600
640
        process_dir = os.path.join(self._sample_dir, str(process_id))
601
641
        shutil.rmtree(process_dir)
602
 
 
603
 
 
604
 
from twisted.python import log
605
 
from twisted.python import failure
606
 
from twisted.trial import reporter
607
 
 
608
 
 
609
 
def install_trial_hack():
610
 
    """
611
 
    Trial's TestCase in Twisted 2.2 had a bug which would prevent
612
 
    certain errors from being reported when being run in a non-trial
613
 
    test runner. This function monkeypatches trial to fix the bug, and
614
 
    only takes effect if using Twisted 2.2.
615
 
    """
616
 
    from twisted.trial.itrial import IReporter
617
 
    if "addError" in IReporter:
618
 
        # We have no need for this monkey patch with newer versions of Twisted.
619
 
        return
620
 
 
621
 
    def run(self, result):
622
 
        """
623
 
        Copied from twisted.trial.unittest.TestCase.run, but some
624
 
        lines from Twisted 2.5.
625
 
        """
626
 
        log.msg("--> %s <--" % (self.id()))
627
 
 
628
 
        # From Twisted 2.5
629
 
        if not isinstance(result, reporter.TestResult):
630
 
            result = PyUnitResultAdapter(result)
631
 
        # End from Twisted 2.5
632
 
 
633
 
        self._timedOut = False
634
 
        if self._shared and self not in self.__class__._instances:
635
 
            self.__class__._instances.add(self)
636
 
        result.startTest(self)
637
 
        if self.getSkip():  # don't run test methods that are marked as .skip
638
 
            result.addSkip(self, self.getSkip())
639
 
            result.stopTest(self)
640
 
            return
641
 
        # From twisted 2.5
642
 
        if hasattr(self, "_installObserver"):
643
 
            self._installObserver()
644
 
        # End from Twisted 2.5
645
 
        self._passed = False
646
 
        first = False
647
 
        if self._shared:
648
 
            first = self._isFirst()
649
 
            self.__class__._instancesRun.add(self)
650
 
        if first:
651
 
            d = self.deferSetUpClass(result)
652
 
        else:
653
 
            d = self.deferSetUp(None, result)
654
 
        try:
655
 
            self._wait(d)
656
 
        finally:
657
 
            self._cleanUp(result)
658
 
            result.stopTest(self)
659
 
            if self._shared and self._isLast():
660
 
                self._initInstances()
661
 
                self._classCleanUp(result)
662
 
            if not self._shared:
663
 
                self._classCleanUp(result)
664
 
    TestCase.run = run
665
 
 
666
 
### Copied from Twisted, to fix a bug in trial in Twisted 2.2! ###
667
 
 
668
 
 
669
 
class UnsupportedTrialFeature(Exception):
670
 
    """A feature of twisted.trial was used that pyunit cannot support."""
671
 
 
672
 
 
673
 
class PyUnitResultAdapter(object):
674
 
    """
675
 
    Wrap a C{TestResult} from the standard library's C{unittest} so that it
676
 
    supports the extended result types from Trial, and also supports
677
 
    L{twisted.python.failure.Failure}s being passed to L{addError} and
678
 
    L{addFailure}.
679
 
 
680
 
    @param original: A C{TestResult} instance from C{unittest}.
681
 
    """
682
 
 
683
 
    def __init__(self, original):
684
 
        self.original = original
685
 
 
686
 
    def _exc_info(self, err):
687
 
        if isinstance(err, failure.Failure):
688
 
            # Unwrap the Failure into a exc_info tuple.
689
 
            err = (err.type, err.value, err.tb)
690
 
        return err
691
 
 
692
 
    def startTest(self, method):
693
 
        # We'll need this later in cleanupErrors.
694
 
        self.__currentTest = method
695
 
        self.original.startTest(method)
696
 
 
697
 
    def stopTest(self, method):
698
 
        self.original.stopTest(method)
699
 
 
700
 
    def addFailure(self, test, fail):
701
 
        self.original.addFailure(test, self._exc_info(fail))
702
 
 
703
 
    def addError(self, test, error):
704
 
        self.original.addError(test, self._exc_info(error))
705
 
 
706
 
    def _unsupported(self, test, feature, info):
707
 
        self.original.addFailure(
708
 
            test,
709
 
            (UnsupportedTrialFeature,
710
 
             UnsupportedTrialFeature(feature, info),
711
 
             None))
712
 
 
713
 
    def addSkip(self, test, reason):
714
 
        """
715
 
        Report the skip as a failure.
716
 
        """
717
 
        self._unsupported(test, 'skip', reason)
718
 
 
719
 
    def addUnexpectedSuccess(self, test, todo):
720
 
        """
721
 
        Report the unexpected success as a failure.
722
 
        """
723
 
        self._unsupported(test, 'unexpected success', todo)
724
 
 
725
 
    def addExpectedFailure(self, test, error):
726
 
        """
727
 
        Report the expected failure (i.e. todo) as a failure.
728
 
        """
729
 
        self._unsupported(test, 'expected failure', error)
730
 
 
731
 
    def addSuccess(self, test):
732
 
        self.original.addSuccess(test)
733
 
 
734
 
    def upDownError(self, method, error, warn, printStatus):
735
 
        pass
736
 
 
737
 
    def cleanupErrors(self, errs):
738
 
        # Let's consider cleanupErrors as REAL errors. In recent
739
 
        # Twisted this is the default behavior, and cleanupErrors
740
 
        # isn't even called.
741
 
        self.addError(self.__currentTest, errs)
742
 
 
743
 
    def startSuite(self, name):
744
 
        pass
745
 
 
746
 
### END COPY FROM TWISTED ###
747
 
 
748
 
install_trial_hack()