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:
Armando Migliaccio 2016-12-16 13:25:25 -08:00
parent 292ec4c901
commit 1c92b539ff
4 changed files with 81 additions and 8 deletions

31
neutron_lib/fixture.py Normal file
View 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

View File

@ -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.

View 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)

View 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))