Skip to content

Commit

Permalink
Test loading of cached metadata in ngclient
Browse files Browse the repository at this point in the history
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 theupdateframework#1681

Signed-off-by: Ivana Atanasova <[email protected]>
  • Loading branch information
Ivana Atanasova authored and ivanayov committed Dec 13, 2021
1 parent bdf1cbb commit a7e9c67
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion tests/test_updater_with_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import tempfile
import unittest
from typing import Optional, Tuple
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 @@ -254,6 +254,44 @@ def test_not_loading_targets_twice(self, wrapped_open: MagicMock) -> None:
updater.get_targetinfo("somepath")
wrapped_open.assert_not_called()

@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)
self.sim.add_delegation("targets", "role1", targets, False, ["*"], None)
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 = self._run_refresh()
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"),
]
)
wrapped_open.reset_mock()

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

0 comments on commit a7e9c67

Please sign in to comment.