Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test loading of cached metadata in ngclient #1703

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion tests/test_updater_top_level_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down