diff options
author | Davanum Srinivas <davanum@gmail.com> | 2016-02-16 10:09:35 -0500 |
---|---|---|
committer | Davanum Srinivas <davanum@gmail.com> | 2016-02-17 06:54:22 -0500 |
commit | 7f098afd2da15ea007ee707086419d71117028fa (patch) | |
tree | 4fc5a1cc6e8c23ef95a152392a3118ed94b9d044 | |
parent | cfe008fbe9dae5aa3cbae9be5125c76c2fc3fc5a (diff) | |
download | oslotest-7f098afd2da15ea007ee707086419d71117028fa.tar.gz |
Hack to get back stopall cleanup behavior feature
So, calling addCleanup in __init__ is not supported anymore in
latest testtools. This breaks the support we used to have in
the base class that registered mock.patch.stopall right at the
beginning, so classes inheriting would not have to worry about
the cleanup. For Mitaka, we add a hack to make it work by
looking at the underlying _cleanups variable. This clearly needs
to be removed as soon as possible.
Closes-Bug: #1545576
Change-Id: I29a77224bfd9d106c711155e0cfc10eac0e6fe36
-rw-r--r-- | oslotest/base.py | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/oslotest/base.py b/oslotest/base.py index cdea425..b5f14d0 100644 --- a/oslotest/base.py +++ b/oslotest/base.py @@ -15,6 +15,8 @@ """Common utilities used in testing""" +import logging + import fixtures from oslotest import createfile from oslotest import log @@ -24,6 +26,8 @@ from oslotest import timeout from six.moves import mock import testtools +LOG = logging.getLogger(__name__) + _TRUE_VALUES = ('True', 'true', '1', 'yes') _LOG_FORMAT = "%(levelname)8s [%(name)s] %(message)s" @@ -81,12 +85,23 @@ class BaseTestCase(testtools.TestCase): # low for comparing most dicts self.maxDiff = 10000 - # Ensure that the mock.patch.stopall cleanup is registered - # before any setUp() methods have a chance to register other - # things to be cleaned up, so it is called last. This allows - # tests to register their own cleanups with a mock.stop method - # so those mocks are not included in the stopall set. - self.addCleanup(mock.patch.stopall) + def addCleanup(self, function, *args, **kwargs): + # NOTE(dims): This is a hack for Mitaka. We'll need to undo this as + # early as possible in Newton and advertise that this hack will not + # be supported anymore. + if (hasattr(self, '_cleanups') and + isinstance(self._cleanups, list)): + if not self._cleanups: + # Ensure that the mock.patch.stopall cleanup is registered + # before any addCleanup() methods have a chance to register + # other things to be cleaned up, so it is called last. This + # allows tests to register their own cleanups with a mock.stop + # method so those mocks are not included in the stopall set. + super(BaseTestCase, self).addCleanup(mock.patch.stopall) + else: + LOG.error('Unable to patch test case. ' + 'mock.patch.stopall cleanup was not registered.') + super(BaseTestCase, self).addCleanup(function, *args, **kwargs) def setUp(self): super(BaseTestCase, self).setUp() |