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
This commit is contained in:
parent
292ec4c901
commit
1c92b539ff
31
neutron_lib/fixture.py
Normal file
31
neutron_lib/fixture.py
Normal file
@ -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
|
@ -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.
|
||||
|
||||
|
31
neutron_lib/tests/unit/test_fixture.py
Normal file
31
neutron_lib/tests/unit/test_fixture.py
Normal file
@ -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)
|
17
releasenotes/notes/directory-fixture-083c5c5f365670d6.yaml
Normal file
17
releasenotes/notes/directory-fixture-083c5c5f365670d6.yaml
Normal file
@ -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))
|
Loading…
Reference in New Issue
Block a user