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):
finally:
f is None or f.close()
def upload(self, file_descriptor, key=None, urlmd5=None, file_name=None,
valid_until=None, architecture=None, **kw):
def upload(self, file_descriptor, key=None, **kw):
''' Upload the file to the server.
If key is None it must only upload to SHACACHE.
Otherwise, it must create a new entry on SHADIR.
......@@ -374,16 +373,9 @@ class NetworkcacheClient(object):
f is None or f.close()
if key is not None:
kw['sha512'] = sha512sum # always update sha512sum
file_name = kw.pop('file', file_name)
if file_name is None or urlmd5 is None:
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)
if not kw:
raise NetworkcacheException('metadata should not be empty')
self.index(key, sha512=sha512sum, **kw)
return sha512sum
......@@ -504,15 +496,13 @@ def cmd_upload(*args):
nc.upload(f)
return
elif args.url:
parser.error('either --file or --url is required')
elif os.path.isdir(args.url):
f = nc.archive(args.url)
f = (nc.archive if os.path.isdir(args.url) else urlopen)(args.url)
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()
nc.upload(f, args.prefix_key + urlmd5 + args.suffix_key, urlmd5=urlmd5,
file_name=os.path.basename(args.url) or "file",
**dict(x.split('=', 1) for x in args.meta))
nc.upload(f, args.prefix_key + urlmd5 + args.suffix_key, **kw)
finally:
f is None or f.close()
......
......@@ -403,13 +403,6 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
next(nc.select('key_another_key' + str(random.random())))
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):
nc = slapos.libnetworkcache.NetworkcacheClient(self.shacache, self.shadir)
nc.upload(self.test_data)
......@@ -578,8 +571,8 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
file_name = 'my file'
nc.upload(self.test_data, key, urlmd5=urlmd5, file_name=file_name)
signed_nc = slapos.libnetworkcache.NetworkcacheClient(
self.shacache, self.shadir, signature_certificate_list=[
self.certificate])
self.shacache, self.shadir, signature_private_key_file=key_file.name,
signature_certificate_list=[self.certificate])
# when no signature is used, all works ok
selected = self.select(nc, key).read()
self.assertEqual(selected, self.test_string)
......@@ -588,9 +581,7 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
# of course if proper key will be used to sign the content uploaded
# into shacache all will work
upload_nc = slapos.libnetworkcache.NetworkcacheClient(self.shacache,
self.shadir, signature_private_key_file=key_file.name)
upload_nc.upload(self.test_data, key, urlmd5=urlmd5, file_name=file_name)
signed_nc.upload(self.test_data, key, urlmd5=urlmd5, file_name=file_name)
selected = self.select(signed_nc, key).read()
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