Add 'clear_all' argument to clear_keydb() in keydb.py

This commit is contained in:
Vladimir Diaz 2016-07-15 13:47:46 -04:00
parent 11ae261ffd
commit 1c72cb2c20
2 changed files with 15 additions and 5 deletions

View file

@ -60,8 +60,8 @@ def tearDown(self):
def test_create_keydb(self):
# Test condition for normal behaviour.
repository_name = 'example_repository'
# The keydb dictionary should contain only the 'default' repository entry.
print('keydb: ' + repr(tuf.keydb._keydb_dict.keys()))
self.assertTrue('default' in tuf.keydb._keydb_dict)
self.assertEqual(1, len(tuf.keydb._keydb_dict))
@ -108,7 +108,7 @@ def test_clear_keydb(self):
self.assertEqual(0, len(tuf.keydb._keydb_dict['default']))
# Test condition for unexpected argument.
self.assertRaises(TypeError, tuf.keydb.clear_keydb, 'default', 'unexpected_argument')
self.assertRaises(TypeError, tuf.keydb.clear_keydb, 'default', False, 'unexpected_argument')
# Test condition for invalid repository name.
self.assertRaises(tuf.FormatError, tuf.keydb.clear_keydb, 0)

View file

@ -398,7 +398,7 @@ def remove_key(keyid, repository_name='default'):
def clear_keydb(repository_name='default'):
def clear_keydb(repository_name='default', clear_all=False):
"""
<Purpose>
@ -409,6 +409,9 @@ def clear_keydb(repository_name='default'):
The name of the repository to clear the key database. If not supplied,
the key database is cleared for the 'default' repository.
clear_all:
Boolean indicating whether to clear the entire keydb.
<Exceptions>
tuf.FormatError, if 'repository_name' is improperly formatted.
@ -422,10 +425,17 @@ def clear_keydb(repository_name='default'):
None.
"""
# Does 'repository_name' have the correct format? Raise 'tuf.FormatError' if
# Do the arguments have the correct format? Raise 'tuf.FormatError' if
# 'repository_name' is improperly formatted.
tuf.formats.NAME_SCHEMA.check_match(repository_name)
tuf.formats.BOOLEAN_SCHEMA.check_match(clear_all)
global _keydb_dict
if clear_all:
_keydb_dict = {}
_keydb_dict['default'] = {}
if repository_name not in _keydb_dict:
raise tuf.InvalidNameError('Repository name does not exist:'
' ' + repr(repository_name))