Skip to content

Commit d1bc201

Browse files
committed
Define a TestTarget dataclass
Use a dataclass for a better representation of the target files in the test data set. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent adcaf58 commit d1bc201

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

tests/test_updater_fetch_target.py

+43-27
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@
1010
import sys
1111
import tempfile
1212
import unittest
13-
from typing import Optional, Tuple
13+
from dataclasses import dataclass
14+
from typing import Optional
1415

1516
from tests import utils
1617
from tests.repository_simulator import RepositorySimulator
1718
from tuf.exceptions import RepositoryError
1819
from tuf.ngclient import Updater
1920

2021

22+
@dataclass
23+
class TestTarget:
24+
path: str
25+
content: bytes
26+
encoded_path: str
27+
28+
2129
class TestFetchTarget(unittest.TestCase):
2230
"""Test ngclient downloading and caching target files."""
2331

@@ -61,32 +69,39 @@ def _init_updater(self) -> Updater:
6169
return updater
6270

6371
targets: utils.DataSet = {
64-
"standard case": ("targetpath", b"content", "targetpath"),
65-
"non-asci case": ("åäö", b"more content", "%C3%A5%C3%A4%C3%B6"),
66-
"subdirectory case": (
67-
"a/b/c/targetpath",
68-
b"dir target content",
69-
"a%2Fb%2Fc%2Ftargetpath",
72+
"standard case": TestTarget(
73+
path="targetpath",
74+
content=b"target content",
75+
encoded_path="targetpath",
76+
),
77+
"non-asci case": TestTarget(
78+
path="åäö",
79+
content=b"more content",
80+
encoded_path="%C3%A5%C3%A4%C3%B6",
81+
),
82+
"subdirectory case": TestTarget(
83+
path="a/b/c/targetpath",
84+
content=b"dir target content",
85+
encoded_path="a%2Fb%2Fc%2Ftargetpath",
7086
),
7187
}
7288

7389
@utils.run_sub_tests_with_dataset(targets)
74-
def test_fetch_target(self, test_case_data: Tuple[str, bytes, str]) -> None:
75-
targetpath, content, encoded_path = test_case_data
76-
path = os.path.join(self.targets_dir, encoded_path)
90+
def test_fetch_target(self, target: TestTarget) -> None:
91+
path = os.path.join(self.targets_dir, target.encoded_path)
7792

7893
updater = self._init_updater()
7994
# target does not exist yet
80-
self.assertIsNone(updater.get_targetinfo(targetpath))
95+
self.assertIsNone(updater.get_targetinfo(target.path))
8196

8297
# Add targets to repository
8398
self.sim.targets.version += 1
84-
self.sim.add_target("targets", content, targetpath)
99+
self.sim.add_target("targets", target.content, target.path)
85100
self.sim.update_snapshot()
86101

87102
updater = self._init_updater()
88103
# target now exists, is not in cache yet
89-
info = updater.get_targetinfo(targetpath)
104+
info = updater.get_targetinfo(target.path)
90105
assert info is not None
91106
# Test without and with explicit local filepath
92107
self.assertIsNone(updater.find_cached_target(info))
@@ -98,7 +113,7 @@ def test_fetch_target(self, test_case_data: Tuple[str, bytes, str]) -> None:
98113
self.assertEqual(path, updater.find_cached_target(info, path))
99114

100115
with open(path, "rb") as f:
101-
self.assertEqual(f.read(), content)
116+
self.assertEqual(f.read(), target.content)
102117

103118
# download using explicit filepath as well
104119
os.remove(path)
@@ -107,61 +122,62 @@ def test_fetch_target(self, test_case_data: Tuple[str, bytes, str]) -> None:
107122
self.assertEqual(path, updater.find_cached_target(info, path))
108123

109124
def test_invalid_target_download(self) -> None:
110-
targetpath = "targetpath"
125+
target = TestTarget("targetpath", b"content", "targetpath")
126+
111127
# Add target to repository
112128
self.sim.targets.version += 1
113-
self.sim.add_target("targets", b"content", targetpath)
129+
self.sim.add_target("targets", target.content, target.path)
114130
self.sim.update_snapshot()
115131

116132
updater = self._init_updater()
117-
info = updater.get_targetinfo(targetpath)
133+
info = updater.get_targetinfo(target.path)
118134
assert info is not None
119135

120136
# Corrupt the file content to not match the hash
121-
self.sim.target_files[targetpath].data = b"conten@"
137+
self.sim.target_files[target.path].data = b"conten@"
122138
with self.assertRaises(RepositoryError):
123139
updater.download_target(info)
124140

125141
# Corrupt the file content to not match the length
126-
self.sim.target_files[targetpath].data = b"cont"
142+
self.sim.target_files[target.path].data = b"cont"
127143
with self.assertRaises(RepositoryError):
128144
updater.download_target(info)
129145

130146
# Verify the file is not persisted in cache
131147
self.assertIsNone(updater.find_cached_target(info))
132148

133149
def test_invalid_target_cache(self) -> None:
134-
targetpath = "targetpath"
135-
content = b"content"
150+
target = TestTarget("targetpath", b"content", "targetpath")
151+
136152
# Add target to repository
137153
self.sim.targets.version += 1
138-
self.sim.add_target("targets", content, targetpath)
154+
self.sim.add_target("targets", target.content, target.path)
139155
self.sim.update_snapshot()
140156

141157
# Download the target
142158
updater = self._init_updater()
143-
info = updater.get_targetinfo(targetpath)
159+
info = updater.get_targetinfo(target.path)
144160
assert info is not None
145161
path = updater.download_target(info)
146162
self.assertEqual(path, updater.find_cached_target(info))
147163

148164
# Add newer content to the same targetpath
149-
content = b"contentv2"
165+
target.content = b"contentv2"
150166
self.sim.targets.version += 1
151-
self.sim.add_target("targets", content, targetpath)
167+
self.sim.add_target("targets", target.content, target.path)
152168
self.sim.update_snapshot()
153169

154170
# Newer content is detected, old cached version is not used
155171
updater = self._init_updater()
156-
info = updater.get_targetinfo(targetpath)
172+
info = updater.get_targetinfo(target.path)
157173
assert info is not None
158174
self.assertIsNone(updater.find_cached_target(info))
159175

160176
# Download target, assert it is in cache and content is the newer
161177
path = updater.download_target(info)
162178
self.assertEqual(path, updater.find_cached_target(info))
163179
with open(path, "rb") as f:
164-
self.assertEqual(f.read(), content)
180+
self.assertEqual(f.read(), target.content)
165181

166182

167183
if __name__ == "__main__":

0 commit comments

Comments
 (0)