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
snaphot and targets are loaded from cache and not downloaded and
that timestamp is loaded from cache and used to verify the new
timestamp

Fixes #1681

Signed-off-by: Ivana Atanasova <[email protected]>
  • Loading branch information
Ivana Atanasova committed Dec 2, 2021
1 parent 171f9ee commit 6d00db6
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions tests/test_updater_with_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
import tempfile
import unittest
from typing import Optional, Tuple
from unittest.mock import MagicMock, Mock, patch
from unittest.mock import call, MagicMock, patch

from tests import utils
from tests.repository_simulator import RepositorySimulator
from tuf.api.metadata import SPECIFICATION_VERSION, TargetFile, Targets
from tuf.api.metadata import SPECIFICATION_VERSION, Targets
from tuf.exceptions import BadVersionNumberError, UnsignedMetadataError
from tuf.ngclient import Updater
from tuf.ngclient import Updater, updater


class TestUpdater(unittest.TestCase):
Expand Down Expand Up @@ -257,6 +257,42 @@ def test_not_loading_targets_twice(self, wrapped_open: MagicMock) -> None:
updater.get_targetinfo("somepath")
wrapped_open.assert_not_called()

def test_update_metadata_to_cache(self) -> None:
updater = self._run_refresh()

# Add targets to repository
self.sim.targets.version += 1
self.sim.add_target("targets", b'some content', self.targets_dir)
self.sim.update_snapshot()

with patch.object(
self.sim, "_fetch_metadata", wraps=self.sim._fetch_metadata
) as wrapped_fetch:

# Make a successful update of valid metadata which stores it in cache
updater = self._run_refresh()

# Clean up calls to fetch during refresh()
wrapped_fetch.reset_mock()

updater.get_targetinfo(self.targets_dir)

# 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(self.targets_dir)

# Test that snaphot and targets are loaded from cache and not downloaded
# and that timestamp is loaded from cache and used to verify the new timestamp
wrapped_fetch.assert_has_calls([call("root", 2), call("timestamp", None)])
wrapped_fetch.reset_mock()


if __name__ == "__main__":
if "--dump" in sys.argv:
Expand Down

0 comments on commit 6d00db6

Please sign in to comment.