From b87ecfc61451291132307eb34acdbd545871a641 Mon Sep 17 00:00:00 2001 From: Angus Salkeld Date: Thu, 14 May 2015 17:35:00 +1000 Subject: [PATCH] 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 --- heat/tests/common.py | 1 + heat/tests/engine/test_software_config.py | 1 - heat/tests/engine/test_stack_action.py | 2 -- heat/tests/test_attributes.py | 11 +++++----- heat/tests/test_constraints.py | 22 ++++++++----------- heat/tests/test_engine_service.py | 4 ---- heat/tests/test_engine_service_stack_watch.py | 2 -- heat/tests/test_metadata_refresh.py | 2 -- heat/tests/test_nova_utils.py | 22 ------------------- heat/tests/test_resource.py | 3 --- heat/tests/test_template.py | 18 +++++---------- heat/tests/test_validate.py | 1 - 12 files changed, 20 insertions(+), 69 deletions(-) diff --git a/heat/tests/common.py b/heat/tests/common.py index 0b158f7cce..7fe4daa91f 100644 --- a/heat/tests/common.py +++ b/heat/tests/common.py @@ -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', diff --git a/heat/tests/engine/test_software_config.py b/heat/tests/engine/test_software_config.py index 6235bccc6a..fd3ac879a6 100644 --- a/heat/tests/engine/test_software_config.py +++ b/heat/tests/engine/test_software_config.py @@ -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( diff --git a/heat/tests/engine/test_stack_action.py b/heat/tests/engine/test_stack_action.py index 02d4a21451..a6f3291f98 100644 --- a/heat/tests/engine/test_stack_action.py +++ b/heat/tests/engine/test_stack_action.py @@ -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') diff --git a/heat/tests/test_attributes.py b/heat/tests/test_attributes.py index 16d8e0b776..becf1b1ea9 100644 --- a/heat/tests/test_attributes.py +++ b/heat/tests/test_attributes.py @@ -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): diff --git a/heat/tests/test_constraints.py b/heat/tests/test_constraints.py index fd468cfde2..053062253d 100644 --- a/heat/tests/test_constraints.py +++ b/heat/tests/test_constraints.py @@ -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'} diff --git a/heat/tests/test_engine_service.py b/heat/tests/test_engine_service.py index 7b56d16e43..8628c34789 100644 --- a/heat/tests/test_engine_service.py +++ b/heat/tests/test_engine_service.py @@ -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' diff --git a/heat/tests/test_engine_service_stack_watch.py b/heat/tests/test_engine_service_stack_watch.py index 08bf51a986..52b6cd3acd 100644 --- a/heat/tests/test_engine_service_stack_watch.py +++ b/heat/tests/test_engine_service_stack_watch.py @@ -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') diff --git a/heat/tests/test_metadata_refresh.py b/heat/tests/test_metadata_refresh.py index 2080771701..00f154edef 100644 --- a/heat/tests/test_metadata_refresh.py +++ b/heat/tests/test_metadata_refresh.py @@ -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', diff --git a/heat/tests/test_nova_utils.py b/heat/tests/test_nova_utils.py index 3044c215ac..4632e8991c 100644 --- a/heat/tests/test_nova_utils.py +++ b/heat/tests/test_nova_utils.py @@ -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)) diff --git a/heat/tests/test_resource.py b/heat/tests/test_resource.py index 959e9a1211..80838b212c 100644 --- a/heat/tests/test_resource.py +++ b/heat/tests/test_resource.py @@ -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()) diff --git a/heat/tests/test_template.py b/heat/tests/test_template.py index 5b1b11760a..82c85c66fe 100644 --- a/heat/tests/test_template.py +++ b/heat/tests/test_template.py @@ -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 diff --git a/heat/tests/test_validate.py b/heat/tests/test_validate.py index bf3ea7c8cc..36e1f0f73b 100644 --- a/heat/tests/test_validate.py +++ b/heat/tests/test_validate.py @@ -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')