mirror of
https://github.com/theupdateframework/python-tuf
synced 2026-05-24 10:08:28 +00:00
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 <iyovcheva@vmware.com>
This commit is contained in:
parent
3d4df876c2
commit
d27c0fd585
1 changed files with 45 additions and 1 deletions
|
|
@ -12,7 +12,7 @@
|
|||
import unittest
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Iterable, Optional
|
||||
from unittest.mock import MagicMock, patch
|
||||
from unittest.mock import MagicMock, call, patch
|
||||
|
||||
from tests import utils
|
||||
from tests.repository_simulator import RepositorySimulator
|
||||
|
|
@ -636,6 +636,50 @@ def test_snapshot_rollback_with_local_snapshot_hash_mismatch(self) -> None:
|
|||
with self.assertRaises(BadVersionNumberError):
|
||||
self._run_refresh()
|
||||
|
||||
@patch.object(builtins, "open", wraps=builtins.open)
|
||||
def test_load_metadata_from_cache(self, wrapped_open: MagicMock) -> None:
|
||||
|
||||
# Add new delegated targets
|
||||
spec_version = ".".join(SPECIFICATION_VERSION)
|
||||
targets = Targets(1, spec_version, self.sim.safe_expiry, {}, None)
|
||||
role = DelegatedRole("role1", [], 1, False, ["*"], None)
|
||||
self.sim.add_delegation("targets", role, targets)
|
||||
self.sim.update_snapshot()
|
||||
|
||||
# Make a successful update of valid metadata which stores it in cache
|
||||
updater = self._run_refresh()
|
||||
updater.get_targetinfo("non_existent_target")
|
||||
|
||||
# Clean up calls to open during refresh()
|
||||
wrapped_open.reset_mock()
|
||||
# Clean up fetch tracker metadata
|
||||
self.sim.fetch_tracker.metadata.clear()
|
||||
|
||||
# Create a new updater and perform a second update while
|
||||
# the metadata is already stored in cache (metadata dir)
|
||||
updater = Updater(
|
||||
self.metadata_dir,
|
||||
"https://example.com/metadata/",
|
||||
self.targets_dir,
|
||||
"https://example.com/targets/",
|
||||
self.sim,
|
||||
)
|
||||
updater.get_targetinfo("non_existent_target")
|
||||
|
||||
# Test that metadata is loaded from cache and not downloaded
|
||||
wrapped_open.assert_has_calls(
|
||||
[
|
||||
call(os.path.join(self.metadata_dir, "root.json"), "rb"),
|
||||
call(os.path.join(self.metadata_dir, "timestamp.json"), "rb"),
|
||||
call(os.path.join(self.metadata_dir, "snapshot.json"), "rb"),
|
||||
call(os.path.join(self.metadata_dir, "targets.json"), "rb"),
|
||||
call(os.path.join(self.metadata_dir, "role1.json"), "rb"),
|
||||
]
|
||||
)
|
||||
|
||||
expected_calls = [("root", 2), ("timestamp", None)]
|
||||
self.assertListEqual(self.sim.fetch_tracker.metadata, expected_calls)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if "--dump" in sys.argv:
|
||||
|
|
|
|||
Loading…
Reference in a new issue