Skip to content

Commit 7379d1c

Browse files
committed
fix(cache-downloader): Skip download if already cached
Enhances the download_dependency function to check for an existing "done" file before initiating a download, preventing unnecessary downloads of already cached dependencies. Adds unit tests to verify this behavior, ensuring that downloads are skipped when the cache is present and triggered when it is absent.
1 parent 1a9d469 commit 7379d1c

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

dockerfiles/cache-downloader/cache-downloader.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ def download_dependency(
1919
version_path = os.path.join(BASE_PATH, release_name, f"python{python_version}")
2020
done_file = os.path.join(version_path, f"{python_version}-done")
2121

22+
if os.path.isfile(done_file):
23+
print(
24+
f"{datetime.datetime.now()}: {release_name} python{python_version} already cached, skipping download"
25+
)
26+
return
27+
2228
# Create the version directory if it doesn't exist
2329
os.makedirs(version_path, exist_ok=True)
2430

@@ -57,7 +63,7 @@ def download_dependency(
5763
f"{datetime.datetime.now()}: Done downloading {release_name} {s3_path} and extracting to {version_path}"
5864
)
5965
# Create the "done" file
60-
open(done_file, "a").close()
66+
Path(done_file).touch()
6167

6268

6369
def submit_downloading(

tests/unit/test_cache_downloader.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import importlib.util
22
import os
33
import shutil
4-
from unittest.mock import patch
4+
from unittest.mock import MagicMock, patch
55

66
import pytest
77

@@ -21,6 +21,7 @@
2121
_spec.loader.exec_module(_mod)
2222

2323
cleanup_old_versions = _mod.cleanup_old_versions
24+
download_dependency = _mod.download_dependency
2425

2526

2627
class TestCleanupOldVersions:
@@ -169,3 +170,44 @@ def _fail_on_v1(path, *args, **kwargs):
169170
assert (tmp_path / "v1").exists()
170171
assert (tmp_path / "v2").exists()
171172
assert (tmp_path / "v3").exists()
173+
174+
175+
class TestDownloadDependency:
176+
def test_skips_download_when_done_file_exists(self, tmp_path):
177+
"""Already-cached version should not trigger a download."""
178+
release = "v1.0.0"
179+
py_ver = "3.11"
180+
version_path = tmp_path / release / f"python{py_ver}"
181+
version_path.mkdir(parents=True)
182+
(version_path / f"{py_ver}-done").write_text("ok")
183+
184+
with patch.object(_mod, "BASE_PATH", str(tmp_path)):
185+
with patch.object(_mod.subprocess, "Popen") as mock_popen:
186+
download_dependency(release, py_ver, "fake-bucket")
187+
188+
mock_popen.assert_not_called()
189+
190+
def test_downloads_when_done_file_missing(self, tmp_path):
191+
"""Missing done file should trigger the download."""
192+
release = "v1.0.0"
193+
py_ver = "3.11"
194+
version_path = tmp_path / release / f"python{py_ver}"
195+
version_path.mkdir(parents=True)
196+
197+
mock_aws = MagicMock()
198+
mock_aws.stdout = MagicMock()
199+
mock_aws.stderr = MagicMock(read=MagicMock(return_value=b""))
200+
mock_aws.wait.return_value = 0
201+
202+
mock_tar = MagicMock()
203+
mock_tar.communicate.return_value = (b"", b"")
204+
mock_tar.returncode = 0
205+
206+
with patch.object(_mod, "BASE_PATH", str(tmp_path)):
207+
with patch.object(
208+
_mod.subprocess, "Popen", side_effect=[mock_aws, mock_tar]
209+
) as mock_popen:
210+
download_dependency(release, py_ver, "fake-bucket")
211+
212+
assert mock_popen.call_count == 2
213+
assert (version_path / f"{py_ver}-done").exists()

0 commit comments

Comments
 (0)