Commit 319b8b18 authored by Julien Muchembled's avatar Julien Muchembled

Do not require useless metadata

url would be more useful than file+urlmd5
(slapos.buildout will be changed this way)

However, prevent the user from passing empty metadata inadvertently.
Of course, that won't reject dummy information but it's unlikely there's
nothing useful to add.
parent 1130d381
...@@ -330,8 +330,7 @@ class NetworkcacheClient(object): ...@@ -330,8 +330,7 @@ class NetworkcacheClient(object):
finally: finally:
f is None or f.close() f is None or f.close()
def upload(self, file_descriptor, key=None, urlmd5=None, file_name=None, def upload(self, file_descriptor, key=None, **kw):
valid_until=None, architecture=None, **kw):
''' Upload the file to the server. ''' Upload the file to the server.
If key is None it must only upload to SHACACHE. If key is None it must only upload to SHACACHE.
Otherwise, it must create a new entry on SHADIR. Otherwise, it must create a new entry on SHADIR.
...@@ -374,16 +373,9 @@ class NetworkcacheClient(object): ...@@ -374,16 +373,9 @@ class NetworkcacheClient(object):
f is None or f.close() f is None or f.close()
if key is not None: if key is not None:
kw['sha512'] = sha512sum # always update sha512sum if not kw:
file_name = kw.pop('file', file_name) raise NetworkcacheException('metadata should not be empty')
if file_name is None or urlmd5 is None: self.index(key, sha512=sha512sum, **kw)
raise NetworkcacheException(
'file_name and urlmd5 are required for non-generic upload')
if valid_until is not None:
kw['valid-until'] = valid_until
if architecture is not None:
kw['architecture'] = architecture
self.index(key, file=file_name, urlmd5=urlmd5, **kw)
return sha512sum return sha512sum
...@@ -504,15 +496,13 @@ def cmd_upload(*args): ...@@ -504,15 +496,13 @@ def cmd_upload(*args):
nc.upload(f) nc.upload(f)
return return
elif args.url: elif args.url:
parser.error('either --file or --url is required') f = (nc.archive if os.path.isdir(args.url) else urlopen)(args.url)
elif os.path.isdir(args.url):
f = nc.archive(args.url)
else: else:
f = urlopen(args.url) parser.error('either --file or --url is required')
kw = dict(x.split('=', 1) for x in args.meta)
kw.setdefault('url', args.url)
urlmd5 = hashlib.md5(args.url.encode()).hexdigest() urlmd5 = hashlib.md5(args.url.encode()).hexdigest()
nc.upload(f, args.prefix_key + urlmd5 + args.suffix_key, urlmd5=urlmd5, nc.upload(f, args.prefix_key + urlmd5 + args.suffix_key, **kw)
file_name=os.path.basename(args.url) or "file",
**dict(x.split('=', 1) for x in args.meta))
finally: finally:
f is None or f.close() f is None or f.close()
......
...@@ -403,13 +403,6 @@ class OnlineTest(OnlineMixin, unittest.TestCase): ...@@ -403,13 +403,6 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
next(nc.select('key_another_key' + str(random.random()))) next(nc.select('key_another_key' + str(random.random())))
self.assertRaised(HTTPError) self.assertRaised(HTTPError)
def test_upload_shadir_no_filename(self):
"""Check scenario with shadir used, but not filename passed"""
with slapos.libnetworkcache.NetworkcacheClient(self.shacache,
self.shadir) as nc:
nc.upload(self.test_data, 'somekey', str(random.random()))
self.assertLog('file_name and urlmd5 are required for non-generic upload')
def test_upload_twice_same(self): def test_upload_twice_same(self):
nc = slapos.libnetworkcache.NetworkcacheClient(self.shacache, self.shadir) nc = slapos.libnetworkcache.NetworkcacheClient(self.shacache, self.shadir)
nc.upload(self.test_data) nc.upload(self.test_data)
...@@ -578,8 +571,8 @@ class OnlineTest(OnlineMixin, unittest.TestCase): ...@@ -578,8 +571,8 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
file_name = 'my file' file_name = 'my file'
nc.upload(self.test_data, key, urlmd5=urlmd5, file_name=file_name) nc.upload(self.test_data, key, urlmd5=urlmd5, file_name=file_name)
signed_nc = slapos.libnetworkcache.NetworkcacheClient( signed_nc = slapos.libnetworkcache.NetworkcacheClient(
self.shacache, self.shadir, signature_certificate_list=[ self.shacache, self.shadir, signature_private_key_file=key_file.name,
self.certificate]) signature_certificate_list=[self.certificate])
# when no signature is used, all works ok # when no signature is used, all works ok
selected = self.select(nc, key).read() selected = self.select(nc, key).read()
self.assertEqual(selected, self.test_string) self.assertEqual(selected, self.test_string)
...@@ -588,9 +581,7 @@ class OnlineTest(OnlineMixin, unittest.TestCase): ...@@ -588,9 +581,7 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
# of course if proper key will be used to sign the content uploaded # of course if proper key will be used to sign the content uploaded
# into shacache all will work # into shacache all will work
upload_nc = slapos.libnetworkcache.NetworkcacheClient(self.shacache, signed_nc.upload(self.test_data, key, urlmd5=urlmd5, file_name=file_name)
self.shadir, signature_private_key_file=key_file.name)
upload_nc.upload(self.test_data, key, urlmd5=urlmd5, file_name=file_name)
selected = self.select(signed_nc, key).read() selected = self.select(signed_nc, key).read()
self.assertEqual(selected, self.test_string) self.assertEqual(selected, self.test_string)
......
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