Clean up stdout/stderr leakage in cmd testing
This cleans up stdout/stderr leaking in cmd testing by using the pattern of MonkeyPatching stdout/stderr to a StringIO object where appropriate. The OutputStreamCapture fixture was using in one place, but that actually is just about moving the output to the subunit stream. That output will eventually be dumped at the end of the runs. When we really don't want that to happen, we need to capture it directly. Change-Id: Ib788ceccd72677d4602b6e6b77e74a4abff8fec5
This commit is contained in:
parent
ffaf1b63e9
commit
6181398939
|
@ -12,9 +12,11 @@
|
|||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import fixtures
|
||||
import mock
|
||||
from oslo_log import log as logging
|
||||
from oslo_reports import guru_meditation_report as gmr
|
||||
from six.moves import StringIO
|
||||
|
||||
from nova.cmd import baseproxy
|
||||
from nova import config
|
||||
|
@ -26,6 +28,11 @@ from nova import version
|
|||
@mock.patch.object(config, 'parse_args', new=lambda *args, **kwargs: None)
|
||||
class BaseProxyTestCase(test.NoDBTestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(BaseProxyTestCase, self).setUp()
|
||||
self.stderr = StringIO()
|
||||
self.useFixture(fixtures.MonkeyPatch('sys.stderr', self.stderr))
|
||||
|
||||
@mock.patch('os.path.exists', return_value=False)
|
||||
# NOTE(mriedem): sys.exit raises TestingException so we can actually exit
|
||||
# the test normally.
|
||||
|
@ -35,6 +42,8 @@ class BaseProxyTestCase(test.NoDBTestCase):
|
|||
self.assertRaises(test.TestingException, baseproxy.proxy,
|
||||
'0.0.0.0', '6080')
|
||||
mock_exit.assert_called_once_with(-1)
|
||||
self.assertEqual(self.stderr.getvalue(),
|
||||
"SSL only and self.pem not found\n")
|
||||
|
||||
@mock.patch('os.path.exists', return_value=False)
|
||||
@mock.patch('sys.exit', side_effect=test.TestingException)
|
||||
|
@ -63,13 +72,12 @@ class BaseProxyTestCase(test.NoDBTestCase):
|
|||
RequestHandlerClass=websocketproxy.NovaProxyRequestHandler)
|
||||
mock_start.assert_called_once_with()
|
||||
|
||||
@mock.patch('sys.stderr.write')
|
||||
@mock.patch('os.path.exists', return_value=False)
|
||||
@mock.patch('sys.exit', side_effect=test.TestingException)
|
||||
def test_proxy_exit_with_error(self, mock_exit, mock_exists, mock_stderr):
|
||||
def test_proxy_exit_with_error(self, mock_exit, mock_exists):
|
||||
self.flags(ssl_only=True)
|
||||
self.assertRaises(test.TestingException, baseproxy.proxy,
|
||||
'0.0.0.0', '6080')
|
||||
mock_stderr.assert_called_once_with(
|
||||
'SSL only and self.pem not found\n')
|
||||
self.assertEqual(self.stderr.getvalue(),
|
||||
"SSL only and self.pem not found\n")
|
||||
mock_exit.assert_called_once_with(-1)
|
||||
|
|
|
@ -17,7 +17,9 @@
|
|||
Unit tests for the common functions used by different CLI interfaces.
|
||||
"""
|
||||
|
||||
import fixtures
|
||||
import mock
|
||||
from six.moves import StringIO
|
||||
|
||||
from nova.cmd import common as cmd_common
|
||||
from nova.db import api
|
||||
|
@ -128,6 +130,8 @@ class TestCmdCommon(test.NoDBTestCase):
|
|||
@mock.patch.object(cmd_common.utils, 'validate_args')
|
||||
@mock.patch.object(cmd_common, 'CONF')
|
||||
def test_get_action_fn_missing_args(self, mock_CONF, mock_validate_args):
|
||||
# Don't leak the actual print call
|
||||
self.useFixture(fixtures.MonkeyPatch('sys.stdout', StringIO()))
|
||||
mock_validate_args.return_value = ['foo']
|
||||
mock_CONF.category.action_fn = mock.sentinel.action_fn
|
||||
mock_CONF.category.action_args = []
|
||||
|
|
|
@ -14,7 +14,9 @@
|
|||
|
||||
import argparse
|
||||
|
||||
import fixtures
|
||||
import mock
|
||||
from six.moves import StringIO
|
||||
|
||||
from nova.cmd import idmapshift
|
||||
from nova import test
|
||||
|
@ -35,8 +37,10 @@ class FakeStat(object):
|
|||
|
||||
|
||||
class BaseTestCase(test.NoDBTestCase):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(BaseTestCase, self).__init__(*args, **kwargs)
|
||||
def setUp(self):
|
||||
super(BaseTestCase, self).setUp()
|
||||
self.useFixture(fixtures.MonkeyPatch('sys.stdout', StringIO()))
|
||||
|
||||
self.uid_maps = [(0, 10000, 10), (10, 20000, 1000)]
|
||||
self.gid_maps = [(0, 10000, 10), (10, 20000, 1000)]
|
||||
|
||||
|
|
|
@ -17,7 +17,9 @@
|
|||
Unit tests for the nova-policy-check CLI interfaces.
|
||||
"""
|
||||
|
||||
import fixtures
|
||||
import mock
|
||||
from six.moves import StringIO
|
||||
|
||||
from nova.cmd import policy_check
|
||||
import nova.conf
|
||||
|
@ -27,7 +29,6 @@ from nova import exception
|
|||
from nova.policies import base as base_policies
|
||||
from nova.policies import instance_actions as ia_policies
|
||||
from nova import test
|
||||
from nova.tests import fixtures
|
||||
from nova.tests.unit import fake_instance
|
||||
from nova.tests.unit import policy_fixture
|
||||
|
||||
|
@ -38,7 +39,8 @@ class TestPolicyCheck(test.NoDBTestCase):
|
|||
|
||||
def setUp(self):
|
||||
super(TestPolicyCheck, self).setUp()
|
||||
self.output = self.useFixture(fixtures.OutputStreamCapture())
|
||||
self.output = StringIO()
|
||||
self.useFixture(fixtures.MonkeyPatch('sys.stdout', self.output))
|
||||
self.policy = self.useFixture(policy_fixture.RealPolicyFixture())
|
||||
self.cmd = policy_check.PolicyCommands()
|
||||
|
||||
|
@ -57,7 +59,7 @@ class TestPolicyCheck(test.NoDBTestCase):
|
|||
mock.sentinel.target)
|
||||
mock_filter_rules.assert_called_once_with(
|
||||
mock_get_context.return_value, '', mock_get_target.return_value)
|
||||
self.assertEqual('\n'.join(fake_rules), self.output.stdout)
|
||||
self.assertEqual('\n'.join(fake_rules) + '\n', self.output.getvalue())
|
||||
|
||||
@mock.patch.object(nova_context, 'RequestContext')
|
||||
@mock.patch.object(policy_check, 'CONF')
|
||||
|
@ -170,7 +172,7 @@ class TestPolicyCheck(test.NoDBTestCase):
|
|||
return_value="x.x.x")
|
||||
def test_main_version(self, mock_version_string):
|
||||
self._check_main(category_name='version')
|
||||
self.assertEqual("x.x.x", self.output.stdout)
|
||||
self.assertEqual("x.x.x\n", self.output.getvalue())
|
||||
|
||||
@mock.patch.object(policy_check.cmd_common, 'print_bash_completion')
|
||||
def test_main_bash_completion(self, mock_print_bash):
|
||||
|
@ -195,4 +197,4 @@ class TestPolicyCheck(test.NoDBTestCase):
|
|||
mock_get_action_fn.return_value = (mock_fn, [], {})
|
||||
|
||||
self._check_main(expected_return_value=1)
|
||||
self.assertIn("error: ", self.output.stdout)
|
||||
self.assertIn("error: ", self.output.getvalue())
|
||||
|
|
Loading…
Reference in New Issue