Skip to content

Commit 46c5731

Browse files
Ivana Atanasovaivanayov
Ivana Atanasova
authored andcommitted
Test loading of cached metadata in ngclient
After making a successful update of valid metadata which stores it in cache and performing a second update with a new updater while the metadata is already stored in cache, this test verifies that timestamp, snaphot and targets are loaded from cache and not downloaded Fixes #1681 Signed-off-by: Ivana Atanasova <[email protected]>
1 parent bdf1cbb commit 46c5731

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

tests/test_updater_with_simulator.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
import tempfile
1313
import unittest
1414
from typing import Optional, Tuple
15-
from unittest.mock import MagicMock, patch
15+
from unittest.mock import MagicMock, call, patch
1616

1717
from tests import utils
1818
from tests.repository_simulator import RepositorySimulator
19-
from tuf.api.metadata import SPECIFICATION_VERSION, Targets
19+
from tuf.api.metadata import SPECIFICATION_VERSION, Metadata, Targets
2020
from tuf.exceptions import BadVersionNumberError, UnsignedMetadataError
2121
from tuf.ngclient import Updater
2222

@@ -254,6 +254,44 @@ def test_not_loading_targets_twice(self, wrapped_open: MagicMock) -> None:
254254
updater.get_targetinfo("somepath")
255255
wrapped_open.assert_not_called()
256256

257+
@patch.object(builtins, "open", wraps=builtins.open)
258+
def test_load_metadata_from_cache(self, wrapped_open: MagicMock) -> None:
259+
260+
# Add new delegated targets
261+
spec_version = ".".join(SPECIFICATION_VERSION)
262+
targets = Targets(1, spec_version, self.sim.safe_expiry, {}, None)
263+
self.sim.add_delegation("targets", "role1", targets, False, ["*"], None)
264+
self.sim.update_snapshot()
265+
266+
# Make a successful update of valid metadata which stores it in cache
267+
updater = self._run_refresh()
268+
updater.get_targetinfo("non_existent_target")
269+
270+
# Clean up calls to open during refresh()
271+
wrapped_open.reset_mock()
272+
# Clean up fetch tracker metadata
273+
self.sim.fetch_tracker.metadata.clear()
274+
275+
# Create a new updater and perform a second update while
276+
# the metadata is already stored in cache (metadata dir)
277+
updater = self._run_refresh()
278+
updater.get_targetinfo("non_existent_target")
279+
280+
# Test that metadata is loaded from cache and not downloaded
281+
wrapped_open.assert_has_calls(
282+
[
283+
call(os.path.join(self.metadata_dir, "root.json"), "rb"),
284+
call(os.path.join(self.metadata_dir, "timestamp.json"), "rb"),
285+
call(os.path.join(self.metadata_dir, "snapshot.json"), "rb"),
286+
call(os.path.join(self.metadata_dir, "targets.json"), "rb"),
287+
call(os.path.join(self.metadata_dir, "role1.json"), "rb"),
288+
]
289+
)
290+
wrapped_open.reset_mock()
291+
292+
expected_calls = [("root", 2), ("timestamp", None)]
293+
self.assertListEqual(self.sim.fetch_tracker.metadata, expected_calls)
294+
257295

258296
if __name__ == "__main__":
259297
if "--dump" in sys.argv:

0 commit comments

Comments
 (0)