From 01b30ccd2d7478401aaaf33ffab2a6662c5dc7b1 Mon Sep 17 00:00:00 2001 From: Jussi Kukkonen Date: Mon, 8 Aug 2022 18:42:52 +0300 Subject: [PATCH] tests: Improve succinct download test * move to the test file that contains all the other download tests * don't write 1000 files: it can be slow in CI * Compare file content to what was originally written (also read the whole file content) * Remove try-except that seems unused Signed-off-by: Jussi Kukkonen --- tests/test_updater_delegation_graphs.py | 39 ------------------------- tests/test_updater_fetch_target.py | 30 +++++++++++++++++++ 2 files changed, 30 insertions(+), 39 deletions(-) diff --git a/tests/test_updater_delegation_graphs.py b/tests/test_updater_delegation_graphs.py index d3a9240e..ca04621d 100644 --- a/tests/test_updater_delegation_graphs.py +++ b/tests/test_updater_delegation_graphs.py @@ -510,45 +510,6 @@ def test_succinct_roles_graph_traversal( finally: self.teardown_subtest() - def test_download_targets_with_succinct_roles(self) -> None: - try: - self.setup_subtest() - self.sim = RepositorySimulator() - self.sim.add_succinct_roles("targets", 8, "bin") - self.sim.update_snapshot() - - assert self.sim.targets.delegations is not None - assert self.sim.targets.delegations.succinct_roles is not None - succinct_roles = self.sim.targets.delegations.succinct_roles - - # Add lots of targets with unique data to imitate a real repository. - for i in range(1000): - target_name = f"target-{i}" - target_bin = succinct_roles.get_role_for_target(target_name) - self.sim.add_target( - target_bin, bytes(target_name, "utf-8"), target_name - ) - - updater = self._init_updater() - # Call explicitly refresh to simplify the expected_calls list. - updater.refresh() - - for i in range(1000): - # Verify that the target info was successfully found. - target_info = updater.get_targetinfo(f"target-{i}") - assert target_info is not None - target_full_path = updater.download_target(target_info) - - # Verify that the target content is the same as the target name. - with open(target_full_path, encoding="utf-8") as target: - target_content = target.readline() - self.assertEqual( - target_content, os.path.basename(target_full_path) - ) - - finally: - self.teardown_subtest() - class TestTargetFileSearch(TestDelegations): r""" diff --git a/tests/test_updater_fetch_target.py b/tests/test_updater_fetch_target.py index a7af0ec1..84caccfd 100644 --- a/tests/test_updater_fetch_target.py +++ b/tests/test_updater_fetch_target.py @@ -121,6 +121,36 @@ def test_fetch_target(self, target: TestTarget) -> None: self.assertEqual(path, updater.find_cached_target(info)) self.assertEqual(path, updater.find_cached_target(info, path)) + def test_download_targets_with_succinct_roles(self) -> None: + self.sim.add_succinct_roles("targets", 8, "bin") + self.sim.update_snapshot() + + assert self.sim.targets.delegations is not None + assert self.sim.targets.delegations.succinct_roles is not None + succinct_roles = self.sim.targets.delegations.succinct_roles + + # Add lots of targets with unique data to imitate a real repository. + for i in range(20): + target_name = f"target-{i}" + target_bin = succinct_roles.get_role_for_target(target_name) + self.sim.add_target( + target_bin, bytes(target_name, "utf-8"), target_name + ) + + # download each target + updater = self._init_updater() + for i in range(20): + target_name = f"target-{i}" + + # Verify that the target info was successfully found. + target_info = updater.get_targetinfo(target_name) + assert target_info is not None + target_full_path = updater.download_target(target_info) + + # Verify that the target content is the same as the target name. + with open(target_full_path, encoding="utf-8") as target: + self.assertEqual(target.read(), target_name) + def test_invalid_target_download(self) -> None: target = TestTarget("targetpath", b"content", "targetpath")