test cleanup: Use oslotest's CaptureOutput fixture

Nova had its own copy of oslotest's CaptureOutput fixture called
OutputStreamCapture (probably predating the oslotest one, actually). DRY
it up.

A future change should be able to remove references to this fixture from
nova.test.TestCase by inheriting from oslotest.base.BaseTestCase instead
of testtools.TestCase. But we may or may not want to do the same for
TestOSAPIFixture since that would pull in fixtures we don't care about
(timeout and tempfile/tempdir).

Change-Id: I18a5d621c9e414452852d4aeb0379be0d30af5df
This commit is contained in:
Eric Fried 2019-09-27 11:04:57 -05:00
parent 5617b1ae96
commit a039827876
3 changed files with 4 additions and 51 deletions

View File

@ -47,6 +47,7 @@ from oslo_utils import timeutils
from oslo_versionedobjects import fixture as ovo_fixture
from oslotest import mock_fixture
from oslotest import moxstubout
from oslotest import output
from oslotest import timeout
import six
from six.moves import builtins
@ -188,8 +189,7 @@ class TestCase(testtools.TestCase):
self.useFixture(fixtures.TempHomeDir())
self.useFixture(log_fixture.get_logging_handle_error_fixture())
self.output = nova_fixtures.OutputStreamCapture()
self.useFixture(self.output)
self.output = self.useFixture(output.CaptureOutput())
self.stdlog = nova_fixtures.StandardLogging()
self.useFixture(self.stdlog)

View File

@ -203,34 +203,6 @@ class StandardLogging(fixtures.Fixture):
self.logger._output.truncate(0)
class OutputStreamCapture(fixtures.Fixture):
"""Capture output streams during tests.
This fixture captures errant printing to stderr / stdout during
the tests and lets us see those streams at the end of the test
runs instead. Useful to see what was happening during failed
tests.
"""
def setUp(self):
super(OutputStreamCapture, self).setUp()
if os.environ.get('OS_STDOUT_CAPTURE') in _TRUE_VALUES:
self.out = self.useFixture(fixtures.StringStream('stdout'))
self.useFixture(
fixtures.MonkeyPatch('sys.stdout', self.out.stream))
if os.environ.get('OS_STDERR_CAPTURE') in _TRUE_VALUES:
self.err = self.useFixture(fixtures.StringStream('stderr'))
self.useFixture(
fixtures.MonkeyPatch('sys.stderr', self.err.stream))
@property
def stderr(self):
return self.err._details["stderr"].as_text()
@property
def stdout(self):
return self.out._details["stdout"].as_text()
class DatabasePoisonFixture(fixtures.Fixture):
def setUp(self):
super(DatabasePoisonFixture, self).setUp()

View File

@ -15,7 +15,6 @@
# under the License.
import copy
import sys
import fixtures as fx
import futurist
@ -26,6 +25,7 @@ from oslo_log import log as logging
from oslo_utils.fixture import uuidsentinel as uuids
from oslo_utils import timeutils
from oslo_utils import uuidutils
from oslotest import output
import sqlalchemy
import testtools
@ -88,25 +88,6 @@ class TestConfFixture(testtools.TestCase):
self._test_override()
class TestOutputStream(testtools.TestCase):
"""Ensure Output Stream capture works as expected.
This has the added benefit of providing a code example of how you
can manipulate the output stream in your own tests.
"""
def test_output(self):
self.useFixture(fx.EnvironmentVariable('OS_STDOUT_CAPTURE', '1'))
self.useFixture(fx.EnvironmentVariable('OS_STDERR_CAPTURE', '1'))
out = self.useFixture(fixtures.OutputStreamCapture())
sys.stdout.write("foo")
sys.stderr.write("bar")
self.assertEqual("foo", out.stdout)
self.assertEqual("bar", out.stderr)
# TODO(sdague): nuke the out and err buffers so it doesn't
# make it to testr
class TestLogging(testtools.TestCase):
def test_default_logging(self):
stdlog = self.useFixture(fixtures.StandardLogging())
@ -150,7 +131,7 @@ class TestOSAPIFixture(testtools.TestCase):
@mock.patch('nova.objects.Service.create')
def test_responds_to_version(self, mock_service_create, mock_get):
"""Ensure the OSAPI server responds to calls sensibly."""
self.useFixture(fixtures.OutputStreamCapture())
self.useFixture(output.CaptureOutput())
self.useFixture(fixtures.StandardLogging())
self.useFixture(conf_fixture.ConfFixture())
self.useFixture(fixtures.RPCFixture('nova.test'))