Skip to content

Commit

Permalink
Improve test_consistent_snapshot
Browse files Browse the repository at this point in the history
- Check for the exact call lists in asserts
- Small docstring improvements
- Minor issues after review

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
  • Loading branch information
sechkova committed Nov 17, 2021
1 parent 01859d5 commit f2ce6de
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions tests/test_updater_consistent_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright 2021, New York University and the TUF contributors
# SPDX-License-Identifier: MIT OR Apache-2.0

"""Test ngclient Updater consistent snapshot"""
"""Test ngclient Updater toggling consistent snapshot"""

import os
import sys
Expand All @@ -23,8 +23,9 @@


class TestConsistentSnapshot(unittest.TestCase):
"""Test different combinations of consistent_snapshot and
prefix_targets_with_hash"""
"""Test different combinations of 'consistent_snapshot' and
'prefix_targets_with_hash' and verify that the correct URLs
are formed for each combination"""

def setUp(self) -> None:
self.temp_dir = tempfile.TemporaryDirectory()
Expand Down Expand Up @@ -118,11 +119,11 @@ def test_top_level_roles_update(self, test_case_data: Dict[str, Any]):

with patch.object(
sim, "_fetch_metadata", wraps=sim._fetch_metadata
) as wrapped_fetch_metadata:
) as wrapped_fetch:
updater.refresh()

# metadata files are fetched with the expected version (or None)
wrapped_fetch_metadata.assert_has_calls(expected_calls)
self.assertListEqual(wrapped_fetch.call_args_list, expected_calls)
# metadata files are always persisted without a version prefix
self._assert_metadata_files_exist(TOP_LEVEL_ROLE_NAMES)

Expand Down Expand Up @@ -160,11 +161,11 @@ def test_delegated_roles_update(self, test_case_data: Dict[str, Any]):

with patch.object(
sim, "_fetch_metadata", wraps=sim._fetch_metadata
) as wrapped_fetch_metadata:
) as wrapped_fetch:
# trigger updater to fetch the delegated metadata
updater.get_targetinfo("anything")
# metadata files are fetched with the expected version (or None)
wrapped_fetch_metadata.assert_has_calls(expected_calls)
self.assertListEqual(wrapped_fetch.call_args_list, expected_calls)
# metadata files are always persisted without a version prefix
self._assert_metadata_files_exist(rolenames)

Expand All @@ -181,7 +182,7 @@ def test_delegated_roles_update(self, test_case_data: Dict[str, Any]):
"prefix_targets": False,
"hash_algo": None,
},
"consistent_snaphot_enabled with prefixed targets": {
"consistent_snaphot enabled with prefixed targets": {
"consistent_snapshot": True,
"prefix_targets": True,
"hash_algo": "sha256",
Expand All @@ -203,29 +204,29 @@ def test_download_targets(self, test_case_data: Dict[str, Any]):
for targetpath in targetpaths:
sim.targets.version += 1
sim.add_target("targets", b"content", targetpath)
sim.update_snapshot()
sim.update_snapshot()

updater = self._init_updater(sim)
updater.config.prefix_targets_with_hash = prefix_targets_with_hash
updater.refresh()

for targetpath in targetpaths:
info = updater.get_targetinfo(targetpath)

with patch.object(
sim, "_fetch_target", wraps=sim._fetch_target
with patch.object(
sim, "_fetch_target", wraps=sim._fetch_target
) as wrapped_fetch_target:

for targetpath in targetpaths:
info = updater.get_targetinfo(targetpath)
updater.download_target(info)
expected_prefix = (
None if not hash_algo else info.hashes[hash_algo]
)
# files are fetched with the expected hash prefix (or None)
wrapped_fetch_target.assert_called_with(
wrapped_fetch_target.assert_called_once_with(
info.path, expected_prefix
)
# target files are always persisted without hash prefix
self._assert_targets_files_exist([info.path])
wrapped_fetch_target.reset_mock()

self._cleanup_dir(self.targets_dir)

Expand Down

0 comments on commit f2ce6de

Please sign in to comment.