Skip to content

Commit 3cdbd5a

Browse files
authored
test artifact caching (#260)
1 parent 893fc34 commit 3cdbd5a

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

CLIENT-CLI.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@ given targets directory. Client must ensure that metadata is up-to-date before d
7272
Client must use exit code 0 if the download succeeded fully and exit code 1 if any part of
7373
the download failed.
7474

75-
Client may use any filename or directory structure it wants within `TARGET_DIR`: The expectation is that if a
76-
targetpath is downloaded twice it is stored with the same filename (even if the content changed).
75+
Client should support artifact caching: if client is requested to download an artifact that has
76+
already been downloaded (with matching hashes), it should not download the artifact again.
77+
78+
Client may use any filename or directory structure it wants within `TARGET_DIR`: The expectation
79+
is that if an artifact is downloaded twice (because the artifact content changed) it is stored in
80+
`TARGET_DIR` with the same filename.
7781

7882
This command may be called multiple times in a test.
7983

clients/python-tuf/python_tuf.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def download_target(
5050
target_info = updater.get_targetinfo(target_name)
5151
if not target_info:
5252
raise RuntimeError(f"{target_name} not found in repository")
53-
updater.download_target(target_info)
53+
if not updater.find_cached_target(target_info):
54+
updater.download_target(target_info)
5455

5556

5657
def main() -> int:

tuf_conformance/test_file_download.py

+25
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,28 @@ def test_download_with_unknown_hash_algorithm(
238238
# and only then realize hash cannot be verified
239239
assert client.download_target(init_data, target_path) == 1
240240
assert client.get_downloaded_target_bytes() == []
241+
242+
243+
def test_artifact_cache(client: ClientRunner, server: SimulatorServer) -> None:
244+
"""In this test client is asked to download the same artifact twice.
245+
The client is expected return the cached content on the second time.
246+
247+
Artifact caching is not required in the specification: clients
248+
that do not support it can mark this test as expected to fail
249+
"""
250+
251+
init_data, repo = server.new_test(client.test_name)
252+
assert client.init_client(init_data) == 0
253+
254+
# Create a test artifact, add it to the repository
255+
target_path = "target_file.txt"
256+
target_content = b"target file contents"
257+
repo.add_artifact(Targets.type, target_content, target_path)
258+
repo.publish([Targets.type, Snapshot.type, Timestamp.type])
259+
260+
# Client is asked to download artifact twice
261+
assert client.download_target(init_data, target_path) == 0
262+
assert client.download_target(init_data, target_path) == 0
263+
264+
# Expect only one download from repository
265+
assert len(repo.artifact_statistics) == 1

0 commit comments

Comments
 (0)