rehome used neutron.tests.tools
This patch rehomes the OpenFixture class and reset_random_seed function both which are from neutron.tests.tools as they are used by consumers today [1]. [1] http://codesearch.openstack.org/?q=from%20neutron.tests%20import%20tools Change-Id: I3d13a747f31e39c11f42e2f543eae3823cfbe356
This commit is contained in:
parent
fc2a81058b
commit
ef9c2ae111
@ -286,3 +286,25 @@ class DBResourceExtendFixture(fixtures.Fixture):
|
|||||||
|
|
||||||
def _restore(self):
|
def _restore(self):
|
||||||
resource_extend._resource_extend_functions = self._backup
|
resource_extend._resource_extend_functions = self._backup
|
||||||
|
|
||||||
|
|
||||||
|
class OpenFixture(fixtures.Fixture):
|
||||||
|
"""Mock access to a specific file while preserving open for others."""
|
||||||
|
|
||||||
|
def __init__(self, filepath, contents=''):
|
||||||
|
self.path = filepath
|
||||||
|
self.contents = contents
|
||||||
|
|
||||||
|
def _setUp(self):
|
||||||
|
self.mock_open = mock.mock_open(read_data=self.contents)
|
||||||
|
self._orig_open = open
|
||||||
|
|
||||||
|
def replacement_open(name, *args, **kwargs):
|
||||||
|
if name == self.path:
|
||||||
|
return self.mock_open(name, *args, **kwargs)
|
||||||
|
return self._orig_open(name, *args, **kwargs)
|
||||||
|
|
||||||
|
self._patch = mock.patch('six.moves.builtins.open',
|
||||||
|
new=replacement_open)
|
||||||
|
self._patch.start()
|
||||||
|
self.addCleanup(self._patch.stop)
|
||||||
|
@ -13,8 +13,10 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
import os
|
||||||
import platform
|
import platform
|
||||||
import random
|
import random
|
||||||
|
import time
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
import fixtures
|
import fixtures
|
||||||
@ -66,3 +68,13 @@ def get_random_cidr(version=4):
|
|||||||
random.randint(3, 254),
|
random.randint(3, 254),
|
||||||
24)
|
24)
|
||||||
return '2001:db8:%x::/%d' % (random.getrandbits(16), 64)
|
return '2001:db8:%x::/%d' % (random.getrandbits(16), 64)
|
||||||
|
|
||||||
|
|
||||||
|
def reset_random_seed():
|
||||||
|
# reset random seed to make sure other processes extracting values from RNG
|
||||||
|
# don't get the same results (useful especially when you then use the
|
||||||
|
# random values to allocate system resources from global pool, like ports
|
||||||
|
# to listen). Use both current time and pid to make sure no tests started
|
||||||
|
# at the same time get the same values from RNG
|
||||||
|
seed = time.time() + os.getpid()
|
||||||
|
random.seed(seed)
|
||||||
|
5
releasenotes/notes/rehome-testools-6fba053249e14d42.yaml
Normal file
5
releasenotes/notes/rehome-testools-6fba053249e14d42.yaml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
features:
|
||||||
|
- The ``OpenFixture`` class is now available in ``neutron_lib.fixtures``.
|
||||||
|
- The ``reset_random_seed`` function is now available in
|
||||||
|
``neutron_lib.tests.tools``.
|
Loading…
Reference in New Issue
Block a user