Skip to content

Commit eb648d1

Browse files
author
Jussi Kukkonen
committed
MetadataBundle: Save original files on disk
Don't use the serialized format as that won't match any hashes in "meta". Add basic tests for updating metadata. Signed-off-by: Jussi Kukkonen <[email protected]>
1 parent 2d155fa commit eb648d1

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

tests/test_metadata_bundle.py

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import logging
22
import os
3+
import shutil
34
import sys
5+
import tempfile
46
import unittest
57

68
from tuf.api import metadata
@@ -11,11 +13,26 @@
1113
logger = logging.getLogger(__name__)
1214

1315
class TestMetadataBundle(unittest.TestCase):
16+
@classmethod
17+
def setUpClass(cls):
18+
cls.temporary_directory = tempfile.mkdtemp(dir=os.getcwd())
19+
20+
@classmethod
21+
def tearDownClass(cls):
22+
shutil.rmtree(cls.temporary_directory)
23+
24+
def setUp(self):
25+
# copy metadata to "local repo"
26+
shutil.copytree(
27+
os.path.join(os.getcwd(), 'repository_data', 'repository', 'metadata'),
28+
self.temporary_directory,
29+
dirs_exist_ok=True
30+
)
31+
1432
def test_local_load(self):
15-
repo_dir = os.path.join(os.getcwd(), 'repository_data', 'repository', 'metadata')
1633

1734
# test loading all local metadata succesfully
18-
bundle = MetadataBundle(repo_dir)
35+
bundle = MetadataBundle(self.temporary_directory)
1936
bundle.root_update_finished()
2037
self.assertTrue(bundle.load_local_timestamp())
2138
self.assertTrue(bundle.load_local_snapshot())
@@ -24,7 +41,7 @@ def test_local_load(self):
2441
self.assertTrue(bundle.load_local_delegated_targets('role2','role1'))
2542

2643
# Make sure loading metadata without its "dependencies" fails
27-
bundle = MetadataBundle(repo_dir)
44+
bundle = MetadataBundle(self.temporary_directory)
2845

2946
with self.assertRaises(RuntimeError):
3047
bundle.load_local_timestamp()
@@ -43,6 +60,42 @@ def test_local_load(self):
4360
self.assertTrue(bundle.load_local_delegated_targets('role1','targets'))
4461
self.assertTrue(bundle.load_local_delegated_targets('role2','role1'))
4562

63+
def test_update(self):
64+
remote_dir = os.path.join(os.getcwd(), 'repository_data', 'repository', 'metadata')
65+
66+
# remove all but root.json from local repo
67+
os.remove(os.path.join(self.temporary_directory, "timestamp.json"))
68+
os.remove(os.path.join(self.temporary_directory, "snapshot.json"))
69+
os.remove(os.path.join(self.temporary_directory, "targets.json"))
70+
os.remove(os.path.join(self.temporary_directory, "role1.json"))
71+
os.remove(os.path.join(self.temporary_directory, "role2.json"))
72+
73+
# test updating metadata succesfully
74+
bundle = MetadataBundle(self.temporary_directory)
75+
bundle.root_update_finished()
76+
77+
with open(os.path.join(remote_dir, "timestamp.json"), "rb") as f:
78+
bundle.update_timestamp(f.read())
79+
with open(os.path.join(remote_dir, "snapshot.json"), "rb") as f:
80+
bundle.update_snapshot(f.read())
81+
with open(os.path.join(remote_dir, "targets.json"), "rb") as f:
82+
bundle.update_targets(f.read())
83+
with open(os.path.join(remote_dir, "role1.json"), "rb") as f:
84+
bundle.update_delegated_targets(f.read(), "role1", "targets")
85+
with open(os.path.join(remote_dir, "role2.json"), "rb") as f:
86+
bundle.update_delegated_targets(f.read(), "role2", "role1")
87+
88+
# test loading the metadata (that should now be locally available)
89+
bundle = MetadataBundle(self.temporary_directory)
90+
bundle.root_update_finished()
91+
self.assertTrue(bundle.load_local_timestamp())
92+
self.assertTrue(bundle.load_local_snapshot())
93+
self.assertTrue(bundle.load_local_targets())
94+
self.assertTrue(bundle.load_local_delegated_targets('role1','targets'))
95+
self.assertTrue(bundle.load_local_delegated_targets('role2','role1'))
96+
97+
# TODO test loading one version, then updating to new versions of each metadata
98+
4699

47100
if __name__ == '__main__':
48101
utils.configure_test_logging(sys.argv)

tuf/client_rework/metadata_bundle.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ def update_root(self, data: bytes):
199199
logger.debug("Updating root")
200200

201201
self._load_intermediate_root(data)
202-
self.root.to_file(os.path.join(self._path, "root.json"))
202+
with open(os.path.join(self._path, "root.json"), "wb") as f:
203+
f.write(data)
203204

204205
def root_update_finished(self):
205206
"""Mark root metadata as final."""
@@ -232,7 +233,8 @@ def update_timestamp(self, data: bytes):
232233
logger.debug("Updating timestamp")
233234

234235
self._load_timestamp(data)
235-
self.timestamp.to_file(os.path.join(self._path, "timestamp.json"))
236+
with open(os.path.join(self._path, "timestamp.json"), "wb") as f:
237+
f.write(data)
236238

237239
def load_local_snapshot(self) -> bool:
238240
"""Load cached snapshot metadata from local storage.
@@ -253,7 +255,8 @@ def update_snapshot(self, data: bytes):
253255
logger.debug("Updating snapshot")
254256

255257
self._load_snapshot(data)
256-
self.snapshot.to_file(os.path.join(self._path, "snapshot.json"))
258+
with open(os.path.join(self._path, "snapshot.json"), "wb") as f:
259+
f.write(data)
257260

258261
def load_local_targets(self) -> bool:
259262
"""Load cached targets metadata from local storage.
@@ -298,7 +301,8 @@ def update_delegated_targets(
298301
logger.debug("Updating %s", role_name)
299302

300303
self._load_delegated_targets(data, role_name, delegator_name)
301-
self[role_name].to_file(os.path.join(self._path, f"{role_name}.json"))
304+
with open(os.path.join(self._path, f"{role_name}.json"), "wb") as f:
305+
f.write(data)
302306

303307
def _load_intermediate_root(self, data: bytes):
304308
"""Verifies and loads 'data' as new root metadata.

0 commit comments

Comments
 (0)