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

This commit:

- makes "both" the default target for clear_cache, as described by the
  documentation;
- makes sure an InvalidRequest exception is raised if another target is
  passed.

Change-Id: I61fccad78fc1b280395e0c590caaa2ee73586d93
This commit is contained in:
Cyril Roelandt 2024-03-19 04:12:47 +01:00
parent 601b21acad
commit 8b84bf0e59
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) cache = self._get_resource(_cache.Cache, None)
return cache.queue(self, image_id) 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 """Clear all images from cache, queue or both
:param target: Specify which target you want to clear :param target: Specify which target you want to clear

View File

@ -62,8 +62,12 @@ class Cache(resource.Resource):
:returns: The server response :returns: The server response
""" """
headers = {} headers = {}
if target != "both": if target in ('cache', 'queue'):
headers = {'x-image-cache-clear-target': target} 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) response = session.delete(self.base_path, headers=headers)
exceptions.raise_from_response(response) exceptions.raise_from_response(response)
return response return response

View File

@ -69,5 +69,22 @@ class TestCache(base.TestCase):
session = mock.Mock() session = mock.Mock()
session.delete = mock.Mock() session.delete = mock.Mock()
sot.clear(session)
session.delete.assert_called_with('/cache', headers={})
sot.clear(session, 'both') sot.clear(session, 'both')
session.delete.assert_called_with('/cache', headers={}) 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

@ -974,3 +974,12 @@ class TestCache(TestImageProxy):
expected_args=[self.proxy, 'both'], expected_args=[self.proxy, 'both'],
) )
mock_get_resource.assert_called_once_with(_cache.Cache, None) 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)