From fb6de9419fe729f2a46dcb10ce6191439d933218 Mon Sep 17 00:00:00 2001 From: Clint Byrum Date: Thu, 25 Apr 2013 16:33:48 -0700 Subject: [PATCH] Removing all prints and capturing logging A new base class for tests is used to reduce repetitive steps used in a majority of tests. This new base class also uses fixtures.FakeLogger to suppress logging, though it will be shown on any failures to aid in debugging the failure. Prints that happen on normal operation of the test suite are all removed as they are not needed and only clutter the output. Change-Id: I0365283ce415c5390fd68bdf1f0b3c8038b1b9af --- heat/tests/common.py | 27 +++++++++++++++++++++++++++ heat/tests/test_api_aws.py | 10 ++-------- heat/tests/test_api_cfn_v1.py | 23 ++++++++--------------- heat/tests/test_api_cloudwatch.py | 18 ++++++------------ heat/tests/test_common_policy.py | 17 ++++++++--------- heat/tests/test_dbinstance.py | 10 +++------- heat/tests/test_loadbalancer.py | 13 ++++--------- heat/tests/test_nested_stack.py | 12 +++--------- heat/tests/test_volume.py | 11 +++-------- heat/tests/utils.py | 4 +--- 10 files changed, 65 insertions(+), 80 deletions(-) create mode 100644 heat/tests/common.py diff --git a/heat/tests/common.py b/heat/tests/common.py new file mode 100644 index 00000000..1d7970ab --- /dev/null +++ b/heat/tests/common.py @@ -0,0 +1,27 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +import fixtures +import mox +import testtools + + +class HeatTestCase(testtools.TestCase): + + def setUp(self): + super(HeatTestCase, self).setUp() + self.m = mox.Mox() + self.addCleanup(self.m.UnsetStubs) + self.useFixture(fixtures.FakeLogger()) diff --git a/heat/tests/test_api_aws.py b/heat/tests/test_api_aws.py index 507b4dcb..a54a5eb6 100644 --- a/heat/tests/test_api_aws.py +++ b/heat/tests/test_api_aws.py @@ -13,7 +13,7 @@ # under the License. -import unittest +from heat.tests.common import HeatTestCase from nose.plugins.attrib import attr from heat.api.aws import utils as api_utils @@ -21,7 +21,7 @@ from heat.api.aws import utils as api_utils @attr(tag=['unit', 'api-aws', 'AWSCommon']) @attr(speed='fast') -class AWSCommon(unittest.TestCase): +class AWSCommon(HeatTestCase): ''' Tests the api/aws common componenents ''' @@ -187,9 +187,3 @@ class AWSCommon(unittest.TestCase): expected = {"bar": 123} result = api_utils.reformat_dict_keys(keymap, data) self.assertEqual(result, expected) - - def setUp(self): - print "setup complete" - - def tearDown(self): - print "teardown complete" diff --git a/heat/tests/test_api_cfn_v1.py b/heat/tests/test_api_cfn_v1.py index 151893e9..a13363f9 100644 --- a/heat/tests/test_api_cfn_v1.py +++ b/heat/tests/test_api_cfn_v1.py @@ -14,9 +14,7 @@ import json import os -import unittest -import mox from nose.plugins.attrib import attr from oslo.config import cfg @@ -29,11 +27,14 @@ from heat.common.wsgi import Request from heat.rpc import api as rpc_api from heat.api.aws import exception import heat.api.cfn.v1.stacks as stacks +from heat.tests.common import HeatTestCase + +policy_path = os.path.dirname(os.path.realpath(__file__)) + "/policy/" @attr(tag=['unit', 'api-cfn-v1-stacks', 'StackController']) @attr(speed='fast') -class CfnStackControllerTest(unittest.TestCase): +class CfnStackControllerTest(HeatTestCase): ''' Tests the API class which acts as the WSGI controller, the endpoint processing API requests after they are routed @@ -87,7 +88,7 @@ class CfnStackControllerTest(unittest.TestCase): params = {'Action': 'ListStacks'} dummy_req = self._dummy_GET_request(params) dummy_req.context.roles = ['heat_stack_user'] - self.controller.policy.policy_path = (self.policy_path + + self.controller.policy.policy_path = (policy_path + 'deny_stack_user.json') self.assertRaises(exception.HeatAccessDeniedError, self.controller._enforce, dummy_req, 'ListStacks') @@ -103,7 +104,7 @@ class CfnStackControllerTest(unittest.TestCase): ).AndRaise(AttributeError) self.m.ReplayAll() - self.controller.policy.policy_path = (self.policy_path + + self.controller.policy.policy_path = (policy_path + 'deny_stack_user.json') self.assertRaises(exception.HeatInternalFailureError, self.controller._enforce, dummy_req, 'ListStacks') @@ -1396,13 +1397,10 @@ class CfnStackControllerTest(unittest.TestCase): self.m.VerifyAll() def setUp(self): - self.maxDiff = None - self.m = mox.Mox() + super(CfnStackControllerTest, self).setUp() - self.path = os.path.dirname(os.path.realpath(__file__)) - self.policy_path = self.path + "/policy/" opts = [ - cfg.StrOpt('config_dir', default=self.policy_path), + cfg.StrOpt('config_dir', default=policy_path), cfg.StrOpt('config_file', default='foo'), cfg.StrOpt('project', default='heat'), ] @@ -1416,8 +1414,3 @@ class CfnStackControllerTest(unittest.TestCase): bind_port = 8000 cfgopts = DummyConfig() self.controller = stacks.StackController(options=cfgopts) - print "setup complete" - - def tearDown(self): - self.m.UnsetStubs() - print "teardown complete" diff --git a/heat/tests/test_api_cloudwatch.py b/heat/tests/test_api_cloudwatch.py index a237f7a3..ed189c68 100644 --- a/heat/tests/test_api_cloudwatch.py +++ b/heat/tests/test_api_cloudwatch.py @@ -13,9 +13,7 @@ # under the License. import os -import unittest -import mox from nose.plugins.attrib import attr from oslo.config import cfg @@ -25,13 +23,13 @@ from heat.openstack.common import rpc from heat.common.wsgi import Request from heat.api.aws import exception import heat.api.cloudwatch.watch as watches -from heat.engine import api as engine_api -from heat.rpc import api as rpc_api +from heat.rpc import api as engine_api +from heat.tests.common import HeatTestCase @attr(tag=['unit', 'api-cloudwatch', 'WatchController']) @attr(speed='fast') -class WatchControllerTest(unittest.TestCase): +class WatchControllerTest(HeatTestCase): ''' Tests the API class which acts as the WSGI controller, the endpoint processing API requests after they are routed @@ -510,9 +508,7 @@ class WatchControllerTest(unittest.TestCase): self.assert_(type(result) == exception.HeatInvalidParameterValueError) def setUp(self): - self.maxDiff = None - self.m = mox.Mox() - + super(WatchControllerTest, self).setUp() self.path = os.path.dirname(os.path.realpath(__file__)) self.policy_path = self.path + "/policy/" opts = [ @@ -522,7 +518,7 @@ class WatchControllerTest(unittest.TestCase): ] cfg.CONF.register_opts(opts) cfg.CONF.set_default('host', 'host') - self.topic = rpc_api.ENGINE_TOPIC + self.topic = engine_api.ENGINE_TOPIC self.api_version = '1.0' # Create WSGI controller instance @@ -531,9 +527,7 @@ class WatchControllerTest(unittest.TestCase): cfgopts = DummyConfig() self.controller = watches.WatchController(options=cfgopts) self.controller.policy.policy_path = None - print "setup complete" def tearDown(self): - self.m.UnsetStubs() self.m.VerifyAll() - print "teardown complete" + super(WatchControllerTest, self).tearDown() diff --git a/heat/tests/test_common_policy.py b/heat/tests/test_common_policy.py index 7915b3c5..af8bb053 100644 --- a/heat/tests/test_common_policy.py +++ b/heat/tests/test_common_policy.py @@ -24,6 +24,8 @@ from heat.common import context from heat.common import policy from heat.common import exception +policy_path = os.path.dirname(os.path.realpath(__file__)) + "/policy/" + @attr(tag=['unit', 'common-policy', 'Enforcer']) @attr(speed='fast') @@ -40,19 +42,16 @@ class TestPolicyEnforcer(unittest.TestCase): "PutMetricAlarm", "PutMetricData", "SetAlarmState") def setUp(self): - self.path = os.path.dirname(os.path.realpath(__file__)) + "/policy/" self.m = mox.Mox() opts = [ - cfg.StrOpt('config_dir', default=self.path), + cfg.StrOpt('config_dir', default=policy_path), cfg.StrOpt('config_file', default='foo'), cfg.StrOpt('project', default='heat'), ] cfg.CONF.register_opts(opts) - print "setup complete" def tearDown(self): self.m.UnsetStubs() - print "teardown complete" def test_policy_cfn_default(self): enforcer = policy.Enforcer(scope='cloudformation') @@ -63,7 +62,7 @@ class TestPolicyEnforcer(unittest.TestCase): enforcer.enforce(ctx, action, {}) def test_policy_cfn_notallowed(self): - pf = self.path + 'notallowed.json' + pf = policy_path + 'notallowed.json' self.m.StubOutWithMock(policy.Enforcer, '_find_policy_file') policy.Enforcer._find_policy_file().MultipleTimes().AndReturn(pf) self.m.ReplayAll() @@ -78,7 +77,7 @@ class TestPolicyEnforcer(unittest.TestCase): self.m.VerifyAll() def test_policy_cfn_deny_stack_user(self): - pf = self.path + 'deny_stack_user.json' + pf = policy_path + 'deny_stack_user.json' self.m.StubOutWithMock(policy.Enforcer, '_find_policy_file') policy.Enforcer._find_policy_file().MultipleTimes().AndReturn(pf) self.m.ReplayAll() @@ -96,7 +95,7 @@ class TestPolicyEnforcer(unittest.TestCase): self.m.VerifyAll() def test_policy_cfn_allow_non_stack_user(self): - pf = self.path + 'deny_stack_user.json' + pf = policy_path + 'deny_stack_user.json' self.m.StubOutWithMock(policy.Enforcer, '_find_policy_file') policy.Enforcer._find_policy_file().MultipleTimes().AndReturn(pf) self.m.ReplayAll() @@ -110,7 +109,7 @@ class TestPolicyEnforcer(unittest.TestCase): self.m.VerifyAll() def test_policy_cw_deny_stack_user(self): - pf = self.path + 'deny_stack_user.json' + pf = policy_path + 'deny_stack_user.json' self.m.StubOutWithMock(policy.Enforcer, '_find_policy_file') policy.Enforcer._find_policy_file().MultipleTimes().AndReturn(pf) self.m.ReplayAll() @@ -128,7 +127,7 @@ class TestPolicyEnforcer(unittest.TestCase): self.m.VerifyAll() def test_policy_cw_allow_non_stack_user(self): - pf = self.path + 'deny_stack_user.json' + pf = policy_path + 'deny_stack_user.json' self.m.StubOutWithMock(policy.Enforcer, '_find_policy_file') policy.Enforcer._find_policy_file().MultipleTimes().AndReturn(pf) self.m.ReplayAll() diff --git a/heat/tests/test_dbinstance.py b/heat/tests/test_dbinstance.py index e4d018f9..dfea6007 100644 --- a/heat/tests/test_dbinstance.py +++ b/heat/tests/test_dbinstance.py @@ -15,7 +15,6 @@ import os -import unittest import mox from nose.plugins.attrib import attr @@ -26,20 +25,17 @@ from heat.common import template_format from heat.engine import parser from heat.engine import scheduler from heat.engine.resources import dbinstance as dbi +from heat.tests.common import HeatTestCase @attr(tag=['unit', 'resource']) @attr(speed='fast') -class DBInstanceTest(unittest.TestCase): +class DBInstanceTest(HeatTestCase): def setUp(self): - self.m = mox.Mox() + super(DBInstanceTest, self).setUp() self.m.StubOutWithMock(dbi.DBInstance, 'create_with_template') self.m.StubOutWithMock(dbi.DBInstance, 'nested') - def tearDown(self): - self.m.UnsetStubs() - print "DBInstanceTest teardown complete" - def load_template(self): self.path = os.path.dirname(os.path.realpath(__file__)).\ replace('heat/tests', 'templates') diff --git a/heat/tests/test_loadbalancer.py b/heat/tests/test_loadbalancer.py index 518af1f3..7ac28b52 100644 --- a/heat/tests/test_loadbalancer.py +++ b/heat/tests/test_loadbalancer.py @@ -13,12 +13,10 @@ # under the License. +import mox import re import os -import unittest -import mox - from nose.plugins.attrib import attr from oslo.config import cfg @@ -33,6 +31,7 @@ from heat.engine.resources import user from heat.engine.resources import loadbalancer as lb from heat.engine.resources import wait_condition as wc from heat.engine.resource import Metadata +from heat.tests.common import HeatTestCase from heat.tests.utils import setup_dummy_db from heat.tests.v1_1 import fakes from heat.tests import fakes as test_fakes @@ -50,10 +49,10 @@ def create_context(mocks, user='lb_test_user', @attr(tag=['unit', 'resource']) @attr(speed='fast') -class LoadBalancerTest(unittest.TestCase): +class LoadBalancerTest(HeatTestCase): def setUp(self): + super(LoadBalancerTest, self).setUp() config.register_engine_opts() - self.m = mox.Mox() self.fc = fakes.FakeClient() self.m.StubOutWithMock(lb.LoadBalancer, 'nova') self.m.StubOutWithMock(instance.Instance, 'nova') @@ -66,10 +65,6 @@ class LoadBalancerTest(unittest.TestCase): 'http://127.0.0.1:8000/v1/waitcondition') setup_dummy_db() - def tearDown(self): - self.m.UnsetStubs() - print "LoadBalancerTest teardown complete" - def load_template(self): self.path = os.path.dirname(os.path.realpath(__file__)).\ replace('heat/tests', 'templates') diff --git a/heat/tests/test_nested_stack.py b/heat/tests/test_nested_stack.py index f2bb96b2..4c1db813 100644 --- a/heat/tests/test_nested_stack.py +++ b/heat/tests/test_nested_stack.py @@ -13,9 +13,6 @@ # under the License. -import unittest -import mox - from nose.plugins.attrib import attr from heat.common import context @@ -24,12 +21,13 @@ from heat.common import template_format from heat.engine import parser from heat.engine.resources import stack as nested_stack from heat.common import urlfetch +from heat.tests.common import HeatTestCase from heat.tests.utils import setup_dummy_db @attr(tag=['unit', 'resource']) @attr(speed='fast') -class NestedStackTest(unittest.TestCase): +class NestedStackTest(HeatTestCase): test_template = ''' HeatTemplateFormatVersion: '2012-12-12' Resources: @@ -49,14 +47,10 @@ Outputs: ''' def setUp(self): - self.m = mox.Mox() + super(NestedStackTest, self).setUp() self.m.StubOutWithMock(urlfetch, 'get') setup_dummy_db() - def tearDown(self): - self.m.UnsetStubs() - print "NestedStackTest teardown complete" - def create_stack(self, template): t = template_format.parse(template) stack = self.parse_stack(t) diff --git a/heat/tests/test_volume.py b/heat/tests/test_volume.py index 5cc39bb5..7fb133f8 100644 --- a/heat/tests/test_volume.py +++ b/heat/tests/test_volume.py @@ -16,8 +16,6 @@ import os import eventlet -import mox -import unittest from nose.plugins.attrib import attr @@ -28,15 +26,16 @@ from heat.engine import parser from heat.engine import scheduler from heat.engine.resources import volume as vol from heat.engine import clients +from heat.tests.common import HeatTestCase from heat.tests.v1_1 import fakes from heat.tests.utils import setup_dummy_db @attr(tag=['unit', 'resource', 'volume']) @attr(speed='fast') -class VolumeTest(unittest.TestCase): +class VolumeTest(HeatTestCase): def setUp(self): - self.m = mox.Mox() + super(VolumeTest, self).setUp() self.fc = fakes.FakeClient() self.m.StubOutWithMock(clients.OpenStackClients, 'cinder') self.m.StubOutWithMock(clients.OpenStackClients, 'nova') @@ -48,10 +47,6 @@ class VolumeTest(unittest.TestCase): self.m.StubOutWithMock(eventlet, 'sleep') setup_dummy_db() - def tearDown(self): - self.m.UnsetStubs() - print "VolumeTest teardown complete" - def load_template(self): self.path = os.path.dirname(os.path.realpath(__file__)).\ replace('heat/tests', 'templates') diff --git a/heat/tests/utils.py b/heat/tests/utils.py index 93056a89..d9da8f3f 100644 --- a/heat/tests/utils.py +++ b/heat/tests/utils.py @@ -72,15 +72,13 @@ def stack_delete_after(test_fn): to ensure tests clean up their stacks regardless of test success/failure """ def wrapped_test(test_cls): - #print "Running test", test_fn.__name__ try: test_fn(test_cls) finally: try: test_cls.stack.delete() except AttributeError: - print "Could not delete stack (already deleted?)" - #print "Exited", test_fn.__name__ + pass return wrapped_test -- 2.45.2