From 7f098afd2da15ea007ee707086419d71117028fa Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Tue, 16 Feb 2016 10:09:35 -0500 Subject: [PATCH] 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 --- oslotest/base.py | 27 +++++++++++++++++++++------ 1 file 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()