Fix handling of swift object name == pseudo-folder name

Reinstate the proper path for swift pseudo-folders so
that they can be differentiated when adjacent to objects
with the same name.

In adding the test code for this patch the test data
also had to be modified to be more correctly mirroring
the API response data we get.

Change-Id: If0df0be612c0eeb999e2633eb6a00da80b2e0cf8
Closes-Bug: 1596568
This commit is contained in:
Richard Jones 2016-06-29 10:45:14 +10:00
parent 36e6bea4ce
commit 086b33a978
4 changed files with 32 additions and 13 deletions

View File

@ -130,7 +130,7 @@ class Objects(generic.View):
# filter out the folder from the listing if we're filtering for # filter out the folder from the listing if we're filtering for
# contents of a (pseudo) folder # contents of a (pseudo) folder
contents = [{ contents = [{
'path': o.name, 'path': o.subdir if isinstance(o, swift.PseudoFolder) else o.name,
'name': o.name.split('/')[-1], 'name': o.name.split('/')[-1],
'bytes': o.bytes, 'bytes': o.bytes,
'is_subdir': isinstance(o, swift.PseudoFolder), 'is_subdir': isinstance(o, swift.PseudoFolder),

View File

@ -81,7 +81,7 @@
ctrl.tableConfig = { ctrl.tableConfig = {
selectAll: true, selectAll: true,
expand: false, expand: false,
trackId: 'name', trackId: 'path',
columns: [ columns: [
{ {
id: 'name', title: 'Name', priority: 1, sortDefault: true, id: 'name', title: 'Name', priority: 1, sortDefault: true,

View File

@ -28,6 +28,7 @@ class SwiftRestTestCase(test.TestCase):
self._containers = TEST.containers.list() self._containers = TEST.containers.list()
self._objects = TEST.objects.list() self._objects = TEST.objects.list()
self._folder = TEST.folder.list() self._folder = TEST.folder.list()
self._folder_alt = TEST.folder_alt.list()
self._subfolder = TEST.subfolder.list() self._subfolder = TEST.subfolder.list()
# #
@ -117,15 +118,27 @@ class SwiftRestTestCase(test.TestCase):
@mock.patch.object(swift.api, 'swift') @mock.patch.object(swift.api, 'swift')
def test_objects_get(self, nc): def test_objects_get(self, nc):
request = self.mock_rest_request(GET={}) request = self.mock_rest_request(GET={})
nc.swift_get_objects.return_value = (self._objects, False) nc.swift_get_objects.return_value = (
self._objects + self._folder, False
)
response = swift.Objects().get(request, u'container one%\u6346') response = swift.Objects().get(request, u'container one%\u6346')
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(len(response.json['items']), 4) self.assertEqual(len(response.json['items']), 5)
self.assertEqual(response.json['items'][3]['path'], self.assertEqual(response.json['items'][3]['path'],
u'test folder%\u6346/test.txt') u'test folder%\u6346/test.txt')
self.assertEqual(response.json['items'][3]['name'], 'test.txt') self.assertEqual(response.json['items'][3]['name'], 'test.txt')
self.assertEqual(response.json['items'][3]['is_object'], True) self.assertEqual(response.json['items'][3]['is_object'], True)
self.assertEqual(response.json['items'][3]['is_subdir'], False) self.assertEqual(response.json['items'][3]['is_subdir'], False)
self.assertEqual(response.json['items'][3]['path'],
u'test folder%\u6346/test.txt')
self.assertEqual(response.json['items'][4]['path'],
u'test folder%\u6346/')
self.assertEqual(response.json['items'][4]['name'],
u'test folder%\u6346')
self.assertEqual(response.json['items'][4]['is_object'], False)
self.assertEqual(response.json['items'][4]['is_subdir'], True)
nc.swift_get_objects.assert_called_once_with(request, nc.swift_get_objects.assert_called_once_with(request,
u'container one%\u6346', u'container one%\u6346',
prefix=None) prefix=None)
@ -199,7 +212,7 @@ class SwiftRestTestCase(test.TestCase):
uf.return_value.is_valid.return_value = True uf.return_value.is_valid.return_value = True
uf.return_value.clean.return_value = {} uf.return_value.clean.return_value = {}
request = self.mock_rest_request() request = self.mock_rest_request()
nc.swift_create_pseudo_folder.return_value = self._folder[0] nc.swift_create_pseudo_folder.return_value = self._folder_alt[0]
response = swift.Object().post(request, 'spam', u'test_folder%\u6346/') response = swift.Object().post(request, 'spam', u'test_folder%\u6346/')
self.assertStatusCode(response, 201) self.assertStatusCode(response, 201)
self.assertEqual( self.assertEqual(

View File

@ -23,6 +23,7 @@ def data(TEST):
TEST.containers = utils.TestDataContainer() TEST.containers = utils.TestDataContainer()
TEST.objects = utils.TestDataContainer() TEST.objects = utils.TestDataContainer()
TEST.folder = utils.TestDataContainer() TEST.folder = utils.TestDataContainer()
TEST.folder_alt = utils.TestDataContainer()
TEST.subfolder = utils.TestDataContainer() TEST.subfolder = utils.TestDataContainer()
# '%' can break URL if not properly url-quoted # '%' can break URL if not properly url-quoted
@ -89,17 +90,22 @@ def data(TEST):
data=obj_data) data=obj_data)
TEST.objects.add(swift_object) TEST.objects.add(swift_object)
folder_dict = {"name": u"test folder%\u6346/", folder_dict = {"subdir": u"test folder%\u6346/"}
"content_type": u"application/pseudo-folder",
"bytes": 128,
"timestamp": timeutils.utcnow().isoformat(),
"_table_data_type": u"subfolders",
"last_modified": None,
"hash": u"object_hash"}
TEST.folder.add(swift.PseudoFolder(folder_dict, container_1.name)) TEST.folder.add(swift.PseudoFolder(folder_dict, container_1.name))
# when the folder is returned as part of a prefix match, this content
# is returned by Swift instead:
folder_dict_alt = {
"name": u"test folder%\u6346/",
"bytes": 0,
"last_modified": timeutils.utcnow().isoformat(),
"content_type": u"application/octet-stream",
"hash": u"object_hash"
}
TEST.folder_alt.add(swift.PseudoFolder(folder_dict_alt, container_1.name))
# just the objects matching the folder prefix # just the objects matching the folder prefix
TEST.subfolder.add(swift.PseudoFolder(folder_dict_alt, container_1.name))
TEST.subfolder.add(swift.StorageObject(object_dict_4, container_1.name, TEST.subfolder.add(swift.StorageObject(object_dict_4, container_1.name,
data=object_dict_4)) data=object_dict_4))
TEST.subfolder.add(swift.PseudoFolder(folder_dict, container_1.name))