From e9c92d68b1c48404c62b0d248b5fdce6b8079e3a Mon Sep 17 00:00:00 2001 From: Matt Riedemann Date: Wed, 7 Jan 2015 14:10:01 -0800 Subject: [PATCH] Add WarningsFixture to only emit DeprecationWarning once in a test run The nova unit test console output can be flooded with deprecation warnings from dependent libraries, this change makes it so they are only logged once. It's worth noting that Keystone has an alternative implementation where they filter deprecation warnings and only error out if they are coming from Keystone code. See commit 9ae6ffe8a. Partial-Bug: #1407736 Co-authored-by: Sean Dague Change-Id: Id8d8866baaf64721fda2b6b2e8358db18920c8ba --- nova/test.py | 1 + nova/tests/fixtures.py | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/nova/test.py b/nova/test.py index 6d9b930e3..b1cea6116 100644 --- a/nova/test.py +++ b/nova/test.py @@ -198,6 +198,7 @@ class TestCase(testtools.TestCase): self.useFixture(nova_fixtures.OutputStreamCapture()) self.useFixture(nova_fixtures.StandardLogging()) + self.useFixture(nova_fixtures.WarningsFixture()) # NOTE(sdague): because of the way we were using the lock # wrapper we eneded up with a lot of tests that started diff --git a/nova/tests/fixtures.py b/nova/tests/fixtures.py index e725c7dca..652fae89a 100644 --- a/nova/tests/fixtures.py +++ b/nova/tests/fixtures.py @@ -21,6 +21,7 @@ import gettext import logging import os import uuid +import warnings import fixtures from oslo.config import cfg @@ -235,3 +236,15 @@ class RPCFixture(fixtures.Fixture): self.messaging_conf.transport_driver = 'fake' self.useFixture(self.messaging_conf) rpc.init(CONF) + + +class WarningsFixture(fixtures.Fixture): + """Filters out warnings during test runs.""" + + def setUp(self): + super(WarningsFixture, self).setUp() + # NOTE(sdague): Make deprecation warnings only happen once. Otherwise + # this gets kind of crazy given the way that upstream python libs use + # this. + warnings.simplefilter("once", DeprecationWarning) + self.addCleanup(warnings.resetwarnings)