Add test case for _write_compressed_metadata()

This commit is contained in:
Vladimir Diaz 2016-08-15 14:02:10 -04:00
parent 53b16aefd1
commit 2ef5f3eb68
2 changed files with 31 additions and 8 deletions

View file

@ -735,6 +735,21 @@ def test_write_metadata_file(self):
self.assertRaises(tuf.FormatError, repo_lib.write_metadata_file,
root_signable, output_filename, version_number,
compression_algorithms, 3)
def test__write_compressed_metadata(self):
# Test for invalid 'compressed_filename' argument and set
# 'write_new_metadata' to False.
file_object = tuf.util.TempFile()
non_existent_filename = \
os.path.join(cls.temporary_directory, 'non-existent_compressed_filename')
write_new_metadata = False
repo_lib._write_compressed_metadata(file_object,
compressed_filename=non_existent_filename,
write_new_metadata=write_new_metadata,
consistent_snapshot=False,
version_number=8)

View file

@ -341,7 +341,6 @@ def _check_role_keys(rolename):
"""
Non-public function that verifies the public and signing keys of 'rolename'.
If either contain an invalid threshold of keys, raise an exception.
'rolename' is the full rolename (e.g., 'targets/unclaimed/django').
"""
# Extract the total number of public and private keys of 'rolename' from its
@ -354,15 +353,13 @@ def _check_role_keys(rolename):
# Raise an exception for an invalid threshold of public keys.
if total_keyids < threshold:
message = repr(rolename) + ' role contains ' + \
repr(total_keyids) + ' / ' + repr(threshold) + ' public keys.'
raise tuf.InsufficientKeysError(message)
raise tuf.InsufficientKeysError(repr(rolename) + ' role contains'
' ' + repr(total_keyids) + ' / ' + repr(threshold) + ' public keys.')
# Raise an exception for an invalid threshold of signing keys.
if total_signatures == 0 and total_signing_keys < threshold:
message = repr(rolename) + ' role contains ' + \
repr(total_signing_keys) + ' / ' + repr(threshold) + ' signing keys.'
raise tuf.InsufficientKeysError(message)
raise tuf.InsufficientKeysError(repr(rolename) + ' role contains'
' ' + repr(total_signing_keys) + ' / ' + repr(threshold) + ' signing keys.')
@ -2059,7 +2056,10 @@ def _write_compressed_metadata(file_object, compressed_filename,
if basename.endswith(compression_extension):
basename = basename.split(compression_extension, 1)[0]
version_and_filename = str(version_number) + '.' + basename + compression_extension
else:
logger.debug('Skipping unsupported compressed file: ' + repr(basename))
consistent_filenames.append(os.path.join(dirname, version_and_filename))
# Move the 'tuf.util.TempFile' object to one of the filenames so that it is
@ -2070,12 +2070,20 @@ def _write_compressed_metadata(file_object, compressed_filename,
logger.info('Saving ' + repr(compressed_filename))
file_object.move(compressed_filename)
else:
logger.debug('Skipping already written compressed file:'
' ' + repr(compressed_filename))
# Save any remaining compressed consistent snapshots.
for consistent_filename in consistent_filenames:
if not os.path.exists(consistent_filename):
logger.info('Linking ' + repr(consistent_filename))
os.link(compressed_filename, consistent_filename)
else:
logger.debug('Skipping linking of already written compressed file: '
' ' + repr(consistent_filename))