From a0398278765f2854595bfa8e232ae4b9a6860aaf Mon Sep 17 00:00:00 2001 From: Eric Fried Date: Fri, 27 Sep 2019 11:04:57 -0500 Subject: [PATCH] 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 --- nova/test.py | 4 ++-- nova/tests/fixtures.py | 28 ---------------------------- nova/tests/unit/test_fixtures.py | 23 ++--------------------- 3 files changed, 4 insertions(+), 51 deletions(-) diff --git a/nova/test.py b/nova/test.py index 433722e3dde6..5f20110a36fb 100644 --- a/nova/test.py +++ b/nova/test.py @@ -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) diff --git a/nova/tests/fixtures.py b/nova/tests/fixtures.py index 2a4b2aa718f4..c77153a2ea8e 100644 --- a/nova/tests/fixtures.py +++ b/nova/tests/fixtures.py @@ -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() diff --git a/nova/tests/unit/test_fixtures.py b/nova/tests/unit/test_fixtures.py index f9845212529f..d724f5c39b74 100644 --- a/nova/tests/unit/test_fixtures.py +++ b/nova/tests/unit/test_fixtures.py @@ -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'))