Skip to content

Commit 2779954

Browse files
committed
New API: Comprehensive serialization testing
The idea of this commit is to separate (de)serialization testing outside test_api.py and make sure we are testing from_dict/to_dict for all possible valid data for all classes. As a result of this commit, there will be a single place where one can have a quick look at what use-cases are we testing for (de)serialization. Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
1 parent de78251 commit 2779954

File tree

2 files changed

+261
-133
lines changed

2 files changed

+261
-133
lines changed

tests/test_api.py

Lines changed: 2 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -260,27 +260,6 @@ def test_metafile_class(self):
260260
self.assertEqual(metafile_obj.to_dict(), data)
261261

262262

263-
def test_targetfile_class(self):
264-
# Test from_dict and to_dict with all attributes.
265-
data = {
266-
"custom": {
267-
"file_permissions": "0644"
268-
},
269-
"hashes": {
270-
"sha256": "65b8c67f51c993d898250f40aa57a317d854900b3a04895464313e48785440da",
271-
"sha512": "467430a68afae8e9f9c0771ea5d78bf0b3a0d79a2d3d3b40c69fde4dd42c461448aef76fcef4f5284931a1ffd0ac096d138ba3a0d6ca83fa8d7285a47a296f77"
272-
},
273-
"length": 31
274-
}
275-
targetfile_obj = TargetFile.from_dict(copy.copy(data))
276-
self.assertEqual(targetfile_obj.to_dict(), data)
277-
278-
# Test from_dict and to_dict without custom.
279-
del data["custom"]
280-
targetfile_obj = TargetFile.from_dict(copy.copy(data))
281-
self.assertEqual(targetfile_obj.to_dict(), data)
282-
283-
284263
def test_metadata_snapshot(self):
285264
snapshot_path = os.path.join(
286265
self.repo_dir, 'metadata', 'snapshot.json')
@@ -299,13 +278,6 @@ def test_metadata_snapshot(self):
299278
snapshot.signed.meta['role1.json'].to_dict(), fileinfo.to_dict()
300279
)
301280

302-
# Test from_dict and to_dict without hashes and length.
303-
snapshot_dict = snapshot.to_dict()
304-
del snapshot_dict['signed']['meta']['role1.json']['length']
305-
del snapshot_dict['signed']['meta']['role1.json']['hashes']
306-
test_dict = copy.deepcopy(snapshot_dict['signed'])
307-
snapshot = Snapshot.from_dict(test_dict)
308-
self.assertEqual(snapshot_dict['signed'], snapshot.to_dict())
309281

310282
def test_metadata_timestamp(self):
311283
timestamp_path = os.path.join(
@@ -344,13 +316,6 @@ def test_metadata_timestamp(self):
344316
timestamp.signed.meta['snapshot.json'].to_dict(), fileinfo.to_dict()
345317
)
346318

347-
# Test from_dict and to_dict without hashes and length.
348-
timestamp_dict = timestamp.to_dict()
349-
del timestamp_dict['signed']['meta']['snapshot.json']['length']
350-
del timestamp_dict['signed']['meta']['snapshot.json']['hashes']
351-
test_dict = copy.deepcopy(timestamp_dict['signed'])
352-
timestamp_test = Timestamp.from_dict(test_dict)
353-
self.assertEqual(timestamp_dict['signed'], timestamp_test.to_dict())
354319

355320
def test_key_class(self):
356321
keys = {
@@ -363,21 +328,12 @@ def test_key_class(self):
363328
},
364329
}
365330
for key_dict in keys.values():
366-
# Testing that the workflow of deserializing and serializing
367-
# a key dictionary doesn't change the content.
368-
test_key_dict = key_dict.copy()
369-
key_obj = Key.from_dict("id", test_key_dict)
370-
self.assertEqual(key_dict, key_obj.to_dict())
371331
# Test creating an instance without a required attribute.
372332
for key in key_dict.keys():
373333
test_key_dict = key_dict.copy()
374334
del test_key_dict[key]
375335
with self.assertRaises(KeyError):
376-
Key.from_dict("id", test_key_dict)
377-
# Test creating a Key instance with wrong keyval format.
378-
key_dict["keyval"] = {}
379-
with self.assertRaises(ValueError):
380-
Key.from_dict("id", key_dict)
336+
Key.from_dict(test_key_dict)
381337

382338

383339
def test_role_class(self):
@@ -396,23 +352,12 @@ def test_role_class(self):
396352
},
397353
}
398354
for role_dict in roles.values():
399-
# Testing that the workflow of deserializing and serializing
400-
# a role dictionary doesn't change the content.
401-
test_role_dict = role_dict.copy()
402-
role_obj = Role.from_dict(test_role_dict)
403-
self.assertEqual(role_dict, role_obj.to_dict())
404355
# Test creating an instance without a required attribute.
405356
for role_attr in role_dict.keys():
406357
test_role_dict = role_dict.copy()
407358
del test_role_dict[role_attr]
408359
with self.assertRaises(KeyError):
409-
Key.from_dict("id", test_role_dict)
410-
# Test creating a Role instance with keyid dublicates.
411-
# for keyid in role_dict["keyids"]:
412-
role_dict["keyids"].append(role_dict["keyids"][0])
413-
test_role_dict = role_dict.copy()
414-
with self.assertRaises(ValueError):
415-
Role.from_dict(test_role_dict)
360+
Key.from_dict(test_role_dict)
416361

417362

418363
def test_metadata_root(self):
@@ -459,84 +404,8 @@ def test_metadata_root(self):
459404
with self.assertRaises(KeyError):
460405
root.signed.remove_key('root', 'nosuchkey')
461406

462-
# Test serializing and deserializing without consistent_snapshot.
463-
root_dict = root.to_dict()
464-
del root_dict["signed"]["consistent_snapshot"]
465-
root = Root.from_dict(copy.deepcopy(root_dict["signed"]))
466-
self.assertEqual(root_dict["signed"], root.to_dict())
467-
468-
def test_delegated_role_class(self):
469-
roles = [
470-
{
471-
"keyids": [
472-
"c8022fa1e9b9cb239a6b362bbdffa9649e61ad2cb699d2e4bc4fdf7930a0e64a"
473-
],
474-
"name": "role1",
475-
"paths": [
476-
"file3.txt"
477-
],
478-
"terminating": False,
479-
"threshold": 1
480-
}
481-
]
482-
for role in roles:
483-
# Testing that the workflow of deserializing and serializing
484-
# a delegation role dictionary doesn't change the content.
485-
key_obj = DelegatedRole.from_dict(role.copy())
486-
self.assertEqual(role, key_obj.to_dict())
487-
488-
# Test creating a DelegatedRole object with both "paths" and
489-
# "path_hash_prefixes" set.
490-
role["path_hash_prefixes"] = "foo"
491-
with self.assertRaises(ValueError):
492-
DelegatedRole.from_dict(role.copy())
493-
494-
# Test creating DelegatedRole only with "path_hash_prefixes" (an empty one)
495-
del role["paths"]
496-
role["path_hash_prefixes"] = []
497-
role_obj = DelegatedRole.from_dict(role.copy())
498-
self.assertEqual(role_obj.to_dict(), role)
499-
500-
# Test creating DelegatedRole only with "paths" (now an empty one)
501-
del role["path_hash_prefixes"]
502-
role["paths"] = []
503-
role_obj = DelegatedRole.from_dict(role.copy())
504-
self.assertEqual(role_obj.to_dict(), role)
505-
506-
# Test creating DelegatedRole without "paths" and
507-
# "path_hash_prefixes" set
508-
del role["paths"]
509-
role_obj = DelegatedRole.from_dict(role.copy())
510-
self.assertEqual(role_obj.to_dict(), role)
511-
512407

513408
def test_delegation_class(self):
514-
roles = [
515-
{
516-
"keyids": [
517-
"c8022fa1e9b9cb239a6b362bbdffa9649e61ad2cb699d2e4bc4fdf7930a0e64a"
518-
],
519-
"name": "role1",
520-
"paths": [
521-
"file3.txt"
522-
],
523-
"terminating": False,
524-
"threshold": 1
525-
}
526-
]
527-
keys = {
528-
"59a4df8af818e9ed7abe0764c0b47b4240952aa0d179b5b78346c470ac30278d":{
529-
"keytype": "ed25519",
530-
"keyval": {
531-
"public": "edcd0a32a07dce33f7c7873aaffbff36d20ea30787574ead335eefd337e4dacd"
532-
},
533-
"scheme": "ed25519"
534-
},
535-
}
536-
delegations_dict = {"keys": keys, "roles": roles}
537-
delegations = Delegations.from_dict(copy.deepcopy(delegations_dict))
538-
self.assertEqual(delegations_dict, delegations.to_dict())
539-
540409
# empty keys and roles
541410
delegations_dict = {"keys":{}, "roles":[]}
542411
delegations = Delegations.from_dict(delegations_dict.copy())

0 commit comments

Comments
 (0)