Skip to content

Commit 2bb4ff6

Browse files
committed
tests: Standardize cache file checking code
This is still copy-paste in three different files but now at least the function is the same in every location and not directly copied. We really should have generic TestCase class... Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
1 parent 166434d commit 2bb4ff6

3 files changed

Lines changed: 33 additions & 21 deletions

File tree

tests/test_updater_delegation_graphs.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,13 @@ def _init_updater(self) -> Updater:
133133
)
134134

135135
def _assert_files_exist(self, roles: Iterable[str]) -> None:
136-
"""Assert that local metadata files exist for 'roles'"""
137-
expected_files = sorted([f"{role}.json" for role in roles])
138-
local_metadata_files = sorted(os.listdir(self.metadata_dir))
139-
self.assertListEqual(local_metadata_files, expected_files)
136+
"""Assert that local metadata files match 'roles'"""
137+
expected_files = [f"{role}.json" for role in roles]
138+
found_files = [
139+
e.name for e in os.scandir(self.metadata_dir) if e.is_file()
140+
]
141+
142+
self.assertListEqual(sorted(found_files), sorted(expected_files))
140143

141144

142145
class TestDelegationsGraphs(TestDelegations):

tests/test_updater_ng.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import sys
1212
import tempfile
1313
import unittest
14-
from typing import Callable, ClassVar
14+
from typing import TYPE_CHECKING, Callable, ClassVar
1515
from unittest.mock import MagicMock, patch
1616

1717
from securesystemslib.signer import Signer
@@ -28,6 +28,9 @@
2828
)
2929
from tuf.ngclient import Updater, UpdaterConfig
3030

31+
if TYPE_CHECKING:
32+
from collections.abc import Iterable
33+
3134
logger = logging.getLogger(__name__)
3235

3336

@@ -149,11 +152,14 @@ def _modify_repository_root(
149152
)
150153
)
151154

152-
def _assert_files(self, roles: list[str]) -> None:
153-
"""Assert that local metadata files exist for 'roles'"""
155+
def _assert_files_exist(self, roles: Iterable[str]) -> None:
156+
"""Assert that local metadata files match 'roles'"""
154157
expected_files = [f"{role}.json" for role in roles]
155-
client_files = sorted(os.listdir(self.client_directory))
156-
self.assertEqual(client_files, expected_files)
158+
found_files = [
159+
e.name for e in os.scandir(self.client_directory) if e.is_file()
160+
]
161+
162+
self.assertListEqual(sorted(found_files), sorted(expected_files))
157163

158164
def test_refresh_and_download(self) -> None:
159165
# Test refresh without consistent targets - targets without hash prefix.
@@ -164,14 +170,14 @@ def test_refresh_and_download(self) -> None:
164170

165171
# top-level metadata is in local directory already
166172
self.updater.refresh()
167-
self._assert_files(
173+
self._assert_files_exist(
168174
[Root.type, Snapshot.type, Targets.type, Timestamp.type]
169175
)
170176

171177
# Get targetinfos, assert that cache does not contain files
172178
info1 = self.updater.get_targetinfo("file1.txt")
173179
assert isinstance(info1, TargetFile)
174-
self._assert_files(
180+
self._assert_files_exist(
175181
[Root.type, Snapshot.type, Targets.type, Timestamp.type]
176182
)
177183

@@ -185,7 +191,7 @@ def test_refresh_and_download(self) -> None:
185191
Targets.type,
186192
Timestamp.type,
187193
]
188-
self._assert_files(expected_files)
194+
self._assert_files_exist(expected_files)
189195
self.assertIsNone(self.updater.find_cached_target(info1))
190196
self.assertIsNone(self.updater.find_cached_target(info3))
191197

@@ -207,10 +213,10 @@ def test_refresh_with_only_local_root(self) -> None:
207213
os.remove(os.path.join(self.client_directory, "targets.json"))
208214
os.remove(os.path.join(self.client_directory, "role1.json"))
209215
os.remove(os.path.join(self.client_directory, "role2.json"))
210-
self._assert_files([Root.type])
216+
self._assert_files_exist([Root.type])
211217

212218
self.updater.refresh()
213-
self._assert_files(
219+
self._assert_files_exist(
214220
[Root.type, Snapshot.type, Targets.type, Timestamp.type]
215221
)
216222

@@ -223,20 +229,20 @@ def test_refresh_with_only_local_root(self) -> None:
223229
Targets.type,
224230
Timestamp.type,
225231
]
226-
self._assert_files(expected_files)
232+
self._assert_files_exist(expected_files)
227233

228234
def test_implicit_refresh_with_only_local_root(self) -> None:
229235
os.remove(os.path.join(self.client_directory, "timestamp.json"))
230236
os.remove(os.path.join(self.client_directory, "snapshot.json"))
231237
os.remove(os.path.join(self.client_directory, "targets.json"))
232238
os.remove(os.path.join(self.client_directory, "role1.json"))
233239
os.remove(os.path.join(self.client_directory, "role2.json"))
234-
self._assert_files(["root"])
240+
self._assert_files_exist(["root"])
235241

236242
# Get targetinfo for 'file3.txt' listed in the delegated role1
237243
self.updater.get_targetinfo("file3.txt")
238244
expected_files = ["role1", "root", "snapshot", "targets", "timestamp"]
239-
self._assert_files(expected_files)
245+
self._assert_files_exist(expected_files)
240246

241247
def test_both_target_urls_not_set(self) -> None:
242248
# target_base_url = None and Updater._target_base_url = None

tests/test_updater_top_level_update.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,13 @@ def _init_updater(self) -> Updater:
100100
)
101101

102102
def _assert_files_exist(self, roles: Iterable[str]) -> None:
103-
"""Assert that local metadata files exist for 'roles'"""
104-
expected_files = sorted([f"{role}.json" for role in roles])
105-
local_metadata_files = sorted(os.listdir(self.metadata_dir))
106-
self.assertListEqual(local_metadata_files, expected_files)
103+
"""Assert that local metadata files match 'roles'"""
104+
expected_files = [f"{role}.json" for role in roles]
105+
found_files = [
106+
e.name for e in os.scandir(self.metadata_dir) if e.is_file()
107+
]
108+
109+
self.assertListEqual(sorted(found_files), sorted(expected_files))
107110

108111
def _assert_content_equals(
109112
self, role: str, version: Optional[int] = None

0 commit comments

Comments
 (0)