Skip to content

Commit d9d5fbb

Browse files
committed
test artifact caching
Artifact caching is not part of the spec but it is a good idea * Client CLI document did not require caching before: Add it in the doc * Add test that verifies that two download commands do not result in two downloads * python test client did not support this so support was added Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
1 parent 893fc34 commit d9d5fbb

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

CLIENT-CLI.md

Lines changed: 6 additions & 2 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,30 @@ 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(
244+
client: ClientRunner, server: SimulatorServer
245+
) -> None:
246+
"""In this test client is asked to download the same artifact twice.
247+
The client is expected return the cached content on the second time.
248+
249+
Artifact caching is not required in the specification: clients
250+
that do not support it can mark this test as expected to fail
251+
"""
252+
253+
init_data, repo = server.new_test(client.test_name)
254+
assert client.init_client(init_data) == 0
255+
256+
# Create a test artifact, add it to the repository
257+
target_path = "target_file.txt"
258+
target_content = b"target file contents"
259+
repo.add_artifact(Targets.type, target_content, target_path)
260+
repo.publish([Targets.type, Snapshot.type, Timestamp.type])
261+
262+
# Client is asked to download artifact twice
263+
assert client.download_target(init_data, target_path) == 0
264+
assert client.download_target(init_data, target_path) == 0
265+
266+
# Expect only one download from repository
267+
assert len(repo.artifact_statistics) == 1

0 commit comments

Comments
 (0)