From 56931099019987a343bdeae432dd15ebea5d7c33 Mon Sep 17 00:00:00 2001 From: Ryan Peters Date: Wed, 22 Jul 2015 15:34:53 -0500 Subject: [PATCH] Fixed getting success msg on failing swift operation Show error message instead of success message when attempting to create a duplicate object or pseudo-folder. Added tests for creating and creating duplicate pseudo-folders. Change-Id: I875d471921d9a37bb6706a17d4fbca5e9428ae93 Closes-Bug: #1478126 --- openstack_dashboard/api/swift.py | 6 +++ .../test/api_tests/swift_tests.py | 54 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/openstack_dashboard/api/swift.py b/openstack_dashboard/api/swift.py index 2f20b03f30..eba627df9e 100644 --- a/openstack_dashboard/api/swift.py +++ b/openstack_dashboard/api/swift.py @@ -277,6 +277,8 @@ def swift_copy_object(request, orig_container_name, orig_object_name, def swift_upload_object(request, container_name, object_name, object_file=None): + if swift_object_exists(request, container_name, object_name): + raise exceptions.AlreadyExists(object_name, 'object') headers = {} size = 0 if object_file: @@ -294,6 +296,10 @@ def swift_upload_object(request, container_name, object_name, def swift_create_pseudo_folder(request, container_name, pseudo_folder_name): + # Make sure the folder name doesn't already exist. + if swift_object_exists(request, container_name, pseudo_folder_name): + name = pseudo_folder_name.strip('/') + raise exceptions.AlreadyExists(name, 'pseudo-folder') headers = {} etag = swift_api(request).put_object(container_name, pseudo_folder_name, diff --git a/openstack_dashboard/test/api_tests/swift_tests.py b/openstack_dashboard/test/api_tests/swift_tests.py index f2d96e851a..990aa8a8b6 100644 --- a/openstack_dashboard/test/api_tests/swift_tests.py +++ b/openstack_dashboard/test/api_tests/swift_tests.py @@ -170,6 +170,34 @@ class SwiftApiTests(test.APITestCase): self.assertEqual(object.name, obj.name) self.assertIsNone(obj.data) + def test_swift_create_pseudo_folder(self): + container = self.containers.first() + folder = self.folder.first() + swift_api = self.stub_swiftclient() + exc = self.exceptions.swift + swift_api.head_object(container.name, folder.name).AndRaise(exc) + swift_api.put_object(container.name, + folder.name, + None, + headers={}).AndReturn(folder) + self.mox.ReplayAll() + # Verification handled by mox, no assertions needed. + api.swift.swift_create_pseudo_folder(self.request, + container.name, + folder.name) + + def test_swift_create_duplicate_folder(self): + container = self.containers.first() + folder = self.folder.first() + swift_api = self.stub_swiftclient() + swift_api.head_object(container.name, folder.name).AndReturn(folder) + self.mox.ReplayAll() + # Verification handled by mox, no assertions needed. + with self.assertRaises(exceptions.AlreadyExists): + api.swift.swift_create_pseudo_folder(self.request, + container.name, + folder.name) + def test_swift_upload_object(self): container = self.containers.first() obj = self.objects.first() @@ -184,6 +212,8 @@ class SwiftApiTests(test.APITestCase): headers = {'X-Object-Meta-Orig-Filename': fake_name} swift_api = self.stub_swiftclient() + exc = self.exceptions.swift + swift_api.head_object(container.name, obj.name).AndRaise(exc) test_file = FakeFile() swift_api.put_object(container.name, obj.name, @@ -197,11 +227,35 @@ class SwiftApiTests(test.APITestCase): obj.name, test_file) + def test_swift_upload_duplicate_object(self): + container = self.containers.first() + obj = self.objects.first() + fake_name = 'fake_object.jpg' + + class FakeFile(object): + def __init__(self): + self.name = fake_name + self.data = obj.data + self.size = len(obj.data) + + swift_api = self.stub_swiftclient() + swift_api.head_object(container.name, obj.name).AndReturn(obj) + test_file = FakeFile() + self.mox.ReplayAll() + + with self.assertRaises(exceptions.AlreadyExists): + api.swift.swift_upload_object(self.request, + container.name, + obj.name, + test_file) + def test_swift_upload_object_without_file(self): container = self.containers.first() obj = self.objects.first() swift_api = self.stub_swiftclient() + exc = self.exceptions.swift + swift_api.head_object(container.name, obj.name).AndRaise(exc) swift_api.put_object(container.name, obj.name, None,