Skip to content

Commit bd17789

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 theupdateframework#1681 Signed-off-by: Ivana Atanasova <[email protected]>
1 parent 171f9ee commit bd17789

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

tests/test_updater_with_simulator.py

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

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

2323

2424
class TestUpdater(unittest.TestCase):
@@ -257,6 +257,44 @@ def test_not_loading_targets_twice(self, wrapped_open: MagicMock) -> None:
257257
updater.get_targetinfo("somepath")
258258
wrapped_open.assert_not_called()
259259

260+
@patch.object(builtins, "open", wraps=builtins.open)
261+
def test_update_metadata_to_cache(self, wrapped_open: MagicMock) -> None:
262+
updater = self._run_refresh()
263+
264+
# Add targets to repository
265+
self.sim.targets.version += 1
266+
self.sim.add_target("targets", b'some content', self.targets_dir)
267+
self.sim.update_snapshot()
268+
269+
# Make a successful update of valid metadata which stores it in cache
270+
updater = self._run_refresh()
271+
272+
# Clean up calls to fetch during refresh()
273+
wrapped_open.reset_mock()
274+
275+
updater.get_targetinfo(self.targets_dir)
276+
277+
# Create a new updater and perform a second update while
278+
# the metadata is already stored in cache (metadata dir)
279+
updater = Updater(
280+
self.metadata_dir,
281+
"https://example.com/metadata/",
282+
self.targets_dir,
283+
"https://example.com/targets/",
284+
self.sim,
285+
)
286+
updater.get_targetinfo(self.targets_dir)
287+
288+
# Test that timestamp, snaphot and targets
289+
# are loaded from cache and not downloaded
290+
wrapped_open.assert_has_calls([
291+
call(os.path.join(self.metadata_dir, "root.json"), "rb"),
292+
call(os.path.join(self.metadata_dir, "timestamp.json"), "rb"),
293+
call(os.path.join(self.metadata_dir, "snapshot.json"), "rb"),
294+
call(os.path.join(self.metadata_dir, "targets.json"), "rb"),
295+
])
296+
wrapped_open.reset_mock()
297+
260298

261299
if __name__ == "__main__":
262300
if "--dump" in sys.argv:

0 commit comments

Comments
 (0)