Merge "Undecorate cache decorated methods on null cache"

This commit is contained in:
Jenkins 2015-10-19 13:29:22 +00:00 committed by Gerrit Code Review
commit aa7fd89a36
2 changed files with 51 additions and 18 deletions

View File

@ -13,6 +13,7 @@
# limitations under the License.
import contextlib
import functools
import hashlib
import inspect
import logging
@ -165,6 +166,9 @@ def operator_cloud(config=None, **kwargs):
cloud_config=cloud_config)
_decorated_methods = []
def _cache_on_arguments(*cache_on_args, **cache_on_kwargs):
def _inner_cache_on_arguments(func):
def _cache_decorator(obj, *args, **kwargs):
@ -178,6 +182,8 @@ def _cache_on_arguments(*cache_on_args, **cache_on_kwargs):
*args, **kwargs)
_cache_decorator.invalidate = invalidate
_cache_decorator.func = func
_decorated_methods.append(func.__name__)
return _cache_decorator
return _inner_cache_on_arguments
@ -277,16 +283,41 @@ class OpenStackCloud(object):
(self.verify, self.cert) = cloud_config.get_requests_verify_args()
self._cache = cache.make_region(
function_key_generator=self._make_cache_key
).configure(
cache_class, expiration_time=cache_interval,
arguments=cache_arguments)
self._servers = []
self._servers_time = 0
self._servers_lock = threading.Lock()
if cache_class != 'dogpile.cache.null':
self._cache = cache.make_region(
function_key_generator=self._make_cache_key
).configure(
cache_class, expiration_time=cache_interval,
arguments=cache_arguments)
else:
def _fake_invalidate(unused):
pass
class _FakeCache(object):
def invalidate(self):
pass
# Don't cache list_servers if we're not caching things.
# Replace this with a more specific cache configuration
# soon.
self._SERVER_LIST_AGE = 0
self._cache = _FakeCache()
# Undecorate cache decorated methods. Otherwise the call stacks
# wind up being stupidly long and hard to debug
for method in _decorated_methods:
meth_obj = getattr(self, method, None)
if not meth_obj:
continue
if (hasattr(meth_obj, 'invalidate')
and hasattr(meth_obj, 'func')):
new_func = functools.partial(meth_obj.func, self)
new_func.invalidate = _fake_invalidate
setattr(self, method, new_func)
self._container_cache = dict()
self._file_hash_cache = dict()

View File

@ -275,8 +275,9 @@ class TestShade(base.TestCase):
self.cloud.update_subnet('123', subnet_name='goofy')
self.assertTrue(mock_client.update_subnet.called)
@mock.patch.object(shade.OpenStackCloud, 'list_flavors')
def test_get_flavor_by_ram(self, mock_list):
@mock.patch.object(shade.OpenStackCloud, 'nova_client')
def test_get_flavor_by_ram(self, mock_nova_client):
class Flavor1(object):
id = '1'
name = 'vanilla ice cream'
@ -289,12 +290,12 @@ class TestShade(base.TestCase):
vanilla = meta.obj_to_dict(Flavor1())
chocolate = meta.obj_to_dict(Flavor2())
mock_list.return_value = [vanilla, chocolate]
mock_nova_client.flavors.list.return_value = [vanilla, chocolate]
flavor = self.cloud.get_flavor_by_ram(ram=150)
self.assertEquals(chocolate, flavor)
@mock.patch.object(shade.OpenStackCloud, 'list_flavors')
def test_get_flavor_by_ram_and_include(self, mock_list):
@mock.patch.object(shade.OpenStackCloud, 'nova_client')
def test_get_flavor_by_ram_and_include(self, mock_nova_client):
class Flavor1(object):
id = '1'
name = 'vanilla ice cream'
@ -313,26 +314,27 @@ class TestShade(base.TestCase):
vanilla = meta.obj_to_dict(Flavor1())
chocolate = meta.obj_to_dict(Flavor2())
strawberry = meta.obj_to_dict(Flavor3())
mock_list.return_value = [vanilla, chocolate, strawberry]
mock_nova_client.flavors.list.return_value = [
vanilla, chocolate, strawberry]
flavor = self.cloud.get_flavor_by_ram(ram=150, include='strawberry')
self.assertEquals(strawberry, flavor)
@mock.patch.object(shade.OpenStackCloud, 'list_flavors')
def test_get_flavor_by_ram_not_found(self, mock_list):
mock_list.return_value = []
@mock.patch.object(shade.OpenStackCloud, 'nova_client')
def test_get_flavor_by_ram_not_found(self, mock_nova_client):
mock_nova_client.flavors.list.return_value = []
self.assertRaises(shade.OpenStackCloudException,
self.cloud.get_flavor_by_ram,
ram=100)
@mock.patch.object(shade.OpenStackCloud, 'list_flavors')
def test_get_flavor_string_and_int(self, mock_list):
@mock.patch.object(shade.OpenStackCloud, 'nova_client')
def test_get_flavor_string_and_int(self, mock_nova_client):
class Flavor1(object):
id = '1'
name = 'vanilla ice cream'
ram = 100
vanilla = meta.obj_to_dict(Flavor1())
mock_list.return_value = [vanilla]
mock_nova_client.flavors.list.return_value = [vanilla]
flavor1 = self.cloud.get_flavor('1')
self.assertEquals(vanilla, flavor1)
flavor2 = self.cloud.get_flavor(1)