Bug fix: delete_object() returns True/False

Our delete APIs return True if the delete succeeded, or False if
the thing being deleted was not found. delete_object() was not doing
this, so this makes it consistent with the other delete API calls.

Also adds missing unit tests for this method.

Change-Id: I0951765193459300f08b0ab804e6ca327c6fa57d
This commit is contained in:
David Shrewsbury 2015-12-11 15:45:42 -05:00
parent 59d448dcc0
commit 8d5abfbf56
3 changed files with 50 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
fixes:
- The delete_object() method was not returning True/False,
similar to other delete methods. It is now consistent with
the other delete APIs.

View File

@ -3646,8 +3646,17 @@ class OpenStackCloud(object):
e.http_reason, e.http_host, e.http_path)) e.http_reason, e.http_host, e.http_path))
def delete_object(self, container, name): def delete_object(self, container, name):
"""Delete an object from a container.
:param string container: Name of the container holding the object.
:param string name: Name of the object to delete.
:returns: True if delete succeeded, False if the object was not found.
:raises: OpenStackCloudException on operation error.
"""
if not self.get_object_metadata(container, name): if not self.get_object_metadata(container, name):
return return False
try: try:
self.manager.submitTask(_tasks.ObjectDelete( self.manager.submitTask(_tasks.ObjectDelete(
container=container, obj=name)) container=container, obj=name))
@ -3655,6 +3664,7 @@ class OpenStackCloud(object):
raise OpenStackCloudException( raise OpenStackCloudException(
"Object deletion failed: %s (%s/%s)" % ( "Object deletion failed: %s (%s/%s)" % (
e.http_reason, e.http_host, e.http_path)) e.http_reason, e.http_host, e.http_path))
return True
def get_object_metadata(self, container, name): def get_object_metadata(self, container, name):
try: try:

View File

@ -288,3 +288,37 @@ class TestObject(base.TestCase):
"ERROR") "ERROR")
self.assertRaises(exc.OpenStackCloudException, self.assertRaises(exc.OpenStackCloudException,
self.cloud.list_objects, 'container_name') self.cloud.list_objects, 'container_name')
@mock.patch.object(shade.OpenStackCloud, 'get_object_metadata')
@mock.patch.object(shade.OpenStackCloud, 'swift_client')
def test_delete_object(self, mock_swift, mock_get_meta):
container_name = 'container_name'
object_name = 'object_name'
mock_get_meta.return_value = {'object': object_name}
self.assertTrue(self.cloud.delete_object(container_name, object_name))
mock_get_meta.assert_called_once_with(container_name, object_name)
mock_swift.delete_object.assert_called_once_with(
container=container_name, obj=object_name
)
@mock.patch.object(shade.OpenStackCloud, 'get_object_metadata')
@mock.patch.object(shade.OpenStackCloud, 'swift_client')
def test_delete_object_not_found(self, mock_swift, mock_get_meta):
container_name = 'container_name'
object_name = 'object_name'
mock_get_meta.return_value = None
self.assertFalse(self.cloud.delete_object(container_name, object_name))
mock_get_meta.assert_called_once_with(container_name, object_name)
self.assertFalse(mock_swift.delete_object.called)
@mock.patch.object(shade.OpenStackCloud, 'get_object_metadata')
@mock.patch.object(shade.OpenStackCloud, 'swift_client')
def test_delete_object_exception(self, mock_swift, mock_get_meta):
container_name = 'container_name'
object_name = 'object_name'
mock_get_meta.return_value = {'object': object_name}
mock_swift.delete_object.side_effect = swift_exc.ClientException(
"ERROR")
self.assertRaises(shade.OpenStackCloudException,
self.cloud.delete_object,
container_name, object_name)