[placement] Use a non-nova log capture fixture

We want to avoid using the nova StandardLogging fixture, to limit
imports from nova, but it has two useful features that we want:

* always capture
* if the chosen log level is more than DEBUG, format DEBUG message
  anyway, but don't output

blueprint: placement-extract

Change-Id: Iadd32c731ebfb5a62308a4d5f907a69f93590935
This commit is contained in:
Chris Dent 2018-07-30 16:45:50 +01:00
parent bee0a133e5
commit 1de7ac302a
4 changed files with 65 additions and 4 deletions

View File

@ -19,6 +19,7 @@ from nova.api.openstack.placement import context
from nova.api.openstack.placement import deploy
from nova.api.openstack.placement.objects import resource_provider
from nova.tests import fixtures
from nova.tests.functional.api.openstack.placement.fixtures import capture
from nova.tests.unit import policy_fixture
@ -48,7 +49,7 @@ class TestCase(testtools.TestCase):
self.useFixture(policy_fixture.PlacementPolicyFixture())
self.useFixture(fixtures.StandardLogging())
self.useFixture(capture.Logging())
self.useFixture(output.CaptureOutput())
# Filter ignorable warnings during test runs.
self.useFixture(fixtures.WarningsFixture())

View File

@ -0,0 +1,59 @@
# All Rights Reserved.
#
# 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 logging
import fixtures
from oslotest import log
class NullHandler(logging.Handler):
"""custom default NullHandler to attempt to format the record.
Used in conjunction with Logging below to detect formatting errors
in debug logs.
"""
def handle(self, record):
self.format(record)
def emit(self, record):
pass
def createLock(self):
self.lock = None
class Logging(log.ConfigureLogging):
"""A logging fixture providing two important fixtures.
One is to capture logs for later inspection.
The other is to make sure that DEBUG logs, even if not captured,
are formatted.
"""
def __init__(self):
super(Logging, self).__init__()
# If level was not otherwise set, default to INFO.
if self.level is None:
self.level = logging.INFO
# Always capture logs, unlike the parent.
self.capture_logs = True
def setUp(self):
super(Logging, self).setUp()
if self.level > logging.DEBUG:
handler = NullHandler()
self.useFixture(fixtures.LogHandler(handler, nuke_handlers=False))
handler.setLevel(logging.DEBUG)

View File

@ -30,6 +30,7 @@ from nova.api.openstack.placement import policies
from nova import rc_fields as fields
from nova.tests import fixtures
from nova.tests.functional.api.openstack.placement.db import test_base as tb
from nova.tests.functional.api.openstack.placement.fixtures import capture
from nova.tests.unit import policy_fixture
from nova.tests import uuidsentinel as uuids
@ -49,7 +50,7 @@ class APIFixture(fixture.GabbiFixture):
# existing nova fixtures that do that. This captures the
# output that happens outside individual tests (for
# example database migrations).
self.standard_logging_fixture = fixtures.StandardLogging()
self.standard_logging_fixture = capture.Logging()
self.standard_logging_fixture.setUp()
self.output_stream_fixture = output.CaptureOutput()
self.output_stream_fixture.setUp()

View File

@ -17,7 +17,7 @@ import wsgi_intercept
from gabbi import driver
from nova.tests import fixtures as nova_fixtures
from nova.tests.functional.api.openstack.placement.fixtures import capture
# TODO(cdent): This whitespace blight will go away post extraction.
from nova.tests.functional.api.openstack.placement.fixtures \
import gabbits as fixtures
@ -35,7 +35,7 @@ def load_tests(loader, tests, pattern):
# capture, for cleaner results reporting.
inner_fixtures = [
output.CaptureOutput,
nova_fixtures.StandardLogging,
capture.Logging,
]
return driver.build_tests(test_dir, loader, host=None,
test_loader_name=__name__,