Catch warnings consistently during tests

Use unittest2.assertWarnsRegex() for making sure we generate the
required warnings.
Use WarningsCapture to not print the other repeated warns to the
console.

Change-Id: I7223f8956394208eaf2eb8a1d651ba1425128bc9
This commit is contained in:
Angus Salkeld 2015-05-14 17:35:00 +10:00
parent e27831cc95
commit b87ecfc614
12 changed files with 20 additions and 69 deletions

View File

@ -77,6 +77,7 @@ class HeatTestCase(testscenarios.WithScenarios,
self.m = mox.Mox()
self.addCleanup(self.m.UnsetStubs)
self.setup_logging()
self.warnings = self.useFixture(fixtures.WarningsCapture())
scheduler.ENABLE_SLEEP = False
self.useFixture(fixtures.MonkeyPatch(
'heat.common.exception._FATAL_EXCEPTION_FORMAT_ERRORS',

View File

@ -37,7 +37,6 @@ class SoftwareConfigServiceTest(common.HeatTestCase):
def setUp(self):
super(SoftwareConfigServiceTest, self).setUp()
self.ctx = utils.dummy_context()
self.patch('heat.engine.service.warnings')
self.engine = service.EngineService('a-host', 'a-topic')
def _create_software_config(

View File

@ -29,7 +29,6 @@ class StackServiceActionsTest(common.HeatTestCase):
def setUp(self):
super(StackServiceActionsTest, self).setUp()
self.ctx = utils.dummy_context()
self.patch('heat.engine.service.warnings')
self.man = service.EngineService('a-host', 'a-topic')
self.man.create_periodic_tasks()
@ -132,7 +131,6 @@ class StackServiceUpdateActionsNotSupportedTest(common.HeatTestCase):
def setUp(self):
super(StackServiceUpdateActionsNotSupportedTest, self).setUp()
self.ctx = utils.dummy_context()
self.patch('heat.engine.service.warnings')
self.man = service.EngineService('a-host', 'a-topic')
@mock.patch.object(stack.Stack, 'load')

View File

@ -72,12 +72,11 @@ class AttributeSchemaTest(common.HeatTestCase):
attrs._attributes['bar_dep'].support_status().message)
def test_old_attribute_schema_format(self):
with mock.patch('heat.engine.attributes.warnings'):
s = 'Test description.'
self.assertIsInstance(attributes.Schema.from_attribute(s),
attributes.Schema)
self.assertEqual('Test description.',
attributes.Schema.from_attribute(s).description)
s = 'Test description.'
self.assertIsInstance(attributes.Schema.from_attribute(s),
attributes.Schema)
self.assertEqual('Test description.',
attributes.Schema.from_attribute(s).description)
class AttributeTest(common.HeatTestCase):

View File

@ -11,7 +11,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
import six
from heat.common import exception
@ -21,25 +20,22 @@ from heat.tests import common
class SchemaTest(common.HeatTestCase):
@mock.patch('warnings.warn')
def test_warn_required_with_default(self, mock_warn):
constraints.Schema(constraints.Schema.STRING, 'A string',
default='wibble', required=True)
def test_warn_required_with_default(self):
msg = ("Option 'required=True' should not be used with any 'default' "
"value (wibble)")
mock_warn.assert_called_once_with(msg)
"value \(wibble\)")
with self.assertWarnsRegex(UserWarning, msg):
constraints.Schema(constraints.Schema.STRING, 'A string',
default='wibble', required=True)
@mock.patch('warnings.warn')
def test_without_warn_only_default(self, mock_warn):
def test_without_warn_only_default(self):
constraints.Schema(constraints.Schema.STRING, 'A string',
default='wibble')
self.assertEqual(0, mock_warn.call_count)
self.assertEqual(0, len(self.warnings.captures))
@mock.patch('warnings.warn')
def test_without_warn_only_required(self, mock_warn):
def test_without_warn_only_required(self):
constraints.Schema(constraints.Schema.STRING, 'A string',
required=True)
self.assertEqual(0, mock_warn.call_count)
self.assertEqual(0, len(self.warnings.captures))
def test_range_schema(self):
d = {'range': {'min': 5, 'max': 10}, 'description': 'a range'}

View File

@ -561,7 +561,6 @@ class StackServiceCreateUpdateDeleteTest(common.HeatTestCase):
def setUp(self):
super(StackServiceCreateUpdateDeleteTest, self).setUp()
self.ctx = utils.dummy_context()
self.patch('heat.engine.service.warnings')
self.man = service.EngineService('a-host', 'a-topic')
self.man.create_periodic_tasks()
@ -1630,7 +1629,6 @@ class StackConvergenceServiceCreateUpdateTest(common.HeatTestCase):
super(StackConvergenceServiceCreateUpdateTest, self).setUp()
cfg.CONF.set_override('convergence_engine', True)
self.ctx = utils.dummy_context()
self.patch('heat.engine.service.warnings')
self.man = service.EngineService('a-host', 'a-topic')
self.man.create_periodic_tasks()
@ -1740,7 +1738,6 @@ class StackServiceAuthorizeTest(common.HeatTestCase):
super(StackServiceAuthorizeTest, self).setUp()
self.ctx = utils.dummy_context(tenant_id='stack_service_test_tenant')
self.patch('heat.engine.service.warnings')
self.eng = service.EngineService('a-host', 'a-topic')
self.eng.engine_id = 'engine-fake-uuid'
cfg.CONF.set_default('heat_stack_user_role', 'stack_user_role')
@ -1833,7 +1830,6 @@ class StackServiceTest(common.HeatTestCase):
super(StackServiceTest, self).setUp()
self.ctx = utils.dummy_context(tenant_id='stack_service_test_tenant')
self.patch('heat.engine.service.warnings')
self.eng = service.EngineService('a-host', 'a-topic')
self.eng.create_periodic_tasks()
self.eng.engine_id = 'engine-fake-uuid'

View File

@ -23,9 +23,7 @@ class StackServiceWatcherTest(common.HeatTestCase):
def setUp(self):
super(StackServiceWatcherTest, self).setUp()
self.ctx = utils.dummy_context(tenant_id='stack_service_test_tenant')
self.patch('heat.engine.service.warnings')
@mock.patch.object(service_stack_watch.stack_object.Stack,
'get_all_by_owner_id')

View File

@ -211,8 +211,6 @@ class MetadataRefreshTest(common.HeatTestCase):
class WaitCondMetadataUpdateTest(common.HeatTestCase):
def setUp(self):
super(WaitCondMetadataUpdateTest, self).setUp()
self.patch('heat.engine.service.warnings')
self.man = service.EngineService('a-host', 'a-topic')
self.man.create_periodic_tasks()
cfg.CONF.set_default('heat_waitcondition_server_url',

View File

@ -35,10 +35,6 @@ class NovaUtilsTests(common.HeatTestCase):
def setUp(self):
super(NovaUtilsTests, self).setUp()
self.nova_client = self.m.CreateMockAnything()
self.mock_warnings = mock.patch(
'heat.engine.nova_utils.warnings')
self.mock_warnings.start()
self.addCleanup(self.mock_warnings.stop)
def test_get_ip(self):
my_image = self.m.CreateMockAnything()
@ -130,13 +126,6 @@ class NovaUtilsTests(common.HeatTestCase):
class NovaUtilsRefreshServerTests(common.HeatTestCase):
def setUp(self):
super(NovaUtilsRefreshServerTests, self).setUp()
self.mock_warnings = mock.patch(
'heat.engine.nova_utils.warnings')
self.mock_warnings.start()
self.addCleanup(self.mock_warnings.stop)
def test_successful_refresh(self):
server = self.m.CreateMockAnything()
server.get().AndReturn(None)
@ -181,10 +170,6 @@ class NovaUtilsUserdataTests(common.HeatTestCase):
def setUp(self):
super(NovaUtilsUserdataTests, self).setUp()
self.nova_client = self.m.CreateMockAnything()
self.mock_warnings = mock.patch(
'heat.engine.nova_utils.warnings')
self.mock_warnings.start()
self.addCleanup(self.mock_warnings.stop)
def test_build_userdata(self):
"""Tests the build_userdata function."""
@ -243,13 +228,6 @@ class NovaUtilsUserdataTests(common.HeatTestCase):
class NovaUtilsMetadataTests(common.HeatTestCase):
def setUp(self):
super(NovaUtilsMetadataTests, self).setUp()
self.mock_warnings = mock.patch(
'heat.engine.nova_utils.warnings')
self.mock_warnings.start()
self.addCleanup(self.mock_warnings.stop)
def test_serialize_string(self):
original = {'test_key': 'simple string value'}
self.assertEqual(original, nova_utils.meta_serialize(original))

View File

@ -68,7 +68,6 @@ class ResourceTest(common.HeatTestCase):
template.Template(empty_template,
env=self.env),
stack_id=str(uuid.uuid4()))
self.patch('heat.engine.resource.warnings')
def test_get_class_ok(self):
cls = resources.global_env().get_class('GenericResourceType')
@ -1310,7 +1309,6 @@ class ResourceAdoptTest(common.HeatTestCase):
super(ResourceAdoptTest, self).setUp()
resource._register_class('GenericResourceType',
generic_rsrc.GenericResource)
self.patch('heat.engine.resource.warnings')
def test_adopt_resource_success(self):
adopt_data = '{}'
@ -1911,7 +1909,6 @@ class MetadataTest(common.HeatTestCase):
scheduler.TaskRunner(self.res.create)()
self.addCleanup(self.stack.delete)
self.patch('heat.engine.resource.warnings')
def test_read_initial(self):
self.assertEqual({'Test': 'Initial metadata'}, self.res.metadata_get())

View File

@ -13,7 +13,6 @@
import copy
import json
import warnings
import fixtures
from oslotest import mockpatch
@ -1021,19 +1020,12 @@ class ResolveDataTest(common.HeatTestCase):
template.Template(empty_template),
tenant_id='bar')
with warnings.catch_warnings(record=True) as ws:
warnings.filterwarnings('always')
# Work around http://bugs.python.org/issue4180
getattr(stack, '__warningregistry__', {}).clear()
test_data = {'foo': 'bar'}
test_data = {'foo': 'bar'}
msg = ('Stack.resolve_runtime_data\(\) is deprecated.'
' Use heat.engine.function.resolve\(\) instead')
with self.assertWarnsRegex(DeprecationWarning, msg):
resolved = stk.resolve_runtime_data(test_data)
self.assertTrue(ws)
self.assertTrue(issubclass(ws[0].category, DeprecationWarning))
self.assertEqual(test_data, resolved)
self.assertEqual(test_data, resolved)
def test_join_split(self):
# join

View File

@ -820,7 +820,6 @@ class validateTest(common.HeatTestCase):
self.gc = fakes_nova.FakeClient()
resources.initialise()
self.ctx = utils.dummy_context()
self.patch('heat.engine.service.warnings')
def _mock_get_image_id_success(self, imageId_input, imageId):
self.m.StubOutWithMock(glance.GlanceClientPlugin, 'get_image_id')