Skip to content

Commit d27c0fd

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 <iyovcheva@vmware.com>
1 parent 3d4df87 commit d27c0fd

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

tests/test_updater_top_level_update.py

+45-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import unittest
1313
from datetime import datetime, timedelta
1414
from typing import Iterable, Optional
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
@@ -636,6 +636,50 @@ def test_snapshot_rollback_with_local_snapshot_hash_mismatch(self) -> None:
636636
with self.assertRaises(BadVersionNumberError):
637637
self._run_refresh()
638638

639+
@patch.object(builtins, "open", wraps=builtins.open)
640+
def test_load_metadata_from_cache(self, wrapped_open: MagicMock) -> None:
641+
642+
# Add new delegated targets
643+
spec_version = ".".join(SPECIFICATION_VERSION)
644+
targets = Targets(1, spec_version, self.sim.safe_expiry, {}, None)
645+
role = DelegatedRole("role1", [], 1, False, ["*"], None)
646+
self.sim.add_delegation("targets", role, targets)
647+
self.sim.update_snapshot()
648+
649+
# Make a successful update of valid metadata which stores it in cache
650+
updater = self._run_refresh()
651+
updater.get_targetinfo("non_existent_target")
652+
653+
# Clean up calls to open during refresh()
654+
wrapped_open.reset_mock()
655+
# Clean up fetch tracker metadata
656+
self.sim.fetch_tracker.metadata.clear()
657+
658+
# Create a new updater and perform a second update while
659+
# the metadata is already stored in cache (metadata dir)
660+
updater = Updater(
661+
self.metadata_dir,
662+
"https://example.com/metadata/",
663+
self.targets_dir,
664+
"https://example.com/targets/",
665+
self.sim,
666+
)
667+
updater.get_targetinfo("non_existent_target")
668+
669+
# Test that metadata is loaded from cache and not downloaded
670+
wrapped_open.assert_has_calls(
671+
[
672+
call(os.path.join(self.metadata_dir, "root.json"), "rb"),
673+
call(os.path.join(self.metadata_dir, "timestamp.json"), "rb"),
674+
call(os.path.join(self.metadata_dir, "snapshot.json"), "rb"),
675+
call(os.path.join(self.metadata_dir, "targets.json"), "rb"),
676+
call(os.path.join(self.metadata_dir, "role1.json"), "rb"),
677+
]
678+
)
679+
680+
expected_calls = [("root", 2), ("timestamp", None)]
681+
self.assertListEqual(self.sim.fetch_tracker.metadata, expected_calls)
682+
639683

640684
if __name__ == "__main__":
641685
if "--dump" in sys.argv:

0 commit comments

Comments
 (0)