Commit 3c216009 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Modify upload methods to return created obj info

Methods affected:
    upload_object
    upload_object_unchunked
    upload_object_by_manifestation

unittests modified accordingly

Refs: #3656
parent 70f186e2
......@@ -112,6 +112,8 @@ class PithosClient(PithosRestClient):
'write':[usr and/or grp names]}
:param public: (bool)
:returns: (dict) created object metadata
"""
self._assert_container()
......@@ -128,7 +130,7 @@ class PithosClient(PithosRestClient):
f = StringIO(data)
else:
data = f.read(size) if size else f.read()
self.object_put(
r = self.object_put(
obj,
data=data,
etag=etag,
......@@ -138,6 +140,7 @@ class PithosClient(PithosRestClient):
permissions=sharing,
public=public,
success=201)
return r.headers
def create_object_by_manifestation(
self, obj,
......@@ -162,9 +165,11 @@ class PithosClient(PithosRestClient):
'write':[usr and/or grp names]}
:param public: (bool)
:returns: (dict) created object metadata
"""
self._assert_container()
self.object_put(
r = self.object_put(
obj,
content_length=0,
etag=etag,
......@@ -174,6 +179,7 @@ class PithosClient(PithosRestClient):
permissions=sharing,
public=public,
manifest='%s/%s' % (self.container, obj))
return r.headers
# upload_* auxiliary methods
def _put_block_async(self, data, hash, upload_gen=None):
......@@ -224,7 +230,7 @@ class PithosClient(PithosRestClient):
permissions=permissions,
public=public,
success=success)
return None if r.status_code == 201 else r.json
return (None if r.status_code == 201 else r.json), r.headers
def _calculate_blocks_for_upload(
self, blocksize, blockhash, size, nblocks, hashes, hmap, fileobj,
......@@ -350,7 +356,7 @@ class PithosClient(PithosRestClient):
hash_cb=hash_cb)
hashmap = dict(bytes=size, hashes=hashes)
missing = self._create_or_get_missing_hashes(
missing, obj_headers = self._create_or_get_missing_hashes(
obj, hashmap,
content_type=content_type,
size=size,
......@@ -362,7 +368,7 @@ class PithosClient(PithosRestClient):
public=public)
if missing is None:
return
return obj_headers
if upload_cb:
upload_gen = upload_cb(len(missing))
......@@ -401,7 +407,7 @@ class PithosClient(PithosRestClient):
thread.join()
raise
self.object_put(
r = self.object_put(
obj,
format='json',
hashmap=True,
......@@ -413,6 +419,7 @@ class PithosClient(PithosRestClient):
permissions=sharing,
public=public,
success=201)
return r.headers
# download_* auxiliary methods
def _get_remote_blocks_info(self, obj, **restargs):
......
......@@ -821,7 +821,10 @@ class PithosClient(TestCase):
tmpFile = self._create_temp_file(num_of_blocks)
# Without kwargs
self.client.upload_object(obj, tmpFile)
exp_headers = dict(id='container id', name='container name')
FR.headers = dict(exp_headers)
r = self.client.upload_object(obj, tmpFile)
self.assert_dicts_are_equal(r, exp_headers)
self.assertEqual(GCI.mock_calls[-1], call())
[call1, call2] = OP.mock_calls
......@@ -888,9 +891,10 @@ class PithosClient(TestCase):
yield
tmpFile.seek(0)
self.client.upload_object(
r = self.client.upload_object(
obj, tmpFile,
hash_cb=blck_gen, upload_cb=upld_gen)
self.assert_dicts_are_equal(r, exp_headers)
for i, c in enumerate(OP.mock_calls[-mock_offset:]):
self.assertEqual(OP.mock_calls[i], c)
......@@ -899,8 +903,9 @@ class PithosClient(TestCase):
tmpFile.seek(0)
ctype = 'video/mpeg'
sharing = dict(read=['u1', 'g1', 'u2'], write=['u1'])
self.client.upload_object(obj, tmpFile,
r = self.client.upload_object(obj, tmpFile,
content_type=ctype, sharing=sharing)
self.assert_dicts_are_equal(r, exp_headers)
self.assertEqual(OP.mock_calls[-1][2]['content_type'], ctype)
self.assert_dicts_are_equal(
OP.mock_calls[-2][2]['permissions'],
......@@ -916,7 +921,9 @@ class PithosClient(TestCase):
content_disposition=ctype + 'd15p051710n',
public=True,
content_encoding='802.11')
self.client.upload_object(obj, tmpFile, **kwargs)
r = self.client.upload_object(obj, tmpFile, **kwargs)
self.assert_dicts_are_equal(r, exp_headers)
kwargs.pop('if_not_exist')
ematch = kwargs.pop('if_etag_match')
etag = kwargs.pop('etag')
......@@ -1044,7 +1051,8 @@ class PithosClient(TestCase):
content_disposition='some content_disposition',
public=True,
permissions=dict(read=['u1', 'g1', 'u2'], write=['u1']))
self.client.upload_object_unchunked(obj, tmpFile)
r = self.client.upload_object_unchunked(obj, tmpFile)
self.assert_dicts_are_equal(r, FR.headers)
self.assertEqual(put.mock_calls[-1][1], (obj,))
self.assertEqual(
sorted(put.mock_calls[-1][2].keys()),
......@@ -1054,7 +1062,8 @@ class PithosClient(TestCase):
kwargs['size'] = kwargs.pop('data')
kwargs['sharing'] = kwargs.pop('permissions')
tmpFile.seek(0)
self.client.upload_object_unchunked(obj, tmpFile, **kwargs)
r = self.client.upload_object_unchunked(obj, tmpFile, **kwargs)
self.assert_dicts_are_equal(r, FR.headers)
pmc = put.mock_calls[-1][2]
for k, v in expected.items():
if k == 'data':
......@@ -1076,12 +1085,14 @@ class PithosClient(TestCase):
content_disposition='some content_disposition',
public=True,
sharing=dict(read=['u1', 'g1', 'u2'], write=['u1']))
self.client.create_object_by_manifestation(obj)
r = self.client.create_object_by_manifestation(obj)
self.assert_dicts_are_equal(r, FR.headers)
expected = dict(content_length=0, manifest=manifest)
for k in kwargs:
expected['permissions' if k == 'sharing' else k] = None
self.assertEqual(put.mock_calls[-1], call(obj, **expected))
self.client.create_object_by_manifestation(obj, **kwargs)
r = self.client.create_object_by_manifestation(obj, **kwargs)
self.assert_dicts_are_equal(r, FR.headers)
expected.update(kwargs)
expected['permissions'] = expected.pop('sharing')
self.assertEqual(put.mock_calls[-1], call(obj, **expected))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment