From 1c92b539ffd5418c39f2e38047fd8e4cf0ff3d58 Mon Sep 17 00:00:00 2001 From: Armando Migliaccio Date: Fri, 16 Dec 2016 13:25:25 -0800 Subject: [PATCH] Add fixture for plugin directory This is needed to stop stubbing out _get_plugin_directory from neutron unit tests, thus preserving testing isolation. Change-Id: Ic94b264068955c67d71b86fe54825b9fba4533ac --- neutron_lib/fixture.py | 31 +++++++++++++++++++ neutron_lib/tests/_base.py | 10 ++---- neutron_lib/tests/unit/test_fixture.py | 31 +++++++++++++++++++ .../directory-fixture-083c5c5f365670d6.yaml | 17 ++++++++++ 4 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 neutron_lib/fixture.py create mode 100644 neutron_lib/tests/unit/test_fixture.py create mode 100644 releasenotes/notes/directory-fixture-083c5c5f365670d6.yaml diff --git a/neutron_lib/fixture.py b/neutron_lib/fixture.py new file mode 100644 index 000000000..991c4175e --- /dev/null +++ b/neutron_lib/fixture.py @@ -0,0 +1,31 @@ +# 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 fixtures + +from neutron_lib.plugins import directory + + +class PluginDirectoryFixture(fixtures.Fixture): + + def __init__(self, plugin_directory=None): + super(PluginDirectoryFixture, self).__init__() + self.plugin_directory = ( + plugin_directory or directory._PluginDirectory()) + + def _setUp(self): + self._orig_directory = directory._PLUGIN_DIRECTORY + directory._PLUGIN_DIRECTORY = self.plugin_directory + self.addCleanup(self._restore) + + def _restore(self): + directory._PLUGIN_DIRECTORY = self._orig_directory diff --git a/neutron_lib/tests/_base.py b/neutron_lib/tests/_base.py index 3662269ce..9756b27a7 100644 --- a/neutron_lib/tests/_base.py +++ b/neutron_lib/tests/_base.py @@ -29,7 +29,7 @@ import testtools from neutron_lib._i18n import _ from neutron_lib import constants from neutron_lib import exceptions -from neutron_lib.plugins import directory +from neutron_lib import fixture from neutron_lib.tests import _post_mortem_debug as post_mortem_debug from neutron_lib.tests import _tools as tools @@ -117,7 +117,7 @@ class BaseTestCase(testtools.TestCase): def setUp(self): super(BaseTestCase, self).setUp() - self.setup_test_directory_instance() + self.useFixture(fixture.PluginDirectoryFixture()) # Enabling 'use_fatal_exceptions' allows us to catch string # substitution format errors in exception messages. @@ -183,12 +183,6 @@ class BaseTestCase(testtools.TestCase): self.addOnException(self.check_for_systemexit) self.orig_pid = os.getpid() - def setup_test_directory_instance(self): - """Give a private copy of the directory to each test.""" - self._plugin_directory = directory._PluginDirectory() - mock.patch.object(directory, '_get_plugin_directory', - return_value=self._plugin_directory).start() - def get_new_temp_dir(self): """Create a new temporary directory. diff --git a/neutron_lib/tests/unit/test_fixture.py b/neutron_lib/tests/unit/test_fixture.py new file mode 100644 index 000000000..9864a6197 --- /dev/null +++ b/neutron_lib/tests/unit/test_fixture.py @@ -0,0 +1,31 @@ +# 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 mock + +from oslotest import base + +from neutron_lib import fixture +from neutron_lib.plugins import directory + + +class PluginDirectoryFixtureTestCase(base.BaseTestCase): + + def setUp(self): + super(PluginDirectoryFixtureTestCase, self).setUp() + self.directory = mock.Mock() + self.useFixture(fixture.PluginDirectoryFixture( + plugin_directory=self.directory)) + + def test_fixture(self): + directory.add_plugin('foo', 'foo') + self.assertTrue(self.directory.add_plugin.called) diff --git a/releasenotes/notes/directory-fixture-083c5c5f365670d6.yaml b/releasenotes/notes/directory-fixture-083c5c5f365670d6.yaml new file mode 100644 index 000000000..36971f8a1 --- /dev/null +++ b/releasenotes/notes/directory-fixture-083c5c5f365670d6.yaml @@ -0,0 +1,17 @@ +--- +features: + - | + Introduced neutron_lib.fixture, and added fixture for plugin + directory ``PluginDirectoryFixture``. An example below: + + .. code-block:: python + + from neutron_lib.plugins import directory + from neutron_lib import fixture + + + def setup_test_directory_instance(self): + """Give a private copy of the directory to each test.""" + self._plugin_directory = directory._PluginDirectory() + self.useFixture(fixture.PluginDirectoryFixture( + plugin_directory=self._plugin_directory))