Prevent extraneous log messages and stdout prints
while running unit tests using run_tests.sh, we can see quite an amount of unwanted log messages and stdout prints as part of the unit test results on the console. This basically makes it difficult to go through the test results and figure whats happening. This patch fixes the cause for those extraneous log messages caused due to a incorrect fixture usage in test_manage.py and also by mocking sys.stdout at the respective unit tests. Closes-bug: #1496898 Change-Id: If986691cb3f36ba357a5c0df34b6df7bf2d8ff86
This commit is contained in:
parent
cef71f71de
commit
367925c770
|
@ -13,6 +13,7 @@
|
|||
import optparse
|
||||
|
||||
import mock
|
||||
from six.moves import StringIO
|
||||
|
||||
from glance.cmd import cache_manage
|
||||
from glance.common import exception
|
||||
|
@ -21,6 +22,7 @@ import glance.image_cache.client
|
|||
from glance.tests import utils as test_utils
|
||||
|
||||
|
||||
@mock.patch('sys.stdout', mock.Mock())
|
||||
class TestGlanceCmdManage(test_utils.BaseTestCase):
|
||||
|
||||
@mock.patch.object(glance.image_cache.client.CacheClient,
|
||||
|
@ -319,13 +321,14 @@ class TestGlanceCmdManage(test_utils.BaseTestCase):
|
|||
|
||||
@mock.patch.object(glance.cmd.cache_manage, 'lookup_command')
|
||||
def test_parse_options_no_parameters(self, mock_lookup):
|
||||
oparser = optparse.OptionParser()
|
||||
cache_manage.create_options(oparser)
|
||||
with mock.patch('sys.stdout', new_callable=StringIO):
|
||||
oparser = optparse.OptionParser()
|
||||
cache_manage.create_options(oparser)
|
||||
|
||||
result = self.assertRaises(SystemExit, cache_manage.parse_options,
|
||||
oparser, [])
|
||||
self.assertEqual(0, result.code)
|
||||
self.assertFalse(mock_lookup.called)
|
||||
result = self.assertRaises(SystemExit, cache_manage.parse_options,
|
||||
oparser, [])
|
||||
self.assertEqual(0, result.code)
|
||||
self.assertFalse(mock_lookup.called)
|
||||
|
||||
@mock.patch.object(optparse.OptionParser, 'print_usage')
|
||||
def test_parse_options_no_arguments(self, mock_printout):
|
||||
|
|
|
@ -16,15 +16,16 @@
|
|||
import fixtures
|
||||
import mock
|
||||
from oslo_db.sqlalchemy import migration
|
||||
import testtools
|
||||
from six.moves import StringIO
|
||||
|
||||
from glance.cmd import manage
|
||||
from glance.db import migration as db_migration
|
||||
from glance.db.sqlalchemy import api as db_api
|
||||
from glance.db.sqlalchemy import metadata as db_metadata
|
||||
from glance.tests import utils as test_utils
|
||||
|
||||
|
||||
class TestManageBase(testtools.TestCase):
|
||||
class TestManageBase(test_utils.BaseTestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestManageBase, self).setUp()
|
||||
|
@ -35,18 +36,15 @@ class TestManageBase(testtools.TestCase):
|
|||
clear_conf()
|
||||
self.addCleanup(clear_conf)
|
||||
|
||||
self.patcher = mock.patch('glance.db.sqlalchemy.api.get_engine')
|
||||
self.patcher.start()
|
||||
self.addCleanup(self.patcher.stop)
|
||||
self.useFixture(fixtures.MonkeyPatch(
|
||||
'oslo_log.log.setup', lambda product_name, version='test': None))
|
||||
|
||||
patcher = mock.patch('glance.db.sqlalchemy.api.get_engine')
|
||||
patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
|
||||
def _main_test_helper(self, argv, func_name=None, *exp_args, **exp_kwargs):
|
||||
self.useFixture(fixtures.MonkeyPatch('sys.argv', argv))
|
||||
|
||||
def setup(product_name, version='unknown'):
|
||||
pass
|
||||
|
||||
self.useFixture(fixtures.MonkeyPatch(
|
||||
'oslo_log.setup', setup))
|
||||
manage.main()
|
||||
func_name.assert_called_once_with(*exp_args, **exp_kwargs)
|
||||
|
||||
|
@ -55,10 +53,11 @@ class TestLegacyManage(TestManageBase):
|
|||
|
||||
@mock.patch.object(migration, 'db_version')
|
||||
def test_legacy_db_version(self, db_version):
|
||||
self._main_test_helper(['glance.cmd.manage', 'db_version'],
|
||||
migration.db_version,
|
||||
db_api.get_engine(),
|
||||
db_migration.MIGRATE_REPO_PATH, 0)
|
||||
with mock.patch('sys.stdout', new_callable=StringIO):
|
||||
self._main_test_helper(['glance.cmd.manage', 'db_version'],
|
||||
migration.db_version,
|
||||
db_api.get_engine(),
|
||||
db_migration.MIGRATE_REPO_PATH, 0)
|
||||
|
||||
@mock.patch.object(migration, 'db_sync')
|
||||
def test_legacy_db_sync(self, db_sync):
|
||||
|
@ -167,10 +166,11 @@ class TestManage(TestManageBase):
|
|||
|
||||
@mock.patch.object(migration, 'db_version')
|
||||
def test_db_version(self, db_version):
|
||||
self._main_test_helper(['glance.cmd.manage', 'db', 'version'],
|
||||
migration.db_version,
|
||||
db_api.get_engine(),
|
||||
db_migration.MIGRATE_REPO_PATH, 0)
|
||||
with mock.patch('sys.stdout', new_callable=StringIO):
|
||||
self._main_test_helper(['glance.cmd.manage', 'db', 'version'],
|
||||
migration.db_version,
|
||||
db_api.get_engine(),
|
||||
db_migration.MIGRATE_REPO_PATH, 0)
|
||||
|
||||
@mock.patch.object(migration, 'db_sync')
|
||||
def test_db_sync(self, db_sync):
|
||||
|
|
Loading…
Reference in New Issue