Merge "image: make sure the target for "clear_cache" is valid"

This commit is contained in:
Zuul 2024-04-22 17:06:36 +00:00 committed by Gerrit Code Review
commit d0718dce12
4 changed files with 32 additions and 2 deletions

View File

@ -101,7 +101,7 @@ class Proxy(proxy.Proxy):
cache = self._get_resource(_cache.Cache, None)
return cache.queue(self, image_id)
def clear_cache(self, target):
def clear_cache(self, target='both'):
"""Clear all images from cache, queue or both
:param target: Specify which target you want to clear

View File

@ -62,8 +62,12 @@ class Cache(resource.Resource):
:returns: The server response
"""
headers = {}
if target != "both":
if target in ('cache', 'queue'):
headers = {'x-image-cache-clear-target': target}
elif target != "both":
raise exceptions.InvalidRequest(
'Target must be "cache", "queue" or "both".'
)
response = session.delete(self.base_path, headers=headers)
exceptions.raise_from_response(response)
return response

View File

@ -69,5 +69,22 @@ class TestCache(base.TestCase):
session = mock.Mock()
session.delete = mock.Mock()
sot.clear(session)
session.delete.assert_called_with('/cache', headers={})
sot.clear(session, 'both')
session.delete.assert_called_with('/cache', headers={})
sot.clear(session, 'cache')
session.delete.assert_called_with(
'/cache', headers={'x-image-cache-clear-target': 'cache'}
)
sot.clear(session, 'queue')
session.delete.assert_called_with(
'/cache', headers={'x-image-cache-clear-target': 'queue'}
)
self.assertRaises(
exceptions.InvalidRequest, sot.clear, session, 'invalid'
)

View File

@ -982,3 +982,12 @@ class TestCache(TestImageProxy):
expected_args=[self.proxy, 'both'],
)
mock_get_resource.assert_called_once_with(_cache.Cache, None)
mock_get_resource.reset_mock()
self._verify(
"openstack.image.v2.cache.Cache.clear",
self.proxy.clear_cache,
method_args=[],
expected_args=[self.proxy, 'both'],
)
mock_get_resource.assert_called_once_with(_cache.Cache, None)